Line data Source code
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 <sstream>
8 :
9 : namespace v8 {
10 : namespace internal {
11 :
12 14 : BasicBlockProfiler::Data::Data(size_t n_blocks)
13 28 : : n_blocks_(n_blocks), block_ids_(n_blocks_), counts_(n_blocks_, 0) {}
14 :
15 :
16 28 : BasicBlockProfiler::Data::~Data() {}
17 :
18 :
19 14 : static void InsertIntoString(std::ostringstream* os, std::string* string) {
20 14 : string->insert(0, os->str());
21 14 : }
22 :
23 :
24 0 : void BasicBlockProfiler::Data::SetCode(std::ostringstream* os) {
25 0 : InsertIntoString(os, &code_);
26 0 : }
27 :
28 :
29 0 : void BasicBlockProfiler::Data::SetFunctionName(std::ostringstream* os) {
30 0 : InsertIntoString(os, &function_name_);
31 0 : }
32 :
33 :
34 14 : void BasicBlockProfiler::Data::SetSchedule(std::ostringstream* os) {
35 14 : InsertIntoString(os, &schedule_);
36 14 : }
37 :
38 :
39 56 : void BasicBlockProfiler::Data::SetBlockId(size_t offset, size_t block_id) {
40 : DCHECK(offset < n_blocks_);
41 112 : block_ids_[offset] = block_id;
42 56 : }
43 :
44 :
45 56 : uint32_t* BasicBlockProfiler::Data::GetCounterAddress(size_t offset) {
46 : DCHECK(offset < n_blocks_);
47 112 : return &counts_[offset];
48 : }
49 :
50 :
51 0 : void BasicBlockProfiler::Data::ResetCounts() {
52 140 : for (size_t i = 0; i < n_blocks_; ++i) {
53 280 : counts_[i] = 0;
54 : }
55 0 : }
56 :
57 :
58 28 : BasicBlockProfiler::BasicBlockProfiler() {}
59 :
60 :
61 14 : BasicBlockProfiler::Data* BasicBlockProfiler::NewData(size_t n_blocks) {
62 14 : Data* data = new Data(n_blocks);
63 14 : data_list_.push_back(data);
64 14 : return data;
65 : }
66 :
67 :
68 14 : BasicBlockProfiler::~BasicBlockProfiler() {
69 42 : for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
70 14 : delete (*i);
71 : }
72 14 : }
73 :
74 :
75 35 : void BasicBlockProfiler::ResetCounts() {
76 105 : for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
77 35 : (*i)->ResetCounts();
78 : }
79 35 : }
80 :
81 :
82 0 : std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler& p) {
83 0 : os << "---- Start Profiling Data ----" << std::endl;
84 : typedef BasicBlockProfiler::DataList::const_iterator iterator;
85 0 : for (iterator i = p.data_list_.begin(); i != p.data_list_.end(); ++i) {
86 0 : os << **i;
87 : }
88 0 : os << "---- End Profiling Data ----" << std::endl;
89 0 : return os;
90 : }
91 :
92 :
93 0 : std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler::Data& d) {
94 : const char* name = "unknown function";
95 0 : if (!d.function_name_.empty()) {
96 : name = d.function_name_.c_str();
97 : }
98 0 : if (!d.schedule_.empty()) {
99 0 : os << "schedule for " << name << std::endl;
100 0 : os << d.schedule_.c_str() << std::endl;
101 : }
102 0 : os << "block counts for " << name << ":" << std::endl;
103 0 : for (size_t i = 0; i < d.n_blocks_; ++i) {
104 0 : os << "block " << d.block_ids_[i] << " : " << d.counts_[i] << std::endl;
105 : }
106 : os << std::endl;
107 0 : if (!d.code_.empty()) {
108 0 : os << d.code_.c_str() << std::endl;
109 : }
110 0 : return os;
111 : }
112 :
113 : } // namespace internal
114 : } // namespace v8
|