1357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org// Copyright 2010 the V8 project authors. All rights reserved.
2357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org// Use of this source code is governed by a BSD-style license that can be
3357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org// found in the LICENSE file.
4357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
5357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org#ifndef V8_V8_PROFILER_H_
6357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org#define V8_V8_PROFILER_H_
7357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
8357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org#include "v8.h"
9357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
10357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org/**
11357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org * Profiler support for the V8 JavaScript engine.
12357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org */
13357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.orgnamespace v8 {
14357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
15357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.orgclass HeapGraphNode;
16357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.orgstruct HeapStatsUpdate;
17357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
18357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.orgtypedef uint32_t SnapshotObjectId;
19357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
20357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org/**
21357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org * CpuProfileNode represents a node in a call graph.
22357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org */
23357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.orgclass V8_EXPORT CpuProfileNode {
24357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org public:
25357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns function name (empty string for anonymous functions.) */
26357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  Handle<String> GetFunctionName() const;
27357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
28357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns id of the script where function is located. */
29357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  int GetScriptId() const;
30357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
31357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns resource name for script from where the function originates. */
32357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  Handle<String> GetScriptResourceName() const;
33357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
34357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
35357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Returns the number, 1-based, of the line where the function originates.
36357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * kNoLineNumberInfo if no line number information is available.
37357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
38357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  int GetLineNumber() const;
39357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
40357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
41357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Returns 1-based number of the column where the function originates.
42357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * kNoColumnNumberInfo if no column number information is available.
43357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
44357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  int GetColumnNumber() const;
45357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
46357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns bailout reason for the function
47357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org    * if the optimization was disabled for it.
48357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org    */
49357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  const char* GetBailoutReason() const;
50357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
51357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
52357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org    * Returns the count of samples where the function was currently executing.
53357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org    */
54357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  unsigned GetHitCount() const;
55357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
56357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns function entry UID. */
57357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  unsigned GetCallUid() const;
58357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
59357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns id of the node. The id is unique within the tree */
60357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  unsigned GetNodeId() const;
61357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
62357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns child nodes count of the node. */
63357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  int GetChildrenCount() const;
64357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
65357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Retrieves a child node by index. */
66357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  const CpuProfileNode* GetChild(int index) const;
67357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
68357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
69357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
70357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org};
71357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
72357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
73357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org/**
74357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org * CpuProfile contains a CPU profile in a form of top-down call tree
75357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org * (from main() down to functions that do all the work).
76357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org */
77357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.orgclass V8_EXPORT CpuProfile {
78357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org public:
79357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns CPU profile title. */
80357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  Handle<String> GetTitle() const;
81357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
82357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns the root node of the top down call tree. */
83357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  const CpuProfileNode* GetTopDownRoot() const;
84357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
85357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
86357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Returns number of samples recorded. The samples are not recorded unless
87357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * |record_samples| parameter of CpuProfiler::StartCpuProfiling is true.
88357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
89357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  int GetSamplesCount() const;
90357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
91357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
92357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Returns profile node corresponding to the top frame the sample at
93357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * the given index.
94357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
95357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  const CpuProfileNode* GetSample(int index) const;
96357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
97357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
98357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Returns the timestamp of the sample. The timestamp is the number of
99357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * microseconds since some unspecified starting point.
100357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * The point is equal to the starting point used by GetStartTime.
101357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
102357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  int64_t GetSampleTimestamp(int index) const;
103357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
104357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
105357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Returns time when the profile recording was started (in microseconds)
106357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * since some unspecified starting point.
107357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
108357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  int64_t GetStartTime() const;
109357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
110357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
111357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Returns time when the profile recording was stopped (in microseconds)
112720dc0bc17114e33b9b2177fcb6726bda9cabd62sgjesse@chromium.org   * since some unspecified starting point.
113357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * The point is equal to the starting point used by GetStartTime.
114357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
115357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  int64_t GetEndTime() const;
116357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
117357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
118357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Deletes the profile and removes it from CpuProfiler's list.
119357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * All pointers to nodes previously returned become invalid.
120357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
121357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  void Delete();
122357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org};
123357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
124357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
125357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org/**
126357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org * Interface for controlling CPU profiling. Instance of the
127357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org * profiler can be retrieved using v8::Isolate::GetCpuProfiler.
128357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org */
129357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.orgclass V8_EXPORT CpuProfiler {
130357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org public:
131357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
132357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Changes default CPU profiler sampling interval to the specified number
133357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * of microseconds. Default interval is 1000us. This method must be called
134357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * when there are no profiles being recorded.
135357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
136357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  void SetSamplingInterval(int us);
137357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
138357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
139357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Starts collecting CPU profile. Title may be an empty string. It
140357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * is allowed to have several profiles being collected at
141357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * once. Attempts to start collecting several profiles with the same
142357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * title are silently ignored. While collecting a profile, functions
1439dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   * from all security contexts are included in it. The token-based
1449dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   * filtering is only performed when querying for a profile.
1459dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   *
1469dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   * |record_samples| parameter controls whether individual samples should
1479dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   * be recorded in addition to the aggregated tree.
1489dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   */
1499dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com  void StartProfiling(Handle<String> title, bool record_samples = false);
1509dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com
1519dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com  /** Deprecated. Use StartProfiling instead. */
152357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  V8_DEPRECATED("Use StartProfiling",
153357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org      void StartCpuProfiling(Handle<String> title,
154357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org                             bool record_samples = false));
155357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
156357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
157357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Stops collecting CPU profile with a given title and returns it.
1589dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   * If the title given is empty, finishes the last profile started.
1599dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   */
1609dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com  CpuProfile* StopProfiling(Handle<String> title);
161357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
162357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Deprecated. Use StopProfiling instead. */
1639dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com  V8_DEPRECATED("Use StopProfiling",
1649dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com      const CpuProfile* StopCpuProfiling(Handle<String> title));
1659dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com
166357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
167357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * Tells the profiler whether the embedder is idle.
168357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
169357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  void SetIdle(bool is_idle);
170357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
1719dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com private:
1729dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com  CpuProfiler();
1739dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com  ~CpuProfiler();
174357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  CpuProfiler(const CpuProfiler&);
175357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  CpuProfiler& operator=(const CpuProfiler&);
176357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org};
177357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
178357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
179357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org/**
180357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org * HeapSnapshotEdge represents a directed connection between heap
1819dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com * graph nodes: from retainers to retained nodes.
1829dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com */
1839dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.comclass V8_EXPORT HeapGraphEdge {
184357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org public:
185357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  enum Type {
186357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org    kContextVariable = 0,  // A variable from a function context.
1872c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kElement = 1,          // An element of an array.
1882c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kProperty = 2,         // A named object property.
1892c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kInternal = 3,         // A link that can't be accessed from JS,
1902c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org                           // thus, its name isn't a real property name
1912c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org                           // (e.g. parts of a ConsString).
1922c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kHidden = 4,           // A link that is needed for proper sizes
1932c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org                           // calculation, but may be hidden from user.
1942c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kShortcut = 5,         // A link that must not be followed during
1952c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org                           // sizes calculation.
1962c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kWeak = 6              // A weak reference (ignored by the GC).
19726c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org  };
19826c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org
19926c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org  /** Returns edge type (see HeapGraphEdge::Type). */
20026c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org  Type GetType() const;
2015ad5acef6bd4ebc785f946d8bcc2a88b1e031827ricow@chromium.org
2022c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /**
2032c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * Returns edge name. This can be a variable name, an element index, or
2042c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * a property name.
2052c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   */
2062c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  Handle<Value> GetName() const;
2072c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2082c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns origin node. */
2092c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  const HeapGraphNode* GetFromNode() const;
2102c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2112c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns destination node. */
2122c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  const HeapGraphNode* GetToNode() const;
2132c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org};
2142c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2152c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2162c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org/**
2172c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org * HeapGraphNode represents a node in a heap graph.
2182c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org */
2192c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.orgclass V8_EXPORT HeapGraphNode {
2202c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org public:
2212c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  enum Type {
2222c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kHidden = 0,         // Hidden node, may be filtered when shown to user.
2232c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kArray = 1,          // An array of elements.
2242c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kString = 2,         // A string.
2252c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kObject = 3,         // A JS object (except for arrays and strings).
2262c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kCode = 4,           // Compiled code.
2272c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kClosure = 5,        // Function closure.
2282c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kRegExp = 6,         // RegExp.
2292c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kHeapNumber = 7,     // Number stored in the heap.
2302c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kNative = 8,         // Native object (not from V8 heap).
2312c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kSynthetic = 9,      // Synthetic object, usualy used for grouping
2322c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org                         // snapshot items together.
2332c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kConsString = 10,    // Concatenated string. A pair of pointers to strings.
2342c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kSlicedString = 11,  // Sliced string. A fragment of another string.
2352c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kSymbol = 12         // A Symbol (ES6).
2362c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  };
2372c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2382c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns node type (see HeapGraphNode::Type). */
2392c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  Type GetType() const;
2402c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2412c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /**
2422c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * Returns node name. Depending on node's type this can be the name
24326c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org   * of the constructor (for objects), the name of the function (for
24426c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org   * closures), string value, or an empty string (for compiled code).
24526c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org   */
24626c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org  Handle<String> GetName() const;
24726c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org
24826c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org  /**
2492c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * Returns node id. For the same heap object, the id remains the same
2502c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * across all snapshots.
2512c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   */
2522c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  SnapshotObjectId GetId() const;
2532c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2542c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns node's own size, in bytes. */
2552c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  V8_DEPRECATED("Use GetShallowSize instead",
2562c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org                int GetSelfSize() const);
2572c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2582c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns node's own size, in bytes. */
2592c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  size_t GetShallowSize() const;
2602c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2614980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  /** Returns child nodes count of the node. */
2624980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  int GetChildrenCount() const;
263145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com
264145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com  /** Retrieves a child by index. */
2654980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  const HeapGraphEdge* GetChild(int index) const;
2664980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org};
2674980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org
268145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com
269145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com/**
270145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com * An interface for exporting data from V8, using "push" model.
271145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com */
272145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.comclass V8_EXPORT OutputStream {  // NOLINT
273145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com public:
2742c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  enum WriteResult {
2752c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kContinue = 0,
2762c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kAbort = 1
2772c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  };
27826c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org  virtual ~OutputStream() {}
2792c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Notify about the end of stream. */
2802c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  virtual void EndOfStream() = 0;
28126c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org  /** Get preferred output chunk size. Called only once. */
28226c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org  virtual int GetChunkSize() { return 1024; }
28326c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org  /**
28426c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org   * Writes the next chunk of snapshot data into the stream. Writing
2852c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * can be stopped by returning kAbort as function result. EndOfStream
28626c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org   * will not be called in case writing was aborted.
2872c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   */
2882c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
2892c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /**
2902c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * Writes the next chunk of heap stats data into the stream. Writing
2912c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * can be stopped by returning kAbort as function result. EndOfStream
2922c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * will not be called in case writing was aborted.
2932c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   */
2942c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
2952c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    return kAbort;
2962c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  }
2972c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org};
2982c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2992c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
3002c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org/**
3012c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org * HeapSnapshots record the state of the JS heap at some moment.
3022c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org */
3032c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.orgclass V8_EXPORT HeapSnapshot {
3042c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org public:
3052c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  enum SerializationFormat {
3062c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kJSON = 0  // See format description near 'Serialize' method.
3072c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  };
3084980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org
3094980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  /** Returns heap snapshot UID (assigned by the profiler.) */
3104980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  unsigned GetUid() const;
3114980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org
3124980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  /** Returns heap snapshot title. */
3134980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  Handle<String> GetTitle() const;
3144980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org
3154980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  /** Returns the root node of the heap graph. */
3164980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  const HeapGraphNode* GetRoot() const;
3174980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org
3182c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns a node by its id. */
3192c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
3202c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
3212c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns total nodes count in the snapshot. */
3222c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  int GetNodesCount() const;
323145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com
324145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com  /** Returns a node by index. */
325145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com  const HeapGraphNode* GetNode(int index) const;
326d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com
327d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com  /** Returns a max seen JS object Id. */
328d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
329d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com
330145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com  /**
331145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * Deletes the snapshot and removes it from HeapProfiler's list.
332145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * All pointers to nodes, edges and paths previously returned become
333145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * invalid.
334145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   */
3352c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  void Delete();
3362c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
3372c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /**
3382c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * Prepare a serialized representation of the snapshot. The result
3392c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * is written into the stream provided in chunks of specified size.
3402c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * The total length of the serialized snapshot is unknown in
3412c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * advance, it can be roughly equal to JS heap size (that means,
3424980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org   * it can be really big - tens of megabytes).
3434980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org   *
344145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * For the JSON format, heap contents are represented as an object
345145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * with the following structure:
346145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   *
347145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   *  {
3484980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org   *    snapshot: {
349d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *      title: "...",
350d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *      uid: nnn,
351d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *      meta: { meta-info },
352d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *      node_count: nnn,
353d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *      edge_count: nnn
354d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *    },
355d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *    nodes: [nodes array],
356d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *    edges: [edges array],
357d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *    strings: [strings array]
358d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *  }
359d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *
360d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   * Nodes reference strings, other nodes, and edges by their indexes
361d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   * in corresponding arrays.
362d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   */
363d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com  void Serialize(OutputStream* stream, SerializationFormat format) const;
364d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com};
365d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com
366d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com
367d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com/**
368d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com * An interface for reporting progress and controlling long-running
369d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com * activities.
370d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com */
371d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.comclass V8_EXPORT ActivityControl {  // NOLINT
372d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com public:
3732c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  enum ControlOption {
3742c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kContinue = 0,
3752c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org    kAbort = 1
3762c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  };
3772c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  virtual ~ActivityControl() {}
3782c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /**
3792c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * Notify about current progress. The activity can be stopped by
3802c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * returning kAbort as the callback result.
3812c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   */
3822c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  virtual ControlOption ReportProgressValue(int done, int total) = 0;
3832c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org};
3842c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
3852c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
3862c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org/**
3872c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org * Interface for controlling heap profiling. Instance of the
3882c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org * profiler can be retrieved using v8::Isolate::GetHeapProfiler.
3892c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org */
390145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.comclass V8_EXPORT HeapProfiler {
391145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com public:
392145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com  /**
393145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * Callback function invoked for obtaining RetainedObjectInfo for
394145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * the given JavaScript wrapper object. It is prohibited to enter V8
395145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * while the callback is running: only getters on the handle and
396145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * GetPointerFromInternalField on the objects are allowed.
3972c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   */
3982c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  typedef RetainedObjectInfo* (*WrapperInfoCallback)
3992c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org      (uint16_t class_id, Handle<Value> wrapper);
400357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
401357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns the number of snapshots taken. */
402357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  int GetSnapshotCount();
403357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
404357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns a snapshot by index. */
405357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  const HeapSnapshot* GetHeapSnapshot(int index);
406357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
407  /**
408   * Returns SnapshotObjectId for a heap object referenced by |value| if
409   * it has been seen by the heap profiler, kUnknownObjectId otherwise.
410   */
411  SnapshotObjectId GetObjectId(Handle<Value> value);
412
413  /**
414   * Returns heap object with given SnapshotObjectId if the object is alive,
415   * otherwise empty handle is returned.
416   */
417  Handle<Value> FindObjectById(SnapshotObjectId id);
418
419  /**
420   * Clears internal map from SnapshotObjectId to heap object. The new objects
421   * will not be added into it unless a heap snapshot is taken or heap object
422   * tracking is kicked off.
423   */
424  void ClearObjectIds();
425
426  /**
427   * A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
428   * it in case heap profiler cannot find id  for the object passed as
429   * parameter. HeapSnapshot::GetNodeById will always return NULL for such id.
430   */
431  static const SnapshotObjectId kUnknownObjectId = 0;
432
433  /**
434   * Callback interface for retrieving user friendly names of global objects.
435   */
436  class ObjectNameResolver {
437   public:
438    /**
439     * Returns name to be used in the heap snapshot for given node. Returned
440     * string must stay alive until snapshot collection is completed.
441     */
442    virtual const char* GetName(Handle<Object> object) = 0;
443   protected:
444    virtual ~ObjectNameResolver() {}
445  };
446
447  /**
448   * Takes a heap snapshot and returns it. Title may be an empty string.
449   */
450  const HeapSnapshot* TakeHeapSnapshot(
451      Handle<String> title,
452      ActivityControl* control = NULL,
453      ObjectNameResolver* global_object_name_resolver = NULL);
454
455  /**
456   * Starts tracking of heap objects population statistics. After calling
457   * this method, all heap objects relocations done by the garbage collector
458   * are being registered.
459   *
460   * |track_allocations| parameter controls whether stack trace of each
461   * allocation in the heap will be recorded and reported as part of
462   * HeapSnapshot.
463   */
464  void StartTrackingHeapObjects(bool track_allocations = false);
465
466  /**
467   * Adds a new time interval entry to the aggregated statistics array. The
468   * time interval entry contains information on the current heap objects
469   * population size. The method also updates aggregated statistics and
470   * reports updates for all previous time intervals via the OutputStream
471   * object. Updates on each time interval are provided as a stream of the
472   * HeapStatsUpdate structure instances.
473   * The return value of the function is the last seen heap object Id.
474   *
475   * StartTrackingHeapObjects must be called before the first call to this
476   * method.
477   */
478  SnapshotObjectId GetHeapStats(OutputStream* stream);
479
480  /**
481   * Stops tracking of heap objects population statistics, cleans up all
482   * collected data. StartHeapObjectsTracking must be called again prior to
483   * calling PushHeapObjectsStats next time.
484   */
485  void StopTrackingHeapObjects();
486
487  /**
488   * Deletes all snapshots taken. All previously returned pointers to
489   * snapshots and their contents become invalid after this call.
490   */
491  void DeleteAllHeapSnapshots();
492
493  /** Binds a callback to embedder's class ID. */
494  void SetWrapperClassInfoProvider(
495      uint16_t class_id,
496      WrapperInfoCallback callback);
497
498  /**
499   * Default value of persistent handle class ID. Must not be used to
500   * define a class. Can be used to reset a class of a persistent
501   * handle.
502   */
503  static const uint16_t kPersistentHandleNoClassId = 0;
504
505  /** Returns memory used for profiler internal data and snapshots. */
506  size_t GetProfilerMemorySize();
507
508  /**
509   * Sets a RetainedObjectInfo for an object group (see V8::SetObjectGroupId).
510   */
511  void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo* info);
512
513 private:
514  HeapProfiler();
515  ~HeapProfiler();
516  HeapProfiler(const HeapProfiler&);
517  HeapProfiler& operator=(const HeapProfiler&);
518};
519
520
521/**
522 * Interface for providing information about embedder's objects
523 * held by global handles. This information is reported in two ways:
524 *
525 *  1. When calling AddObjectGroup, an embedder may pass
526 *     RetainedObjectInfo instance describing the group.  To collect
527 *     this information while taking a heap snapshot, V8 calls GC
528 *     prologue and epilogue callbacks.
529 *
530 *  2. When a heap snapshot is collected, V8 additionally
531 *     requests RetainedObjectInfos for persistent handles that
532 *     were not previously reported via AddObjectGroup.
533 *
534 * Thus, if an embedder wants to provide information about native
535 * objects for heap snapshots, he can do it in a GC prologue
536 * handler, and / or by assigning wrapper class ids in the following way:
537 *
538 *  1. Bind a callback to class id by calling SetWrapperClassInfoProvider.
539 *  2. Call SetWrapperClassId on certain persistent handles.
540 *
541 * V8 takes ownership of RetainedObjectInfo instances passed to it and
542 * keeps them alive only during snapshot collection. Afterwards, they
543 * are freed by calling the Dispose class function.
544 */
545class V8_EXPORT RetainedObjectInfo {  // NOLINT
546 public:
547  /** Called by V8 when it no longer needs an instance. */
548  virtual void Dispose() = 0;
549
550  /** Returns whether two instances are equivalent. */
551  virtual bool IsEquivalent(RetainedObjectInfo* other) = 0;
552
553  /**
554   * Returns hash value for the instance. Equivalent instances
555   * must have the same hash value.
556   */
557  virtual intptr_t GetHash() = 0;
558
559  /**
560   * Returns human-readable label. It must be a null-terminated UTF-8
561   * encoded string. V8 copies its contents during a call to GetLabel.
562   */
563  virtual const char* GetLabel() = 0;
564
565  /**
566   * Returns human-readable group label. It must be a null-terminated UTF-8
567   * encoded string. V8 copies its contents during a call to GetGroupLabel.
568   * Heap snapshot generator will collect all the group names, create
569   * top level entries with these names and attach the objects to the
570   * corresponding top level group objects. There is a default
571   * implementation which is required because embedders don't have their
572   * own implementation yet.
573   */
574  virtual const char* GetGroupLabel() { return GetLabel(); }
575
576  /**
577   * Returns element count in case if a global handle retains
578   * a subgraph by holding one of its nodes.
579   */
580  virtual intptr_t GetElementCount() { return -1; }
581
582  /** Returns embedder's object size in bytes. */
583  virtual intptr_t GetSizeInBytes() { return -1; }
584
585 protected:
586  RetainedObjectInfo() {}
587  virtual ~RetainedObjectInfo() {}
588
589 private:
590  RetainedObjectInfo(const RetainedObjectInfo&);
591  RetainedObjectInfo& operator=(const RetainedObjectInfo&);
592};
593
594
595/**
596 * A struct for exporting HeapStats data from V8, using "push" model.
597 * See HeapProfiler::GetHeapStats.
598 */
599struct HeapStatsUpdate {
600  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
601    : index(index), count(count), size(size) { }
602  uint32_t index;  // Index of the time interval that was changed.
603  uint32_t count;  // New value of count field for the interval with this index.
604  uint32_t size;  // New value of size field for the interval with this index.
605};
606
607
608}  // namespace v8
609
610
611#endif  // V8_V8_PROFILER_H_
612