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 
Get()21 BasicBlockProfiler* BasicBlockProfiler::Get() {
22   return kBasicBlockProfiler.Pointer();
23 }
24 
Data(size_t n_blocks)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 
~Data()30 BasicBlockProfiler::Data::~Data() {}
31 
32 
InsertIntoString(std::ostringstream * os,std::string * string)33 static void InsertIntoString(std::ostringstream* os, std::string* string) {
34   string->insert(0, os->str());
35 }
36 
InsertIntoString(const char * data,std::string * string)37 static void InsertIntoString(const char* data, std::string* string) {
38   string->insert(0, data);
39 }
40 
SetCode(std::ostringstream * os)41 void BasicBlockProfiler::Data::SetCode(std::ostringstream* os) {
42   InsertIntoString(os, &code_);
43 }
44 
SetFunctionName(std::unique_ptr<char[]> name)45 void BasicBlockProfiler::Data::SetFunctionName(std::unique_ptr<char[]> name) {
46   InsertIntoString(name.get(), &function_name_);
47 }
48 
SetSchedule(std::ostringstream * os)49 void BasicBlockProfiler::Data::SetSchedule(std::ostringstream* os) {
50   InsertIntoString(os, &schedule_);
51 }
52 
SetBlockRpoNumber(size_t offset,int32_t block_rpo)53 void BasicBlockProfiler::Data::SetBlockRpoNumber(size_t offset,
54                                                  int32_t block_rpo) {
55   DCHECK(offset < n_blocks_);
56   block_rpo_numbers_[offset] = block_rpo;
57 }
58 
GetCounterAddress(size_t offset)59 intptr_t BasicBlockProfiler::Data::GetCounterAddress(size_t offset) {
60   DCHECK(offset < n_blocks_);
61   return reinterpret_cast<intptr_t>(&(counts_[offset]));
62 }
63 
64 
ResetCounts()65 void BasicBlockProfiler::Data::ResetCounts() {
66   for (size_t i = 0; i < n_blocks_; ++i) {
67     counts_[i] = 0;
68   }
69 }
70 
71 
BasicBlockProfiler()72 BasicBlockProfiler::BasicBlockProfiler() {}
73 
74 
NewData(size_t n_blocks)75 BasicBlockProfiler::Data* BasicBlockProfiler::NewData(size_t n_blocks) {
76   base::LockGuard<base::Mutex> lock(&data_list_mutex_);
77   Data* data = new Data(n_blocks);
78   data_list_.push_back(data);
79   return data;
80 }
81 
82 
~BasicBlockProfiler()83 BasicBlockProfiler::~BasicBlockProfiler() {
84   for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
85     delete (*i);
86   }
87 }
88 
89 
ResetCounts()90 void BasicBlockProfiler::ResetCounts() {
91   for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
92     (*i)->ResetCounts();
93   }
94 }
95 
96 
operator <<(std::ostream & os,const BasicBlockProfiler & p)97 std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler& p) {
98   os << "---- Start Profiling Data ----" << std::endl;
99   typedef BasicBlockProfiler::DataList::const_iterator iterator;
100   for (iterator i = p.data_list_.begin(); i != p.data_list_.end(); ++i) {
101     os << **i;
102   }
103   os << "---- End Profiling Data ----" << std::endl;
104   return os;
105 }
106 
107 
operator <<(std::ostream & os,const BasicBlockProfiler::Data & d)108 std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler::Data& d) {
109   int block_count_sum = std::accumulate(d.counts_.begin(), d.counts_.end(), 0);
110   if (block_count_sum == 0) return os;
111   const char* name = "unknown function";
112   if (!d.function_name_.empty()) {
113     name = d.function_name_.c_str();
114   }
115   if (!d.schedule_.empty()) {
116     os << "schedule for " << name << " (B0 entered " << d.counts_[0]
117        << " times)" << std::endl;
118     os << d.schedule_.c_str() << std::endl;
119   }
120   os << "block counts for " << name << ":" << std::endl;
121   std::vector<std::pair<int32_t, uint32_t>> pairs;
122   pairs.reserve(d.n_blocks_);
123   for (size_t i = 0; i < d.n_blocks_; ++i) {
124     pairs.push_back(std::make_pair(d.block_rpo_numbers_[i], d.counts_[i]));
125   }
126   std::sort(pairs.begin(), pairs.end(),
127             [=](std::pair<int32_t, uint32_t> left,
128                 std::pair<int32_t, uint32_t> right) {
129               if (right.second == left.second)
130                 return left.first < right.first;
131               return right.second < left.second;
132             });
133   for (auto it : pairs) {
134     if (it.second == 0) break;
135     os << "block B" << it.first << " : " << it.second << std::endl;
136   }
137   os << std::endl;
138   if (!d.code_.empty()) {
139     os << d.code_.c_str() << std::endl;
140   }
141   return os;
142 }
143 
144 }  // namespace internal
145 }  // namespace v8
146