5 #include "src/basic-block-profiler.h" 11 #include "src/base/lazy-instance.h" 17 base::LazyInstance<BasicBlockProfiler>::type kBasicBlockProfiler =
18 LAZY_INSTANCE_INITIALIZER;
21 BasicBlockProfiler* BasicBlockProfiler::Get() {
22 return kBasicBlockProfiler.Pointer();
25 BasicBlockProfiler::Data::Data(
size_t n_blocks)
26 : n_blocks_(n_blocks),
27 block_rpo_numbers_(n_blocks_),
28 counts_(n_blocks_, 0) {}
30 static void InsertIntoString(std::ostringstream* os, std::string*
string) {
31 string->insert(0, os->str());
34 static void InsertIntoString(
const char* data, std::string*
string) {
35 string->insert(0, data);
38 void BasicBlockProfiler::Data::SetCode(std::ostringstream* os) {
39 InsertIntoString(os, &code_);
42 void BasicBlockProfiler::Data::SetFunctionName(std::unique_ptr<
char[]> name) {
43 InsertIntoString(name.get(), &function_name_);
46 void BasicBlockProfiler::Data::SetSchedule(std::ostringstream* os) {
47 InsertIntoString(os, &schedule_);
50 void BasicBlockProfiler::Data::SetBlockRpoNumber(
size_t offset,
52 DCHECK(offset < n_blocks_);
53 block_rpo_numbers_[offset] = block_rpo;
56 intptr_t BasicBlockProfiler::Data::GetCounterAddress(
size_t offset) {
57 DCHECK(offset < n_blocks_);
58 return reinterpret_cast<intptr_t
>(&(counts_[offset]));
62 void BasicBlockProfiler::Data::ResetCounts() {
63 for (
size_t i = 0;
i < n_blocks_; ++
i) {
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);
76 BasicBlockProfiler::~BasicBlockProfiler() {
77 for (DataList::iterator
i = data_list_.begin();
i != data_list_.end(); ++
i) {
83 void BasicBlockProfiler::ResetCounts() {
84 for (DataList::iterator
i = data_list_.begin();
i != data_list_.end(); ++
i) {
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) {
96 os <<
"---- End Profiling Data ----" << std::endl;
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();
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;
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]));
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;
126 for (
auto it : pairs) {
127 if (it.second == 0)
break;
128 os <<
"block B" << it.first <<
" : " << it.second << std::endl;
131 if (!d.code_.empty()) {
132 os << d.code_.c_str() << std::endl;