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