blob: 3e1952c7ade9b0ae6e2e9198508ae08e15620f7c [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// 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
Kristian Monsen25f61362010-05-21 11:50:48 +0100112 static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
Steve Block6ded16b2010-05-10 14:33:55 +0100113};
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 /**
Leon Clarkef7060e22010-06-03 12:02:55 +0100143 * 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 /**
Steve Block6ded16b2010-05-10 14:33:55 +0100152 * 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. */
Leon Clarkef7060e22010-06-03 12:02:55 +0100158 static const CpuProfile* GetProfile(
159 int index,
160 Handle<Value> security_token = Handle<Value>());
Steve Block6ded16b2010-05-10 14:33:55 +0100161
162 /** Returns a profile by uid. */
Leon Clarkef7060e22010-06-03 12:02:55 +0100163 static const CpuProfile* FindProfile(
164 unsigned uid,
165 Handle<Value> security_token = Handle<Value>());
Steve Block6ded16b2010-05-10 14:33:55 +0100166
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
Leon Clarkef7060e22010-06-03 12:02:55 +0100171 * 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.
Steve Block6ded16b2010-05-10 14:33:55 +0100174 */
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 */
Leon Clarkef7060e22010-06-03 12:02:55 +0100181 static const CpuProfile* StopProfiling(
182 Handle<String> title,
183 Handle<Value> security_token = Handle<Value>());
Steve Block6ded16b2010-05-10 14:33:55 +0100184};
185
186
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100187class 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 CONTEXT_VARIABLE = 0, // A variable from a function context.
198 ELEMENT = 1, // An element of an array.
199 PROPERTY = 2, // A named object property.
200 INTERNAL = 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 INTERNAL = 0, // Internal node, a virtual one, for housekeeping.
244 ARRAY = 1, // An array of elements.
245 STRING = 2, // A string.
246 OBJECT = 3, // A JS object (except for arrays and strings).
247 CODE = 4, // Compiled code.
248 CLOSURE = 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 /** Returns node's own size, in bytes. */
262 int GetSelfSize() const;
263
264 /** Returns node's network (self + reachable nodes) size, in bytes. */
265 int GetTotalSize() const;
266
267 /**
268 * Returns node's private size, in bytes. That is, the size of memory
269 * that will be reclaimed having this node collected.
270 */
271 int GetPrivateSize() const;
272
273 /** Returns child nodes count of the node. */
274 int GetChildrenCount() const;
275
276 /** Retrieves a child by index. */
277 const HeapGraphEdge* GetChild(int index) const;
278
279 /** Returns retainer nodes count of the node. */
280 int GetRetainersCount() const;
281
282 /** Returns a retainer by index. */
283 const HeapGraphEdge* GetRetainer(int index) const;
284
285 /** Returns the number of simple retaining paths from the root to the node. */
286 int GetRetainingPathsCount() const;
287
288 /** Returns a retaining path by index. */
289 const HeapGraphPath* GetRetainingPath(int index) const;
290};
291
292
293/**
294 * HeapSnapshots record the state of the JS heap at some moment.
295 */
296class V8EXPORT HeapSnapshot {
297 public:
298 /** Returns heap snapshot UID (assigned by the profiler.) */
299 unsigned GetUid() const;
300
301 /** Returns heap snapshot title. */
302 Handle<String> GetTitle() const;
303
304 /** Returns the root node of the heap graph. */
305 const HeapGraphNode* GetHead() const;
306};
307
308
309/**
310 * Interface for controlling heap profiling.
311 */
312class V8EXPORT HeapProfiler {
313 public:
314 /** Returns the number of snapshots taken. */
315 static int GetSnapshotsCount();
316
317 /** Returns a snapshot by index. */
318 static const HeapSnapshot* GetSnapshot(int index);
319
320 /** Returns a profile by uid. */
321 static const HeapSnapshot* FindSnapshot(unsigned uid);
322
323 /** Takes a heap snapshot and returns it. Title may be an empty string. */
324 static const HeapSnapshot* TakeSnapshot(Handle<String> title);
325};
326
327
Steve Block6ded16b2010-05-10 14:33:55 +0100328} // namespace v8
329
330
331#undef V8EXPORT
332
333
334#endif // V8_V8_PROFILER_H_