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 12 : BasicBlockProfiler::Data::Data(size_t n_blocks)
13 24 : : n_blocks_(n_blocks), block_ids_(n_blocks_), counts_(n_blocks_, 0) {}
14 :
15 :
16 24 : BasicBlockProfiler::Data::~Data() {}
17 :
18 :
19 12 : static void InsertIntoString(std::ostringstream* os, std::string* string) {
20 12 : string->insert(0, os->str());
21 12 : }
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 12 : void BasicBlockProfiler::Data::SetSchedule(std::ostringstream* os) {
35 12 : InsertIntoString(os, &schedule_);
36 12 : }
37 :
38 :
39 48 : void BasicBlockProfiler::Data::SetBlockId(size_t offset, size_t block_id) {
40 : DCHECK(offset < n_blocks_);
41 96 : block_ids_[offset] = block_id;
42 48 : }
43 :
44 :
45 48 : uint32_t* BasicBlockProfiler::Data::GetCounterAddress(size_t offset) {
46 : DCHECK(offset < n_blocks_);
47 96 : return &counts_[offset];
48 : }
49 :
50 :
51 0 : void BasicBlockProfiler::Data::ResetCounts() {
52 120 : for (size_t i = 0; i < n_blocks_; ++i) {
53 240 : counts_[i] = 0;
54 : }
55 0 : }
56 :
57 :
58 24 : BasicBlockProfiler::BasicBlockProfiler() {}
59 :
60 :
61 12 : BasicBlockProfiler::Data* BasicBlockProfiler::NewData(size_t n_blocks) {
62 12 : Data* data = new Data(n_blocks);
63 12 : data_list_.push_back(data);
64 12 : return data;
65 : }
66 :
67 :
68 12 : BasicBlockProfiler::~BasicBlockProfiler() {
69 36 : for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
70 12 : delete (*i);
71 : }
72 12 : }
73 :
74 :
75 30 : void BasicBlockProfiler::ResetCounts() {
76 90 : for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
77 30 : (*i)->ResetCounts();
78 : }
79 30 : }
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
|