v8-profiler.h revision 791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6
1// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_V8_PROFILER_H_
29#define V8_V8_PROFILER_H_
30
31#include "v8.h"
32
33#ifdef _WIN32
34// Setup for Windows DLL export/import. See v8.h in this directory for
35// information on how to build/use V8 as a DLL.
36#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
37#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
38  build configuration to ensure that at most one of these is set
39#endif
40
41#ifdef BUILDING_V8_SHARED
42#define V8EXPORT __declspec(dllexport)
43#elif USING_V8_SHARED
44#define V8EXPORT __declspec(dllimport)
45#else
46#define V8EXPORT
47#endif
48
49#else  // _WIN32
50
51// Setup for Linux shared library export. See v8.h in this directory for
52// information on how to build/use V8 as shared library.
53#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
54#define V8EXPORT __attribute__ ((visibility("default")))
55#else  // defined(__GNUC__) && (__GNUC__ >= 4)
56#define V8EXPORT
57#endif  // defined(__GNUC__) && (__GNUC__ >= 4)
58
59#endif  // _WIN32
60
61
62/**
63 * Profiler support for the V8 JavaScript engine.
64 */
65namespace v8 {
66
67
68/**
69 * CpuProfileNode represents a node in a call graph.
70 */
71class V8EXPORT CpuProfileNode {
72 public:
73  /** Returns function name (empty string for anonymous functions.) */
74  Handle<String> GetFunctionName() const;
75
76  /** Returns resource name for script from where the function originates. */
77  Handle<String> GetScriptResourceName() const;
78
79  /**
80   * Returns the number, 1-based, of the line where the function originates.
81   * kNoLineNumberInfo if no line number information is available.
82   */
83  int GetLineNumber() const;
84
85  /**
86   * Returns total (self + children) execution time of the function,
87   * in milliseconds, estimated by samples count.
88   */
89  double GetTotalTime() const;
90
91  /**
92   * Returns self execution time of the function, in milliseconds,
93   * estimated by samples count.
94   */
95  double GetSelfTime() const;
96
97  /** Returns the count of samples where function exists. */
98  double GetTotalSamplesCount() const;
99
100  /** Returns the count of samples where function was currently executing. */
101  double GetSelfSamplesCount() const;
102
103  /** Returns function entry UID. */
104  unsigned GetCallUid() const;
105
106  /** Returns child nodes count of the node. */
107  int GetChildrenCount() const;
108
109  /** Retrieves a child node by index. */
110  const CpuProfileNode* GetChild(int index) const;
111
112  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
113};
114
115
116/**
117 * CpuProfile contains a CPU profile in a form of two call trees:
118 *  - top-down (from main() down to functions that do all the work);
119 *  - bottom-up call graph (in backward direction).
120 */
121class V8EXPORT CpuProfile {
122 public:
123  /** Returns CPU profile UID (assigned by the profiler.) */
124  unsigned GetUid() const;
125
126  /** Returns CPU profile title. */
127  Handle<String> GetTitle() const;
128
129  /** Returns the root node of the bottom up call tree. */
130  const CpuProfileNode* GetBottomUpRoot() const;
131
132  /** Returns the root node of the top down call tree. */
133  const CpuProfileNode* GetTopDownRoot() const;
134};
135
136
137/**
138 * Interface for controlling CPU profiling.
139 */
140class V8EXPORT CpuProfiler {
141 public:
142  /**
143   * A note on security tokens usage. As scripts from different
144   * origins can run inside a single V8 instance, it is possible to
145   * have functions from different security contexts intermixed in a
146   * single CPU profile. To avoid exposing function names belonging to
147   * other contexts, filtering by security token is performed while
148   * obtaining profiling results.
149   */
150
151  /**
152   * Returns the number of profiles collected (doesn't include
153   * profiles that are being collected at the moment of call.)
154   */
155  static int GetProfilesCount();
156
157  /** Returns a profile by index. */
158  static const CpuProfile* GetProfile(
159      int index,
160      Handle<Value> security_token = Handle<Value>());
161
162  /** Returns a profile by uid. */
163  static const CpuProfile* FindProfile(
164      unsigned uid,
165      Handle<Value> security_token = Handle<Value>());
166
167  /**
168   * Starts collecting CPU profile. Title may be an empty string. It
169   * is allowed to have several profiles being collected at
170   * once. Attempts to start collecting several profiles with the same
171   * title are silently ignored. While collecting a profile, functions
172   * from all security contexts are included in it. The token-based
173   * filtering is only performed when querying for a profile.
174   */
175  static void StartProfiling(Handle<String> title);
176
177  /**
178   * Stops collecting CPU profile with a given title and returns it.
179   * If the title given is empty, finishes the last profile started.
180   */
181  static const CpuProfile* StopProfiling(
182      Handle<String> title,
183      Handle<Value> security_token = Handle<Value>());
184};
185
186
187class HeapGraphNode;
188
189
190/**
191 * HeapSnapshotEdge represents a directed connection between heap
192 * graph nodes: from retaners to retained nodes.
193 */
194class V8EXPORT HeapGraphEdge {
195 public:
196  enum Type {
197    kContextVariable = 0,  // A variable from a function context.
198    kElement = 1,          // An element of an array.
199    kProperty = 2,         // A named object property.
200    kInternal = 3          // A link that can't be accessed from JS,
201                           // thus, its name isn't a real property name.
202  };
203
204  /** Returns edge type (see HeapGraphEdge::Type). */
205  Type GetType() const;
206
207  /**
208   * Returns edge name. This can be a variable name, an element index, or
209   * a property name.
210   */
211  Handle<Value> GetName() const;
212
213  /** Returns origin node. */
214  const HeapGraphNode* GetFromNode() const;
215
216  /** Returns destination node. */
217  const HeapGraphNode* GetToNode() const;
218};
219
220
221class V8EXPORT HeapGraphPath {
222 public:
223  /** Returns the number of edges in the path. */
224  int GetEdgesCount() const;
225
226  /** Returns an edge from the path. */
227  const HeapGraphEdge* GetEdge(int index) const;
228
229  /** Returns origin node. */
230  const HeapGraphNode* GetFromNode() const;
231
232  /** Returns destination node. */
233  const HeapGraphNode* GetToNode() const;
234};
235
236
237/**
238 * HeapGraphNode represents a node in a heap graph.
239 */
240class V8EXPORT HeapGraphNode {
241 public:
242  enum Type {
243    kInternal = 0,   // Internal node, a virtual one, for housekeeping.
244    kArray = 1,      // An array of elements.
245    kString = 2,     // A string.
246    kObject = 3,     // A JS object (except for arrays and strings).
247    kCode = 4,       // Compiled code.
248    kClosure = 5     // Function closure.
249  };
250
251  /** Returns node type (see HeapGraphNode::Type). */
252  Type GetType() const;
253
254  /**
255   * Returns node name. Depending on node's type this can be the name
256   * of the constructor (for objects), the name of the function (for
257   * closures), string value, or an empty string (for compiled code).
258   */
259  Handle<String> GetName() const;
260
261  /**
262   * Returns node id. For the same heap object, the id remains the same
263   * across all snapshots. Not applicable to aggregated heap snapshots
264   * as they only contain aggregated instances.
265   */
266  uint64_t GetId() const;
267
268  /**
269   * Returns the number of instances. Only applicable to aggregated
270   * heap snapshots.
271   */
272  int GetInstancesCount() const;
273
274  /** Returns node's own size, in bytes. */
275  int GetSelfSize() const;
276
277  /** Returns node's network (self + reachable nodes) size, in bytes. */
278  int GetReachableSize() const;
279
280  /**
281   * Returns node's retained size, in bytes. That is, self + sizes of
282   * the objects that are reachable only from this object. In other
283   * words, the size of memory that will be reclaimed having this node
284   * collected.
285   */
286  int GetRetainedSize() const;
287
288  /** Returns child nodes count of the node. */
289  int GetChildrenCount() const;
290
291  /** Retrieves a child by index. */
292  const HeapGraphEdge* GetChild(int index) const;
293
294  /** Returns retainer nodes count of the node. */
295  int GetRetainersCount() const;
296
297  /** Returns a retainer by index. */
298  const HeapGraphEdge* GetRetainer(int index) const;
299
300  /** Returns the number of simple retaining paths from the root to the node. */
301  int GetRetainingPathsCount() const;
302
303  /** Returns a retaining path by index. */
304  const HeapGraphPath* GetRetainingPath(int index) const;
305};
306
307
308class V8EXPORT HeapSnapshotsDiff {
309 public:
310  /** Returns the root node for added nodes. */
311  const HeapGraphNode* GetAdditionsRoot() const;
312
313  /** Returns the root node for deleted nodes. */
314  const HeapGraphNode* GetDeletionsRoot() const;
315};
316
317
318/**
319 * HeapSnapshots record the state of the JS heap at some moment.
320 */
321class V8EXPORT HeapSnapshot {
322 public:
323  enum Type {
324    kFull = 0,       // Heap snapshot with all instances and references.
325    kAggregated = 1  // Snapshot doesn't contain individual heap entries,
326                     //instead they are grouped by constructor name.
327  };
328
329  /** Returns heap snapshot type. */
330  Type GetType() const;
331
332  /** Returns heap snapshot UID (assigned by the profiler.) */
333  unsigned GetUid() const;
334
335  /** Returns heap snapshot title. */
336  Handle<String> GetTitle() const;
337
338  /** Returns the root node of the heap graph. */
339  const HeapGraphNode* GetRoot() const;
340
341  /**
342   * Returns a diff between this snapshot and another one. Only snapshots
343   * of the same type can be compared.
344   */
345  const HeapSnapshotsDiff* CompareWith(const HeapSnapshot* snapshot) const;
346};
347
348
349/**
350 * Interface for controlling heap profiling.
351 */
352class V8EXPORT HeapProfiler {
353 public:
354  /** Returns the number of snapshots taken. */
355  static int GetSnapshotsCount();
356
357  /** Returns a snapshot by index. */
358  static const HeapSnapshot* GetSnapshot(int index);
359
360  /** Returns a profile by uid. */
361  static const HeapSnapshot* FindSnapshot(unsigned uid);
362
363  /**
364   * Takes a heap snapshot and returns it. Title may be an empty string.
365   * See HeapSnapshot::Type for types description.
366   */
367  static const HeapSnapshot* TakeSnapshot(
368      Handle<String> title,
369      HeapSnapshot::Type type = HeapSnapshot::kFull);
370};
371
372
373}  // namespace v8
374
375
376#undef V8EXPORT
377
378
379#endif  // V8_V8_PROFILER_H_
380