V8 API Reference Guide generated from the header files
v8-profiler.h
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 <unordered_set>
9 #include <vector>
10 #include "v8.h" // NOLINT(build/include)
11 
15 namespace v8 {
16 
17 class HeapGraphNode;
18 struct HeapStatsUpdate;
19 
20 typedef uint32_t SnapshotObjectId;
21 
22 
24  int script_id;
25  size_t position;
26 };
27 
28 } // namespace v8
29 
30 #ifdef V8_OS_WIN
31 template class V8_EXPORT std::vector<v8::CpuProfileDeoptFrame>;
32 #endif
33 
34 namespace v8 {
35 
36 struct V8_EXPORT CpuProfileDeoptInfo {
38  const char* deopt_reason;
39  std::vector<CpuProfileDeoptFrame> stack;
40 };
41 
42 } // namespace v8
43 
44 #ifdef V8_OS_WIN
45 template class V8_EXPORT std::vector<v8::CpuProfileDeoptInfo>;
46 #endif
47 
48 namespace v8 {
49 
55 class V8_EXPORT TracingCpuProfiler {
56  public:
57  V8_DEPRECATED(
58  "The profiler is created automatically with the isolate.\n"
59  "No need to create it explicitly.",
60  static std::unique_ptr<TracingCpuProfiler> Create(Isolate*));
61 
62  virtual ~TracingCpuProfiler() = default;
63 
64  protected:
65  TracingCpuProfiler() = default;
66 };
67 
68 // TickSample captures the information collected for each sample.
69 struct TickSample {
70  // Internal profiling (with --prof + tools/$OS-tick-processor) wants to
71  // include the runtime function we're calling. Externally exposed tick
72  // samples don't care.
73  enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame };
74 
75  TickSample()
76  : state(OTHER),
77  pc(nullptr),
78  external_callback_entry(nullptr),
79  frames_count(0),
80  has_external_callback(false),
81  update_stats(true) {}
82 
95  void Init(Isolate* isolate, const v8::RegisterState& state,
96  RecordCEntryFrame record_c_entry_frame, bool update_stats,
97  bool use_simulator_reg_state = true);
118  static bool GetStackSample(Isolate* isolate, v8::RegisterState* state,
119  RecordCEntryFrame record_c_entry_frame,
120  void** frames, size_t frames_limit,
121  v8::SampleInfo* sample_info,
122  bool use_simulator_reg_state = true);
123  StateTag state; // The state of the VM.
124  void* pc; // Instruction pointer.
125  union {
126  void* tos; // Top stack value (*sp).
127  void* external_callback_entry;
128  };
129  static const unsigned kMaxFramesCountLog2 = 8;
130  static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1;
131  void* stack[kMaxFramesCount]; // Call stack.
132  unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames.
133  bool has_external_callback : 1;
134  bool update_stats : 1; // Whether the sample should update aggregated stats.
135 };
136 
140 class V8_EXPORT CpuProfileNode {
141  public:
142  struct LineTick {
144  int line;
145 
147  unsigned int hit_count;
148  };
149 
151  Local<String> GetFunctionName() const;
152 
158  const char* GetFunctionNameStr() const;
159 
161  int GetScriptId() const;
162 
164  Local<String> GetScriptResourceName() const;
165 
171  const char* GetScriptResourceNameStr() const;
172 
177  int GetLineNumber() const;
178 
183  int GetColumnNumber() const;
184 
188  unsigned int GetHitLineCount() const;
189 
195  bool GetLineTicks(LineTick* entries, unsigned int length) const;
196 
200  const char* GetBailoutReason() const;
201 
205  unsigned GetHitCount() const;
206 
208  V8_DEPRECATE_SOON(
209  "Use GetScriptId, GetLineNumber, and GetColumnNumber instead.",
210  unsigned GetCallUid() const);
211 
213  unsigned GetNodeId() const;
214 
216  int GetChildrenCount() const;
217 
219  const CpuProfileNode* GetChild(int index) const;
220 
222  const std::vector<CpuProfileDeoptInfo>& GetDeoptInfos() const;
223 
224  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
225  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
226 };
227 
228 
233 class V8_EXPORT CpuProfile {
234  public:
236  Local<String> GetTitle() const;
237 
239  const CpuProfileNode* GetTopDownRoot() const;
240 
245  int GetSamplesCount() const;
246 
251  const CpuProfileNode* GetSample(int index) const;
252 
258  int64_t GetSampleTimestamp(int index) const;
259 
264  int64_t GetStartTime() const;
265 
271  int64_t GetEndTime() const;
272 
277  void Delete();
278 };
279 
280 enum CpuProfilingMode {
281  // In the resulting CpuProfile tree, intermediate nodes in a stack trace
282  // (from the root to a leaf) will have line numbers that point to the start
283  // line of the function, rather than the line of the callsite of the child.
284  kLeafNodeLineNumbers,
285  // In the resulting CpuProfile tree, nodes are separated based on the line
286  // number of their callsite in their parent.
287  kCallerLineNumbers,
288 };
289 
294 class V8_EXPORT CpuProfiler {
295  public:
301  static CpuProfiler* New(Isolate* isolate);
302 
308  static void CollectSample(Isolate* isolate);
309 
313  void Dispose();
314 
320  void SetSamplingInterval(int us);
321 
333  void StartProfiling(Local<String> title, CpuProfilingMode mode,
334  bool record_samples = false);
340  void StartProfiling(Local<String> title, bool record_samples = false);
341 
346  CpuProfile* StopProfiling(Local<String> title);
347 
353  V8_DEPRECATED("Use static CollectSample(Isolate*) instead.",
354  void CollectSample());
355 
359  V8_DEPRECATED("Use Isolate::SetIdle(bool) instead.",
360  void SetIdle(bool is_idle));
361 
362  private:
363  CpuProfiler();
364  ~CpuProfiler();
365  CpuProfiler(const CpuProfiler&);
366  CpuProfiler& operator=(const CpuProfiler&);
367 };
368 
369 
374 class V8_EXPORT HeapGraphEdge {
375  public:
376  enum Type {
377  kContextVariable = 0, // A variable from a function context.
378  kElement = 1, // An element of an array.
379  kProperty = 2, // A named object property.
380  kInternal = 3, // A link that can't be accessed from JS,
381  // thus, its name isn't a real property name
382  // (e.g. parts of a ConsString).
383  kHidden = 4, // A link that is needed for proper sizes
384  // calculation, but may be hidden from user.
385  kShortcut = 5, // A link that must not be followed during
386  // sizes calculation.
387  kWeak = 6 // A weak reference (ignored by the GC).
388  };
389 
391  Type GetType() const;
392 
397  Local<Value> GetName() const;
398 
400  const HeapGraphNode* GetFromNode() const;
401 
403  const HeapGraphNode* GetToNode() const;
404 };
405 
406 
410 class V8_EXPORT HeapGraphNode {
411  public:
412  enum Type {
413  kHidden = 0, // Hidden node, may be filtered when shown to user.
414  kArray = 1, // An array of elements.
415  kString = 2, // A string.
416  kObject = 3, // A JS object (except for arrays and strings).
417  kCode = 4, // Compiled code.
418  kClosure = 5, // Function closure.
419  kRegExp = 6, // RegExp.
420  kHeapNumber = 7, // Number stored in the heap.
421  kNative = 8, // Native object (not from V8 heap).
422  kSynthetic = 9, // Synthetic object, usually used for grouping
423  // snapshot items together.
424  kConsString = 10, // Concatenated string. A pair of pointers to strings.
425  kSlicedString = 11, // Sliced string. A fragment of another string.
426  kSymbol = 12, // A Symbol (ES6).
427  kBigInt = 13 // BigInt.
428  };
429 
431  Type GetType() const;
432 
438  Local<String> GetName() const;
439 
444  SnapshotObjectId GetId() const;
445 
447  size_t GetShallowSize() const;
448 
450  int GetChildrenCount() const;
451 
453  const HeapGraphEdge* GetChild(int index) const;
454 };
455 
456 
460 class V8_EXPORT OutputStream { // NOLINT
461  public:
462  enum WriteResult {
463  kContinue = 0,
464  kAbort = 1
465  };
466  virtual ~OutputStream() {}
468  virtual void EndOfStream() = 0;
470  virtual int GetChunkSize() { return 1024; }
476  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
482  virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
483  return kAbort;
484  }
485 };
486 
487 
491 class V8_EXPORT HeapSnapshot {
492  public:
493  enum SerializationFormat {
494  kJSON = 0 // See format description near 'Serialize' method.
495  };
496 
498  const HeapGraphNode* GetRoot() const;
499 
501  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
502 
504  int GetNodesCount() const;
505 
507  const HeapGraphNode* GetNode(int index) const;
508 
510  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
511 
517  void Delete();
518 
545  void Serialize(OutputStream* stream,
546  SerializationFormat format = kJSON) const;
547 };
548 
549 
554 class V8_EXPORT ActivityControl { // NOLINT
555  public:
556  enum ControlOption {
557  kContinue = 0,
558  kAbort = 1
559  };
560  virtual ~ActivityControl() {}
565  virtual ControlOption ReportProgressValue(int done, int total) = 0;
566 };
567 
568 
573 class V8_EXPORT AllocationProfile {
574  public:
575  struct Allocation {
579  size_t size;
580 
584  unsigned int count;
585  };
586 
590  struct Node {
596 
602 
608 
613 
619 
625 
631  std::vector<Node*> children;
632 
636  std::vector<Allocation> allocations;
637  };
638 
644  virtual Node* GetRootNode() = 0;
645 
646  virtual ~AllocationProfile() {}
647 
648  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
649  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
650 };
651 
666 class V8_EXPORT EmbedderGraph {
667  public:
668  class Node {
669  public:
670  Node() = default;
671  virtual ~Node() = default;
672  virtual const char* Name() = 0;
673  virtual size_t SizeInBytes() = 0;
679  virtual Node* WrapperNode() { return nullptr; }
680  virtual bool IsRootNode() { return false; }
682  virtual bool IsEmbedderNode() { return true; }
686  virtual const char* NamePrefix() { return nullptr; }
687 
688  private:
689  Node(const Node&) = delete;
690  Node& operator=(const Node&) = delete;
691  };
692 
697  virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
698 
703  virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
704 
710  virtual void AddEdge(Node* from, Node* to) = 0;
711 
712  virtual ~EmbedderGraph() = default;
713 };
714 
719 class V8_EXPORT HeapProfiler {
720  public:
721  enum SamplingFlags {
722  kSamplingNoFlags = 0,
723  kSamplingForceGC = 1 << 0,
724  };
725 
726  typedef std::unordered_set<const v8::PersistentBase<v8::Value>*>
727  RetainerChildren;
728  typedef std::vector<std::pair<v8::RetainedObjectInfo*, RetainerChildren>>
729  RetainerGroups;
730  typedef std::vector<std::pair<const v8::PersistentBase<v8::Value>*,
732  RetainerEdges;
733 
734  struct RetainerInfos {
735  RetainerGroups groups;
736  RetainerEdges edges;
737  };
738 
742  typedef RetainerInfos (*GetRetainerInfosCallback)(v8::Isolate* isolate);
743 
750  typedef RetainedObjectInfo* (*WrapperInfoCallback)(uint16_t class_id,
751  Local<Value> wrapper);
752 
759  typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
760  v8::EmbedderGraph* graph,
761  void* data);
762 
764  typedef void (*LegacyBuildEmbedderGraphCallback)(v8::Isolate* isolate,
765  v8::EmbedderGraph* graph);
766 
768  int GetSnapshotCount();
769 
771  const HeapSnapshot* GetHeapSnapshot(int index);
772 
777  SnapshotObjectId GetObjectId(Local<Value> value);
778 
783  Local<Value> FindObjectById(SnapshotObjectId id);
784 
790  void ClearObjectIds();
791 
797  static const SnapshotObjectId kUnknownObjectId = 0;
798 
803  public:
808  virtual const char* GetName(Local<Object> object) = 0;
809 
810  protected:
811  virtual ~ObjectNameResolver() {}
812  };
813 
817  const HeapSnapshot* TakeHeapSnapshot(
818  ActivityControl* control = NULL,
819  ObjectNameResolver* global_object_name_resolver = NULL);
820 
830  void StartTrackingHeapObjects(bool track_allocations = false);
831 
845  SnapshotObjectId GetHeapStats(OutputStream* stream,
846  int64_t* timestamp_us = NULL);
847 
853  void StopTrackingHeapObjects();
854 
882  bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
883  int stack_depth = 16,
884  SamplingFlags flags = kSamplingNoFlags);
885 
889  void StopSamplingHeapProfiler();
890 
897  AllocationProfile* GetAllocationProfile();
898 
903  void DeleteAllHeapSnapshots();
904 
906  V8_DEPRECATED(
907  "Use AddBuildEmbedderGraphCallback to provide info about embedder nodes",
908  void SetWrapperClassInfoProvider(uint16_t class_id,
909  WrapperInfoCallback callback));
910 
911  V8_DEPRECATED(
912  "Use AddBuildEmbedderGraphCallback to provide info about embedder nodes",
913  void SetGetRetainerInfosCallback(GetRetainerInfosCallback callback));
914 
915  V8_DEPRECATE_SOON(
916  "Use AddBuildEmbedderGraphCallback to provide info about embedder nodes",
917  void SetBuildEmbedderGraphCallback(
918  LegacyBuildEmbedderGraphCallback callback));
919  void AddBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
920  void* data);
921  void RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
922  void* data);
923 
929  static const uint16_t kPersistentHandleNoClassId = 0;
930 
931  private:
932  HeapProfiler();
933  ~HeapProfiler();
934  HeapProfiler(const HeapProfiler&);
935  HeapProfiler& operator=(const HeapProfiler&);
936 };
937 
962 class V8_EXPORT RetainedObjectInfo { // NOLINT
963  public:
965  virtual void Dispose() = 0;
966 
968  virtual bool IsEquivalent(RetainedObjectInfo* other) = 0;
969 
974  virtual intptr_t GetHash() = 0;
975 
980  virtual const char* GetLabel() = 0;
981 
991  virtual const char* GetGroupLabel() { return GetLabel(); }
992 
997  virtual intptr_t GetElementCount() { return -1; }
998 
1000  virtual intptr_t GetSizeInBytes() { return -1; }
1001 
1002  protected:
1003  RetainedObjectInfo() {}
1004  virtual ~RetainedObjectInfo() {}
1005 
1006  private:
1007  RetainedObjectInfo(const RetainedObjectInfo&);
1008  RetainedObjectInfo& operator=(const RetainedObjectInfo&);
1009 };
1010 
1011 
1017  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
1018  : index(index), count(count), size(size) { }
1019  uint32_t index; // Index of the time interval that was changed.
1020  uint32_t count; // New value of count field for the interval with this index.
1021  uint32_t size; // New value of size field for the interval with this index.
1022 };
1023 
1024 #define CODE_EVENTS_LIST(V) \
1025  V(Builtin) \
1026  V(Callback) \
1027  V(Eval) \
1028  V(Function) \
1029  V(InterpretedFunction) \
1030  V(Handler) \
1031  V(BytecodeHandler) \
1032  V(LazyCompile) \
1033  V(RegExp) \
1034  V(Script) \
1035  V(Stub)
1036 
1042  kUnknownType = 0
1043 #define V(Name) , k##Name##Type
1044  CODE_EVENTS_LIST(V)
1045 #undef V
1046 };
1047 
1051 class V8_EXPORT CodeEvent {
1052  public:
1053  uintptr_t GetCodeStartAddress();
1054  size_t GetCodeSize();
1055  Local<String> GetFunctionName();
1056  Local<String> GetScriptName();
1057  int GetScriptLine();
1058  int GetScriptColumn();
1064  CodeEventType GetCodeType();
1065  const char* GetComment();
1066 
1067  static const char* GetCodeEventTypeName(CodeEventType code_event_type);
1068 };
1069 
1073 class V8_EXPORT CodeEventHandler {
1074  public:
1080  explicit CodeEventHandler(Isolate* isolate);
1081  virtual ~CodeEventHandler();
1082 
1083  virtual void Handle(CodeEvent* code_event) = 0;
1084 
1085  void Enable();
1086  void Disable();
1087 
1088  private:
1089  CodeEventHandler();
1091  CodeEventHandler& operator=(const CodeEventHandler&);
1092  void* internal_listener_;
1093 };
1094 
1095 } // namespace v8
1096 
1097 
1098 #endif // V8_V8_PROFILER_H_
unsigned int hit_count
Definition: v8-profiler.h:147
Definition: v8-profiler.h:962
Definition: v8-profiler.h:575
Definition: v8-profiler.h:233
virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate *data, int count)
Definition: v8-profiler.h:482
const char * deopt_reason
Definition: v8-profiler.h:38
virtual Node * WrapperNode()
Definition: v8-profiler.h:679
size_t size
Definition: v8-profiler.h:579
int script_id
Definition: v8-profiler.h:607
Definition: v8-profiler.h:1073
virtual intptr_t GetElementCount()
Definition: v8-profiler.h:997
int start_position
Definition: v8-profiler.h:612
unsigned int count
Definition: v8-profiler.h:584
Definition: v8-profiler.h:410
Definition: v8.h:2635
Definition: v8-profiler.h:36
Definition: v8-profiler.h:69
Local< String > name
Definition: v8-profiler.h:595
virtual bool IsEmbedderNode()
Definition: v8-profiler.h:682
Definition: v8.h:1952
Definition: v8.h:114
Local< String > script_name
Definition: v8-profiler.h:601
virtual int GetChunkSize()
Definition: v8-profiler.h:470
Definition: v8-profiler.h:1016
std::vector< Node * > children
Definition: v8-profiler.h:631
Definition: v8-profiler.h:374
virtual intptr_t GetSizeInBytes()
Definition: v8-profiler.h:1000
Definition: v8-platform.h:16
Definition: v8-profiler.h:590
Definition: v8-profiler.h:23
Definition: v8-profiler.h:142
Definition: v8-profiler.h:573
int column_number
Definition: v8-profiler.h:624
Definition: v8-profiler.h:460
Definition: v8-profiler.h:734
Definition: v8-profiler.h:554
Definition: v8.h:1960
Definition: v8-profiler.h:294
CodeEventType
Definition: v8-profiler.h:1041
Definition: v8-profiler.h:802
virtual const char * GetGroupLabel()
Definition: v8-profiler.h:991
Definition: v8-profiler.h:666
Definition: v8-profiler.h:719
virtual const char * NamePrefix()
Definition: v8-profiler.h:686
Definition: v8-profiler.h:668
Definition: v8-profiler.h:140
Definition: v8.h:7191
std::vector< Allocation > allocations
Definition: v8-profiler.h:636
int line_number
Definition: v8-profiler.h:618
Definition: v8-profiler.h:55
Definition: v8-profiler.h:1051
Definition: v8-profiler.h:491
int line
Definition: v8-profiler.h:144
Definition: v8.h:119