basic-block-profiler.h 3.29 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_DIAGNOSTICS_BASIC_BLOCK_PROFILER_H_
#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 20 21

namespace v8 {
namespace internal {

22 23
class OnHeapBasicBlockProfilerData;

24 25 26 27 28
class BasicBlockProfilerData {
 public:
  explicit BasicBlockProfilerData(size_t n_blocks);
  V8_EXPORT_PRIVATE BasicBlockProfilerData(
      Handle<OnHeapBasicBlockProfilerData> js_heap_data, Isolate* isolate);
29 30
  V8_EXPORT_PRIVATE BasicBlockProfilerData(
      OnHeapBasicBlockProfilerData js_heap_data);
31

32 33 34
  BasicBlockProfilerData(const BasicBlockProfilerData&) = delete;
  BasicBlockProfilerData& operator=(const BasicBlockProfilerData&) = delete;

35
  size_t n_blocks() const {
36 37
    DCHECK_EQ(block_ids_.size(), counts_.size());
    return block_ids_.size();
38
  }
39
  const double* counts() const { return &counts_[0]; }
40 41 42 43

  void SetCode(const std::ostringstream& os);
  void SetFunctionName(std::unique_ptr<char[]> name);
  void SetSchedule(const std::ostringstream& os);
44 45
  void SetBlockId(size_t offset, int32_t id);
  void SetHash(int hash);
46 47 48 49 50 51

  // Copy the data from this object into an equivalent object stored on the JS
  // heap, so that it can survive snapshotting and relocation. This must
  // happen on the main thread during finalization of the compilation.
  Handle<OnHeapBasicBlockProfilerData> CopyToJSHeap(Isolate* isolate);

52 53
  void Log(Isolate* isolate);

54 55 56 57 58 59 60
 private:
  friend class BasicBlockProfiler;
  friend std::ostream& operator<<(std::ostream& os,
                                  const BasicBlockProfilerData& s);

  V8_EXPORT_PRIVATE void ResetCounts();

61 62
  void CopyFromJSHeap(OnHeapBasicBlockProfilerData js_heap_data);

63 64
  // These vectors are indexed by reverse post-order block number.
  std::vector<int32_t> block_ids_;
65
  std::vector<double> counts_;
66 67 68
  std::string function_name_;
  std::string schedule_;
  std::string code_;
69
  int hash_ = 0;
70 71
};

72 73
class BasicBlockProfiler {
 public:
74
  using DataList = std::list<std::unique_ptr<BasicBlockProfilerData>>;
75

76
  BasicBlockProfiler() = default;
77
  ~BasicBlockProfiler() = default;
78 79
  BasicBlockProfiler(const BasicBlockProfiler&) = delete;
  BasicBlockProfiler& operator=(const BasicBlockProfiler&) = delete;
80

81
  V8_EXPORT_PRIVATE static BasicBlockProfiler* Get();
82 83
  BasicBlockProfilerData* NewData(size_t n_blocks);
  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 90 91
  // snapshot.
  V8_EXPORT_PRIVATE std::vector<bool> GetCoverageBitmap(Isolate* isolate);

92 93 94 95
  const DataList* data_list() { return &data_list_; }

 private:
  DataList data_list_;
96
  base::Mutex data_list_mutex_;
97 98
};

99
std::ostream& operator<<(std::ostream& os, const BasicBlockProfilerData& s);
100 101 102 103

}  // namespace internal
}  // namespace v8

104
#endif  // V8_DIAGNOSTICS_BASIC_BLOCK_PROFILER_H_