• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include <memory>
11 #include <unordered_set>
12 #include <vector>
13 
14 #include "v8.h"  // NOLINT(build/include_directory)
15 
16 /**
17  * Profiler support for the V8 JavaScript engine.
18  */
19 namespace v8 {
20 
21 class HeapGraphNode;
22 struct HeapStatsUpdate;
23 
24 using NativeObject = void*;
25 using SnapshotObjectId = uint32_t;
26 
27 struct CpuProfileDeoptFrame {
28   int script_id;
29   size_t position;
30 };
31 
32 namespace internal {
33 class CpuProfile;
34 }  // namespace internal
35 
36 }  // namespace v8
37 
38 #ifdef V8_OS_WIN
39 template class V8_EXPORT std::vector<v8::CpuProfileDeoptFrame>;
40 #endif
41 
42 namespace v8 {
43 
44 struct V8_EXPORT CpuProfileDeoptInfo {
45   /** A pointer to a static string owned by v8. */
46   const char* deopt_reason;
47   std::vector<CpuProfileDeoptFrame> stack;
48 };
49 
50 }  // namespace v8
51 
52 #ifdef V8_OS_WIN
53 template class V8_EXPORT std::vector<v8::CpuProfileDeoptInfo>;
54 #endif
55 
56 namespace v8 {
57 
58 /**
59  * CpuProfileNode represents a node in a call graph.
60  */
61 class V8_EXPORT CpuProfileNode {
62  public:
63   struct LineTick {
64     /** The 1-based number of the source line where the function originates. */
65     int line;
66 
67     /** The count of samples associated with the source line. */
68     unsigned int hit_count;
69   };
70 
71   // An annotation hinting at the source of a CpuProfileNode.
72   enum SourceType {
73     // User-supplied script with associated resource information.
74     kScript = 0,
75     // Native scripts and provided builtins.
76     kBuiltin = 1,
77     // Callbacks into native code.
78     kCallback = 2,
79     // VM-internal functions or state.
80     kInternal = 3,
81     // A node that failed to symbolize.
82     kUnresolved = 4,
83   };
84 
85   /** Returns function name (empty string for anonymous functions.) */
86   Local<String> GetFunctionName() const;
87 
88   /**
89    * Returns function name (empty string for anonymous functions.)
90    * The string ownership is *not* passed to the caller. It stays valid until
91    * profile is deleted. The function is thread safe.
92    */
93   const char* GetFunctionNameStr() const;
94 
95   /** Returns id of the script where function is located. */
96   int GetScriptId() const;
97 
98   /** Returns resource name for script from where the function originates. */
99   Local<String> GetScriptResourceName() const;
100 
101   /**
102    * Returns resource name for script from where the function originates.
103    * The string ownership is *not* passed to the caller. It stays valid until
104    * profile is deleted. The function is thread safe.
105    */
106   const char* GetScriptResourceNameStr() const;
107 
108   /**
109    * Return true if the script from where the function originates is flagged as
110    * being shared cross-origin.
111    */
112   bool IsScriptSharedCrossOrigin() const;
113 
114   /**
115    * Returns the number, 1-based, of the line where the function originates.
116    * kNoLineNumberInfo if no line number information is available.
117    */
118   int GetLineNumber() const;
119 
120   /**
121    * Returns 1-based number of the column where the function originates.
122    * kNoColumnNumberInfo if no column number information is available.
123    */
124   int GetColumnNumber() const;
125 
126   /**
127    * Returns the number of the function's source lines that collect the samples.
128    */
129   unsigned int GetHitLineCount() const;
130 
131   /** Returns the set of source lines that collect the samples.
132    *  The caller allocates buffer and responsible for releasing it.
133    *  True if all available entries are copied, otherwise false.
134    *  The function copies nothing if buffer is not large enough.
135    */
136   bool GetLineTicks(LineTick* entries, unsigned int length) const;
137 
138   /** Returns bailout reason for the function
139     * if the optimization was disabled for it.
140     */
141   const char* GetBailoutReason() const;
142 
143   /**
144     * Returns the count of samples where the function was currently executing.
145     */
146   unsigned GetHitCount() const;
147 
148   /** Returns id of the node. The id is unique within the tree */
149   unsigned GetNodeId() const;
150 
151   /**
152    * Gets the type of the source which the node was captured from.
153    */
154   SourceType GetSourceType() const;
155 
156   /** Returns child nodes count of the node. */
157   int GetChildrenCount() const;
158 
159   /** Retrieves a child node by index. */
160   const CpuProfileNode* GetChild(int index) const;
161 
162   /** Retrieves the ancestor node, or null if the root. */
163   const CpuProfileNode* GetParent() const;
164 
165   /** Retrieves deopt infos for the node. */
166   const std::vector<CpuProfileDeoptInfo>& GetDeoptInfos() const;
167 
168   static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
169   static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
170 };
171 
172 
173 /**
174  * CpuProfile contains a CPU profile in a form of top-down call tree
175  * (from main() down to functions that do all the work).
176  */
177 class V8_EXPORT CpuProfile {
178  public:
179   /** Returns CPU profile title. */
180   Local<String> GetTitle() const;
181 
182   /** Returns the root node of the top down call tree. */
183   const CpuProfileNode* GetTopDownRoot() const;
184 
185   /**
186    * Returns number of samples recorded. The samples are not recorded unless
187    * |record_samples| parameter of CpuProfiler::StartCpuProfiling is true.
188    */
189   int GetSamplesCount() const;
190 
191   /**
192    * Returns profile node corresponding to the top frame the sample at
193    * the given index.
194    */
195   const CpuProfileNode* GetSample(int index) const;
196 
197   /**
198    * Returns the timestamp of the sample. The timestamp is the number of
199    * microseconds since some unspecified starting point.
200    * The point is equal to the starting point used by GetStartTime.
201    */
202   int64_t GetSampleTimestamp(int index) const;
203 
204   /**
205    * Returns time when the profile recording was started (in microseconds)
206    * since some unspecified starting point.
207    */
208   int64_t GetStartTime() const;
209 
210   /**
211    * Returns time when the profile recording was stopped (in microseconds)
212    * since some unspecified starting point.
213    * The point is equal to the starting point used by GetStartTime.
214    */
215   int64_t GetEndTime() const;
216 
217   /**
218    * Deletes the profile and removes it from CpuProfiler's list.
219    * All pointers to nodes previously returned become invalid.
220    */
221   void Delete();
222 };
223 
224 enum CpuProfilingMode {
225   // In the resulting CpuProfile tree, intermediate nodes in a stack trace
226   // (from the root to a leaf) will have line numbers that point to the start
227   // line of the function, rather than the line of the callsite of the child.
228   kLeafNodeLineNumbers,
229   // In the resulting CpuProfile tree, nodes are separated based on the line
230   // number of their callsite in their parent.
231   kCallerLineNumbers,
232 };
233 
234 // Determines how names are derived for functions sampled.
235 enum CpuProfilingNamingMode {
236   // Use the immediate name of functions at compilation time.
237   kStandardNaming,
238   // Use more verbose naming for functions without names, inferred from scope
239   // where possible.
240   kDebugNaming,
241 };
242 
243 enum CpuProfilingLoggingMode {
244   // Enables logging when a profile is active, and disables logging when all
245   // profiles are detached.
246   kLazyLogging,
247   // Enables logging for the lifetime of the CpuProfiler. Calls to
248   // StartRecording are faster, at the expense of runtime overhead.
249   kEagerLogging,
250 };
251 
252 // Enum for returning profiling status. Once StartProfiling is called,
253 // we want to return to clients whether the profiling was able to start
254 // correctly, or return a descriptive error.
255 enum class CpuProfilingStatus {
256   kStarted,
257   kAlreadyStarted,
258   kErrorTooManyProfilers
259 };
260 
261 /**
262  * Optional profiling attributes.
263  */
264 class V8_EXPORT CpuProfilingOptions {
265  public:
266   // Indicates that the sample buffer size should not be explicitly limited.
267   static const unsigned kNoSampleLimit = UINT_MAX;
268 
269   /**
270    * \param mode Type of computation of stack frame line numbers.
271    * \param max_samples The maximum number of samples that should be recorded by
272    *                    the profiler. Samples obtained after this limit will be
273    *                    discarded.
274    * \param sampling_interval_us controls the profile-specific target
275    *                             sampling interval. The provided sampling
276    *                             interval will be snapped to the next lowest
277    *                             non-zero multiple of the profiler's sampling
278    *                             interval, set via SetSamplingInterval(). If
279    *                             zero, the sampling interval will be equal to
280    *                             the profiler's sampling interval.
281    * \param filter_context Deprecated option to filter by context, currently a
282    *                       no-op.
283    */
284   CpuProfilingOptions(
285       CpuProfilingMode mode = kLeafNodeLineNumbers,
286       unsigned max_samples = kNoSampleLimit, int sampling_interval_us = 0,
287       MaybeLocal<Context> filter_context = MaybeLocal<Context>());
288 
mode()289   CpuProfilingMode mode() const { return mode_; }
max_samples()290   unsigned max_samples() const { return max_samples_; }
sampling_interval_us()291   int sampling_interval_us() const { return sampling_interval_us_; }
292 
293  private:
294   friend class internal::CpuProfile;
295 
296   CpuProfilingMode mode_;
297   unsigned max_samples_;
298   int sampling_interval_us_;
299 };
300 
301 /**
302  * Interface for controlling CPU profiling. Instance of the
303  * profiler can be created using v8::CpuProfiler::New method.
304  */
305 class V8_EXPORT CpuProfiler {
306  public:
307   /**
308    * Creates a new CPU profiler for the |isolate|. The isolate must be
309    * initialized. The profiler object must be disposed after use by calling
310    * |Dispose| method.
311    */
312   static CpuProfiler* New(Isolate* isolate,
313                           CpuProfilingNamingMode = kDebugNaming,
314                           CpuProfilingLoggingMode = kLazyLogging);
315 
316   /**
317    * Synchronously collect current stack sample in all profilers attached to
318    * the |isolate|. The call does not affect number of ticks recorded for
319    * the current top node.
320    */
321   static void CollectSample(Isolate* isolate);
322 
323   /**
324    * Disposes the CPU profiler object.
325    */
326   void Dispose();
327 
328   /**
329    * Changes default CPU profiler sampling interval to the specified number
330    * of microseconds. Default interval is 1000us. This method must be called
331    * when there are no profiles being recorded.
332    */
333   void SetSamplingInterval(int us);
334 
335   /**
336    * Sets whether or not the profiler should prioritize consistency of sample
337    * periodicity on Windows. Disabling this can greatly reduce CPU usage, but
338    * may result in greater variance in sample timings from the platform's
339    * scheduler. Defaults to enabled. This method must be called when there are
340    * no profiles being recorded.
341    */
342   void SetUsePreciseSampling(bool);
343 
344   /**
345    * Starts collecting a CPU profile. Title may be an empty string. Several
346    * profiles may be collected at once. Attempts to start collecting several
347    * profiles with the same title are silently ignored.
348    */
349   CpuProfilingStatus StartProfiling(Local<String> title,
350                                     CpuProfilingOptions options);
351 
352   /**
353    * Starts profiling with the same semantics as above, except with expanded
354    * parameters.
355    *
356    * |record_samples| parameter controls whether individual samples should
357    * be recorded in addition to the aggregated tree.
358    *
359    * |max_samples| controls the maximum number of samples that should be
360    * recorded by the profiler. Samples obtained after this limit will be
361    * discarded.
362    */
363   CpuProfilingStatus StartProfiling(
364       Local<String> title, CpuProfilingMode mode, bool record_samples = false,
365       unsigned max_samples = CpuProfilingOptions::kNoSampleLimit);
366   /**
367    * The same as StartProfiling above, but the CpuProfilingMode defaults to
368    * kLeafNodeLineNumbers mode, which was the previous default behavior of the
369    * profiler.
370    */
371   CpuProfilingStatus StartProfiling(Local<String> title,
372                                     bool record_samples = false);
373 
374   /**
375    * Stops collecting CPU profile with a given title and returns it.
376    * If the title given is empty, finishes the last profile started.
377    */
378   CpuProfile* StopProfiling(Local<String> title);
379 
380   /**
381    * Generate more detailed source positions to code objects. This results in
382    * better results when mapping profiling samples to script source.
383    */
384   static void UseDetailedSourcePositionsForProfiling(Isolate* isolate);
385 
386  private:
387   CpuProfiler();
388   ~CpuProfiler();
389   CpuProfiler(const CpuProfiler&);
390   CpuProfiler& operator=(const CpuProfiler&);
391 };
392 
393 /**
394  * HeapSnapshotEdge represents a directed connection between heap
395  * graph nodes: from retainers to retained nodes.
396  */
397 class V8_EXPORT HeapGraphEdge {
398  public:
399   enum Type {
400     kContextVariable = 0,  // A variable from a function context.
401     kElement = 1,          // An element of an array.
402     kProperty = 2,         // A named object property.
403     kInternal = 3,         // A link that can't be accessed from JS,
404                            // thus, its name isn't a real property name
405                            // (e.g. parts of a ConsString).
406     kHidden = 4,           // A link that is needed for proper sizes
407                            // calculation, but may be hidden from user.
408     kShortcut = 5,         // A link that must not be followed during
409                            // sizes calculation.
410     kWeak = 6              // A weak reference (ignored by the GC).
411   };
412 
413   /** Returns edge type (see HeapGraphEdge::Type). */
414   Type GetType() const;
415 
416   /**
417    * Returns edge name. This can be a variable name, an element index, or
418    * a property name.
419    */
420   Local<Value> GetName() const;
421 
422   /** Returns origin node. */
423   const HeapGraphNode* GetFromNode() const;
424 
425   /** Returns destination node. */
426   const HeapGraphNode* GetToNode() const;
427 };
428 
429 
430 /**
431  * HeapGraphNode represents a node in a heap graph.
432  */
433 class V8_EXPORT HeapGraphNode {
434  public:
435   enum Type {
436     kHidden = 0,         // Hidden node, may be filtered when shown to user.
437     kArray = 1,          // An array of elements.
438     kString = 2,         // A string.
439     kObject = 3,         // A JS object (except for arrays and strings).
440     kCode = 4,           // Compiled code.
441     kClosure = 5,        // Function closure.
442     kRegExp = 6,         // RegExp.
443     kHeapNumber = 7,     // Number stored in the heap.
444     kNative = 8,         // Native object (not from V8 heap).
445     kSynthetic = 9,      // Synthetic object, usually used for grouping
446                          // snapshot items together.
447     kConsString = 10,    // Concatenated string. A pair of pointers to strings.
448     kSlicedString = 11,  // Sliced string. A fragment of another string.
449     kSymbol = 12,        // A Symbol (ES6).
450     kBigInt = 13         // BigInt.
451   };
452 
453   /** Returns node type (see HeapGraphNode::Type). */
454   Type GetType() const;
455 
456   /**
457    * Returns node name. Depending on node's type this can be the name
458    * of the constructor (for objects), the name of the function (for
459    * closures), string value, or an empty string (for compiled code).
460    */
461   Local<String> GetName() const;
462 
463   /**
464    * Returns node id. For the same heap object, the id remains the same
465    * across all snapshots.
466    */
467   SnapshotObjectId GetId() const;
468 
469   /** Returns node's own size, in bytes. */
470   size_t GetShallowSize() const;
471 
472   /** Returns child nodes count of the node. */
473   int GetChildrenCount() const;
474 
475   /** Retrieves a child by index. */
476   const HeapGraphEdge* GetChild(int index) const;
477 };
478 
479 
480 /**
481  * An interface for exporting data from V8, using "push" model.
482  */
483 class V8_EXPORT OutputStream {  // NOLINT
484  public:
485   enum WriteResult {
486     kContinue = 0,
487     kAbort = 1
488   };
489   virtual ~OutputStream() = default;
490   /** Notify about the end of stream. */
491   virtual void EndOfStream() = 0;
492   /** Get preferred output chunk size. Called only once. */
GetChunkSize()493   virtual int GetChunkSize() { return 1024; }
494   /**
495    * Writes the next chunk of snapshot data into the stream. Writing
496    * can be stopped by returning kAbort as function result. EndOfStream
497    * will not be called in case writing was aborted.
498    */
499   virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
500   /**
501    * Writes the next chunk of heap stats data into the stream. Writing
502    * can be stopped by returning kAbort as function result. EndOfStream
503    * will not be called in case writing was aborted.
504    */
WriteHeapStatsChunk(HeapStatsUpdate * data,int count)505   virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
506     return kAbort;
507   }
508 };
509 
510 
511 /**
512  * HeapSnapshots record the state of the JS heap at some moment.
513  */
514 class V8_EXPORT HeapSnapshot {
515  public:
516   enum SerializationFormat {
517     kJSON = 0  // See format description near 'Serialize' method.
518   };
519 
520   /** Returns the root node of the heap graph. */
521   const HeapGraphNode* GetRoot() const;
522 
523   /** Returns a node by its id. */
524   const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
525 
526   /** Returns total nodes count in the snapshot. */
527   int GetNodesCount() const;
528 
529   /** Returns a node by index. */
530   const HeapGraphNode* GetNode(int index) const;
531 
532   /** Returns a max seen JS object Id. */
533   SnapshotObjectId GetMaxSnapshotJSObjectId() const;
534 
535   /**
536    * Deletes the snapshot and removes it from HeapProfiler's list.
537    * All pointers to nodes, edges and paths previously returned become
538    * invalid.
539    */
540   void Delete();
541 
542   /**
543    * Prepare a serialized representation of the snapshot. The result
544    * is written into the stream provided in chunks of specified size.
545    * The total length of the serialized snapshot is unknown in
546    * advance, it can be roughly equal to JS heap size (that means,
547    * it can be really big - tens of megabytes).
548    *
549    * For the JSON format, heap contents are represented as an object
550    * with the following structure:
551    *
552    *  {
553    *    snapshot: {
554    *      title: "...",
555    *      uid: nnn,
556    *      meta: { meta-info },
557    *      node_count: nnn,
558    *      edge_count: nnn
559    *    },
560    *    nodes: [nodes array],
561    *    edges: [edges array],
562    *    strings: [strings array]
563    *  }
564    *
565    * Nodes reference strings, other nodes, and edges by their indexes
566    * in corresponding arrays.
567    */
568   void Serialize(OutputStream* stream,
569                  SerializationFormat format = kJSON) const;
570 };
571 
572 
573 /**
574  * An interface for reporting progress and controlling long-running
575  * activities.
576  */
577 class V8_EXPORT ActivityControl {  // NOLINT
578  public:
579   enum ControlOption {
580     kContinue = 0,
581     kAbort = 1
582   };
583   virtual ~ActivityControl() = default;
584   /**
585    * Notify about current progress. The activity can be stopped by
586    * returning kAbort as the callback result.
587    */
588   virtual ControlOption ReportProgressValue(int done, int total) = 0;
589 };
590 
591 
592 /**
593  * AllocationProfile is a sampled profile of allocations done by the program.
594  * This is structured as a call-graph.
595  */
596 class V8_EXPORT AllocationProfile {
597  public:
598   struct Allocation {
599     /**
600      * Size of the sampled allocation object.
601      */
602     size_t size;
603 
604     /**
605      * The number of objects of such size that were sampled.
606      */
607     unsigned int count;
608   };
609 
610   /**
611    * Represents a node in the call-graph.
612    */
613   struct Node {
614     /**
615      * Name of the function. May be empty for anonymous functions or if the
616      * script corresponding to this function has been unloaded.
617      */
618     Local<String> name;
619 
620     /**
621      * Name of the script containing the function. May be empty if the script
622      * name is not available, or if the script has been unloaded.
623      */
624     Local<String> script_name;
625 
626     /**
627      * id of the script where the function is located. May be equal to
628      * v8::UnboundScript::kNoScriptId in cases where the script doesn't exist.
629      */
630     int script_id;
631 
632     /**
633      * Start position of the function in the script.
634      */
635     int start_position;
636 
637     /**
638      * 1-indexed line number where the function starts. May be
639      * kNoLineNumberInfo if no line number information is available.
640      */
641     int line_number;
642 
643     /**
644      * 1-indexed column number where the function starts. May be
645      * kNoColumnNumberInfo if no line number information is available.
646      */
647     int column_number;
648 
649     /**
650      * Unique id of the node.
651      */
652     uint32_t node_id;
653 
654     /**
655      * List of callees called from this node for which we have sampled
656      * allocations. The lifetime of the children is scoped to the containing
657      * AllocationProfile.
658      */
659     std::vector<Node*> children;
660 
661     /**
662      * List of self allocations done by this node in the call-graph.
663      */
664     std::vector<Allocation> allocations;
665   };
666 
667   /**
668    * Represent a single sample recorded for an allocation.
669    */
670   struct Sample {
671     /**
672      * id of the node in the profile tree.
673      */
674     uint32_t node_id;
675 
676     /**
677      * Size of the sampled allocation object.
678      */
679     size_t size;
680 
681     /**
682      * The number of objects of such size that were sampled.
683      */
684     unsigned int count;
685 
686     /**
687      * Unique time-ordered id of the allocation sample. Can be used to track
688      * what samples were added or removed between two snapshots.
689      */
690     uint64_t sample_id;
691   };
692 
693   /**
694    * Returns the root node of the call-graph. The root node corresponds to an
695    * empty JS call-stack. The lifetime of the returned Node* is scoped to the
696    * containing AllocationProfile.
697    */
698   virtual Node* GetRootNode() = 0;
699   virtual const std::vector<Sample>& GetSamples() = 0;
700 
701   virtual ~AllocationProfile() = default;
702 
703   static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
704   static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
705 };
706 
707 /**
708  * An object graph consisting of embedder objects and V8 objects.
709  * Edges of the graph are strong references between the objects.
710  * The embedder can build this graph during heap snapshot generation
711  * to include the embedder objects in the heap snapshot.
712  * Usage:
713  * 1) Define derived class of EmbedderGraph::Node for embedder objects.
714  * 2) Set the build embedder graph callback on the heap profiler using
715  *    HeapProfiler::AddBuildEmbedderGraphCallback.
716  * 3) In the callback use graph->AddEdge(node1, node2) to add an edge from
717  *    node1 to node2.
718  * 4) To represent references from/to V8 object, construct V8 nodes using
719  *    graph->V8Node(value).
720  */
721 class V8_EXPORT EmbedderGraph {
722  public:
723   class Node {
724    public:
725     /**
726      * Detachedness specifies whether an object is attached or detached from the
727      * main application state. While unkown in general, there may be objects
728      * that specifically know their state. V8 passes this information along in
729      * the snapshot. Users of the snapshot may use it to annotate the object
730      * graph.
731      */
732     enum class Detachedness : uint8_t {
733       kUnknown = 0,
734       kAttached = 1,
735       kDetached = 2,
736     };
737 
738     Node() = default;
739     virtual ~Node() = default;
740     virtual const char* Name() = 0;
741     virtual size_t SizeInBytes() = 0;
742     /**
743      * The corresponding V8 wrapper node if not null.
744      * During heap snapshot generation the embedder node and the V8 wrapper
745      * node will be merged into one node to simplify retaining paths.
746      */
WrapperNode()747     virtual Node* WrapperNode() { return nullptr; }
IsRootNode()748     virtual bool IsRootNode() { return false; }
749     /** Must return true for non-V8 nodes. */
IsEmbedderNode()750     virtual bool IsEmbedderNode() { return true; }
751     /**
752      * Optional name prefix. It is used in Chrome for tagging detached nodes.
753      */
NamePrefix()754     virtual const char* NamePrefix() { return nullptr; }
755 
756     /**
757      * Returns the NativeObject that can be used for querying the
758      * |HeapSnapshot|.
759      */
GetNativeObject()760     virtual NativeObject GetNativeObject() { return nullptr; }
761 
762     /**
763      * Detachedness state of a given object. While unkown in general, there may
764      * be objects that specifically know their state. V8 passes this information
765      * along in the snapshot. Users of the snapshot may use it to annotate the
766      * object graph.
767      */
GetDetachedness()768     virtual Detachedness GetDetachedness() { return Detachedness::kUnknown; }
769 
770     Node(const Node&) = delete;
771     Node& operator=(const Node&) = delete;
772   };
773 
774   /**
775    * Returns a node corresponding to the given V8 value. Ownership is not
776    * transferred. The result pointer is valid while the graph is alive.
777    */
778   virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
779 
780   /**
781    * Adds the given node to the graph and takes ownership of the node.
782    * Returns a raw pointer to the node that is valid while the graph is alive.
783    */
784   virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
785 
786   /**
787    * Adds an edge that represents a strong reference from the given
788    * node |from| to the given node |to|. The nodes must be added to the graph
789    * before calling this function.
790    *
791    * If name is nullptr, the edge will have auto-increment indexes, otherwise
792    * it will be named accordingly.
793    */
794   virtual void AddEdge(Node* from, Node* to, const char* name = nullptr) = 0;
795 
796   virtual ~EmbedderGraph() = default;
797 };
798 
799 /**
800  * Interface for controlling heap profiling. Instance of the
801  * profiler can be retrieved using v8::Isolate::GetHeapProfiler.
802  */
803 class V8_EXPORT HeapProfiler {
804  public:
805   enum SamplingFlags {
806     kSamplingNoFlags = 0,
807     kSamplingForceGC = 1 << 0,
808   };
809 
810   /**
811    * Callback function invoked during heap snapshot generation to retrieve
812    * the embedder object graph. The callback should use graph->AddEdge(..) to
813    * add references between the objects.
814    * The callback must not trigger garbage collection in V8.
815    */
816   typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
817                                              v8::EmbedderGraph* graph,
818                                              void* data);
819 
820   /**
821    * Callback function invoked during heap snapshot generation to retrieve
822    * the detachedness state of an object referenced by a TracedReference.
823    *
824    * The callback takes Local<Value> as parameter to allow the embedder to
825    * unpack the TracedReference into a Local and reuse that Local for different
826    * purposes.
827    */
828   using GetDetachednessCallback = EmbedderGraph::Node::Detachedness (*)(
829       v8::Isolate* isolate, const v8::Local<v8::Value>& v8_value,
830       uint16_t class_id, void* data);
831 
832   /** Returns the number of snapshots taken. */
833   int GetSnapshotCount();
834 
835   /** Returns a snapshot by index. */
836   const HeapSnapshot* GetHeapSnapshot(int index);
837 
838   /**
839    * Returns SnapshotObjectId for a heap object referenced by |value| if
840    * it has been seen by the heap profiler, kUnknownObjectId otherwise.
841    */
842   SnapshotObjectId GetObjectId(Local<Value> value);
843 
844   /**
845    * Returns SnapshotObjectId for a native object referenced by |value| if it
846    * has been seen by the heap profiler, kUnknownObjectId otherwise.
847    */
848   SnapshotObjectId GetObjectId(NativeObject value);
849 
850   /**
851    * Returns heap object with given SnapshotObjectId if the object is alive,
852    * otherwise empty handle is returned.
853    */
854   Local<Value> FindObjectById(SnapshotObjectId id);
855 
856   /**
857    * Clears internal map from SnapshotObjectId to heap object. The new objects
858    * will not be added into it unless a heap snapshot is taken or heap object
859    * tracking is kicked off.
860    */
861   void ClearObjectIds();
862 
863   /**
864    * A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
865    * it in case heap profiler cannot find id  for the object passed as
866    * parameter. HeapSnapshot::GetNodeById will always return NULL for such id.
867    */
868   static const SnapshotObjectId kUnknownObjectId = 0;
869 
870   /**
871    * Callback interface for retrieving user friendly names of global objects.
872    */
873   class ObjectNameResolver {
874    public:
875     /**
876      * Returns name to be used in the heap snapshot for given node. Returned
877      * string must stay alive until snapshot collection is completed.
878      */
879     virtual const char* GetName(Local<Object> object) = 0;
880 
881    protected:
882     virtual ~ObjectNameResolver() = default;
883   };
884 
885   /**
886    * Takes a heap snapshot and returns it.
887    */
888   const HeapSnapshot* TakeHeapSnapshot(
889       ActivityControl* control = nullptr,
890       ObjectNameResolver* global_object_name_resolver = nullptr,
891       bool treat_global_objects_as_roots = true);
892 
893   /**
894    * Starts tracking of heap objects population statistics. After calling
895    * this method, all heap objects relocations done by the garbage collector
896    * are being registered.
897    *
898    * |track_allocations| parameter controls whether stack trace of each
899    * allocation in the heap will be recorded and reported as part of
900    * HeapSnapshot.
901    */
902   void StartTrackingHeapObjects(bool track_allocations = false);
903 
904   /**
905    * Adds a new time interval entry to the aggregated statistics array. The
906    * time interval entry contains information on the current heap objects
907    * population size. The method also updates aggregated statistics and
908    * reports updates for all previous time intervals via the OutputStream
909    * object. Updates on each time interval are provided as a stream of the
910    * HeapStatsUpdate structure instances.
911    * If |timestamp_us| is supplied, timestamp of the new entry will be written
912    * into it. The return value of the function is the last seen heap object Id.
913    *
914    * StartTrackingHeapObjects must be called before the first call to this
915    * method.
916    */
917   SnapshotObjectId GetHeapStats(OutputStream* stream,
918                                 int64_t* timestamp_us = nullptr);
919 
920   /**
921    * Stops tracking of heap objects population statistics, cleans up all
922    * collected data. StartHeapObjectsTracking must be called again prior to
923    * calling GetHeapStats next time.
924    */
925   void StopTrackingHeapObjects();
926 
927   /**
928    * Starts gathering a sampling heap profile. A sampling heap profile is
929    * similar to tcmalloc's heap profiler and Go's mprof. It samples object
930    * allocations and builds an online 'sampling' heap profile. At any point in
931    * time, this profile is expected to be a representative sample of objects
932    * currently live in the system. Each sampled allocation includes the stack
933    * trace at the time of allocation, which makes this really useful for memory
934    * leak detection.
935    *
936    * This mechanism is intended to be cheap enough that it can be used in
937    * production with minimal performance overhead.
938    *
939    * Allocations are sampled using a randomized Poisson process. On average, one
940    * allocation will be sampled every |sample_interval| bytes allocated. The
941    * |stack_depth| parameter controls the maximum number of stack frames to be
942    * captured on each allocation.
943    *
944    * NOTE: This is a proof-of-concept at this point. Right now we only sample
945    * newspace allocations. Support for paged space allocation (e.g. pre-tenured
946    * objects, large objects, code objects, etc.) and native allocations
947    * doesn't exist yet, but is anticipated in the future.
948    *
949    * Objects allocated before the sampling is started will not be included in
950    * the profile.
951    *
952    * Returns false if a sampling heap profiler is already running.
953    */
954   bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
955                                  int stack_depth = 16,
956                                  SamplingFlags flags = kSamplingNoFlags);
957 
958   /**
959    * Stops the sampling heap profile and discards the current profile.
960    */
961   void StopSamplingHeapProfiler();
962 
963   /**
964    * Returns the sampled profile of allocations allocated (and still live) since
965    * StartSamplingHeapProfiler was called. The ownership of the pointer is
966    * transferred to the caller. Returns nullptr if sampling heap profiler is not
967    * active.
968    */
969   AllocationProfile* GetAllocationProfile();
970 
971   /**
972    * Deletes all snapshots taken. All previously returned pointers to
973    * snapshots and their contents become invalid after this call.
974    */
975   void DeleteAllHeapSnapshots();
976 
977   void AddBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
978                                      void* data);
979   void RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
980                                         void* data);
981 
982   void SetGetDetachednessCallback(GetDetachednessCallback callback, void* data);
983 
984   /**
985    * Default value of persistent handle class ID. Must not be used to
986    * define a class. Can be used to reset a class of a persistent
987    * handle.
988    */
989   static const uint16_t kPersistentHandleNoClassId = 0;
990 
991  private:
992   HeapProfiler();
993   ~HeapProfiler();
994   HeapProfiler(const HeapProfiler&);
995   HeapProfiler& operator=(const HeapProfiler&);
996 };
997 
998 /**
999  * A struct for exporting HeapStats data from V8, using "push" model.
1000  * See HeapProfiler::GetHeapStats.
1001  */
1002 struct HeapStatsUpdate {
HeapStatsUpdateHeapStatsUpdate1003   HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
1004     : index(index), count(count), size(size) { }
1005   uint32_t index;  // Index of the time interval that was changed.
1006   uint32_t count;  // New value of count field for the interval with this index.
1007   uint32_t size;  // New value of size field for the interval with this index.
1008 };
1009 
1010 #define CODE_EVENTS_LIST(V) \
1011   V(Builtin)                \
1012   V(Callback)               \
1013   V(Eval)                   \
1014   V(Function)               \
1015   V(InterpretedFunction)    \
1016   V(Handler)                \
1017   V(BytecodeHandler)        \
1018   V(LazyCompile)            \
1019   V(RegExp)                 \
1020   V(Script)                 \
1021   V(Stub)                   \
1022   V(Relocation)
1023 
1024 /**
1025  * Note that this enum may be extended in the future. Please include a default
1026  * case if this enum is used in a switch statement.
1027  */
1028 enum CodeEventType {
1029   kUnknownType = 0
1030 #define V(Name) , k##Name##Type
1031   CODE_EVENTS_LIST(V)
1032 #undef V
1033 };
1034 
1035 /**
1036  * Representation of a code creation event
1037  */
1038 class V8_EXPORT CodeEvent {
1039  public:
1040   uintptr_t GetCodeStartAddress();
1041   size_t GetCodeSize();
1042   Local<String> GetFunctionName();
1043   Local<String> GetScriptName();
1044   int GetScriptLine();
1045   int GetScriptColumn();
1046   /**
1047    * NOTE (mmarchini): We can't allocate objects in the heap when we collect
1048    * existing code, and both the code type and the comment are not stored in the
1049    * heap, so we return those as const char*.
1050    */
1051   CodeEventType GetCodeType();
1052   const char* GetComment();
1053 
1054   static const char* GetCodeEventTypeName(CodeEventType code_event_type);
1055 
1056   uintptr_t GetPreviousCodeStartAddress();
1057 };
1058 
1059 /**
1060  * Interface to listen to code creation and code relocation events.
1061  */
1062 class V8_EXPORT CodeEventHandler {
1063  public:
1064   /**
1065    * Creates a new listener for the |isolate|. The isolate must be initialized.
1066    * The listener object must be disposed after use by calling |Dispose| method.
1067    * Multiple listeners can be created for the same isolate.
1068    */
1069   explicit CodeEventHandler(Isolate* isolate);
1070   virtual ~CodeEventHandler();
1071 
1072   /**
1073    * Handle is called every time a code object is created or moved. Information
1074    * about each code event will be available through the `code_event`
1075    * parameter.
1076    *
1077    * When the CodeEventType is kRelocationType, the code for this CodeEvent has
1078    * moved from `GetPreviousCodeStartAddress()` to `GetCodeStartAddress()`.
1079    */
1080   virtual void Handle(CodeEvent* code_event) = 0;
1081 
1082   /**
1083    * Call `Enable()` to starts listening to code creation and code relocation
1084    * events. These events will be handled by `Handle()`.
1085    */
1086   void Enable();
1087 
1088   /**
1089    * Call `Disable()` to stop listening to code creation and code relocation
1090    * events.
1091    */
1092   void Disable();
1093 
1094  private:
1095   CodeEventHandler();
1096   CodeEventHandler(const CodeEventHandler&);
1097   CodeEventHandler& operator=(const CodeEventHandler&);
1098   void* internal_listener_;
1099 };
1100 
1101 }  // namespace v8
1102 
1103 
1104 #endif  // V8_V8_PROFILER_H_
1105