1357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org// Copyright 2010 the V8 project authors. All rights reserved.
23484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org// Use of this source code is governed by a BSD-style license that can be
33484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@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
1500a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.orgclass HeapGraphNode;
1600a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.orgstruct HeapStatsUpdate;
1700a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org
181456e708d277e725ca42a03463af16fe471c9210jkummerow@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 */
23594006017e46d82ed7146611dc12c20e3c509c7ddanno@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
2893a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org  /** Returns id of the script where function is located. */
2993a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org  int GetScriptId() const;
3093a47f4837f2137c8d8349250fd8e91da3108126jkummerow@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
40dd6d9eedcac6e3b5adfb7702649ac32def9c3585mvstanton@chromium.org  /**
41dd6d9eedcac6e3b5adfb7702649ac32def9c3585mvstanton@chromium.org   * Returns 1-based number of the column where the function originates.
42dd6d9eedcac6e3b5adfb7702649ac32def9c3585mvstanton@chromium.org   * kNoColumnNumberInfo if no column number information is available.
43dd6d9eedcac6e3b5adfb7702649ac32def9c3585mvstanton@chromium.org   */
44dd6d9eedcac6e3b5adfb7702649ac32def9c3585mvstanton@chromium.org  int GetColumnNumber() const;
45dd6d9eedcac6e3b5adfb7702649ac32def9c3585mvstanton@chromium.org
462c9426bdda5e95459527292063d885c98180cb0fjkummerow@chromium.org  /** Returns bailout reason for the function
472c9426bdda5e95459527292063d885c98180cb0fjkummerow@chromium.org    * if the optimization was disabled for it.
482c9426bdda5e95459527292063d885c98180cb0fjkummerow@chromium.org    */
492c9426bdda5e95459527292063d885c98180cb0fjkummerow@chromium.org  const char* GetBailoutReason() const;
502c9426bdda5e95459527292063d885c98180cb0fjkummerow@chromium.org
51594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.org  /**
52594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.org    * Returns the count of samples where the function was currently executing.
53594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.org    */
54594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.org  unsigned GetHitCount() const;
55594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.org
56357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /** Returns function entry UID. */
57357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  unsigned GetCallUid() const;
58357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
59f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  /** Returns id of the node. The id is unique within the tree */
60f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  unsigned GetNodeId() const;
61f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@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
68720dc0bc17114e33b9b2177fcb6726bda9cabd62sgjesse@chromium.org  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
69dd6d9eedcac6e3b5adfb7702649ac32def9c3585mvstanton@chromium.org  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
70357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org};
71357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
72357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
73357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org/**
74876cca833d7212e476250d102cad185cdcfa9dfesvenpanne@chromium.org * CpuProfile contains a CPU profile in a form of top-down call tree
75876cca833d7212e476250d102cad185cdcfa9dfesvenpanne@chromium.org * (from main() down to functions that do all the work).
76357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org */
77594006017e46d82ed7146611dc12c20e3c509c7ddanno@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;
84ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org
85ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org  /**
86865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * Returns number of samples recorded. The samples are not recorded unless
87865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * |record_samples| parameter of CpuProfiler::StartCpuProfiling is true.
88865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   */
89f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  int GetSamplesCount() const;
90f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org
91f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  /**
92865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * Returns profile node corresponding to the top frame the sample at
93865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * the given index.
94865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   */
95f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  const CpuProfileNode* GetSample(int index) const;
96f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org
97f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  /**
98865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * Returns the timestamp of the sample. The timestamp is the number of
99865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * microseconds since some unspecified starting point.
100865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * The point is equal to the starting point used by GetStartTime.
101865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   */
102865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org  int64_t GetSampleTimestamp(int index) const;
103865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org
104865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org  /**
105865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * Returns time when the profile recording was started (in microseconds)
106865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * since some unspecified starting point.
107865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   */
10889acc0bef1acf051e807b057dbcbcd478a8ad301danno@chromium.org  int64_t GetStartTime() const;
10989acc0bef1acf051e807b057dbcbcd478a8ad301danno@chromium.org
11089acc0bef1acf051e807b057dbcbcd478a8ad301danno@chromium.org  /**
111865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * Returns time when the profile recording was stopped (in microseconds)
112865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * since some unspecified starting point.
113865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   * The point is equal to the starting point used by GetStartTime.
114865f51ff8c94f86f4c97636d70addc0f29e79674machenbach@chromium.org   */
11589acc0bef1acf051e807b057dbcbcd478a8ad301danno@chromium.org  int64_t GetEndTime() const;
11689acc0bef1acf051e807b057dbcbcd478a8ad301danno@chromium.org
11789acc0bef1acf051e807b057dbcbcd478a8ad301danno@chromium.org  /**
118ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   * Deletes the profile and removes it from CpuProfiler's list.
119ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   * All pointers to nodes previously returned become invalid.
120ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   */
121ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org  void Delete();
122357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org};
123357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
124357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
125357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org/**
126f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org * Interface for controlling CPU profiling. Instance of the
127f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org * profiler can be retrieved using v8::Isolate::GetCpuProfiler.
128357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org */
129594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.orgclass V8_EXPORT CpuProfiler {
130357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org public:
131357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org  /**
1323d00d0a753cf5e5091f883517e6612ece769f999jkummerow@chromium.org   * Changes default CPU profiler sampling interval to the specified number
1333d00d0a753cf5e5091f883517e6612ece769f999jkummerow@chromium.org   * of microseconds. Default interval is 1000us. This method must be called
1343d00d0a753cf5e5091f883517e6612ece769f999jkummerow@chromium.org   * when there are no profiles being recorded.
1359dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   */
1363d00d0a753cf5e5091f883517e6612ece769f999jkummerow@chromium.org  void SetSamplingInterval(int us);
1379dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com
1389dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com  /**
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
1429dfbea4c7d423c7bc1db94425cb78e7f7cf41f78erik.corry@gmail.com   * 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.
145f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org   *
146f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org   * |record_samples| parameter controls whether individual samples should
147f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org   * be recorded in addition to the aggregated tree.
148357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
1496b6df382019a622ba20133e47bbe2e6f323b013bdslomov@chromium.org  void StartProfiling(Handle<String> title, bool record_samples = false);
1506b6df382019a622ba20133e47bbe2e6f323b013bdslomov@chromium.org
1516b6df382019a622ba20133e47bbe2e6f323b013bdslomov@chromium.org  /** Deprecated. Use StartProfiling instead. */
1522f599e5925b02d78bd78703b44741d6b27e53a44machenbach@chromium.org  V8_DEPRECATED("Use StartProfiling",
1532f599e5925b02d78bd78703b44741d6b27e53a44machenbach@chromium.org      void StartCpuProfiling(Handle<String> title,
1542f599e5925b02d78bd78703b44741d6b27e53a44machenbach@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.
158357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   * If the title given is empty, finishes the last profile started.
159357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org   */
1606b6df382019a622ba20133e47bbe2e6f323b013bdslomov@chromium.org  CpuProfile* StopProfiling(Handle<String> title);
1616b6df382019a622ba20133e47bbe2e6f323b013bdslomov@chromium.org
1626b6df382019a622ba20133e47bbe2e6f323b013bdslomov@chromium.org  /** Deprecated. Use StopProfiling instead. */
1632f599e5925b02d78bd78703b44741d6b27e53a44machenbach@chromium.org  V8_DEPRECATED("Use StopProfiling",
1642f599e5925b02d78bd78703b44741d6b27e53a44machenbach@chromium.org      const CpuProfile* StopCpuProfiling(Handle<String> title));
165ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org
166ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org  /**
167594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.org   * Tells the profiler whether the embedder is idle.
168594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.org   */
169594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.org  void SetIdle(bool is_idle);
170594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.org
171f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org private:
172f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  CpuProfiler();
173f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  ~CpuProfiler();
174f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  CpuProfiler(const CpuProfiler&);
175f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  CpuProfiler& operator=(const CpuProfiler&);
176357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org};
177357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
178357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
1792c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org/**
1802c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org * HeapSnapshotEdge represents a directed connection between heap
1817304bcac06a6a63b9f3dcebac2eeceada87ca146vegorov@chromium.org * graph nodes: from retainers to retained nodes.
1822c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org */
183594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.orgclass V8_EXPORT HeapGraphEdge {
1842c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org public:
1852c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  enum Type {
18626c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org    kContextVariable = 0,  // A variable from a function context.
18726c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org    kElement = 1,          // An element of an array.
18826c16f8ef35ec25d36420512a4ceaa74ea2e2b05vegorov@chromium.org    kProperty = 2,         // A named object property.
18921b5e95db1c650dfc2ba8e11d010bb01293f85c5vegorov@chromium.org    kInternal = 3,         // A link that can't be accessed from JS,
19021b5e95db1c650dfc2ba8e11d010bb01293f85c5vegorov@chromium.org                           // thus, its name isn't a real property name
19121b5e95db1c650dfc2ba8e11d010bb01293f85c5vegorov@chromium.org                           // (e.g. parts of a ConsString).
19221b5e95db1c650dfc2ba8e11d010bb01293f85c5vegorov@chromium.org    kHidden = 4,           // A link that is needed for proper sizes
19321b5e95db1c650dfc2ba8e11d010bb01293f85c5vegorov@chromium.org                           // calculation, but may be hidden from user.
19464e3a4be4a99f31920128de34573c8ac9038de42ricow@chromium.org    kShortcut = 5,         // A link that must not be followed during
19521b5e95db1c650dfc2ba8e11d010bb01293f85c5vegorov@chromium.org                           // sizes calculation.
19664e3a4be4a99f31920128de34573c8ac9038de42ricow@chromium.org    kWeak = 6              // A weak reference (ignored by the GC).
1972c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  };
1982c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
1992c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns edge type (see HeapGraphEdge::Type). */
2002c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  Type GetType() const;
2012c186ca6690a1cb19ec7584e71f167234587c87cwhesse@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 */
219594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.orgclass V8_EXPORT HeapGraphNode {
2202c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org public:
2212c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  enum Type {
222fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kHidden = 0,         // Hidden node, may be filtered when shown to user.
223fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kArray = 1,          // An array of elements.
224fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kString = 2,         // A string.
225fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kObject = 3,         // A JS object (except for arrays and strings).
226fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kCode = 4,           // Compiled code.
227fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kClosure = 5,        // Function closure.
228fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kRegExp = 6,         // RegExp.
229fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kHeapNumber = 7,     // Number stored in the heap.
230fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kNative = 8,         // Native object (not from V8 heap).
231fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kSynthetic = 9,      // Synthetic object, usualy used for grouping
232fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org                         // snapshot items together.
233fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kConsString = 10,    // Concatenated string. A pair of pointers to strings.
234fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@chromium.org    kSlicedString = 11,  // Sliced string. A fragment of another string.
235fb547e07aef43e02715c5d6c1530e84bb3cbba02machenbach@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
2432c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * of the constructor (for objects), the name of the function (for
2442c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   * closures), string value, or an empty string (for compiled code).
2452c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org   */
2462c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  Handle<String> GetName() const;
2472c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2484980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  /**
2494980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org   * Returns node id. For the same heap object, the id remains the same
25004921a8093ce8bbec34084bd742b7aa3d299be15ager@chromium.org   * across all snapshots.
2514980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org   */
2521456e708d277e725ca42a03463af16fe471c9210jkummerow@chromium.org  SnapshotObjectId GetId() const;
2534980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org
2542c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns node's own size, in bytes. */
255f5a24546072ecdbbd6372c85c42157e01e913561titzer@chromium.org  V8_DEPRECATED("Use GetShallowSize instead",
256f5a24546072ecdbbd6372c85c42157e01e913561titzer@chromium.org                int GetSelfSize() const);
257f5a24546072ecdbbd6372c85c42157e01e913561titzer@chromium.org
258f5a24546072ecdbbd6372c85c42157e01e913561titzer@chromium.org  /** Returns node's own size, in bytes. */
259f5a24546072ecdbbd6372c85c42157e01e913561titzer@chromium.org  size_t GetShallowSize() const;
2602c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2612c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns child nodes count of the node. */
2622c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  int GetChildrenCount() const;
2632c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2642c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Retrieves a child by index. */
2652c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  const HeapGraphEdge* GetChild(int index) const;
2662c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org};
2672c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2682c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
2692c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org/**
27000a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org * An interface for exporting data from V8, using "push" model.
27100a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org */
27200a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.orgclass V8_EXPORT OutputStream {  // NOLINT
27300a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org public:
27400a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  enum WriteResult {
27500a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org    kContinue = 0,
27600a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org    kAbort = 1
27700a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  };
27800a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  virtual ~OutputStream() {}
27900a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  /** Notify about the end of stream. */
28000a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  virtual void EndOfStream() = 0;
28100a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  /** Get preferred output chunk size. Called only once. */
28200a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  virtual int GetChunkSize() { return 1024; }
28300a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  /**
28400a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   * Writes the next chunk of snapshot data into the stream. Writing
28500a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   * can be stopped by returning kAbort as function result. EndOfStream
28600a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   * will not be called in case writing was aborted.
28700a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   */
28800a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
28900a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  /**
29000a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   * Writes the next chunk of heap stats data into the stream. Writing
29100a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   * can be stopped by returning kAbort as function result. EndOfStream
29200a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   * will not be called in case writing was aborted.
29300a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   */
29400a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
29500a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org    return kAbort;
2963c3c8d733702cb2b41471efa5eead1faf5b5711bmachenbach@chromium.org  }
29700a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org};
29800a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org
29900a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org
30000a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org/**
3012c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org * HeapSnapshots record the state of the JS heap at some moment.
3022c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org */
303594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.orgclass V8_EXPORT HeapSnapshot {
3042c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org public:
305d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com  enum SerializationFormat {
306d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com    kJSON = 0  // See format description near 'Serialize' method.
307145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com  };
308145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com
3092c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns heap snapshot UID (assigned by the profiler.) */
3102c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  unsigned GetUid() const;
3112c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
3122c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns heap snapshot title. */
3132c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  Handle<String> GetTitle() const;
3142c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
3152c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org  /** Returns the root node of the heap graph. */
3164980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org  const HeapGraphNode* GetRoot() const;
3174980dff4208f9b77bc5320af43d7cc4b2a3d9688ricow@chromium.org
318a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org  /** Returns a node by its id. */
3191456e708d277e725ca42a03463af16fe471c9210jkummerow@chromium.org  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
320a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org
321ac2828d8d201b0631783404187688fbb786458a3lrn@chromium.org  /** Returns total nodes count in the snapshot. */
322ac2828d8d201b0631783404187688fbb786458a3lrn@chromium.org  int GetNodesCount() const;
323ac2828d8d201b0631783404187688fbb786458a3lrn@chromium.org
324ac2828d8d201b0631783404187688fbb786458a3lrn@chromium.org  /** Returns a node by index. */
325ac2828d8d201b0631783404187688fbb786458a3lrn@chromium.org  const HeapGraphNode* GetNode(int index) const;
326ac2828d8d201b0631783404187688fbb786458a3lrn@chromium.org
3271456e708d277e725ca42a03463af16fe471c9210jkummerow@chromium.org  /** Returns a max seen JS object Id. */
3281456e708d277e725ca42a03463af16fe471c9210jkummerow@chromium.org  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
3291456e708d277e725ca42a03463af16fe471c9210jkummerow@chromium.org
330145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com  /**
331ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   * Deletes the snapshot and removes it from HeapProfiler's list.
332ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   * All pointers to nodes, edges and paths previously returned become
333ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   * invalid.
334ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   */
335ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org  void Delete();
336ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org
337ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org  /**
338d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   * Prepare a serialized representation of the snapshot. The result
339d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   * is written into the stream provided in chunks of specified size.
340d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   * The total length of the serialized snapshot is unknown in
3417304bcac06a6a63b9f3dcebac2eeceada87ca146vegorov@chromium.org   * advance, it can be roughly equal to JS heap size (that means,
342d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   * it can be really big - tens of megabytes).
343d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *
344d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   * For the JSON format, heap contents are represented as an object
345d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   * with the following structure:
346d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *
347d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *  {
348ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *    snapshot: {
349ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *      title: "...",
350ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *      uid: nnn,
351ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *      meta: { meta-info },
352ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *      node_count: nnn,
353ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *      edge_count: nnn
354ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *    },
355ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *    nodes: [nodes array],
356ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *    edges: [edges array],
357ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   *    strings: [strings array]
358d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *  }
359d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   *
360ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   * Nodes reference strings, other nodes, and edges by their indexes
361ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   * in corresponding arrays.
362d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com   */
363d88afa260e45de10e729b05a20146184a488aff7erik.corry@gmail.com  void Serialize(OutputStream* stream, SerializationFormat format) const;
3642c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org};
3652c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
3662c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
36700a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org/**
36800a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org * An interface for reporting progress and controlling long-running
36900a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org * activities.
37000a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org */
37100a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.orgclass V8_EXPORT ActivityControl {  // NOLINT
37200a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org public:
37300a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  enum ControlOption {
37400a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org    kContinue = 0,
37500a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org    kAbort = 1
37600a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  };
37700a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  virtual ~ActivityControl() {}
37800a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  /**
37900a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   * Notify about current progress. The activity can be stopped by
38000a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   * returning kAbort as the callback result.
38100a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org   */
38200a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org  virtual ControlOption ReportProgressValue(int done, int total) = 0;
38300a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org};
38400a4ad70bb8883074ba7630edd7140762bcb86a0machenbach@chromium.org
385b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
3862c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org/**
387f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org * Interface for controlling heap profiling. Instance of the
388f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org * profiler can be retrieved using v8::Isolate::GetHeapProfiler.
3892c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org */
390594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.orgclass V8_EXPORT HeapProfiler {
3912c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org public:
392b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  /**
393b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * Callback function invoked for obtaining RetainedObjectInfo for
394b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * the given JavaScript wrapper object. It is prohibited to enter V8
395b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * while the callback is running: only getters on the handle and
396b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * GetPointerFromInternalField on the objects are allowed.
397b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   */
398b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  typedef RetainedObjectInfo* (*WrapperInfoCallback)
399b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org      (uint16_t class_id, Handle<Value> wrapper);
400b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
401f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  /** Returns the number of snapshots taken. */
402f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  int GetSnapshotCount();
4032c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
404f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  /** Returns a snapshot by index. */
405f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  const HeapSnapshot* GetHeapSnapshot(int index);
4062c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
407145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com  /**
408ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   * Returns SnapshotObjectId for a heap object referenced by |value| if
409ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   * it has been seen by the heap profiler, kUnknownObjectId otherwise.
410ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   */
411f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  SnapshotObjectId GetObjectId(Handle<Value> value);
412ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com
413ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com  /**
4144f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org   * Returns heap object with given SnapshotObjectId if the object is alive,
4154f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org   * otherwise empty handle is returned.
4164f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org   */
4174f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  Handle<Value> FindObjectById(SnapshotObjectId id);
4184f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
4194f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  /**
4204f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org   * Clears internal map from SnapshotObjectId to heap object. The new objects
4214f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org   * will not be added into it unless a heap snapshot is taken or heap object
4224f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org   * tracking is kicked off.
4234f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org   */
4244f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  void ClearObjectIds();
4254f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org
4264f99be9ff2091451687891a05d99cc31990de709hpayer@chromium.org  /**
427ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   * A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
428ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   * it in case heap profiler cannot find id  for the object passed as
429ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   * parameter. HeapSnapshot::GetNodeById will always return NULL for such id.
430ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com   */
431ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com  static const SnapshotObjectId kUnknownObjectId = 0;
432ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com
433ed49e965b5cafa35395084dbfb79f4e07930f10ferik.corry@gmail.com  /**
43432280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org   * Callback interface for retrieving user friendly names of global objects.
43532280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org   */
43632280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org  class ObjectNameResolver {
437f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org   public:
43832280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org    /**
43932280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org     * Returns name to be used in the heap snapshot for given node. Returned
44032280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org     * string must stay alive until snapshot collection is completed.
44132280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org     */
44232280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org    virtual const char* GetName(Handle<Object> object) = 0;
443f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org   protected:
44432280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org    virtual ~ObjectNameResolver() {}
44532280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org  };
44632280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org
44732280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org  /**
448145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   * Takes a heap snapshot and returns it. Title may be an empty string.
449145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com   */
450f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  const HeapSnapshot* TakeHeapSnapshot(
451145eff58d4f6ac0dcc53abb556dbf3cac6c3280aerik.corry@gmail.com      Handle<String> title,
45232280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org      ActivityControl* control = NULL,
45332280cf2786219b2d9a668f7f00778fb59ac40b3mstarzinger@chromium.org      ObjectNameResolver* global_object_name_resolver = NULL);
454b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
455ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org  /**
45628faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * Starts tracking of heap objects population statistics. After calling
45728faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * this method, all heap objects relocations done by the garbage collector
45828faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * are being registered.
459ce9c514a4e015930324b2b45326a478a69535388machenbach@chromium.org   *
460ce9c514a4e015930324b2b45326a478a69535388machenbach@chromium.org   * |track_allocations| parameter controls whether stack trace of each
461ce9c514a4e015930324b2b45326a478a69535388machenbach@chromium.org   * allocation in the heap will be recorded and reported as part of
462ce9c514a4e015930324b2b45326a478a69535388machenbach@chromium.org   * HeapSnapshot.
46328faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   */
464ce9c514a4e015930324b2b45326a478a69535388machenbach@chromium.org  void StartTrackingHeapObjects(bool track_allocations = false);
46528faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org
46628faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org  /**
46728faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * Adds a new time interval entry to the aggregated statistics array. The
46828faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * time interval entry contains information on the current heap objects
46928faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * population size. The method also updates aggregated statistics and
47028faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * reports updates for all previous time intervals via the OutputStream
471fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org   * object. Updates on each time interval are provided as a stream of the
472fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org   * HeapStatsUpdate structure instances.
473400388edd471bd4d4a97b21c52c1024cd1cc5708rossberg@chromium.org   * The return value of the function is the last seen heap object Id.
47428faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   *
475f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org   * StartTrackingHeapObjects must be called before the first call to this
47628faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * method.
47728faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   */
478f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  SnapshotObjectId GetHeapStats(OutputStream* stream);
47928faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org
48028faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org  /**
48128faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * Stops tracking of heap objects population statistics, cleans up all
48228faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * collected data. StartHeapObjectsTracking must be called again prior to
48328faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   * calling PushHeapObjectsStats next time.
48428faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org   */
485f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  void StopTrackingHeapObjects();
48628faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org
48728faa982749c4aa9c090939453dea14bb118f613jkummerow@chromium.org  /**
488ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   * Deletes all snapshots taken. All previously returned pointers to
489ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   * snapshots and their contents become invalid after this call.
490ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org   */
491f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  void DeleteAllHeapSnapshots();
492ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org
493f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  /** Binds a callback to embedder's class ID. */
494f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  void SetWrapperClassInfoProvider(
495f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org      uint16_t class_id,
496f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org      WrapperInfoCallback callback);
497b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
498b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  /**
499b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * Default value of persistent handle class ID. Must not be used to
500b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * define a class. Can be used to reset a class of a persistent
501b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * handle.
502b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   */
503b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  static const uint16_t kPersistentHandleNoClassId = 0;
504bbceb57d27ec53f6d6212e690ead3174192ea3f9erik.corry@gmail.com
505f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  /** Returns memory used for profiler internal data and snapshots. */
506f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  size_t GetProfilerMemorySize();
507f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org
508ca29dd85fa02449d17188f5a6ff9a7cdf2ad9680danno@chromium.org  /**
509ca29dd85fa02449d17188f5a6ff9a7cdf2ad9680danno@chromium.org   * Sets a RetainedObjectInfo for an object group (see V8::SetObjectGroupId).
510ca29dd85fa02449d17188f5a6ff9a7cdf2ad9680danno@chromium.org   */
511ca29dd85fa02449d17188f5a6ff9a7cdf2ad9680danno@chromium.org  void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo* info);
512ca29dd85fa02449d17188f5a6ff9a7cdf2ad9680danno@chromium.org
513f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org private:
514f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  HeapProfiler();
515f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  ~HeapProfiler();
516f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  HeapProfiler(const HeapProfiler&);
517f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org  HeapProfiler& operator=(const HeapProfiler&);
518b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org};
519b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
520b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
521b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org/**
522b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org * Interface for providing information about embedder's objects
523b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org * held by global handles. This information is reported in two ways:
524b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *
525b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *  1. When calling AddObjectGroup, an embedder may pass
526b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *     RetainedObjectInfo instance describing the group.  To collect
527b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *     this information while taking a heap snapshot, V8 calls GC
528b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *     prologue and epilogue callbacks.
529b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *
530b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *  2. When a heap snapshot is collected, V8 additionally
531b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *     requests RetainedObjectInfos for persistent handles that
532b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *     were not previously reported via AddObjectGroup.
533b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *
534b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org * Thus, if an embedder wants to provide information about native
535b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org * objects for heap snapshots, he can do it in a GC prologue
536b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org * handler, and / or by assigning wrapper class ids in the following way:
537b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *
5381510d58cbcf57c82a10e7d390bfe21a7ae68ba43mstarzinger@chromium.org *  1. Bind a callback to class id by calling SetWrapperClassInfoProvider.
539b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *  2. Call SetWrapperClassId on certain persistent handles.
540b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org *
541b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org * V8 takes ownership of RetainedObjectInfo instances passed to it and
542b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org * keeps them alive only during snapshot collection. Afterwards, they
543b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org * are freed by calling the Dispose class function.
544b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org */
545594006017e46d82ed7146611dc12c20e3c509c7ddanno@chromium.orgclass V8_EXPORT RetainedObjectInfo {  // NOLINT
546b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org public:
547b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  /** Called by V8 when it no longer needs an instance. */
548b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  virtual void Dispose() = 0;
549b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
550b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  /** Returns whether two instances are equivalent. */
551b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  virtual bool IsEquivalent(RetainedObjectInfo* other) = 0;
552b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
553b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  /**
554b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * Returns hash value for the instance. Equivalent instances
555b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * must have the same hash value.
556b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   */
557b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  virtual intptr_t GetHash() = 0;
558b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
559b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  /**
560fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org   * Returns human-readable label. It must be a null-terminated UTF-8
561b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * encoded string. V8 copies its contents during a call to GetLabel.
562b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   */
563b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  virtual const char* GetLabel() = 0;
564b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
565b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  /**
566fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org   * Returns human-readable group label. It must be a null-terminated UTF-8
567fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org   * encoded string. V8 copies its contents during a call to GetGroupLabel.
568fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org   * Heap snapshot generator will collect all the group names, create
569fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org   * top level entries with these names and attach the objects to the
570fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org   * corresponding top level group objects. There is a default
571fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org   * implementation which is required because embedders don't have their
572fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org   * own implementation yet.
573fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org   */
574fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org  virtual const char* GetGroupLabel() { return GetLabel(); }
575fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org
576fa458e413c3e5b8d479e49258d060b7bb4567c57danno@chromium.org  /**
577b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * Returns element count in case if a global handle retains
578b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   * a subgraph by holding one of its nodes.
579b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org   */
580b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  virtual intptr_t GetElementCount() { return -1; }
581b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
582b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  /** Returns embedder's object size in bytes. */
583b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  virtual intptr_t GetSizeInBytes() { return -1; }
584b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
585b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org protected:
586b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  RetainedObjectInfo() {}
587b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  virtual ~RetainedObjectInfo() {}
588b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org
589b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org private:
590b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  RetainedObjectInfo(const RetainedObjectInfo&);
591b08986cb66c3f6687247cb6da186c1e73057e399whesse@chromium.org  RetainedObjectInfo& operator=(const RetainedObjectInfo&);
5922c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org};
5932c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
5942c186ca6690a1cb19ec7584e71f167234587c87cwhesse@chromium.org
595fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org/**
596fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org * A struct for exporting HeapStats data from V8, using "push" model.
597f705b5034dc5bc422ac1019b591469a7d0534772mstarzinger@chromium.org * See HeapProfiler::GetHeapStats.
598fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org */
599fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.orgstruct HeapStatsUpdate {
600fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
601fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org    : index(index), count(count), size(size) { }
602fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org  uint32_t index;  // Index of the time interval that was changed.
603fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org  uint32_t count;  // New value of count field for the interval with this index.
604fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org  uint32_t size;  // New value of size field for the interval with this index.
605fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org};
606fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org
607fb0463312815affb1f0e9d5e351b7f9a3422e3a2svenpanne@chromium.org
608357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org}  // namespace v8
609357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
610357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org
611357bf65ed5309ac3a2c4bf20b6ce7770488787c2ager@chromium.org#endif  // V8_V8_PROFILER_H_
612