basic-block-profiler.cc 2.71 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/basic-block-profiler.h"

7 8
#include <sstream>

9 10 11 12
namespace v8 {
namespace internal {

BasicBlockProfiler::Data::Data(size_t n_blocks)
13
    : n_blocks_(n_blocks), block_ids_(n_blocks_), counts_(n_blocks_, 0) {}
14 15 16 17 18


BasicBlockProfiler::Data::~Data() {}


19 20
static void InsertIntoString(std::ostringstream* os, std::string* string) {
  string->insert(0, os->str());
21 22 23
}


24
void BasicBlockProfiler::Data::SetCode(std::ostringstream* os) {
25 26 27 28
  InsertIntoString(os, &code_);
}


29
void BasicBlockProfiler::Data::SetFunctionName(std::ostringstream* os) {
30 31 32 33
  InsertIntoString(os, &function_name_);
}


34
void BasicBlockProfiler::Data::SetSchedule(std::ostringstream* os) {
35 36 37 38
  InsertIntoString(os, &schedule_);
}


39
void BasicBlockProfiler::Data::SetBlockId(size_t offset, size_t block_id) {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  DCHECK(offset < n_blocks_);
  block_ids_[offset] = block_id;
}


uint32_t* BasicBlockProfiler::Data::GetCounterAddress(size_t offset) {
  DCHECK(offset < n_blocks_);
  return &counts_[offset];
}


void BasicBlockProfiler::Data::ResetCounts() {
  for (size_t i = 0; i < n_blocks_; ++i) {
    counts_[i] = 0;
  }
}


BasicBlockProfiler::BasicBlockProfiler() {}


BasicBlockProfiler::Data* BasicBlockProfiler::NewData(size_t n_blocks) {
  Data* data = new Data(n_blocks);
  data_list_.push_back(data);
  return data;
}


BasicBlockProfiler::~BasicBlockProfiler() {
  for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
    delete (*i);
  }
}


void BasicBlockProfiler::ResetCounts() {
  for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
    (*i)->ResetCounts();
  }
}


82 83
std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler& p) {
  os << "---- Start Profiling Data ----" << std::endl;
84 85 86 87
  typedef BasicBlockProfiler::DataList::const_iterator iterator;
  for (iterator i = p.data_list_.begin(); i != p.data_list_.end(); ++i) {
    os << **i;
  }
88
  os << "---- End Profiling Data ----" << std::endl;
89 90 91 92
  return os;
}


93
std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler::Data& d) {
94 95 96 97 98
  const char* name = "unknown function";
  if (!d.function_name_.empty()) {
    name = d.function_name_.c_str();
  }
  if (!d.schedule_.empty()) {
99 100
    os << "schedule for " << name << std::endl;
    os << d.schedule_.c_str() << std::endl;
101
  }
102
  os << "block counts for " << name << ":" << std::endl;
103
  for (size_t i = 0; i < d.n_blocks_; ++i) {
104
    os << "block " << d.block_ids_[i] << " : " << d.counts_[i] << std::endl;
105
  }
106
  os << std::endl;
107
  if (!d.code_.empty()) {
108
    os << d.code_.c_str() << std::endl;
109 110 111 112 113 114
  }
  return os;
}

}  // namespace internal
}  // namespace v8