v8  7.0.276(node11.14.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 <unordered_set>
9 #include <vector>
10 #include "v8.h" // NOLINT(build/include)
11 
15 namespace v8 {
16 
17 class HeapGraphNode;
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 
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 
50 // TickSample captures the information collected for each sample.
51 struct TickSample {
52  // Internal profiling (with --prof + tools/$OS-tick-processor) wants to
53  // include the runtime function we're calling. Externally exposed tick
54  // samples don't care.
55  enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame };
56 
58  : state(OTHER),
59  pc(nullptr),
60  external_callback_entry(nullptr),
61  frames_count(0),
62  has_external_callback(false),
63  update_stats(true) {}
64 
77  void Init(Isolate* isolate, const v8::RegisterState& state,
78  RecordCEntryFrame record_c_entry_frame, bool update_stats,
79  bool use_simulator_reg_state = true);
100  static bool GetStackSample(Isolate* isolate, v8::RegisterState* state,
101  RecordCEntryFrame record_c_entry_frame,
102  void** frames, size_t frames_limit,
103  v8::SampleInfo* sample_info,
104  bool use_simulator_reg_state = true);
105  StateTag state; // The state of the VM.
106  void* pc; // Instruction pointer.
107  union {
108  void* tos; // Top stack value (*sp).
110  };
111  static const unsigned kMaxFramesCountLog2 = 8;
112  static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1;
113  void* stack[kMaxFramesCount]; // Call stack.
114  unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames.
116  bool update_stats : 1; // Whether the sample should update aggregated stats.
117 };
118 
123  public:
124  struct LineTick {
126  int line;
127 
129  unsigned int hit_count;
130  };
131 
133  Local<String> GetFunctionName() const;
134 
140  const char* GetFunctionNameStr() const;
141 
143  int GetScriptId() const;
144 
146  Local<String> GetScriptResourceName() const;
147 
153  const char* GetScriptResourceNameStr() const;
154 
159  int GetLineNumber() const;
160 
165  int GetColumnNumber() const;
166 
170  unsigned int GetHitLineCount() const;
171 
177  bool GetLineTicks(LineTick* entries, unsigned int length) const;
178 
182  const char* GetBailoutReason() const;
183 
187  unsigned GetHitCount() const;
188 
191  "Use GetScriptId, GetLineNumber, and GetColumnNumber instead.",
192  unsigned GetCallUid() const);
193 
195  unsigned GetNodeId() const;
196 
198  int GetChildrenCount() const;
199 
201  const CpuProfileNode* GetChild(int index) const;
202 
204  const std::vector<CpuProfileDeoptInfo>& GetDeoptInfos() const;
205 
206  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
207  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
208 };
209 
210 
216  public:
218  Local<String> GetTitle() const;
219 
221  const CpuProfileNode* GetTopDownRoot() const;
222 
227  int GetSamplesCount() const;
228 
233  const CpuProfileNode* GetSample(int index) const;
234 
240  int64_t GetSampleTimestamp(int index) const;
241 
246  int64_t GetStartTime() const;
247 
253  int64_t GetEndTime() const;
254 
259  void Delete();
260 };
261 
263  // In the resulting CpuProfile tree, intermediate nodes in a stack trace
264  // (from the root to a leaf) will have line numbers that point to the start
265  // line of the function, rather than the line of the callsite of the child.
267  // In the resulting CpuProfile tree, nodes are separated based on the line
268  // number of their callsite in their parent.
270 };
271 
277  public:
283  static CpuProfiler* New(Isolate* isolate);
284 
290  static void CollectSample(Isolate* isolate);
291 
295  void Dispose();
296 
302  void SetSamplingInterval(int us);
303 
315  void StartProfiling(Local<String> title, CpuProfilingMode mode,
316  bool record_samples = false);
322  void StartProfiling(Local<String> title, bool record_samples = false);
323 
328  CpuProfile* StopProfiling(Local<String> title);
329 
335  V8_DEPRECATED("Use static CollectSample(Isolate*) instead.",
336  void CollectSample());
337 
341  V8_DEPRECATED("Use Isolate::SetIdle(bool) instead.",
342  void SetIdle(bool is_idle));
343 
344  private:
345  CpuProfiler();
346  ~CpuProfiler();
347  CpuProfiler(const CpuProfiler&);
348  CpuProfiler& operator=(const CpuProfiler&);
349 };
350 
351 
357  public:
358  enum Type {
359  kContextVariable = 0, // A variable from a function context.
360  kElement = 1, // An element of an array.
361  kProperty = 2, // A named object property.
362  kInternal = 3, // A link that can't be accessed from JS,
363  // thus, its name isn't a real property name
364  // (e.g. parts of a ConsString).
365  kHidden = 4, // A link that is needed for proper sizes
366  // calculation, but may be hidden from user.
367  kShortcut = 5, // A link that must not be followed during
368  // sizes calculation.
369  kWeak = 6 // A weak reference (ignored by the GC).
370  };
371 
373  Type GetType() const;
374 
379  Local<Value> GetName() const;
380 
382  const HeapGraphNode* GetFromNode() const;
383 
385  const HeapGraphNode* GetToNode() const;
386 };
387 
388 
393  public:
394  enum Type {
395  kHidden = 0, // Hidden node, may be filtered when shown to user.
396  kArray = 1, // An array of elements.
397  kString = 2, // A string.
398  kObject = 3, // A JS object (except for arrays and strings).
399  kCode = 4, // Compiled code.
400  kClosure = 5, // Function closure.
401  kRegExp = 6, // RegExp.
402  kHeapNumber = 7, // Number stored in the heap.
403  kNative = 8, // Native object (not from V8 heap).
404  kSynthetic = 9, // Synthetic object, usually used for grouping
405  // snapshot items together.
406  kConsString = 10, // Concatenated string. A pair of pointers to strings.
407  kSlicedString = 11, // Sliced string. A fragment of another string.
408  kSymbol = 12, // A Symbol (ES6).
409  kBigInt = 13 // BigInt.
410  };
411 
413  Type GetType() const;
414 
420  Local<String> GetName() const;
421 
426  SnapshotObjectId GetId() const;
427 
429  size_t GetShallowSize() const;
430 
432  int GetChildrenCount() const;
433 
435  const HeapGraphEdge* GetChild(int index) const;
436 };
437 
438 
442 class V8_EXPORT OutputStream { // NOLINT
443  public:
444  enum WriteResult {
445  kContinue = 0,
446  kAbort = 1
447  };
448  virtual ~OutputStream() {}
450  virtual void EndOfStream() = 0;
452  virtual int GetChunkSize() { return 1024; }
458  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
465  return kAbort;
466  }
467 };
468 
469 
474  public:
476  kJSON = 0 // See format description near 'Serialize' method.
477  };
478 
480  const HeapGraphNode* GetRoot() const;
481 
483  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
484 
486  int GetNodesCount() const;
487 
489  const HeapGraphNode* GetNode(int index) const;
490 
492  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
493 
499  void Delete();
500 
527  void Serialize(OutputStream* stream,
528  SerializationFormat format = kJSON) const;
529 };
530 
531 
536 class V8_EXPORT ActivityControl { // NOLINT
537  public:
539  kContinue = 0,
540  kAbort = 1
541  };
542  virtual ~ActivityControl() {}
547  virtual ControlOption ReportProgressValue(int done, int total) = 0;
548 };
549 
550 
556  public:
557  struct Allocation {
561  size_t size;
562 
566  unsigned int count;
567  };
568 
572  struct Node {
578 
584 
590 
595 
601 
607 
613  std::vector<Node*> children;
614 
618  std::vector<Allocation> allocations;
619  };
620 
626  virtual Node* GetRootNode() = 0;
627 
628  virtual ~AllocationProfile() {}
629 
630  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
631  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
632 };
633 
649  public:
650  class Node {
651  public:
652  Node() = default;
653  virtual ~Node() = default;
654  virtual const char* Name() = 0;
655  virtual size_t SizeInBytes() = 0;
661  virtual Node* WrapperNode() { return nullptr; }
662  virtual bool IsRootNode() { return false; }
664  virtual bool IsEmbedderNode() { return true; }
668  virtual const char* NamePrefix() { return nullptr; }
669 
670  private:
671  Node(const Node&) = delete;
672  Node& operator=(const Node&) = delete;
673  };
674 
679  virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
680 
685  virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
686 
695  virtual void AddEdge(Node* from, Node* to, const char* name = nullptr) = 0;
696 
697  virtual ~EmbedderGraph() = default;
698 };
699 
705  public:
707  kSamplingNoFlags = 0,
708  kSamplingForceGC = 1 << 0,
709  };
710 
711  typedef std::unordered_set<const v8::PersistentBase<v8::Value>*>
713  typedef std::vector<std::pair<v8::RetainedObjectInfo*, RetainerChildren>>
715  typedef std::vector<std::pair<const v8::PersistentBase<v8::Value>*,
718 
719  struct RetainerInfos {
722  };
723 
727  typedef RetainerInfos (*GetRetainerInfosCallback)(v8::Isolate* isolate);
728 
735  typedef RetainedObjectInfo* (*WrapperInfoCallback)(uint16_t class_id,
736  Local<Value> wrapper);
737 
744  typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
745  v8::EmbedderGraph* graph,
746  void* data);
747 
749  typedef void (*LegacyBuildEmbedderGraphCallback)(v8::Isolate* isolate,
750  v8::EmbedderGraph* graph);
751 
753  int GetSnapshotCount();
754 
756  const HeapSnapshot* GetHeapSnapshot(int index);
757 
762  SnapshotObjectId GetObjectId(Local<Value> value);
763 
768  Local<Value> FindObjectById(SnapshotObjectId id);
769 
775  void ClearObjectIds();
776 
782  static const SnapshotObjectId kUnknownObjectId = 0;
783 
788  public:
793  virtual const char* GetName(Local<Object> object) = 0;
794 
795  protected:
796  virtual ~ObjectNameResolver() {}
797  };
798 
802  const HeapSnapshot* TakeHeapSnapshot(
803  ActivityControl* control = NULL,
804  ObjectNameResolver* global_object_name_resolver = NULL);
805 
815  void StartTrackingHeapObjects(bool track_allocations = false);
816 
830  SnapshotObjectId GetHeapStats(OutputStream* stream,
831  int64_t* timestamp_us = NULL);
832 
838  void StopTrackingHeapObjects();
839 
867  bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
868  int stack_depth = 16,
869  SamplingFlags flags = kSamplingNoFlags);
870 
874  void StopSamplingHeapProfiler();
875 
882  AllocationProfile* GetAllocationProfile();
883 
888  void DeleteAllHeapSnapshots();
889 
892  "Use AddBuildEmbedderGraphCallback to provide info about embedder nodes",
893  void SetWrapperClassInfoProvider(uint16_t class_id,
894  WrapperInfoCallback callback));
895 
897  "Use AddBuildEmbedderGraphCallback to provide info about embedder nodes",
898  void SetGetRetainerInfosCallback(GetRetainerInfosCallback callback));
899 
901  "Use AddBuildEmbedderGraphCallback to provide info about embedder nodes",
902  void SetBuildEmbedderGraphCallback(
903  LegacyBuildEmbedderGraphCallback callback));
904  void AddBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
905  void* data);
906  void RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
907  void* data);
908 
914  static const uint16_t kPersistentHandleNoClassId = 0;
915 
916  private:
917  HeapProfiler();
918  ~HeapProfiler();
919  HeapProfiler(const HeapProfiler&);
920  HeapProfiler& operator=(const HeapProfiler&);
921 };
922 
947 class V8_EXPORT RetainedObjectInfo { // NOLINT
948  public:
950  virtual void Dispose() = 0;
951 
953  virtual bool IsEquivalent(RetainedObjectInfo* other) = 0;
954 
959  virtual intptr_t GetHash() = 0;
960 
965  virtual const char* GetLabel() = 0;
966 
976  virtual const char* GetGroupLabel() { return GetLabel(); }
977 
982  virtual intptr_t GetElementCount() { return -1; }
983 
985  virtual intptr_t GetSizeInBytes() { return -1; }
986 
987  protected:
989  virtual ~RetainedObjectInfo() {}
990 
991  private:
993  RetainedObjectInfo& operator=(const RetainedObjectInfo&);
994 };
995 
996 
1002  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
1003  : index(index), count(count), size(size) { }
1004  uint32_t index; // Index of the time interval that was changed.
1005  uint32_t count; // New value of count field for the interval with this index.
1006  uint32_t size; // New value of size field for the interval with this index.
1007 };
1008 
1009 #define CODE_EVENTS_LIST(V) \
1010  V(Builtin) \
1011  V(Callback) \
1012  V(Eval) \
1013  V(Function) \
1014  V(InterpretedFunction) \
1015  V(Handler) \
1016  V(BytecodeHandler) \
1017  V(LazyCompile) \
1018  V(RegExp) \
1019  V(Script) \
1020  V(Stub)
1021 
1028 #define V(Name) , k##Name##Type
1030 #undef V
1031 };
1032 
1037  public:
1038  uintptr_t GetCodeStartAddress();
1039  size_t GetCodeSize();
1040  Local<String> GetFunctionName();
1041  Local<String> GetScriptName();
1042  int GetScriptLine();
1043  int GetScriptColumn();
1049  CodeEventType GetCodeType();
1050  const char* GetComment();
1051 
1052  static const char* GetCodeEventTypeName(CodeEventType code_event_type);
1053 };
1054 
1059  public:
1065  explicit CodeEventHandler(Isolate* isolate);
1066  virtual ~CodeEventHandler();
1067 
1068  virtual void Handle(CodeEvent* code_event) = 0;
1069 
1070  void Enable();
1071  void Disable();
1072 
1073  private:
1074  CodeEventHandler();
1076  CodeEventHandler& operator=(const CodeEventHandler&);
1077  void* internal_listener_;
1078 };
1079 
1080 } // namespace v8
1081 
1082 
1083 #endif // V8_V8_PROFILER_H_
std::unordered_set< const v8::PersistentBase< v8::Value > * > RetainerChildren
Definition: v8-profiler.h:712
virtual const char * NamePrefix()
Definition: v8-profiler.h:668
std::vector< std::pair< v8::RetainedObjectInfo *, RetainerChildren > > RetainerGroups
Definition: v8-profiler.h:714
const char * data() const
Definition: v8.h:6303
virtual ~OutputStream()
Definition: v8-profiler.h:448
StateTag
Definition: v8.h:1920
Definition: v8.h:1926
Definition: v8.h:2602
std::vector< Node * > children
Definition: v8-profiler.h:613
StateTag state
Definition: v8-profiler.h:105
#define V8_DEPRECATE_SOON(message, declarator)
Definition: v8config.h:348
virtual bool IsRootNode()
Definition: v8-profiler.h:662
CpuProfilingMode
Definition: v8-profiler.h:262
Definition: libplatform.h:13
virtual int GetChunkSize()
Definition: v8-profiler.h:452
virtual bool IsEmbedderNode()
Definition: v8-profiler.h:664
HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
Definition: v8-profiler.h:1002
#define CODE_EVENTS_LIST(V)
Definition: v8-profiler.h:1009
std::vector< CpuProfileDeoptFrame > stack
Definition: v8-profiler.h:39
virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate *data, int count)
Definition: v8-profiler.h:464
std::vector< std::pair< const v8::PersistentBase< v8::Value > *, const v8::PersistentBase< v8::Value > * > > RetainerEdges
Definition: v8-profiler.h:717
virtual ~RetainedObjectInfo()
Definition: v8-profiler.h:989
virtual ~ActivityControl()
Definition: v8-profiler.h:542
#define V8_DEPRECATED(message, declarator)
Definition: v8config.h:333
std::vector< Allocation > allocations
Definition: v8-profiler.h:618
Local< T > Handle
Definition: v8.h:441
virtual ~AllocationProfile()
Definition: v8-profiler.h:628
const char * deopt_reason
Definition: v8-profiler.h:38
bool has_external_callback
Definition: v8-profiler.h:115
uint32_t SnapshotObjectId
Definition: v8-profiler.h:18
virtual Node * WrapperNode()
Definition: v8-profiler.h:661
CodeEventType
Definition: v8-profiler.h:1026
unsigned frames_count
Definition: v8-profiler.h:114
virtual intptr_t GetElementCount()
Definition: v8-profiler.h:982
virtual intptr_t GetSizeInBytes()
Definition: v8-profiler.h:985
Local< String > script_name
Definition: v8-profiler.h:583
#define V8_EXPORT
Definition: v8.h:56
Definition: v8.h:114
virtual const char * GetGroupLabel()
Definition: v8-profiler.h:976
void * external_callback_entry
Definition: v8-profiler.h:109
#define V(Name)
size_t length() const
Definition: v8.h:6304