blob: d0215205f5fa97d8eadc1f2547df734f53c11a24 [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Block6ded16b2010-05-10 14:33:55 +01004
5#ifndef V8_V8_PROFILER_H_
6#define V8_V8_PROFILER_H_
7
8#include "v8.h"
9
Steve Block6ded16b2010-05-10 14:33:55 +010010/**
11 * Profiler support for the V8 JavaScript engine.
12 */
13namespace v8 {
14
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015class HeapGraphNode;
16struct HeapStatsUpdate;
17
18typedef uint32_t SnapshotObjectId;
Steve Block6ded16b2010-05-10 14:33:55 +010019
20/**
21 * CpuProfileNode represents a node in a call graph.
22 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023class V8_EXPORT CpuProfileNode {
Steve Block6ded16b2010-05-10 14:33:55 +010024 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040025 struct LineTick {
26 /** The 1-based number of the source line where the function originates. */
27 int line;
28
29 /** The count of samples associated with the source line. */
30 unsigned int hit_count;
31 };
32
Steve Block6ded16b2010-05-10 14:33:55 +010033 /** Returns function name (empty string for anonymous functions.) */
34 Handle<String> GetFunctionName() const;
35
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 /** Returns id of the script where function is located. */
37 int GetScriptId() const;
38
Steve Block6ded16b2010-05-10 14:33:55 +010039 /** Returns resource name for script from where the function originates. */
40 Handle<String> GetScriptResourceName() const;
41
42 /**
43 * Returns the number, 1-based, of the line where the function originates.
44 * kNoLineNumberInfo if no line number information is available.
45 */
46 int GetLineNumber() const;
47
48 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 * Returns 1-based number of the column where the function originates.
50 * kNoColumnNumberInfo if no column number information is available.
Steve Block6ded16b2010-05-10 14:33:55 +010051 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 int GetColumnNumber() const;
53
Emily Bernierd0a1eb72015-03-24 16:35:39 -040054 /**
55 * Returns the number of the function's source lines that collect the samples.
56 */
57 unsigned int GetHitLineCount() const;
58
59 /** Returns the set of source lines that collect the samples.
60 * The caller allocates buffer and responsible for releasing it.
61 * True if all available entries are copied, otherwise false.
62 * The function copies nothing if buffer is not large enough.
63 */
64 bool GetLineTicks(LineTick* entries, unsigned int length) const;
65
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 /** Returns bailout reason for the function
67 * if the optimization was disabled for it.
68 */
69 const char* GetBailoutReason() const;
Steve Block6ded16b2010-05-10 14:33:55 +010070
71 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 * Returns the count of samples where the function was currently executing.
73 */
74 unsigned GetHitCount() const;
Steve Block6ded16b2010-05-10 14:33:55 +010075
76 /** Returns function entry UID. */
77 unsigned GetCallUid() const;
78
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 /** Returns id of the node. The id is unique within the tree */
80 unsigned GetNodeId() const;
81
Steve Block6ded16b2010-05-10 14:33:55 +010082 /** Returns child nodes count of the node. */
83 int GetChildrenCount() const;
84
85 /** Retrieves a child node by index. */
86 const CpuProfileNode* GetChild(int index) const;
87
Kristian Monsen25f61362010-05-21 11:50:48 +010088 static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
Steve Block6ded16b2010-05-10 14:33:55 +010090};
91
92
93/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 * CpuProfile contains a CPU profile in a form of top-down call tree
95 * (from main() down to functions that do all the work).
Steve Block6ded16b2010-05-10 14:33:55 +010096 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097class V8_EXPORT CpuProfile {
Steve Block6ded16b2010-05-10 14:33:55 +010098 public:
Steve Block6ded16b2010-05-10 14:33:55 +010099 /** Returns CPU profile title. */
100 Handle<String> GetTitle() const;
101
Steve Block6ded16b2010-05-10 14:33:55 +0100102 /** Returns the root node of the top down call tree. */
103 const CpuProfileNode* GetTopDownRoot() const;
Steve Block44f0eee2011-05-26 01:26:41 +0100104
105 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106 * Returns number of samples recorded. The samples are not recorded unless
107 * |record_samples| parameter of CpuProfiler::StartCpuProfiling is true.
108 */
109 int GetSamplesCount() const;
110
111 /**
112 * Returns profile node corresponding to the top frame the sample at
113 * the given index.
114 */
115 const CpuProfileNode* GetSample(int index) const;
116
117 /**
118 * Returns the timestamp of the sample. The timestamp is the number of
119 * microseconds since some unspecified starting point.
120 * The point is equal to the starting point used by GetStartTime.
121 */
122 int64_t GetSampleTimestamp(int index) const;
123
124 /**
125 * Returns time when the profile recording was started (in microseconds)
126 * since some unspecified starting point.
127 */
128 int64_t GetStartTime() const;
129
130 /**
131 * Returns time when the profile recording was stopped (in microseconds)
132 * since some unspecified starting point.
133 * The point is equal to the starting point used by GetStartTime.
134 */
135 int64_t GetEndTime() const;
136
137 /**
Steve Block44f0eee2011-05-26 01:26:41 +0100138 * Deletes the profile and removes it from CpuProfiler's list.
139 * All pointers to nodes previously returned become invalid.
Steve Block44f0eee2011-05-26 01:26:41 +0100140 */
141 void Delete();
Steve Block6ded16b2010-05-10 14:33:55 +0100142};
143
144
145/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 * Interface for controlling CPU profiling. Instance of the
147 * profiler can be retrieved using v8::Isolate::GetCpuProfiler.
Steve Block6ded16b2010-05-10 14:33:55 +0100148 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149class V8_EXPORT CpuProfiler {
Steve Block6ded16b2010-05-10 14:33:55 +0100150 public:
151 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 * Changes default CPU profiler sampling interval to the specified number
153 * of microseconds. Default interval is 1000us. This method must be called
154 * when there are no profiles being recorded.
Leon Clarkef7060e22010-06-03 12:02:55 +0100155 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 void SetSamplingInterval(int us);
Steve Block6ded16b2010-05-10 14:33:55 +0100157
158 /**
159 * Starts collecting CPU profile. Title may be an empty string. It
160 * is allowed to have several profiles being collected at
161 * once. Attempts to start collecting several profiles with the same
Leon Clarkef7060e22010-06-03 12:02:55 +0100162 * title are silently ignored. While collecting a profile, functions
163 * from all security contexts are included in it. The token-based
164 * filtering is only performed when querying for a profile.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 *
166 * |record_samples| parameter controls whether individual samples should
167 * be recorded in addition to the aggregated tree.
Steve Block6ded16b2010-05-10 14:33:55 +0100168 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 void StartProfiling(Handle<String> title, bool record_samples = false);
170
171 /** Deprecated. Use StartProfiling instead. */
172 V8_DEPRECATED("Use StartProfiling",
173 void StartCpuProfiling(Handle<String> title,
174 bool record_samples = false));
Steve Block6ded16b2010-05-10 14:33:55 +0100175
176 /**
177 * Stops collecting CPU profile with a given title and returns it.
178 * If the title given is empty, finishes the last profile started.
179 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 CpuProfile* StopProfiling(Handle<String> title);
181
182 /** Deprecated. Use StopProfiling instead. */
183 V8_DEPRECATED("Use StopProfiling",
184 const CpuProfile* StopCpuProfiling(Handle<String> title));
Steve Block44f0eee2011-05-26 01:26:41 +0100185
186 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 * Tells the profiler whether the embedder is idle.
Steve Block44f0eee2011-05-26 01:26:41 +0100188 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189 void SetIdle(bool is_idle);
190
191 private:
192 CpuProfiler();
193 ~CpuProfiler();
194 CpuProfiler(const CpuProfiler&);
195 CpuProfiler& operator=(const CpuProfiler&);
Steve Block6ded16b2010-05-10 14:33:55 +0100196};
197
198
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100199/**
200 * HeapSnapshotEdge represents a directed connection between heap
Ben Murdoch257744e2011-11-30 15:57:28 +0000201 * graph nodes: from retainers to retained nodes.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100202 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203class V8_EXPORT HeapGraphEdge {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100204 public:
205 enum Type {
Iain Merrick75681382010-08-19 15:07:18 +0100206 kContextVariable = 0, // A variable from a function context.
207 kElement = 1, // An element of an array.
208 kProperty = 2, // A named object property.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800209 kInternal = 3, // A link that can't be accessed from JS,
210 // thus, its name isn't a real property name
211 // (e.g. parts of a ConsString).
212 kHidden = 4, // A link that is needed for proper sizes
213 // calculation, but may be hidden from user.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100214 kShortcut = 5, // A link that must not be followed during
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800215 // sizes calculation.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100216 kWeak = 6 // A weak reference (ignored by the GC).
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100217 };
218
219 /** Returns edge type (see HeapGraphEdge::Type). */
220 Type GetType() const;
221
222 /**
223 * Returns edge name. This can be a variable name, an element index, or
224 * a property name.
225 */
226 Handle<Value> GetName() const;
227
228 /** Returns origin node. */
229 const HeapGraphNode* GetFromNode() const;
230
231 /** Returns destination node. */
232 const HeapGraphNode* GetToNode() const;
233};
234
235
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100236/**
237 * HeapGraphNode represents a node in a heap graph.
238 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239class V8_EXPORT HeapGraphNode {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100240 public:
241 enum Type {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242 kHidden = 0, // Hidden node, may be filtered when shown to user.
243 kArray = 1, // An array of elements.
244 kString = 2, // A string.
245 kObject = 3, // A JS object (except for arrays and strings).
246 kCode = 4, // Compiled code.
247 kClosure = 5, // Function closure.
248 kRegExp = 6, // RegExp.
249 kHeapNumber = 7, // Number stored in the heap.
250 kNative = 8, // Native object (not from V8 heap).
251 kSynthetic = 9, // Synthetic object, usualy used for grouping
252 // snapshot items together.
253 kConsString = 10, // Concatenated string. A pair of pointers to strings.
254 kSlicedString = 11, // Sliced string. A fragment of another string.
255 kSymbol = 12 // A Symbol (ES6).
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100256 };
257
258 /** Returns node type (see HeapGraphNode::Type). */
259 Type GetType() const;
260
261 /**
262 * Returns node name. Depending on node's type this can be the name
263 * of the constructor (for objects), the name of the function (for
264 * closures), string value, or an empty string (for compiled code).
265 */
266 Handle<String> GetName() const;
267
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100268 /**
269 * Returns node id. For the same heap object, the id remains the same
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000270 * across all snapshots.
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100271 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272 SnapshotObjectId GetId() const;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100273
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100274 /** Returns node's own size, in bytes. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 V8_DEPRECATED("Use GetShallowSize instead",
276 int GetSelfSize() const);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100277
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000278 /** Returns node's own size, in bytes. */
279 size_t GetShallowSize() const;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100280
281 /** Returns child nodes count of the node. */
282 int GetChildrenCount() const;
283
284 /** Retrieves a child by index. */
285 const HeapGraphEdge* GetChild(int index) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286};
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100287
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100288
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289/**
290 * An interface for exporting data from V8, using "push" model.
291 */
292class V8_EXPORT OutputStream { // NOLINT
293 public:
294 enum WriteResult {
295 kContinue = 0,
296 kAbort = 1
297 };
298 virtual ~OutputStream() {}
299 /** Notify about the end of stream. */
300 virtual void EndOfStream() = 0;
301 /** Get preferred output chunk size. Called only once. */
302 virtual int GetChunkSize() { return 1024; }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800303 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000304 * Writes the next chunk of snapshot data into the stream. Writing
305 * can be stopped by returning kAbort as function result. EndOfStream
306 * will not be called in case writing was aborted.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800307 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000309 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310 * Writes the next chunk of heap stats data into the stream. Writing
311 * can be stopped by returning kAbort as function result. EndOfStream
312 * will not be called in case writing was aborted.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000313 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
315 return kAbort;
316 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100317};
318
319
320/**
321 * HeapSnapshots record the state of the JS heap at some moment.
322 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323class V8_EXPORT HeapSnapshot {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100324 public:
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100325 enum SerializationFormat {
326 kJSON = 0 // See format description near 'Serialize' method.
Steve Block791712a2010-08-27 10:21:07 +0100327 };
328
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100329 /** Returns heap snapshot UID (assigned by the profiler.) */
330 unsigned GetUid() const;
331
332 /** Returns heap snapshot title. */
333 Handle<String> GetTitle() const;
334
335 /** Returns the root node of the heap graph. */
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100336 const HeapGraphNode* GetRoot() const;
337
Ben Murdochb0fe1622011-05-05 13:52:32 +0100338 /** Returns a node by its id. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339 const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100340
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000341 /** Returns total nodes count in the snapshot. */
342 int GetNodesCount() const;
343
344 /** Returns a node by index. */
345 const HeapGraphNode* GetNode(int index) const;
346
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 /** Returns a max seen JS object Id. */
348 SnapshotObjectId GetMaxSnapshotJSObjectId() const;
349
Steve Block791712a2010-08-27 10:21:07 +0100350 /**
Steve Block44f0eee2011-05-26 01:26:41 +0100351 * Deletes the snapshot and removes it from HeapProfiler's list.
352 * All pointers to nodes, edges and paths previously returned become
353 * invalid.
Steve Block791712a2010-08-27 10:21:07 +0100354 */
Steve Block44f0eee2011-05-26 01:26:41 +0100355 void Delete();
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100356
357 /**
358 * Prepare a serialized representation of the snapshot. The result
359 * is written into the stream provided in chunks of specified size.
360 * The total length of the serialized snapshot is unknown in
Ben Murdoch257744e2011-11-30 15:57:28 +0000361 * advance, it can be roughly equal to JS heap size (that means,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100362 * it can be really big - tens of megabytes).
363 *
364 * For the JSON format, heap contents are represented as an object
365 * with the following structure:
366 *
367 * {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368 * snapshot: {
369 * title: "...",
370 * uid: nnn,
371 * meta: { meta-info },
372 * node_count: nnn,
373 * edge_count: nnn
374 * },
375 * nodes: [nodes array],
376 * edges: [edges array],
377 * strings: [strings array]
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100378 * }
379 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 * Nodes reference strings, other nodes, and edges by their indexes
381 * in corresponding arrays.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100382 */
383 void Serialize(OutputStream* stream, SerializationFormat format) const;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100384};
385
386
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387/**
388 * An interface for reporting progress and controlling long-running
389 * activities.
390 */
391class V8_EXPORT ActivityControl { // NOLINT
392 public:
393 enum ControlOption {
394 kContinue = 0,
395 kAbort = 1
396 };
397 virtual ~ActivityControl() {}
398 /**
399 * Notify about current progress. The activity can be stopped by
400 * returning kAbort as the callback result.
401 */
402 virtual ControlOption ReportProgressValue(int done, int total) = 0;
403};
404
Steve Block44f0eee2011-05-26 01:26:41 +0100405
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100406/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000407 * Interface for controlling heap profiling. Instance of the
408 * profiler can be retrieved using v8::Isolate::GetHeapProfiler.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100409 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000410class V8_EXPORT HeapProfiler {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100411 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100412 /**
413 * Callback function invoked for obtaining RetainedObjectInfo for
414 * the given JavaScript wrapper object. It is prohibited to enter V8
415 * while the callback is running: only getters on the handle and
416 * GetPointerFromInternalField on the objects are allowed.
417 */
418 typedef RetainedObjectInfo* (*WrapperInfoCallback)
419 (uint16_t class_id, Handle<Value> wrapper);
420
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100421 /** Returns the number of snapshots taken. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422 int GetSnapshotCount();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100423
424 /** Returns a snapshot by index. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 const HeapSnapshot* GetHeapSnapshot(int index);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100426
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 /**
428 * Returns SnapshotObjectId for a heap object referenced by |value| if
429 * it has been seen by the heap profiler, kUnknownObjectId otherwise.
430 */
431 SnapshotObjectId GetObjectId(Handle<Value> value);
432
433 /**
434 * Returns heap object with given SnapshotObjectId if the object is alive,
435 * otherwise empty handle is returned.
436 */
437 Handle<Value> FindObjectById(SnapshotObjectId id);
438
439 /**
440 * Clears internal map from SnapshotObjectId to heap object. The new objects
441 * will not be added into it unless a heap snapshot is taken or heap object
442 * tracking is kicked off.
443 */
444 void ClearObjectIds();
445
446 /**
447 * A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
448 * it in case heap profiler cannot find id for the object passed as
449 * parameter. HeapSnapshot::GetNodeById will always return NULL for such id.
450 */
451 static const SnapshotObjectId kUnknownObjectId = 0;
452
453 /**
454 * Callback interface for retrieving user friendly names of global objects.
455 */
456 class ObjectNameResolver {
457 public:
458 /**
459 * Returns name to be used in the heap snapshot for given node. Returned
460 * string must stay alive until snapshot collection is completed.
461 */
462 virtual const char* GetName(Handle<Object> object) = 0;
463 protected:
464 virtual ~ObjectNameResolver() {}
465 };
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100466
Steve Block791712a2010-08-27 10:21:07 +0100467 /**
468 * Takes a heap snapshot and returns it. Title may be an empty string.
Steve Block791712a2010-08-27 10:21:07 +0100469 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000470 const HeapSnapshot* TakeHeapSnapshot(
Steve Block791712a2010-08-27 10:21:07 +0100471 Handle<String> title,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 ActivityControl* control = NULL,
473 ObjectNameResolver* global_object_name_resolver = NULL);
474
475 /**
476 * Starts tracking of heap objects population statistics. After calling
477 * this method, all heap objects relocations done by the garbage collector
478 * are being registered.
479 *
480 * |track_allocations| parameter controls whether stack trace of each
481 * allocation in the heap will be recorded and reported as part of
482 * HeapSnapshot.
483 */
484 void StartTrackingHeapObjects(bool track_allocations = false);
485
486 /**
487 * Adds a new time interval entry to the aggregated statistics array. The
488 * time interval entry contains information on the current heap objects
489 * population size. The method also updates aggregated statistics and
490 * reports updates for all previous time intervals via the OutputStream
491 * object. Updates on each time interval are provided as a stream of the
492 * HeapStatsUpdate structure instances.
493 * The return value of the function is the last seen heap object Id.
494 *
495 * StartTrackingHeapObjects must be called before the first call to this
496 * method.
497 */
498 SnapshotObjectId GetHeapStats(OutputStream* stream);
499
500 /**
501 * Stops tracking of heap objects population statistics, cleans up all
502 * collected data. StartHeapObjectsTracking must be called again prior to
503 * calling PushHeapObjectsStats next time.
504 */
505 void StopTrackingHeapObjects();
Steve Block44f0eee2011-05-26 01:26:41 +0100506
507 /**
508 * Deletes all snapshots taken. All previously returned pointers to
509 * snapshots and their contents become invalid after this call.
510 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000511 void DeleteAllHeapSnapshots();
Steve Block44f0eee2011-05-26 01:26:41 +0100512
513 /** Binds a callback to embedder's class ID. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000514 void SetWrapperClassInfoProvider(
Steve Block44f0eee2011-05-26 01:26:41 +0100515 uint16_t class_id,
516 WrapperInfoCallback callback);
517
518 /**
519 * Default value of persistent handle class ID. Must not be used to
520 * define a class. Can be used to reset a class of a persistent
521 * handle.
522 */
523 static const uint16_t kPersistentHandleNoClassId = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100524
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000525 /** Returns memory used for profiler internal data and snapshots. */
526 size_t GetProfilerMemorySize();
527
528 /**
529 * Sets a RetainedObjectInfo for an object group (see V8::SetObjectGroupId).
530 */
531 void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo* info);
532
533 private:
534 HeapProfiler();
535 ~HeapProfiler();
536 HeapProfiler(const HeapProfiler&);
537 HeapProfiler& operator=(const HeapProfiler&);
Steve Block44f0eee2011-05-26 01:26:41 +0100538};
539
540
541/**
542 * Interface for providing information about embedder's objects
543 * held by global handles. This information is reported in two ways:
544 *
545 * 1. When calling AddObjectGroup, an embedder may pass
546 * RetainedObjectInfo instance describing the group. To collect
547 * this information while taking a heap snapshot, V8 calls GC
548 * prologue and epilogue callbacks.
549 *
550 * 2. When a heap snapshot is collected, V8 additionally
551 * requests RetainedObjectInfos for persistent handles that
552 * were not previously reported via AddObjectGroup.
553 *
554 * Thus, if an embedder wants to provide information about native
555 * objects for heap snapshots, he can do it in a GC prologue
556 * handler, and / or by assigning wrapper class ids in the following way:
557 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558 * 1. Bind a callback to class id by calling SetWrapperClassInfoProvider.
Steve Block44f0eee2011-05-26 01:26:41 +0100559 * 2. Call SetWrapperClassId on certain persistent handles.
560 *
561 * V8 takes ownership of RetainedObjectInfo instances passed to it and
562 * keeps them alive only during snapshot collection. Afterwards, they
563 * are freed by calling the Dispose class function.
564 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565class V8_EXPORT RetainedObjectInfo { // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +0100566 public:
567 /** Called by V8 when it no longer needs an instance. */
568 virtual void Dispose() = 0;
569
570 /** Returns whether two instances are equivalent. */
571 virtual bool IsEquivalent(RetainedObjectInfo* other) = 0;
572
573 /**
574 * Returns hash value for the instance. Equivalent instances
575 * must have the same hash value.
576 */
577 virtual intptr_t GetHash() = 0;
578
579 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100580 * Returns human-readable label. It must be a null-terminated UTF-8
Steve Block44f0eee2011-05-26 01:26:41 +0100581 * encoded string. V8 copies its contents during a call to GetLabel.
582 */
583 virtual const char* GetLabel() = 0;
584
585 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100586 * Returns human-readable group label. It must be a null-terminated UTF-8
587 * encoded string. V8 copies its contents during a call to GetGroupLabel.
588 * Heap snapshot generator will collect all the group names, create
589 * top level entries with these names and attach the objects to the
590 * corresponding top level group objects. There is a default
591 * implementation which is required because embedders don't have their
592 * own implementation yet.
593 */
594 virtual const char* GetGroupLabel() { return GetLabel(); }
595
596 /**
Steve Block44f0eee2011-05-26 01:26:41 +0100597 * Returns element count in case if a global handle retains
598 * a subgraph by holding one of its nodes.
599 */
600 virtual intptr_t GetElementCount() { return -1; }
601
602 /** Returns embedder's object size in bytes. */
603 virtual intptr_t GetSizeInBytes() { return -1; }
604
605 protected:
606 RetainedObjectInfo() {}
607 virtual ~RetainedObjectInfo() {}
608
609 private:
610 RetainedObjectInfo(const RetainedObjectInfo&);
611 RetainedObjectInfo& operator=(const RetainedObjectInfo&);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100612};
613
614
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000615/**
616 * A struct for exporting HeapStats data from V8, using "push" model.
617 * See HeapProfiler::GetHeapStats.
618 */
619struct HeapStatsUpdate {
620 HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
621 : index(index), count(count), size(size) { }
622 uint32_t index; // Index of the time interval that was changed.
623 uint32_t count; // New value of count field for the interval with this index.
624 uint32_t size; // New value of size field for the interval with this index.
625};
626
627
Steve Block6ded16b2010-05-10 14:33:55 +0100628} // namespace v8
629
630
Steve Block6ded16b2010-05-10 14:33:55 +0100631#endif // V8_V8_PROFILER_H_