v8  7.4.288(node12.0.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 
348  static void UseDetailedSourcePositionsForProfiling(Isolate* isolate);
349 
350  private:
351  CpuProfiler();
352  ~CpuProfiler();
353  CpuProfiler(const CpuProfiler&);
354  CpuProfiler& operator=(const CpuProfiler&);
355 };
356 
357 
363  public:
364  enum Type {
365  kContextVariable = 0, // A variable from a function context.
366  kElement = 1, // An element of an array.
367  kProperty = 2, // A named object property.
368  kInternal = 3, // A link that can't be accessed from JS,
369  // thus, its name isn't a real property name
370  // (e.g. parts of a ConsString).
371  kHidden = 4, // A link that is needed for proper sizes
372  // calculation, but may be hidden from user.
373  kShortcut = 5, // A link that must not be followed during
374  // sizes calculation.
375  kWeak = 6 // A weak reference (ignored by the GC).
376  };
377 
379  Type GetType() const;
380 
385  Local<Value> GetName() const;
386 
388  const HeapGraphNode* GetFromNode() const;
389 
391  const HeapGraphNode* GetToNode() const;
392 };
393 
394 
399  public:
400  enum Type {
401  kHidden = 0, // Hidden node, may be filtered when shown to user.
402  kArray = 1, // An array of elements.
403  kString = 2, // A string.
404  kObject = 3, // A JS object (except for arrays and strings).
405  kCode = 4, // Compiled code.
406  kClosure = 5, // Function closure.
407  kRegExp = 6, // RegExp.
408  kHeapNumber = 7, // Number stored in the heap.
409  kNative = 8, // Native object (not from V8 heap).
410  kSynthetic = 9, // Synthetic object, usually used for grouping
411  // snapshot items together.
412  kConsString = 10, // Concatenated string. A pair of pointers to strings.
413  kSlicedString = 11, // Sliced string. A fragment of another string.
414  kSymbol = 12, // A Symbol (ES6).
415  kBigInt = 13 // BigInt.
416  };
417 
419  Type GetType() const;
420 
426  Local<String> GetName() const;
427 
432  SnapshotObjectId GetId() const;
433 
435  size_t GetShallowSize() const;
436 
438  int GetChildrenCount() const;
439 
441  const HeapGraphEdge* GetChild(int index) const;
442 };
443 
444 
448 class V8_EXPORT OutputStream { // NOLINT
449  public:
450  enum WriteResult {
451  kContinue = 0,
452  kAbort = 1
453  };
454  virtual ~OutputStream() = default;
456  virtual void EndOfStream() = 0;
458  virtual int GetChunkSize() { return 1024; }
464  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
470  virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
471  return kAbort;
472  }
473 };
474 
475 
480  public:
482  kJSON = 0 // See format description near 'Serialize' method.
483  };
484 
486  const HeapGraphNode* GetRoot() const;
487 
489  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
490 
492  int GetNodesCount() const;
493 
495  const HeapGraphNode* GetNode(int index) const;
496 
498  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
499 
505  void Delete();
506 
533  void Serialize(OutputStream* stream,
534  SerializationFormat format = kJSON) const;
535 };
536 
537 
542 class V8_EXPORT ActivityControl { // NOLINT
543  public:
545  kContinue = 0,
546  kAbort = 1
547  };
548  virtual ~ActivityControl() = default;
553  virtual ControlOption ReportProgressValue(int done, int total) = 0;
554 };
555 
556 
562  public:
563  struct Allocation {
567  size_t size;
568 
572  unsigned int count;
573  };
574 
578  struct Node {
584 
590 
596 
601 
607 
613 
617  uint32_t node_id;
618 
624  std::vector<Node*> children;
625 
629  std::vector<Allocation> allocations;
630  };
631 
635  struct Sample {
639  uint32_t node_id;
640 
644  size_t size;
645 
649  unsigned int count;
650 
655  uint64_t sample_id;
656  };
657 
663  virtual Node* GetRootNode() = 0;
664  virtual const std::vector<Sample>& GetSamples() = 0;
665 
666  virtual ~AllocationProfile() = default;
667 
668  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
669  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
670 };
671 
687  public:
688  class Node {
689  public:
690  Node() = default;
691  virtual ~Node() = default;
692  virtual const char* Name() = 0;
693  virtual size_t SizeInBytes() = 0;
699  virtual Node* WrapperNode() { return nullptr; }
700  virtual bool IsRootNode() { return false; }
702  virtual bool IsEmbedderNode() { return true; }
706  virtual const char* NamePrefix() { return nullptr; }
707 
708  private:
709  Node(const Node&) = delete;
710  Node& operator=(const Node&) = delete;
711  };
712 
717  virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
718 
723  virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
724 
733  virtual void AddEdge(Node* from, Node* to, const char* name = nullptr) = 0;
734 
735  virtual ~EmbedderGraph() = default;
736 };
737 
743  public:
745  kSamplingNoFlags = 0,
746  kSamplingForceGC = 1 << 0,
747  };
748 
755  typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
756  v8::EmbedderGraph* graph,
757  void* data);
758 
760  typedef void (*LegacyBuildEmbedderGraphCallback)(v8::Isolate* isolate,
761  v8::EmbedderGraph* graph);
762 
764  int GetSnapshotCount();
765 
767  const HeapSnapshot* GetHeapSnapshot(int index);
768 
773  SnapshotObjectId GetObjectId(Local<Value> value);
774 
779  Local<Value> FindObjectById(SnapshotObjectId id);
780 
786  void ClearObjectIds();
787 
793  static const SnapshotObjectId kUnknownObjectId = 0;
794 
799  public:
804  virtual const char* GetName(Local<Object> object) = 0;
805 
806  protected:
807  virtual ~ObjectNameResolver() = default;
808  };
809 
813  const HeapSnapshot* TakeHeapSnapshot(
814  ActivityControl* control = nullptr,
815  ObjectNameResolver* global_object_name_resolver = nullptr);
816 
826  void StartTrackingHeapObjects(bool track_allocations = false);
827 
841  SnapshotObjectId GetHeapStats(OutputStream* stream,
842  int64_t* timestamp_us = nullptr);
843 
849  void StopTrackingHeapObjects();
850 
878  bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
879  int stack_depth = 16,
880  SamplingFlags flags = kSamplingNoFlags);
881 
885  void StopSamplingHeapProfiler();
886 
893  AllocationProfile* GetAllocationProfile();
894 
899  void DeleteAllHeapSnapshots();
900 
902  "Use AddBuildEmbedderGraphCallback to provide info about embedder nodes",
903  void SetBuildEmbedderGraphCallback(
904  LegacyBuildEmbedderGraphCallback callback));
905  void AddBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
906  void* data);
907  void RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
908  void* data);
909 
915  static const uint16_t kPersistentHandleNoClassId = 0;
916 
917  private:
918  HeapProfiler();
919  ~HeapProfiler();
920  HeapProfiler(const HeapProfiler&);
921  HeapProfiler& operator=(const HeapProfiler&);
922 };
923 
929  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
930  : index(index), count(count), size(size) { }
931  uint32_t index; // Index of the time interval that was changed.
932  uint32_t count; // New value of count field for the interval with this index.
933  uint32_t size; // New value of size field for the interval with this index.
934 };
935 
936 #define CODE_EVENTS_LIST(V) \
937  V(Builtin) \
938  V(Callback) \
939  V(Eval) \
940  V(Function) \
941  V(InterpretedFunction) \
942  V(Handler) \
943  V(BytecodeHandler) \
944  V(LazyCompile) \
945  V(RegExp) \
946  V(Script) \
947  V(Stub)
948 
955 #define V(Name) , k##Name##Type
957 #undef V
958 };
959 
964  public:
965  uintptr_t GetCodeStartAddress();
966  size_t GetCodeSize();
967  Local<String> GetFunctionName();
968  Local<String> GetScriptName();
969  int GetScriptLine();
970  int GetScriptColumn();
976  CodeEventType GetCodeType();
977  const char* GetComment();
978 
979  static const char* GetCodeEventTypeName(CodeEventType code_event_type);
980 };
981 
986  public:
992  explicit CodeEventHandler(Isolate* isolate);
993  virtual ~CodeEventHandler();
994 
995  virtual void Handle(CodeEvent* code_event) = 0;
996 
997  void Enable();
998  void Disable();
999 
1000  private:
1001  CodeEventHandler();
1003  CodeEventHandler& operator=(const CodeEventHandler&);
1004  void* internal_listener_;
1005 };
1006 
1007 } // namespace v8
1008 
1009 
1010 #endif // V8_V8_PROFILER_H_
virtual const char * NamePrefix()
Definition: v8-profiler.h:706
#define V8_EXPORT
Definition: v8config.h:377
StateTag
Definition: v8.h:1938
Definition: v8.h:1944
Definition: v8.h:2616
std::vector< Node * > children
Definition: v8-profiler.h:624
StateTag state
Definition: v8-profiler.h:105
#define V8_DEPRECATE_SOON(message, declarator)
Definition: v8config.h:329
virtual bool IsRootNode()
Definition: v8-profiler.h:700
CpuProfilingMode
Definition: v8-profiler.h:262
Definition: libplatform.h:13
virtual int GetChunkSize()
Definition: v8-profiler.h:458
virtual bool IsEmbedderNode()
Definition: v8-profiler.h:702
HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
Definition: v8-profiler.h:929
#define CODE_EVENTS_LIST(V)
Definition: v8-profiler.h:936
std::vector< CpuProfileDeoptFrame > stack
Definition: v8-profiler.h:39
virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate *data, int count)
Definition: v8-profiler.h:470
#define V8_DEPRECATED(message, declarator)
Definition: v8config.h:314
std::vector< Allocation > allocations
Definition: v8-profiler.h:629
Local< T > Handle
Definition: v8.h:323
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:699
CodeEventType
Definition: v8-profiler.h:953
unsigned frames_count
Definition: v8-profiler.h:114
Local< String > script_name
Definition: v8-profiler.h:589
Definition: v8.h:86
void * external_callback_entry
Definition: v8-profiler.h:109
#define V(Name)