v8  6.2.414(node8.16.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 
56  public:
57  static std::unique_ptr<TracingCpuProfiler> Create(Isolate*);
58  virtual ~TracingCpuProfiler() = default;
59 
60  protected:
61  TracingCpuProfiler() = default;
62 };
63 
64 // TickSample captures the information collected for each sample.
65 struct TickSample {
66  // Internal profiling (with --prof + tools/$OS-tick-processor) wants to
67  // include the runtime function we're calling. Externally exposed tick
68  // samples don't care.
69  enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame };
70 
72  : state(OTHER),
73  pc(nullptr),
74  external_callback_entry(nullptr),
75  frames_count(0),
76  has_external_callback(false),
77  update_stats(true) {}
78 
91  void Init(Isolate* isolate, const v8::RegisterState& state,
92  RecordCEntryFrame record_c_entry_frame, bool update_stats,
93  bool use_simulator_reg_state = true);
114  static bool GetStackSample(Isolate* isolate, v8::RegisterState* state,
115  RecordCEntryFrame record_c_entry_frame,
116  void** frames, size_t frames_limit,
117  v8::SampleInfo* sample_info,
118  bool use_simulator_reg_state = true);
119  StateTag state; // The state of the VM.
120  void* pc; // Instruction pointer.
121  union {
122  void* tos; // Top stack value (*sp).
124  };
125  static const unsigned kMaxFramesCountLog2 = 8;
126  static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1;
127  void* stack[kMaxFramesCount]; // Call stack.
128  unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames.
130  bool update_stats : 1; // Whether the sample should update aggregated stats.
131 };
132 
137  public:
138  struct LineTick {
140  int line;
141 
143  unsigned int hit_count;
144  };
145 
147  Local<String> GetFunctionName() const;
148 
154  const char* GetFunctionNameStr() const;
155 
157  int GetScriptId() const;
158 
160  Local<String> GetScriptResourceName() const;
161 
167  const char* GetScriptResourceNameStr() const;
168 
173  int GetLineNumber() const;
174 
179  int GetColumnNumber() const;
180 
184  unsigned int GetHitLineCount() const;
185 
191  bool GetLineTicks(LineTick* entries, unsigned int length) const;
192 
196  const char* GetBailoutReason() const;
197 
201  unsigned GetHitCount() const;
202 
205  "Use GetScriptId, GetLineNumber, and GetColumnNumber instead.",
206  unsigned GetCallUid() const);
207 
209  unsigned GetNodeId() const;
210 
212  int GetChildrenCount() const;
213 
215  const CpuProfileNode* GetChild(int index) const;
216 
218  const std::vector<CpuProfileDeoptInfo>& GetDeoptInfos() const;
219 
220  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
221  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
222 };
223 
224 
230  public:
232  Local<String> GetTitle() const;
233 
235  const CpuProfileNode* GetTopDownRoot() const;
236 
241  int GetSamplesCount() const;
242 
247  const CpuProfileNode* GetSample(int index) const;
248 
254  int64_t GetSampleTimestamp(int index) const;
255 
260  int64_t GetStartTime() const;
261 
267  int64_t GetEndTime() const;
268 
273  void Delete();
274 };
275 
281  public:
287  static CpuProfiler* New(Isolate* isolate);
288 
292  void Dispose();
293 
299  void SetSamplingInterval(int us);
300 
312  void StartProfiling(Local<String> title, bool record_samples = false);
313 
318  CpuProfile* StopProfiling(Local<String> title);
319 
325  void CollectSample();
326 
330  void SetIdle(bool is_idle);
331 
332  private:
333  CpuProfiler();
334  ~CpuProfiler();
335  CpuProfiler(const CpuProfiler&);
336  CpuProfiler& operator=(const CpuProfiler&);
337 };
338 
339 
345  public:
346  enum Type {
347  kContextVariable = 0, // A variable from a function context.
348  kElement = 1, // An element of an array.
349  kProperty = 2, // A named object property.
350  kInternal = 3, // A link that can't be accessed from JS,
351  // thus, its name isn't a real property name
352  // (e.g. parts of a ConsString).
353  kHidden = 4, // A link that is needed for proper sizes
354  // calculation, but may be hidden from user.
355  kShortcut = 5, // A link that must not be followed during
356  // sizes calculation.
357  kWeak = 6 // A weak reference (ignored by the GC).
358  };
359 
361  Type GetType() const;
362 
367  Local<Value> GetName() const;
368 
370  const HeapGraphNode* GetFromNode() const;
371 
373  const HeapGraphNode* GetToNode() const;
374 };
375 
376 
381  public:
382  enum Type {
383  kHidden = 0, // Hidden node, may be filtered when shown to user.
384  kArray = 1, // An array of elements.
385  kString = 2, // A string.
386  kObject = 3, // A JS object (except for arrays and strings).
387  kCode = 4, // Compiled code.
388  kClosure = 5, // Function closure.
389  kRegExp = 6, // RegExp.
390  kHeapNumber = 7, // Number stored in the heap.
391  kNative = 8, // Native object (not from V8 heap).
392  kSynthetic = 9, // Synthetic object, usually used for grouping
393  // snapshot items together.
394  kConsString = 10, // Concatenated string. A pair of pointers to strings.
395  kSlicedString = 11, // Sliced string. A fragment of another string.
396  kSymbol = 12 // A Symbol (ES6).
397  };
398 
400  Type GetType() const;
401 
407  Local<String> GetName() const;
408 
413  SnapshotObjectId GetId() const;
414 
416  size_t GetShallowSize() const;
417 
419  int GetChildrenCount() const;
420 
422  const HeapGraphEdge* GetChild(int index) const;
423 };
424 
425 
429 class V8_EXPORT OutputStream { // NOLINT
430  public:
431  enum WriteResult {
432  kContinue = 0,
433  kAbort = 1
434  };
435  virtual ~OutputStream() {}
437  virtual void EndOfStream() = 0;
439  virtual int GetChunkSize() { return 1024; }
445  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
451  virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
452  return kAbort;
453  }
454 };
455 
456 
461  public:
463  kJSON = 0 // See format description near 'Serialize' method.
464  };
465 
467  const HeapGraphNode* GetRoot() const;
468 
470  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
471 
473  int GetNodesCount() const;
474 
476  const HeapGraphNode* GetNode(int index) const;
477 
479  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
480 
486  void Delete();
487 
514  void Serialize(OutputStream* stream,
515  SerializationFormat format = kJSON) const;
516 };
517 
518 
523 class V8_EXPORT ActivityControl { // NOLINT
524  public:
526  kContinue = 0,
527  kAbort = 1
528  };
529  virtual ~ActivityControl() {}
534  virtual ControlOption ReportProgressValue(int done, int total) = 0;
535 };
536 
537 
543  public:
544  struct Allocation {
548  size_t size;
549 
553  unsigned int count;
554  };
555 
559  struct Node {
565 
571 
577 
582 
588 
594 
600  std::vector<Node*> children;
601 
605  std::vector<Allocation> allocations;
606  };
607 
613  virtual Node* GetRootNode() = 0;
614 
615  virtual ~AllocationProfile() {}
616 
617  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
618  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
619 };
620 
621 
627  public:
629  kSamplingNoFlags = 0,
630  kSamplingForceGC = 1 << 0,
631  };
632 
633  typedef std::unordered_set<const v8::PersistentBase<v8::Value>*>
635  typedef std::vector<std::pair<v8::RetainedObjectInfo*, RetainerChildren>>
637  typedef std::vector<std::pair<const v8::PersistentBase<v8::Value>*,
640 
641  struct RetainerInfos {
644  };
645 
649  typedef RetainerInfos (*GetRetainerInfosCallback)(v8::Isolate* isolate);
650 
657  typedef RetainedObjectInfo* (*WrapperInfoCallback)(uint16_t class_id,
658  Local<Value> wrapper);
659 
661  int GetSnapshotCount();
662 
664  const HeapSnapshot* GetHeapSnapshot(int index);
665 
670  SnapshotObjectId GetObjectId(Local<Value> value);
671 
676  Local<Value> FindObjectById(SnapshotObjectId id);
677 
683  void ClearObjectIds();
684 
690  static const SnapshotObjectId kUnknownObjectId = 0;
691 
696  public:
701  virtual const char* GetName(Local<Object> object) = 0;
702 
703  protected:
704  virtual ~ObjectNameResolver() {}
705  };
706 
710  const HeapSnapshot* TakeHeapSnapshot(
711  ActivityControl* control = NULL,
712  ObjectNameResolver* global_object_name_resolver = NULL);
713 
723  void StartTrackingHeapObjects(bool track_allocations = false);
724 
738  SnapshotObjectId GetHeapStats(OutputStream* stream,
739  int64_t* timestamp_us = NULL);
740 
746  void StopTrackingHeapObjects();
747 
775  bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
776  int stack_depth = 16,
777  SamplingFlags flags = kSamplingNoFlags);
778 
782  void StopSamplingHeapProfiler();
783 
790  AllocationProfile* GetAllocationProfile();
791 
796  void DeleteAllHeapSnapshots();
797 
799  void SetWrapperClassInfoProvider(
800  uint16_t class_id,
801  WrapperInfoCallback callback);
802 
803  void SetGetRetainerInfosCallback(GetRetainerInfosCallback callback);
804 
810  static const uint16_t kPersistentHandleNoClassId = 0;
811 
812  private:
813  HeapProfiler();
814  ~HeapProfiler();
815  HeapProfiler(const HeapProfiler&);
816  HeapProfiler& operator=(const HeapProfiler&);
817 };
818 
843 class V8_EXPORT RetainedObjectInfo { // NOLINT
844  public:
846  virtual void Dispose() = 0;
847 
849  virtual bool IsEquivalent(RetainedObjectInfo* other) = 0;
850 
855  virtual intptr_t GetHash() = 0;
856 
861  virtual const char* GetLabel() = 0;
862 
872  virtual const char* GetGroupLabel() { return GetLabel(); }
873 
878  virtual intptr_t GetElementCount() { return -1; }
879 
881  virtual intptr_t GetSizeInBytes() { return -1; }
882 
883  protected:
885  virtual ~RetainedObjectInfo() {}
886 
887  private:
889  RetainedObjectInfo& operator=(const RetainedObjectInfo&);
890 };
891 
892 
898  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
899  : index(index), count(count), size(size) { }
900  uint32_t index; // Index of the time interval that was changed.
901  uint32_t count; // New value of count field for the interval with this index.
902  uint32_t size; // New value of size field for the interval with this index.
903 };
904 
905 
906 } // namespace v8
907 
908 
909 #endif // V8_V8_PROFILER_H_
std::unordered_set< const v8::PersistentBase< v8::Value > * > RetainerChildren
Definition: v8-profiler.h:634
std::vector< std::pair< v8::RetainedObjectInfo *, RetainerChildren > > RetainerGroups
Definition: v8-profiler.h:636
virtual ~OutputStream()
Definition: v8-profiler.h:435
StateTag
Definition: v8.h:1724
Definition: v8.h:1730
std::vector< Node * > children
Definition: v8-profiler.h:600
StateTag state
Definition: v8-profiler.h:119
#define V8_DEPRECATE_SOON(message, declarator)
Definition: v8config.h:340
Definition: libplatform.h:12
virtual int GetChunkSize()
Definition: v8-profiler.h:439
HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
Definition: v8-profiler.h:898
std::vector< CpuProfileDeoptFrame > stack
Definition: v8-profiler.h:39
virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate *data, int count)
Definition: v8-profiler.h:451
std::vector< std::pair< const v8::PersistentBase< v8::Value > *, const v8::PersistentBase< v8::Value > * > > RetainerEdges
Definition: v8-profiler.h:639
virtual ~RetainedObjectInfo()
Definition: v8-profiler.h:885
virtual ~ActivityControl()
Definition: v8-profiler.h:529
std::vector< Allocation > allocations
Definition: v8-profiler.h:605
virtual ~AllocationProfile()
Definition: v8-profiler.h:615
const char * deopt_reason
Definition: v8-profiler.h:38
bool has_external_callback
Definition: v8-profiler.h:129
uint32_t SnapshotObjectId
Definition: v8-profiler.h:18
unsigned frames_count
Definition: v8-profiler.h:128
virtual intptr_t GetElementCount()
Definition: v8-profiler.h:878
virtual intptr_t GetSizeInBytes()
Definition: v8-profiler.h:881
Local< String > script_name
Definition: v8-profiler.h:570
#define V8_EXPORT
Definition: v8.h:56
Definition: v8.h:112
virtual const char * GetGroupLabel()
Definition: v8-profiler.h:872
void * external_callback_entry
Definition: v8-profiler.h:123