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 #ifndef V8_BASIC_BLOCK_PROFILER_H_
6 #define V8_BASIC_BLOCK_PROFILER_H_
7 
8 #include <iosfwd>
9 #include <list>
10 #include <string>
11 #include <vector>
12 
13 #include "src/base/macros.h"
14 #include "src/globals.h"
15 
16 namespace v8 {
17 namespace internal {
18 
19 class BasicBlockProfiler {
20  public:
21   class Data {
22    public:
n_blocks()23     size_t n_blocks() const { return n_blocks_; }
counts()24     const uint32_t* counts() const { return &counts_[0]; }
25 
26     void SetCode(std::ostringstream* os);
27     void SetFunctionName(std::ostringstream* os);
28     void SetSchedule(std::ostringstream* os);
29     void SetBlockId(size_t offset, size_t block_id);
30     uint32_t* GetCounterAddress(size_t offset);
31 
32    private:
33     friend class BasicBlockProfiler;
34     friend std::ostream& operator<<(std::ostream& os,
35                                     const BasicBlockProfiler::Data& s);
36 
37     explicit Data(size_t n_blocks);
38     ~Data();
39 
40     void ResetCounts();
41 
42     const size_t n_blocks_;
43     std::vector<size_t> block_ids_;
44     std::vector<uint32_t> counts_;
45     std::string function_name_;
46     std::string schedule_;
47     std::string code_;
48     DISALLOW_COPY_AND_ASSIGN(Data);
49   };
50 
51   typedef std::list<Data*> DataList;
52 
53   BasicBlockProfiler();
54   ~BasicBlockProfiler();
55 
56   Data* NewData(size_t n_blocks);
57   void ResetCounts();
58 
data_list()59   const DataList* data_list() { return &data_list_; }
60 
61  private:
62   friend V8_EXPORT_PRIVATE std::ostream& operator<<(
63       std::ostream& os, const BasicBlockProfiler& s);
64 
65   DataList data_list_;
66 
67   DISALLOW_COPY_AND_ASSIGN(BasicBlockProfiler);
68 };
69 
70 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
71                                            const BasicBlockProfiler& s);
72 std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler::Data& s);
73 
74 }  // namespace internal
75 }  // namespace v8
76 
77 #endif  // V8_BASIC_BLOCK_PROFILER_H_
78