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_DIAGNOSTICS_BASIC_BLOCK_PROFILER_H_ 6 #define V8_DIAGNOSTICS_BASIC_BLOCK_PROFILER_H_ 7 8 #include <iosfwd> 9 #include <list> 10 #include <memory> 11 #include <string> 12 #include <vector> 13 14 #include "src/base/macros.h" 15 #include "src/base/platform/mutex.h" 16 #include "src/common/globals.h" 17 #include "src/objects/shared-function-info.h" 18 19 namespace v8 { 20 namespace internal { 21 22 class OnHeapBasicBlockProfilerData; 23 24 class BasicBlockProfilerData { 25 public: 26 explicit BasicBlockProfilerData(size_t n_blocks); 27 V8_EXPORT_PRIVATE BasicBlockProfilerData( 28 Handle<OnHeapBasicBlockProfilerData> js_heap_data, Isolate* isolate); 29 V8_EXPORT_PRIVATE BasicBlockProfilerData( 30 OnHeapBasicBlockProfilerData js_heap_data); 31 n_blocks()32 size_t n_blocks() const { 33 DCHECK_EQ(block_ids_.size(), counts_.size()); 34 return block_ids_.size(); 35 } counts()36 const uint32_t* counts() const { return &counts_[0]; } 37 38 void SetCode(const std::ostringstream& os); 39 void SetFunctionName(std::unique_ptr<char[]> name); 40 void SetSchedule(const std::ostringstream& os); 41 void SetBlockId(size_t offset, int32_t id); 42 void SetHash(int hash); 43 44 // Copy the data from this object into an equivalent object stored on the JS 45 // heap, so that it can survive snapshotting and relocation. This must 46 // happen on the main thread during finalization of the compilation. 47 Handle<OnHeapBasicBlockProfilerData> CopyToJSHeap(Isolate* isolate); 48 49 void Log(Isolate* isolate); 50 51 private: 52 friend class BasicBlockProfiler; 53 friend std::ostream& operator<<(std::ostream& os, 54 const BasicBlockProfilerData& s); 55 56 V8_EXPORT_PRIVATE void ResetCounts(); 57 58 // These vectors are indexed by reverse post-order block number. 59 std::vector<int32_t> block_ids_; 60 std::vector<uint32_t> counts_; 61 std::string function_name_; 62 std::string schedule_; 63 std::string code_; 64 int hash_; 65 DISALLOW_COPY_AND_ASSIGN(BasicBlockProfilerData); 66 }; 67 68 class BasicBlockProfiler { 69 public: 70 using DataList = std::list<std::unique_ptr<BasicBlockProfilerData>>; 71 72 BasicBlockProfiler() = default; 73 ~BasicBlockProfiler() = default; 74 75 V8_EXPORT_PRIVATE static BasicBlockProfiler* Get(); 76 BasicBlockProfilerData* NewData(size_t n_blocks); 77 V8_EXPORT_PRIVATE void ResetCounts(Isolate* isolate); 78 V8_EXPORT_PRIVATE bool HasData(Isolate* isolate); 79 V8_EXPORT_PRIVATE void Print(std::ostream& os, Isolate* isolate); 80 81 // Coverage bitmap in this context includes only on heap BasicBlockProfiler 82 // data It is used to export coverage of builtins function loaded from 83 // snapshot. 84 V8_EXPORT_PRIVATE std::vector<bool> GetCoverageBitmap(Isolate* isolate); 85 data_list()86 const DataList* data_list() { return &data_list_; } 87 88 private: 89 DataList data_list_; 90 base::Mutex data_list_mutex_; 91 92 DISALLOW_COPY_AND_ASSIGN(BasicBlockProfiler); 93 }; 94 95 std::ostream& operator<<(std::ostream& os, const BasicBlockProfilerData& s); 96 97 } // namespace internal 98 } // namespace v8 99 100 #endif // V8_DIAGNOSTICS_BASIC_BLOCK_PROFILER_H_ 101