V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
basic-block-profiler.cc
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/basic-block-profiler.h"
6 
7 #include <algorithm>
8 #include <numeric>
9 #include <sstream>
10 
11 #include "src/base/lazy-instance.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 namespace {
17 base::LazyInstance<BasicBlockProfiler>::type kBasicBlockProfiler =
18  LAZY_INSTANCE_INITIALIZER;
19 }
20 
21 BasicBlockProfiler* BasicBlockProfiler::Get() {
22  return kBasicBlockProfiler.Pointer();
23 }
24 
25 BasicBlockProfiler::Data::Data(size_t n_blocks)
26  : n_blocks_(n_blocks),
27  block_rpo_numbers_(n_blocks_),
28  counts_(n_blocks_, 0) {}
29 
30 static void InsertIntoString(std::ostringstream* os, std::string* string) {
31  string->insert(0, os->str());
32 }
33 
34 static void InsertIntoString(const char* data, std::string* string) {
35  string->insert(0, data);
36 }
37 
38 void BasicBlockProfiler::Data::SetCode(std::ostringstream* os) {
39  InsertIntoString(os, &code_);
40 }
41 
42 void BasicBlockProfiler::Data::SetFunctionName(std::unique_ptr<char[]> name) {
43  InsertIntoString(name.get(), &function_name_);
44 }
45 
46 void BasicBlockProfiler::Data::SetSchedule(std::ostringstream* os) {
47  InsertIntoString(os, &schedule_);
48 }
49 
50 void BasicBlockProfiler::Data::SetBlockRpoNumber(size_t offset,
51  int32_t block_rpo) {
52  DCHECK(offset < n_blocks_);
53  block_rpo_numbers_[offset] = block_rpo;
54 }
55 
56 intptr_t BasicBlockProfiler::Data::GetCounterAddress(size_t offset) {
57  DCHECK(offset < n_blocks_);
58  return reinterpret_cast<intptr_t>(&(counts_[offset]));
59 }
60 
61 
62 void BasicBlockProfiler::Data::ResetCounts() {
63  for (size_t i = 0; i < n_blocks_; ++i) {
64  counts_[i] = 0;
65  }
66 }
67 
68 BasicBlockProfiler::Data* BasicBlockProfiler::NewData(size_t n_blocks) {
69  base::MutexGuard lock(&data_list_mutex_);
70  Data* data = new Data(n_blocks);
71  data_list_.push_back(data);
72  return data;
73 }
74 
75 
76 BasicBlockProfiler::~BasicBlockProfiler() {
77  for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
78  delete (*i);
79  }
80 }
81 
82 
83 void BasicBlockProfiler::ResetCounts() {
84  for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
85  (*i)->ResetCounts();
86  }
87 }
88 
89 
90 std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler& p) {
91  os << "---- Start Profiling Data ----" << std::endl;
92  typedef BasicBlockProfiler::DataList::const_iterator iterator;
93  for (iterator i = p.data_list_.begin(); i != p.data_list_.end(); ++i) {
94  os << **i;
95  }
96  os << "---- End Profiling Data ----" << std::endl;
97  return os;
98 }
99 
100 
101 std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler::Data& d) {
102  int block_count_sum = std::accumulate(d.counts_.begin(), d.counts_.end(), 0);
103  if (block_count_sum == 0) return os;
104  const char* name = "unknown function";
105  if (!d.function_name_.empty()) {
106  name = d.function_name_.c_str();
107  }
108  if (!d.schedule_.empty()) {
109  os << "schedule for " << name << " (B0 entered " << d.counts_[0]
110  << " times)" << std::endl;
111  os << d.schedule_.c_str() << std::endl;
112  }
113  os << "block counts for " << name << ":" << std::endl;
114  std::vector<std::pair<int32_t, uint32_t>> pairs;
115  pairs.reserve(d.n_blocks_);
116  for (size_t i = 0; i < d.n_blocks_; ++i) {
117  pairs.push_back(std::make_pair(d.block_rpo_numbers_[i], d.counts_[i]));
118  }
119  std::sort(pairs.begin(), pairs.end(),
120  [=](std::pair<int32_t, uint32_t> left,
121  std::pair<int32_t, uint32_t> right) {
122  if (right.second == left.second)
123  return left.first < right.first;
124  return right.second < left.second;
125  });
126  for (auto it : pairs) {
127  if (it.second == 0) break;
128  os << "block B" << it.first << " : " << it.second << std::endl;
129  }
130  os << std::endl;
131  if (!d.code_.empty()) {
132  os << d.code_.c_str() << std::endl;
133  }
134  return os;
135 }
136 
137 } // namespace internal
138 } // namespace v8
Definition: libplatform.h:13