V8  latest master commit
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 <limits.h>
9 #include <memory>
10 #include <unordered_set>
11 #include <vector>
12 
13 #include "v8.h" // NOLINT(build/include_directory)
14 
18 namespace v8 {
19 
20 class HeapGraphNode;
21 struct HeapStatsUpdate;
22 
23 using NativeObject = void*;
24 using SnapshotObjectId = uint32_t;
25 
27  int script_id;
28  size_t position;
29 };
30 
31 namespace internal {
32 class CpuProfile;
33 } // namespace internal
34 
35 } // namespace v8
36 
37 #ifdef V8_OS_WIN
38 template class V8_EXPORT std::vector<v8::CpuProfileDeoptFrame>;
39 #endif
40 
41 namespace v8 {
42 
45  const char* deopt_reason;
46  std::vector<CpuProfileDeoptFrame> stack;
47 };
48 
49 } // namespace v8
50 
51 #ifdef V8_OS_WIN
52 template class V8_EXPORT std::vector<v8::CpuProfileDeoptInfo>;
53 #endif
54 
55 namespace v8 {
56 
61  public:
62  struct LineTick {
64  int line;
65 
67  unsigned int hit_count;
68  };
69 
70  // An annotation hinting at the source of a CpuProfileNode.
71  enum SourceType {
72  // User-supplied script with associated resource information.
73  kScript = 0,
74  // Native scripts and provided builtins.
75  kBuiltin = 1,
76  // Callbacks into native code.
77  kCallback = 2,
78  // VM-internal functions or state.
79  kInternal = 3,
80  // A node that failed to symbolize.
81  kUnresolved = 4,
82  };
83 
85  Local<String> GetFunctionName() const;
86 
92  const char* GetFunctionNameStr() const;
93 
95  int GetScriptId() const;
96 
98  Local<String> GetScriptResourceName() const;
99 
105  const char* GetScriptResourceNameStr() const;
106 
111  bool IsScriptSharedCrossOrigin() const;
112 
117  int GetLineNumber() const;
118 
123  int GetColumnNumber() const;
124 
128  unsigned int GetHitLineCount() const;
129 
135  bool GetLineTicks(LineTick* entries, unsigned int length) const;
136 
140  const char* GetBailoutReason() const;
141 
145  unsigned GetHitCount() const;
146 
148  unsigned GetNodeId() const;
149 
153  SourceType GetSourceType() const;
154 
156  int GetChildrenCount() const;
157 
159  const CpuProfileNode* GetChild(int index) const;
160 
162  const CpuProfileNode* GetParent() const;
163 
165  const std::vector<CpuProfileDeoptInfo>& GetDeoptInfos() const;
166 
167  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
168  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
169 };
170 
171 
177  public:
179  Local<String> GetTitle() const;
180 
182  const CpuProfileNode* GetTopDownRoot() const;
183 
188  int GetSamplesCount() const;
189 
194  const CpuProfileNode* GetSample(int index) const;
195 
201  int64_t GetSampleTimestamp(int index) const;
202 
207  int64_t GetStartTime() const;
208 
214  int64_t GetEndTime() const;
215 
220  void Delete();
221 };
222 
224  // In the resulting CpuProfile tree, intermediate nodes in a stack trace
225  // (from the root to a leaf) will have line numbers that point to the start
226  // line of the function, rather than the line of the callsite of the child.
228  // In the resulting CpuProfile tree, nodes are separated based on the line
229  // number of their callsite in their parent.
231 };
232 
233 // Determines how names are derived for functions sampled.
235  // Use the immediate name of functions at compilation time.
237  // Use more verbose naming for functions without names, inferred from scope
238  // where possible.
240 };
241 
243  // Enables logging when a profile is active, and disables logging when all
244  // profiles are detached.
246  // Enables logging for the lifetime of the CpuProfiler. Calls to
247  // StartRecording are faster, at the expense of runtime overhead.
249 };
250 
255  public:
256  // Indicates that the sample buffer size should not be explicitly limited.
257  static const unsigned kNoSampleLimit = UINT_MAX;
258 
274  unsigned max_samples = kNoSampleLimit, int sampling_interval_us = 0,
275  MaybeLocal<Context> filter_context = MaybeLocal<Context>());
276 
277  CpuProfilingMode mode() const { return mode_; }
278  unsigned max_samples() const { return max_samples_; }
279  int sampling_interval_us() const { return sampling_interval_us_; }
280 
281  private:
282  friend class internal::CpuProfile;
283 
284  bool has_filter_context() const { return !filter_context_.IsEmpty(); }
285  void* raw_filter_context() const;
286 
287  CpuProfilingMode mode_;
288  unsigned max_samples_;
289  int sampling_interval_us_;
291 };
292 
298  public:
304  static CpuProfiler* New(Isolate* isolate,
307 
313  static void CollectSample(Isolate* isolate);
314 
318  void Dispose();
319 
325  void SetSamplingInterval(int us);
326 
334  void SetUsePreciseSampling(bool);
335 
341  void StartProfiling(Local<String> title, CpuProfilingOptions options);
342 
354  void StartProfiling(
355  Local<String> title, CpuProfilingMode mode, bool record_samples = false,
356  unsigned max_samples = CpuProfilingOptions::kNoSampleLimit);
362  void StartProfiling(Local<String> title, bool record_samples = false);
363 
368  CpuProfile* StopProfiling(Local<String> title);
369 
374  static void UseDetailedSourcePositionsForProfiling(Isolate* isolate);
375 
376  private:
377  CpuProfiler();
378  ~CpuProfiler();
379  CpuProfiler(const CpuProfiler&);
380  CpuProfiler& operator=(const CpuProfiler&);
381 };
382 
388  public:
389  enum Type {
390  kContextVariable = 0, // A variable from a function context.
391  kElement = 1, // An element of an array.
392  kProperty = 2, // A named object property.
393  kInternal = 3, // A link that can't be accessed from JS,
394  // thus, its name isn't a real property name
395  // (e.g. parts of a ConsString).
396  kHidden = 4, // A link that is needed for proper sizes
397  // calculation, but may be hidden from user.
398  kShortcut = 5, // A link that must not be followed during
399  // sizes calculation.
400  kWeak = 6 // A weak reference (ignored by the GC).
401  };
402 
404  Type GetType() const;
405 
410  Local<Value> GetName() const;
411 
413  const HeapGraphNode* GetFromNode() const;
414 
416  const HeapGraphNode* GetToNode() const;
417 };
418 
419 
424  public:
425  enum Type {
426  kHidden = 0, // Hidden node, may be filtered when shown to user.
427  kArray = 1, // An array of elements.
428  kString = 2, // A string.
429  kObject = 3, // A JS object (except for arrays and strings).
430  kCode = 4, // Compiled code.
431  kClosure = 5, // Function closure.
432  kRegExp = 6, // RegExp.
433  kHeapNumber = 7, // Number stored in the heap.
434  kNative = 8, // Native object (not from V8 heap).
435  kSynthetic = 9, // Synthetic object, usually used for grouping
436  // snapshot items together.
437  kConsString = 10, // Concatenated string. A pair of pointers to strings.
438  kSlicedString = 11, // Sliced string. A fragment of another string.
439  kSymbol = 12, // A Symbol (ES6).
440  kBigInt = 13 // BigInt.
441  };
442 
444  Type GetType() const;
445 
451  Local<String> GetName() const;
452 
457  SnapshotObjectId GetId() const;
458 
460  size_t GetShallowSize() const;
461 
463  int GetChildrenCount() const;
464 
466  const HeapGraphEdge* GetChild(int index) const;
467 };
468 
469 
473 class V8_EXPORT OutputStream { // NOLINT
474  public:
475  enum WriteResult {
476  kContinue = 0,
477  kAbort = 1
478  };
479  virtual ~OutputStream() = default;
481  virtual void EndOfStream() = 0;
483  virtual int GetChunkSize() { return 1024; }
489  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
495  virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
496  return kAbort;
497  }
498 };
499 
500 
505  public:
507  kJSON = 0 // See format description near 'Serialize' method.
508  };
509 
511  const HeapGraphNode* GetRoot() const;
512 
514  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
515 
517  int GetNodesCount() const;
518 
520  const HeapGraphNode* GetNode(int index) const;
521 
523  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
524 
530  void Delete();
531 
558  void Serialize(OutputStream* stream,
559  SerializationFormat format = kJSON) const;
560 };
561 
562 
567 class V8_EXPORT ActivityControl { // NOLINT
568  public:
570  kContinue = 0,
571  kAbort = 1
572  };
573  virtual ~ActivityControl() = default;
578  virtual ControlOption ReportProgressValue(int done, int total) = 0;
579 };
580 
581 
587  public:
588  struct Allocation {
592  size_t size;
593 
597  unsigned int count;
598  };
599 
603  struct Node {
609 
615 
621 
626 
632 
638 
642  uint32_t node_id;
643 
649  std::vector<Node*> children;
650 
654  std::vector<Allocation> allocations;
655  };
656 
660  struct Sample {
664  uint32_t node_id;
665 
669  size_t size;
670 
674  unsigned int count;
675 
680  uint64_t sample_id;
681  };
682 
688  virtual Node* GetRootNode() = 0;
689  virtual const std::vector<Sample>& GetSamples() = 0;
690 
691  virtual ~AllocationProfile() = default;
692 
693  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
694  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
695 };
696 
712  public:
713  class Node {
714  public:
715  Node() = default;
716  virtual ~Node() = default;
717  virtual const char* Name() = 0;
718  virtual size_t SizeInBytes() = 0;
724  virtual Node* WrapperNode() { return nullptr; }
725  virtual bool IsRootNode() { return false; }
727  virtual bool IsEmbedderNode() { return true; }
731  virtual const char* NamePrefix() { return nullptr; }
732 
737  virtual NativeObject GetNativeObject() { return nullptr; }
738 
739  Node(const Node&) = delete;
740  Node& operator=(const Node&) = delete;
741  };
742 
747  virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
748 
753  virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
754 
763  virtual void AddEdge(Node* from, Node* to, const char* name = nullptr) = 0;
764 
765  virtual ~EmbedderGraph() = default;
766 };
767 
773  public:
775  kSamplingNoFlags = 0,
776  kSamplingForceGC = 1 << 0,
777  };
778 
785  typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
786  v8::EmbedderGraph* graph,
787  void* data);
788 
790  int GetSnapshotCount();
791 
793  const HeapSnapshot* GetHeapSnapshot(int index);
794 
799  SnapshotObjectId GetObjectId(Local<Value> value);
800 
805  SnapshotObjectId GetObjectId(NativeObject value);
806 
811  Local<Value> FindObjectById(SnapshotObjectId id);
812 
818  void ClearObjectIds();
819 
825  static const SnapshotObjectId kUnknownObjectId = 0;
826 
831  public:
836  virtual const char* GetName(Local<Object> object) = 0;
837 
838  protected:
839  virtual ~ObjectNameResolver() = default;
840  };
841 
845  const HeapSnapshot* TakeHeapSnapshot(
846  ActivityControl* control = nullptr,
847  ObjectNameResolver* global_object_name_resolver = nullptr,
848  bool treat_global_objects_as_roots = true);
849 
859  void StartTrackingHeapObjects(bool track_allocations = false);
860 
874  SnapshotObjectId GetHeapStats(OutputStream* stream,
875  int64_t* timestamp_us = nullptr);
876 
882  void StopTrackingHeapObjects();
883 
911  bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
912  int stack_depth = 16,
913  SamplingFlags flags = kSamplingNoFlags);
914 
918  void StopSamplingHeapProfiler();
919 
926  AllocationProfile* GetAllocationProfile();
927 
932  void DeleteAllHeapSnapshots();
933 
934  void AddBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
935  void* data);
936  void RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
937  void* data);
938 
944  static const uint16_t kPersistentHandleNoClassId = 0;
945 
946  private:
947  HeapProfiler();
948  ~HeapProfiler();
949  HeapProfiler(const HeapProfiler&);
950  HeapProfiler& operator=(const HeapProfiler&);
951 };
952 
958  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
959  : index(index), count(count), size(size) { }
960  uint32_t index; // Index of the time interval that was changed.
961  uint32_t count; // New value of count field for the interval with this index.
962  uint32_t size; // New value of size field for the interval with this index.
963 };
964 
965 #define CODE_EVENTS_LIST(V) \
966  V(Builtin) \
967  V(Callback) \
968  V(Eval) \
969  V(Function) \
970  V(InterpretedFunction) \
971  V(Handler) \
972  V(BytecodeHandler) \
973  V(LazyCompile) \
974  V(RegExp) \
975  V(Script) \
976  V(Stub) \
977  V(Relocation)
978 
985 #define V(Name) , k##Name##Type
987 #undef V
988 };
989 
994  public:
995  uintptr_t GetCodeStartAddress();
996  size_t GetCodeSize();
997  Local<String> GetFunctionName();
998  Local<String> GetScriptName();
999  int GetScriptLine();
1000  int GetScriptColumn();
1006  CodeEventType GetCodeType();
1007  const char* GetComment();
1008 
1009  static const char* GetCodeEventTypeName(CodeEventType code_event_type);
1010 
1011  uintptr_t GetPreviousCodeStartAddress();
1012 };
1013 
1018  public:
1024  explicit CodeEventHandler(Isolate* isolate);
1025  virtual ~CodeEventHandler();
1026 
1035  virtual void Handle(CodeEvent* code_event) = 0;
1036 
1041  void Enable();
1042 
1047  void Disable();
1048 
1049  private:
1050  CodeEventHandler();
1052  CodeEventHandler& operator=(const CodeEventHandler&);
1053  void* internal_listener_;
1054 };
1055 
1056 } // namespace v8
1057 
1058 
1059 #endif // V8_V8_PROFILER_H_
v8::MaybeLocal
Definition: v8.h:92
v8::Name
Definition: v8.h:2916
v8::kLazyLogging
@ kLazyLogging
Definition: v8-profiler.h:245
v8::HeapStatsUpdate::HeapStatsUpdate
HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
Definition: v8-profiler.h:958
v8::SnapshotObjectId
uint32_t SnapshotObjectId
Definition: v8-profiler.h:24
v8::CodeEvent
Definition: v8-profiler.h:993
v8::AllocationProfile::Node::name
Local< String > name
Definition: v8-profiler.h:608
v8::CpuProfileDeoptFrame::script_id
int script_id
Definition: v8-profiler.h:27
v8::OutputStream::WriteHeapStatsChunk
virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate *data, int count)
Definition: v8-profiler.h:495
v8::CodeEventType
CodeEventType
Definition: v8-profiler.h:983
v8::kEagerLogging
@ kEagerLogging
Definition: v8-profiler.h:248
v8::AllocationProfile::Sample::count
unsigned int count
Definition: v8-profiler.h:674
v8::CpuProfilingLoggingMode
CpuProfilingLoggingMode
Definition: v8-profiler.h:242
v8::AllocationProfile::Node::script_id
int script_id
Definition: v8-profiler.h:620
v8::Handle
Local< T > Handle
Definition: v8.h:346
v8::OutputStream::GetChunkSize
virtual int GetChunkSize()
Definition: v8-profiler.h:483
v8::HeapStatsUpdate::index
uint32_t index
Definition: v8-profiler.h:960
v8::ActivityControl::ControlOption
ControlOption
Definition: v8-profiler.h:569
v8::CpuProfileNode::LineTick::hit_count
unsigned int hit_count
Definition: v8-profiler.h:67
v8::NativeObject
void * NativeObject
Definition: v8-profiler.h:23
v8::AllocationProfile::Sample
Definition: v8-profiler.h:660
v8::CpuProfileDeoptInfo::stack
std::vector< CpuProfileDeoptFrame > stack
Definition: v8-profiler.h:46
v8::CpuProfileDeoptInfo::deopt_reason
const char * deopt_reason
Definition: v8-profiler.h:45
v8::AllocationProfile::Node::children
std::vector< Node * > children
Definition: v8-profiler.h:649
v8::AllocationProfile::Node::column_number
int column_number
Definition: v8-profiler.h:637
v8::CpuProfileNode
Definition: v8-profiler.h:60
v8::AllocationProfile::Allocation::count
unsigned int count
Definition: v8-profiler.h:597
v8::AllocationProfile::Sample::sample_id
uint64_t sample_id
Definition: v8-profiler.h:680
v8::CpuProfileNode::LineTick
Definition: v8-profiler.h:62
v8::HeapGraphNode
Definition: v8-profiler.h:423
v8::CpuProfilingOptions
Definition: v8-profiler.h:254
v8::CpuProfileNode::SourceType
SourceType
Definition: v8-profiler.h:71
v8::HeapGraphEdge::Type
Type
Definition: v8-profiler.h:389
v8::HeapGraphEdge
Definition: v8-profiler.h:387
v8::HeapProfiler::ObjectNameResolver
Definition: v8-profiler.h:830
v8::CpuProfilingMode
CpuProfilingMode
Definition: v8-profiler.h:223
v8::EmbedderGraph::Node::NamePrefix
virtual const char * NamePrefix()
Definition: v8-profiler.h:731
V8_EXPORT
#define V8_EXPORT
Definition: v8config.h:467
v8::Persistent
Definition: v8.h:97
v8::AllocationProfile::Node::allocations
std::vector< Allocation > allocations
Definition: v8-profiler.h:654
v8::HeapStatsUpdate
Definition: v8-profiler.h:957
v8::AllocationProfile::Node::start_position
int start_position
Definition: v8-profiler.h:625
v8::EmbedderGraph
Definition: v8-profiler.h:711
v8::HeapProfiler::SamplingFlags
SamplingFlags
Definition: v8-profiler.h:774
CODE_EVENTS_LIST
#define CODE_EVENTS_LIST(V)
Definition: v8-profiler.h:965
v8::Isolate
Definition: v8.h:8129
v8::CodeEventHandler
Definition: v8-profiler.h:1017
V
#define V(Name)
v8::EmbedderGraph::Node::GetNativeObject
virtual NativeObject GetNativeObject()
Definition: v8-profiler.h:737
v8::AllocationProfile
Definition: v8-profiler.h:586
v8::OutputStream::WriteResult
WriteResult
Definition: v8-profiler.h:475
v8::CpuProfileDeoptFrame::position
size_t position
Definition: v8-profiler.h:28
v8
Definition: libplatform.h:15
v8::ActivityControl
Definition: v8-profiler.h:567
v8::CpuProfilingOptions::mode
CpuProfilingMode mode() const
Definition: v8-profiler.h:277
v8::Local
Definition: v8.h:90
v8::AllocationProfile::Allocation
Definition: v8-profiler.h:588
v8::kDebugNaming
@ kDebugNaming
Definition: v8-profiler.h:239
v8::EmbedderGraph::Node::IsRootNode
virtual bool IsRootNode()
Definition: v8-profiler.h:725
v8::EmbedderGraph::Node::IsEmbedderNode
virtual bool IsEmbedderNode()
Definition: v8-profiler.h:727
v8::EmbedderGraph::Node::WrapperNode
virtual Node * WrapperNode()
Definition: v8-profiler.h:724
v8::AllocationProfile::Sample::size
size_t size
Definition: v8-profiler.h:669
v8::AllocationProfile::Allocation::size
size_t size
Definition: v8-profiler.h:592
v8::AllocationProfile::Node
Definition: v8-profiler.h:603
v8::CpuProfileDeoptInfo
Definition: v8-profiler.h:43
v8::kCallerLineNumbers
@ kCallerLineNumbers
Definition: v8-profiler.h:230
v8::OutputStream
Definition: v8-profiler.h:473
v8::CpuProfileDeoptFrame
Definition: v8-profiler.h:26
v8::CpuProfileNode::LineTick::line
int line
Definition: v8-profiler.h:64
v8::kStandardNaming
@ kStandardNaming
Definition: v8-profiler.h:236
v8::HeapStatsUpdate::count
uint32_t count
Definition: v8-profiler.h:961
v8.h
v8::AllocationProfile::Node::line_number
int line_number
Definition: v8-profiler.h:631
v8::CpuProfilingNamingMode
CpuProfilingNamingMode
Definition: v8-profiler.h:234
v8::kLeafNodeLineNumbers
@ kLeafNodeLineNumbers
Definition: v8-profiler.h:227
v8::HeapSnapshot::SerializationFormat
SerializationFormat
Definition: v8-profiler.h:506
v8::AllocationProfile::Node::node_id
uint32_t node_id
Definition: v8-profiler.h:642
v8::AllocationProfile::Node::script_name
Local< String > script_name
Definition: v8-profiler.h:614
v8::HeapGraphNode::Type
Type
Definition: v8-profiler.h:425
v8::EmbedderGraph::Node
Definition: v8-profiler.h:713
v8::CpuProfilingOptions::max_samples
unsigned max_samples() const
Definition: v8-profiler.h:278
v8::CpuProfilingOptions::sampling_interval_us
int sampling_interval_us() const
Definition: v8-profiler.h:279
v8::HeapStatsUpdate::size
uint32_t size
Definition: v8-profiler.h:962
v8::CpuProfiler
Definition: v8-profiler.h:297
v8::kUnknownType
@ kUnknownType
Definition: v8-profiler.h:984
v8::HeapSnapshot
Definition: v8-profiler.h:504
v8::CpuProfile
Definition: v8-profiler.h:176
v8::AllocationProfile::Sample::node_id
uint32_t node_id
Definition: v8-profiler.h:664
v8::HeapProfiler
Definition: v8-profiler.h:772