v8  7.9.317(node13.2.0)
V8 is Google's open source JavaScript engine
v8-profiler.h
Go to the documentation of this file.
1 // Copyright 2010 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_V8_PROFILER_H_
6 #define V8_V8_PROFILER_H_
7 
8 #include <limits.h>
9 #include <memory>
10 #include <unordered_set>
11 #include <vector>
12 
13 #include "v8.h" // NOLINT(build/include)
14 
18 namespace v8 {
19 
20 class HeapGraphNode;
21 struct HeapStatsUpdate;
22 
23 using NativeObject = void*;
24 using SnapshotObjectId = uint32_t;
25 
27  int script_id;
28  size_t position;
29 };
30 
31 namespace internal {
32 class CpuProfile;
33 } // namespace internal
34 
35 } // namespace v8
36 
37 #ifdef V8_OS_WIN
38 template class V8_EXPORT std::vector<v8::CpuProfileDeoptFrame>;
39 #endif
40 
41 namespace v8 {
42 
45  const char* deopt_reason;
46  std::vector<CpuProfileDeoptFrame> stack;
47 };
48 
49 } // namespace v8
50 
51 #ifdef V8_OS_WIN
52 template class V8_EXPORT std::vector<v8::CpuProfileDeoptInfo>;
53 #endif
54 
55 namespace v8 {
56 
61  public:
62  struct LineTick {
64  int line;
65 
67  unsigned int hit_count;
68  };
69 
70  // An annotation hinting at the source of a CpuProfileNode.
71  enum SourceType {
72  // User-supplied script with associated resource information.
73  kScript = 0,
74  // Native scripts and provided builtins.
75  kBuiltin = 1,
76  // Callbacks into native code.
77  kCallback = 2,
78  // VM-internal functions or state.
79  kInternal = 3,
80  // A node that failed to symbolize.
81  kUnresolved = 4,
82  };
83 
85  Local<String> GetFunctionName() const;
86 
92  const char* GetFunctionNameStr() const;
93 
95  int GetScriptId() const;
96 
98  Local<String> GetScriptResourceName() const;
99 
105  const char* GetScriptResourceNameStr() const;
106 
111  bool IsScriptSharedCrossOrigin() const;
112 
117  int GetLineNumber() const;
118 
123  int GetColumnNumber() const;
124 
128  unsigned int GetHitLineCount() const;
129 
135  bool GetLineTicks(LineTick* entries, unsigned int length) const;
136 
140  const char* GetBailoutReason() const;
141 
145  unsigned GetHitCount() const;
146 
148  V8_DEPRECATED("Use GetScriptId, GetLineNumber, and GetColumnNumber instead.")
149  unsigned GetCallUid() const;
150 
152  unsigned GetNodeId() const;
153 
157  SourceType GetSourceType() const;
158 
160  int GetChildrenCount() const;
161 
163  const CpuProfileNode* GetChild(int index) const;
164 
166  const CpuProfileNode* GetParent() const;
167 
169  const std::vector<CpuProfileDeoptInfo>& GetDeoptInfos() const;
170 
171  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
172  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
173 };
174 
175 
181  public:
183  Local<String> GetTitle() const;
184 
186  const CpuProfileNode* GetTopDownRoot() const;
187 
192  int GetSamplesCount() const;
193 
198  const CpuProfileNode* GetSample(int index) const;
199 
205  int64_t GetSampleTimestamp(int index) const;
206 
211  int64_t GetStartTime() const;
212 
218  int64_t GetEndTime() const;
219 
224  void Delete();
225 };
226 
228  // In the resulting CpuProfile tree, intermediate nodes in a stack trace
229  // (from the root to a leaf) will have line numbers that point to the start
230  // line of the function, rather than the line of the callsite of the child.
232  // In the resulting CpuProfile tree, nodes are separated based on the line
233  // number of their callsite in their parent.
235 };
236 
237 // Determines how names are derived for functions sampled.
239  // Use the immediate name of functions at compilation time.
241  // Use more verbose naming for functions without names, inferred from scope
242  // where possible.
244 };
245 
247  // Enables logging when a profile is active, and disables logging when all
248  // profiles are detached.
250  // Enables logging for the lifetime of the CpuProfiler. Calls to
251  // StartRecording are faster, at the expense of runtime overhead.
253 };
254 
259  public:
260  // Indicates that the sample buffer size should not be explicitly limited.
261  static const unsigned kNoSampleLimit = UINT_MAX;
262 
278  unsigned max_samples = kNoSampleLimit, int sampling_interval_us = 0,
279  MaybeLocal<Context> filter_context = MaybeLocal<Context>());
280 
281  CpuProfilingMode mode() const { return mode_; }
282  unsigned max_samples() const { return max_samples_; }
283  int sampling_interval_us() const { return sampling_interval_us_; }
284 
285  private:
286  friend class internal::CpuProfile;
287 
288  bool has_filter_context() const { return !filter_context_.IsEmpty(); }
289  void* raw_filter_context() const;
290 
291  CpuProfilingMode mode_;
292  unsigned max_samples_;
293  int sampling_interval_us_;
295 };
296 
302  public:
308  static CpuProfiler* New(Isolate* isolate,
311 
317  static void CollectSample(Isolate* isolate);
318 
322  void Dispose();
323 
329  void SetSamplingInterval(int us);
330 
338  void SetUsePreciseSampling(bool);
339 
345  void StartProfiling(Local<String> title, CpuProfilingOptions options);
346 
358  void StartProfiling(
359  Local<String> title, CpuProfilingMode mode, bool record_samples = false,
360  unsigned max_samples = CpuProfilingOptions::kNoSampleLimit);
366  void StartProfiling(Local<String> title, bool record_samples = false);
367 
372  CpuProfile* StopProfiling(Local<String> title);
373 
378  static void UseDetailedSourcePositionsForProfiling(Isolate* isolate);
379 
380  private:
381  CpuProfiler();
382  ~CpuProfiler();
383  CpuProfiler(const CpuProfiler&);
384  CpuProfiler& operator=(const CpuProfiler&);
385 };
386 
392  public:
393  enum Type {
394  kContextVariable = 0, // A variable from a function context.
395  kElement = 1, // An element of an array.
396  kProperty = 2, // A named object property.
397  kInternal = 3, // A link that can't be accessed from JS,
398  // thus, its name isn't a real property name
399  // (e.g. parts of a ConsString).
400  kHidden = 4, // A link that is needed for proper sizes
401  // calculation, but may be hidden from user.
402  kShortcut = 5, // A link that must not be followed during
403  // sizes calculation.
404  kWeak = 6 // A weak reference (ignored by the GC).
405  };
406 
408  Type GetType() const;
409 
414  Local<Value> GetName() const;
415 
417  const HeapGraphNode* GetFromNode() const;
418 
420  const HeapGraphNode* GetToNode() const;
421 };
422 
423 
428  public:
429  enum Type {
430  kHidden = 0, // Hidden node, may be filtered when shown to user.
431  kArray = 1, // An array of elements.
432  kString = 2, // A string.
433  kObject = 3, // A JS object (except for arrays and strings).
434  kCode = 4, // Compiled code.
435  kClosure = 5, // Function closure.
436  kRegExp = 6, // RegExp.
437  kHeapNumber = 7, // Number stored in the heap.
438  kNative = 8, // Native object (not from V8 heap).
439  kSynthetic = 9, // Synthetic object, usually used for grouping
440  // snapshot items together.
441  kConsString = 10, // Concatenated string. A pair of pointers to strings.
442  kSlicedString = 11, // Sliced string. A fragment of another string.
443  kSymbol = 12, // A Symbol (ES6).
444  kBigInt = 13 // BigInt.
445  };
446 
448  Type GetType() const;
449 
455  Local<String> GetName() const;
456 
461  SnapshotObjectId GetId() const;
462 
464  size_t GetShallowSize() const;
465 
467  int GetChildrenCount() const;
468 
470  const HeapGraphEdge* GetChild(int index) const;
471 };
472 
473 
477 class V8_EXPORT OutputStream { // NOLINT
478  public:
479  enum WriteResult {
480  kContinue = 0,
481  kAbort = 1
482  };
483  virtual ~OutputStream() = default;
485  virtual void EndOfStream() = 0;
487  virtual int GetChunkSize() { return 1024; }
493  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
499  virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
500  return kAbort;
501  }
502 };
503 
504 
509  public:
511  kJSON = 0 // See format description near 'Serialize' method.
512  };
513 
515  const HeapGraphNode* GetRoot() const;
516 
518  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
519 
521  int GetNodesCount() const;
522 
524  const HeapGraphNode* GetNode(int index) const;
525 
527  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
528 
534  void Delete();
535 
562  void Serialize(OutputStream* stream,
563  SerializationFormat format = kJSON) const;
564 };
565 
566 
571 class V8_EXPORT ActivityControl { // NOLINT
572  public:
574  kContinue = 0,
575  kAbort = 1
576  };
577  virtual ~ActivityControl() = default;
582  virtual ControlOption ReportProgressValue(int done, int total) = 0;
583 };
584 
585 
591  public:
592  struct Allocation {
596  size_t size;
597 
601  unsigned int count;
602  };
603 
607  struct Node {
613 
619 
625 
630 
636 
642 
646  uint32_t node_id;
647 
653  std::vector<Node*> children;
654 
658  std::vector<Allocation> allocations;
659  };
660 
664  struct Sample {
668  uint32_t node_id;
669 
673  size_t size;
674 
678  unsigned int count;
679 
684  uint64_t sample_id;
685  };
686 
692  virtual Node* GetRootNode() = 0;
693  virtual const std::vector<Sample>& GetSamples() = 0;
694 
695  virtual ~AllocationProfile() = default;
696 
697  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
698  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
699 };
700 
716  public:
717  class Node {
718  public:
719  Node() = default;
720  virtual ~Node() = default;
721  virtual const char* Name() = 0;
722  virtual size_t SizeInBytes() = 0;
728  virtual Node* WrapperNode() { return nullptr; }
729  virtual bool IsRootNode() { return false; }
731  virtual bool IsEmbedderNode() { return true; }
735  virtual const char* NamePrefix() { return nullptr; }
736 
741  virtual NativeObject GetNativeObject() { return nullptr; }
742 
743  Node(const Node&) = delete;
744  Node& operator=(const Node&) = delete;
745  };
746 
751  virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
752 
757  virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
758 
767  virtual void AddEdge(Node* from, Node* to, const char* name = nullptr) = 0;
768 
769  virtual ~EmbedderGraph() = default;
770 };
771 
777  public:
779  kSamplingNoFlags = 0,
780  kSamplingForceGC = 1 << 0,
781  };
782 
789  typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
790  v8::EmbedderGraph* graph,
791  void* data);
792 
794  int GetSnapshotCount();
795 
797  const HeapSnapshot* GetHeapSnapshot(int index);
798 
803  SnapshotObjectId GetObjectId(Local<Value> value);
804 
809  SnapshotObjectId GetObjectId(NativeObject value);
810 
815  Local<Value> FindObjectById(SnapshotObjectId id);
816 
822  void ClearObjectIds();
823 
829  static const SnapshotObjectId kUnknownObjectId = 0;
830 
835  public:
840  virtual const char* GetName(Local<Object> object) = 0;
841 
842  protected:
843  virtual ~ObjectNameResolver() = default;
844  };
845 
849  const HeapSnapshot* TakeHeapSnapshot(
850  ActivityControl* control = nullptr,
851  ObjectNameResolver* global_object_name_resolver = nullptr);
852 
862  void StartTrackingHeapObjects(bool track_allocations = false);
863 
877  SnapshotObjectId GetHeapStats(OutputStream* stream,
878  int64_t* timestamp_us = nullptr);
879 
885  void StopTrackingHeapObjects();
886 
914  bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
915  int stack_depth = 16,
916  SamplingFlags flags = kSamplingNoFlags);
917 
921  void StopSamplingHeapProfiler();
922 
929  AllocationProfile* GetAllocationProfile();
930 
935  void DeleteAllHeapSnapshots();
936 
937  void AddBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
938  void* data);
939  void RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
940  void* data);
941 
947  static const uint16_t kPersistentHandleNoClassId = 0;
948 
949  private:
950  HeapProfiler();
951  ~HeapProfiler();
952  HeapProfiler(const HeapProfiler&);
953  HeapProfiler& operator=(const HeapProfiler&);
954 };
955 
961  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
962  : index(index), count(count), size(size) { }
963  uint32_t index; // Index of the time interval that was changed.
964  uint32_t count; // New value of count field for the interval with this index.
965  uint32_t size; // New value of size field for the interval with this index.
966 };
967 
968 #define CODE_EVENTS_LIST(V) \
969  V(Builtin) \
970  V(Callback) \
971  V(Eval) \
972  V(Function) \
973  V(InterpretedFunction) \
974  V(Handler) \
975  V(BytecodeHandler) \
976  V(LazyCompile) \
977  V(RegExp) \
978  V(Script) \
979  V(Stub) \
980  V(Relocation)
981 
988 #define V(Name) , k##Name##Type
990 #undef V
991 };
992 
997  public:
998  uintptr_t GetCodeStartAddress();
999  size_t GetCodeSize();
1000  Local<String> GetFunctionName();
1001  Local<String> GetScriptName();
1002  int GetScriptLine();
1003  int GetScriptColumn();
1009  CodeEventType GetCodeType();
1010  const char* GetComment();
1011 
1012  static const char* GetCodeEventTypeName(CodeEventType code_event_type);
1013 
1014  uintptr_t GetPreviousCodeStartAddress();
1015 };
1016 
1021  public:
1027  explicit CodeEventHandler(Isolate* isolate);
1028  virtual ~CodeEventHandler();
1029 
1038  virtual void Handle(CodeEvent* code_event) = 0;
1039 
1044  void Enable();
1045 
1050  void Disable();
1051 
1052  private:
1053  CodeEventHandler();
1055  CodeEventHandler& operator=(const CodeEventHandler&);
1056  void* internal_listener_;
1057 };
1058 
1059 } // namespace v8
1060 
1061 
1062 #endif // V8_V8_PROFILER_H_
v8::HeapGraphNode::Type
Type
Definition: v8-profiler.h:429
v8::MaybeLocal
Definition: v8.h:90
v8::AllocationProfile::Node::column_number
int column_number
Definition: v8-profiler.h:641
v8::CpuProfilingMode
CpuProfilingMode
Definition: v8-profiler.h:227
v8::AllocationProfile::Node::line_number
int line_number
Definition: v8-profiler.h:635
v8::Name
Definition: v8.h:2816
v8::kLazyLogging
Definition: v8-profiler.h:249
v8::kCallerLineNumbers
Definition: v8-profiler.h:234
v8::CpuProfilingNamingMode
CpuProfilingNamingMode
Definition: v8-profiler.h:238
v8::AllocationProfile::Sample::sample_id
uint64_t sample_id
Definition: v8-profiler.h:684
v8::CodeEvent
Definition: v8-profiler.h:996
v8::kDebugNaming
Definition: v8-profiler.h:243
v8::ActivityControl::ControlOption
ControlOption
Definition: v8-profiler.h:573
v8::CpuProfileNode::LineTick::line
int line
Definition: v8-profiler.h:64
v8::HeapProfiler::SamplingFlags
SamplingFlags
Definition: v8-profiler.h:778
v8::OutputStream::GetChunkSize
virtual int GetChunkSize()
Definition: v8-profiler.h:487
v8::AllocationProfile::Allocation::size
size_t size
Definition: v8-profiler.h:596
v8::AllocationProfile::Sample
Definition: v8-profiler.h:664
v8::AllocationProfile::Node::node_id
uint32_t node_id
Definition: v8-profiler.h:646
v8::Handle
Local< T > Handle
Definition: v8.h:337
v8::CodeEventType
CodeEventType
Definition: v8-profiler.h:986
v8::CpuProfileNode
Definition: v8-profiler.h:60
v8::CpuProfilingOptions::max_samples
unsigned max_samples() const
Definition: v8-profiler.h:282
v8::CpuProfileNode::LineTick
Definition: v8-profiler.h:62
v8::CpuProfileNode::LineTick::hit_count
unsigned int hit_count
Definition: v8-profiler.h:67
v8::HeapGraphNode
Definition: v8-profiler.h:427
v8::CpuProfilingOptions::mode
CpuProfilingMode mode() const
Definition: v8-profiler.h:281
v8::CpuProfilingOptions::sampling_interval_us
int sampling_interval_us() const
Definition: v8-profiler.h:283
v8::CpuProfilingOptions
Definition: v8-profiler.h:258
v8::SnapshotObjectId
uint32_t SnapshotObjectId
Definition: v8-profiler.h:24
v8::HeapGraphEdge
Definition: v8-profiler.h:391
v8::HeapStatsUpdate::size
uint32_t size
Definition: v8-profiler.h:965
v8::EmbedderGraph::Node::NamePrefix
virtual const char * NamePrefix()
Definition: v8-profiler.h:735
v8::HeapProfiler::ObjectNameResolver
Definition: v8-profiler.h:834
v8::HeapStatsUpdate::index
uint32_t index
Definition: v8-profiler.h:963
v8::AllocationProfile::Node::children
std::vector< Node * > children
Definition: v8-profiler.h:653
V8_EXPORT
#define V8_EXPORT
Definition: v8config.h:455
v8::Persistent
Definition: v8.h:95
v8::Message
Definition: v8.h:1946
v8::HeapStatsUpdate
Definition: v8-profiler.h:960
v8::AllocationProfile::Sample::size
size_t size
Definition: v8-profiler.h:673
v8::EmbedderGraph
Definition: v8-profiler.h:715
CODE_EVENTS_LIST
#define CODE_EVENTS_LIST(V)
Definition: v8-profiler.h:968
v8::Isolate
Definition: v8.h:7773
v8::CodeEventHandler
Definition: v8-profiler.h:1020
v8::kLeafNodeLineNumbers
Definition: v8-profiler.h:231
V
#define V(Name)
v8::AllocationProfile
Definition: v8-profiler.h:590
v8::kStandardNaming
Definition: v8-profiler.h:240
v8
Definition: libplatform.h:15
v8::ActivityControl
Definition: v8-profiler.h:571
v8::CpuProfileDeoptFrame::script_id
int script_id
Definition: v8-profiler.h:27
v8::Local
Definition: v8.h:88
v8::AllocationProfile::Sample::count
unsigned int count
Definition: v8-profiler.h:678
v8::AllocationProfile::Allocation
Definition: v8-profiler.h:592
v8::AllocationProfile::Node::script_id
int script_id
Definition: v8-profiler.h:624
v8::CpuProfilingLoggingMode
CpuProfilingLoggingMode
Definition: v8-profiler.h:246
v8::AllocationProfile::Node::script_name
Local< String > script_name
Definition: v8-profiler.h:618
v8::kUnknownType
Definition: v8-profiler.h:987
V8_DEPRECATED
#define V8_DEPRECATED(message)
Definition: v8config.h:393
v8::AllocationProfile::Node
Definition: v8-profiler.h:607
v8::HeapStatsUpdate::HeapStatsUpdate
HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
Definition: v8-profiler.h:961
v8::AllocationProfile::Node::name
Local< String > name
Definition: v8-profiler.h:612
v8::CpuProfileDeoptInfo
Definition: v8-profiler.h:43
v8::EmbedderGraph::Node::IsRootNode
virtual bool IsRootNode()
Definition: v8-profiler.h:729
v8::OutputStream
Definition: v8-profiler.h:477
v8::CpuProfileDeoptFrame
Definition: v8-profiler.h:26
v8::EmbedderGraph::Node::GetNativeObject
virtual NativeObject GetNativeObject()
Definition: v8-profiler.h:741
v8.h
v8::OutputStream::WriteHeapStatsChunk
virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate *data, int count)
Definition: v8-profiler.h:499
v8::CpuProfileDeoptInfo::deopt_reason
const char * deopt_reason
Definition: v8-profiler.h:45
v8::OutputStream::WriteResult
WriteResult
Definition: v8-profiler.h:479
v8::NativeObject
void * NativeObject
Definition: v8-profiler.h:23
v8::AllocationProfile::Node::allocations
std::vector< Allocation > allocations
Definition: v8-profiler.h:658
v8::EmbedderGraph::Node
Definition: v8-profiler.h:717
v8::HeapStatsUpdate::count
uint32_t count
Definition: v8-profiler.h:964
v8::CpuProfileNode::SourceType
SourceType
Definition: v8-profiler.h:71
v8::kEagerLogging
Definition: v8-profiler.h:252
v8::AllocationProfile::Node::start_position
int start_position
Definition: v8-profiler.h:629
v8::CpuProfiler
Definition: v8-profiler.h:301
v8::EmbedderGraph::Node::WrapperNode
virtual Node * WrapperNode()
Definition: v8-profiler.h:728
v8::AllocationProfile::Sample::node_id
uint32_t node_id
Definition: v8-profiler.h:668
v8::HeapSnapshot::SerializationFormat
SerializationFormat
Definition: v8-profiler.h:510
v8::HeapSnapshot
Definition: v8-profiler.h:508
v8::CpuProfile
Definition: v8-profiler.h:180
v8::EmbedderGraph::Node::IsEmbedderNode
virtual bool IsEmbedderNode()
Definition: v8-profiler.h:731
v8::CpuProfileDeoptInfo::stack
std::vector< CpuProfileDeoptFrame > stack
Definition: v8-profiler.h:46
v8::AllocationProfile::Allocation::count
unsigned int count
Definition: v8-profiler.h:601
v8::HeapProfiler
Definition: v8-profiler.h:776
v8::CpuProfileDeoptFrame::position
size_t position
Definition: v8-profiler.h:28
v8::HeapGraphEdge::Type
Type
Definition: v8-profiler.h:393