v8  6.8.275(node10.15.3)
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 
56  public:
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 
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).
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.
134  bool update_stats : 1; // Whether the sample should update aggregated stats.
135 };
136 
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 
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 
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 
285  public:
291  static CpuProfiler* New(Isolate* isolate);
292 
298  static void CollectSample(Isolate* isolate);
299 
303  void Dispose();
304 
310  void SetSamplingInterval(int us);
311 
323  void StartProfiling(Local<String> title, bool record_samples = false);
324 
329  CpuProfile* StopProfiling(Local<String> title);
330 
336  V8_DEPRECATED("Use static CollectSample(Isolate*) instead.",
337  void CollectSample());
338 
342  V8_DEPRECATED("Use Isolate::SetIdle(bool) instead.",
343  void SetIdle(bool is_idle));
344 
345  private:
346  CpuProfiler();
347  ~CpuProfiler();
348  CpuProfiler(const CpuProfiler&);
349  CpuProfiler& operator=(const CpuProfiler&);
350 };
351 
352 
358  public:
359  enum Type {
360  kContextVariable = 0, // A variable from a function context.
361  kElement = 1, // An element of an array.
362  kProperty = 2, // A named object property.
363  kInternal = 3, // A link that can't be accessed from JS,
364  // thus, its name isn't a real property name
365  // (e.g. parts of a ConsString).
366  kHidden = 4, // A link that is needed for proper sizes
367  // calculation, but may be hidden from user.
368  kShortcut = 5, // A link that must not be followed during
369  // sizes calculation.
370  kWeak = 6 // A weak reference (ignored by the GC).
371  };
372 
374  Type GetType() const;
375 
380  Local<Value> GetName() const;
381 
383  const HeapGraphNode* GetFromNode() const;
384 
386  const HeapGraphNode* GetToNode() const;
387 };
388 
389 
394  public:
395  enum Type {
396  kHidden = 0, // Hidden node, may be filtered when shown to user.
397  kArray = 1, // An array of elements.
398  kString = 2, // A string.
399  kObject = 3, // A JS object (except for arrays and strings).
400  kCode = 4, // Compiled code.
401  kClosure = 5, // Function closure.
402  kRegExp = 6, // RegExp.
403  kHeapNumber = 7, // Number stored in the heap.
404  kNative = 8, // Native object (not from V8 heap).
405  kSynthetic = 9, // Synthetic object, usually used for grouping
406  // snapshot items together.
407  kConsString = 10, // Concatenated string. A pair of pointers to strings.
408  kSlicedString = 11, // Sliced string. A fragment of another string.
409  kSymbol = 12, // A Symbol (ES6).
410  kBigInt = 13 // BigInt.
411  };
412 
414  Type GetType() const;
415 
421  Local<String> GetName() const;
422 
427  SnapshotObjectId GetId() const;
428 
430  size_t GetShallowSize() const;
431 
433  int GetChildrenCount() const;
434 
436  const HeapGraphEdge* GetChild(int index) const;
437 };
438 
439 
443 class V8_EXPORT OutputStream { // NOLINT
444  public:
445  enum WriteResult {
446  kContinue = 0,
447  kAbort = 1
448  };
449  virtual ~OutputStream() {}
451  virtual void EndOfStream() = 0;
453  virtual int GetChunkSize() { return 1024; }
459  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
465  virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
466  return kAbort;
467  }
468 };
469 
470 
475  public:
477  kJSON = 0 // See format description near 'Serialize' method.
478  };
479 
481  const HeapGraphNode* GetRoot() const;
482 
484  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
485 
487  int GetNodesCount() const;
488 
490  const HeapGraphNode* GetNode(int index) const;
491 
493  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
494 
500  void Delete();
501 
528  void Serialize(OutputStream* stream,
529  SerializationFormat format = kJSON) const;
530 };
531 
532 
537 class V8_EXPORT ActivityControl { // NOLINT
538  public:
540  kContinue = 0,
541  kAbort = 1
542  };
543  virtual ~ActivityControl() {}
548  virtual ControlOption ReportProgressValue(int done, int total) = 0;
549 };
550 
551 
557  public:
558  struct Allocation {
562  size_t size;
563 
567  unsigned int count;
568  };
569 
573  struct Node {
579 
585 
591 
596 
602 
608 
614  std::vector<Node*> children;
615 
619  std::vector<Allocation> allocations;
620  };
621 
627  virtual Node* GetRootNode() = 0;
628 
629  virtual ~AllocationProfile() {}
630 
631  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
632  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
633 };
634 
650  public:
651  class Node {
652  public:
653  Node() = default;
654  virtual ~Node() = default;
655  virtual const char* Name() = 0;
656  virtual size_t SizeInBytes() = 0;
662  virtual Node* WrapperNode() { return nullptr; }
663  virtual bool IsRootNode() { return false; }
665  virtual bool IsEmbedderNode() { return true; }
669  virtual const char* NamePrefix() { return nullptr; }
670 
671  private:
672  Node(const Node&) = delete;
673  Node& operator=(const Node&) = delete;
674  };
675 
680  virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
681 
686  virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
687 
693  virtual void AddEdge(Node* from, Node* to) = 0;
694 
695  virtual ~EmbedderGraph() = default;
696 };
697 
703  public:
705  kSamplingNoFlags = 0,
706  kSamplingForceGC = 1 << 0,
707  };
708 
709  typedef std::unordered_set<const v8::PersistentBase<v8::Value>*>
711  typedef std::vector<std::pair<v8::RetainedObjectInfo*, RetainerChildren>>
713  typedef std::vector<std::pair<const v8::PersistentBase<v8::Value>*,
716 
717  struct RetainerInfos {
720  };
721 
725  typedef RetainerInfos (*GetRetainerInfosCallback)(v8::Isolate* isolate);
726 
733  typedef RetainedObjectInfo* (*WrapperInfoCallback)(uint16_t class_id,
734  Local<Value> wrapper);
735 
742  typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
743  v8::EmbedderGraph* graph);
744 
746  int GetSnapshotCount();
747 
749  const HeapSnapshot* GetHeapSnapshot(int index);
750 
755  SnapshotObjectId GetObjectId(Local<Value> value);
756 
761  Local<Value> FindObjectById(SnapshotObjectId id);
762 
768  void ClearObjectIds();
769 
775  static const SnapshotObjectId kUnknownObjectId = 0;
776 
781  public:
786  virtual const char* GetName(Local<Object> object) = 0;
787 
788  protected:
789  virtual ~ObjectNameResolver() {}
790  };
791 
795  const HeapSnapshot* TakeHeapSnapshot(
796  ActivityControl* control = NULL,
797  ObjectNameResolver* global_object_name_resolver = NULL);
798 
808  void StartTrackingHeapObjects(bool track_allocations = false);
809 
823  SnapshotObjectId GetHeapStats(OutputStream* stream,
824  int64_t* timestamp_us = NULL);
825 
831  void StopTrackingHeapObjects();
832 
860  bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
861  int stack_depth = 16,
862  SamplingFlags flags = kSamplingNoFlags);
863 
867  void StopSamplingHeapProfiler();
868 
875  AllocationProfile* GetAllocationProfile();
876 
881  void DeleteAllHeapSnapshots();
882 
885  "Use SetBuildEmbedderGraphCallback to provide info about embedder nodes",
886  void SetWrapperClassInfoProvider(uint16_t class_id,
887  WrapperInfoCallback callback));
888 
890  "Use SetBuildEmbedderGraphCallback to provide info about embedder nodes",
891  void SetGetRetainerInfosCallback(GetRetainerInfosCallback callback));
892 
893  void SetBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback);
894 
900  static const uint16_t kPersistentHandleNoClassId = 0;
901 
902  private:
903  HeapProfiler();
904  ~HeapProfiler();
905  HeapProfiler(const HeapProfiler&);
906  HeapProfiler& operator=(const HeapProfiler&);
907 };
908 
933 class V8_EXPORT RetainedObjectInfo { // NOLINT
934  public:
936  virtual void Dispose() = 0;
937 
939  virtual bool IsEquivalent(RetainedObjectInfo* other) = 0;
940 
945  virtual intptr_t GetHash() = 0;
946 
951  virtual const char* GetLabel() = 0;
952 
962  virtual const char* GetGroupLabel() { return GetLabel(); }
963 
968  virtual intptr_t GetElementCount() { return -1; }
969 
971  virtual intptr_t GetSizeInBytes() { return -1; }
972 
973  protected:
975  virtual ~RetainedObjectInfo() {}
976 
977  private:
979  RetainedObjectInfo& operator=(const RetainedObjectInfo&);
980 };
981 
982 
988  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
989  : index(index), count(count), size(size) { }
990  uint32_t index; // Index of the time interval that was changed.
991  uint32_t count; // New value of count field for the interval with this index.
992  uint32_t size; // New value of size field for the interval with this index.
993 };
994 
995 
996 } // namespace v8
997 
998 
999 #endif // V8_V8_PROFILER_H_
std::unordered_set< const v8::PersistentBase< v8::Value > * > RetainerChildren
Definition: v8-profiler.h:710
virtual const char * NamePrefix()
Definition: v8-profiler.h:669
std::vector< std::pair< v8::RetainedObjectInfo *, RetainerChildren > > RetainerGroups
Definition: v8-profiler.h:712
virtual ~OutputStream()
Definition: v8-profiler.h:449
StateTag
Definition: v8.h:1919
Definition: v8.h:1925
Definition: v8.h:2615
std::vector< Node * > children
Definition: v8-profiler.h:614
StateTag state
Definition: v8-profiler.h:123
#define V8_DEPRECATE_SOON(message, declarator)
Definition: v8config.h:346
virtual bool IsRootNode()
Definition: v8-profiler.h:663
Definition: libplatform.h:13
virtual int GetChunkSize()
Definition: v8-profiler.h:453
virtual bool IsEmbedderNode()
Definition: v8-profiler.h:665
HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
Definition: v8-profiler.h:988
std::vector< CpuProfileDeoptFrame > stack
Definition: v8-profiler.h:39
virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate *data, int count)
Definition: v8-profiler.h:465
std::vector< std::pair< const v8::PersistentBase< v8::Value > *, const v8::PersistentBase< v8::Value > * > > RetainerEdges
Definition: v8-profiler.h:715
virtual ~RetainedObjectInfo()
Definition: v8-profiler.h:975
virtual ~ActivityControl()
Definition: v8-profiler.h:543
#define V8_DEPRECATED(message, declarator)
Definition: v8config.h:331
std::vector< Allocation > allocations
Definition: v8-profiler.h:619
virtual ~AllocationProfile()
Definition: v8-profiler.h:629
const char * deopt_reason
Definition: v8-profiler.h:38
bool has_external_callback
Definition: v8-profiler.h:133
uint32_t SnapshotObjectId
Definition: v8-profiler.h:18
virtual Node * WrapperNode()
Definition: v8-profiler.h:662
unsigned frames_count
Definition: v8-profiler.h:132
virtual intptr_t GetElementCount()
Definition: v8-profiler.h:968
virtual intptr_t GetSizeInBytes()
Definition: v8-profiler.h:971
Local< String > script_name
Definition: v8-profiler.h:584
#define V8_EXPORT
Definition: v8.h:56
Definition: v8.h:115
virtual const char * GetGroupLabel()
Definition: v8-profiler.h:962
void * external_callback_entry
Definition: v8-profiler.h:127