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