libv8 6.3.292.48.1-universal-darwin-18

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,238 @@
1
+ // Copyright 2013 the V8 project authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ #ifndef V8_V8_PLATFORM_H_
6
+ #define V8_V8_PLATFORM_H_
7
+
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+ #include <memory>
11
+ #include <string>
12
+
13
+ namespace v8 {
14
+
15
+ class Isolate;
16
+
17
+ /**
18
+ * A Task represents a unit of work.
19
+ */
20
+ class Task {
21
+ public:
22
+ virtual ~Task() = default;
23
+
24
+ virtual void Run() = 0;
25
+ };
26
+
27
+ /**
28
+ * An IdleTask represents a unit of work to be performed in idle time.
29
+ * The Run method is invoked with an argument that specifies the deadline in
30
+ * seconds returned by MonotonicallyIncreasingTime().
31
+ * The idle task is expected to complete by this deadline.
32
+ */
33
+ class IdleTask {
34
+ public:
35
+ virtual ~IdleTask() = default;
36
+ virtual void Run(double deadline_in_seconds) = 0;
37
+ };
38
+
39
+ /**
40
+ * The interface represents complex arguments to trace events.
41
+ */
42
+ class ConvertableToTraceFormat {
43
+ public:
44
+ virtual ~ConvertableToTraceFormat() = default;
45
+
46
+ /**
47
+ * Append the class info to the provided |out| string. The appended
48
+ * data must be a valid JSON object. Strings must be properly quoted, and
49
+ * escaped. There is no processing applied to the content after it is
50
+ * appended.
51
+ */
52
+ virtual void AppendAsTraceFormat(std::string* out) const = 0;
53
+ };
54
+
55
+ /**
56
+ * V8 Tracing controller.
57
+ *
58
+ * Can be implemented by an embedder to record trace events from V8.
59
+ */
60
+ class TracingController {
61
+ public:
62
+ virtual ~TracingController() = default;
63
+
64
+ /**
65
+ * Called by TRACE_EVENT* macros, don't call this directly.
66
+ * The name parameter is a category group for example:
67
+ * TRACE_EVENT0("v8,parse", "V8.Parse")
68
+ * The pointer returned points to a value with zero or more of the bits
69
+ * defined in CategoryGroupEnabledFlags.
70
+ **/
71
+ virtual const uint8_t* GetCategoryGroupEnabled(const char* name) {
72
+ static uint8_t no = 0;
73
+ return &no;
74
+ }
75
+
76
+ /**
77
+ * Adds a trace event to the platform tracing system. This function call is
78
+ * usually the result of a TRACE_* macro from trace_event_common.h when
79
+ * tracing and the category of the particular trace are enabled. It is not
80
+ * advisable to call this function on its own; it is really only meant to be
81
+ * used by the trace macros. The returned handle can be used by
82
+ * UpdateTraceEventDuration to update the duration of COMPLETE events.
83
+ */
84
+ virtual uint64_t AddTraceEvent(
85
+ char phase, const uint8_t* category_enabled_flag, const char* name,
86
+ const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
87
+ const char** arg_names, const uint8_t* arg_types,
88
+ const uint64_t* arg_values,
89
+ std::unique_ptr<ConvertableToTraceFormat>* arg_convertables,
90
+ unsigned int flags) {
91
+ return 0;
92
+ }
93
+
94
+ /**
95
+ * Sets the duration field of a COMPLETE trace event. It must be called with
96
+ * the handle returned from AddTraceEvent().
97
+ **/
98
+ virtual void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
99
+ const char* name, uint64_t handle) {}
100
+
101
+ class TraceStateObserver {
102
+ public:
103
+ virtual ~TraceStateObserver() = default;
104
+ virtual void OnTraceEnabled() = 0;
105
+ virtual void OnTraceDisabled() = 0;
106
+ };
107
+
108
+ /** Adds tracing state change observer. */
109
+ virtual void AddTraceStateObserver(TraceStateObserver*) {}
110
+
111
+ /** Removes tracing state change observer. */
112
+ virtual void RemoveTraceStateObserver(TraceStateObserver*) {}
113
+ };
114
+
115
+ /**
116
+ * V8 Platform abstraction layer.
117
+ *
118
+ * The embedder has to provide an implementation of this interface before
119
+ * initializing the rest of V8.
120
+ */
121
+ class Platform {
122
+ public:
123
+ /**
124
+ * This enum is used to indicate whether a task is potentially long running,
125
+ * or causes a long wait. The embedder might want to use this hint to decide
126
+ * whether to execute the task on a dedicated thread.
127
+ */
128
+ enum ExpectedRuntime {
129
+ kShortRunningTask,
130
+ kLongRunningTask
131
+ };
132
+
133
+ virtual ~Platform() = default;
134
+
135
+ /**
136
+ * Enables the embedder to respond in cases where V8 can't allocate large
137
+ * blocks of memory. V8 retries the failed allocation once after calling this
138
+ * method. On success, execution continues; otherwise V8 exits with a fatal
139
+ * error.
140
+ * Embedder overrides of this function must NOT call back into V8.
141
+ */
142
+ virtual void OnCriticalMemoryPressure() {}
143
+
144
+ /**
145
+ * Gets the number of threads that are used to execute background tasks. Is
146
+ * used to estimate the number of tasks a work package should be split into.
147
+ * A return value of 0 means that there are no background threads available.
148
+ * Note that a value of 0 won't prohibit V8 from posting tasks using
149
+ * |CallOnBackgroundThread|.
150
+ */
151
+ virtual size_t NumberOfAvailableBackgroundThreads() { return 0; }
152
+
153
+ /**
154
+ * Schedules a task to be invoked on a background thread. |expected_runtime|
155
+ * indicates that the task will run a long time. The Platform implementation
156
+ * takes ownership of |task|. There is no guarantee about order of execution
157
+ * of tasks wrt order of scheduling, nor is there a guarantee about the
158
+ * thread the task will be run on.
159
+ */
160
+ virtual void CallOnBackgroundThread(Task* task,
161
+ ExpectedRuntime expected_runtime) = 0;
162
+
163
+ /**
164
+ * Schedules a task to be invoked on a foreground thread wrt a specific
165
+ * |isolate|. Tasks posted for the same isolate should be execute in order of
166
+ * scheduling. The definition of "foreground" is opaque to V8.
167
+ */
168
+ virtual void CallOnForegroundThread(Isolate* isolate, Task* task) = 0;
169
+
170
+ /**
171
+ * Schedules a task to be invoked on a foreground thread wrt a specific
172
+ * |isolate| after the given number of seconds |delay_in_seconds|.
173
+ * Tasks posted for the same isolate should be execute in order of
174
+ * scheduling. The definition of "foreground" is opaque to V8.
175
+ */
176
+ virtual void CallDelayedOnForegroundThread(Isolate* isolate, Task* task,
177
+ double delay_in_seconds) = 0;
178
+
179
+ /**
180
+ * Schedules a task to be invoked on a foreground thread wrt a specific
181
+ * |isolate| when the embedder is idle.
182
+ * Requires that SupportsIdleTasks(isolate) is true.
183
+ * Idle tasks may be reordered relative to other task types and may be
184
+ * starved for an arbitrarily long time if no idle time is available.
185
+ * The definition of "foreground" is opaque to V8.
186
+ */
187
+ virtual void CallIdleOnForegroundThread(Isolate* isolate, IdleTask* task) {
188
+ // TODO(ulan): Make this function abstract after V8 roll in Chromium.
189
+ }
190
+
191
+ /**
192
+ * Returns true if idle tasks are enabled for the given |isolate|.
193
+ */
194
+ virtual bool IdleTasksEnabled(Isolate* isolate) {
195
+ // TODO(ulan): Make this function abstract after V8 roll in Chromium.
196
+ return false;
197
+ }
198
+
199
+ /**
200
+ * Monotonically increasing time in seconds from an arbitrary fixed point in
201
+ * the past. This function is expected to return at least
202
+ * millisecond-precision values. For this reason,
203
+ * it is recommended that the fixed point be no further in the past than
204
+ * the epoch.
205
+ **/
206
+ virtual double MonotonicallyIncreasingTime() = 0;
207
+
208
+ /**
209
+ * Current wall-clock time in milliseconds since epoch.
210
+ * This function is expected to return at least millisecond-precision values.
211
+ */
212
+ virtual double CurrentClockTimeMillis() = 0;
213
+
214
+ typedef void (*StackTracePrinter)();
215
+
216
+ /**
217
+ * Returns a function pointer that print a stack trace of the current stack
218
+ * on invocation. Disables printing of the stack trace if nullptr.
219
+ */
220
+ virtual StackTracePrinter GetStackTracePrinter() { return nullptr; }
221
+
222
+ /**
223
+ * Returns an instance of a v8::TracingController. This must be non-nullptr.
224
+ */
225
+ virtual TracingController* GetTracingController() = 0;
226
+
227
+ protected:
228
+ /**
229
+ * Default implementation of current wall-clock time in milliseconds
230
+ * since epoch. Useful for implementing |CurrentClockTimeMillis| if
231
+ * nothing special needed.
232
+ */
233
+ static double SystemClockTimeMillis();
234
+ };
235
+
236
+ } // namespace v8
237
+
238
+ #endif // V8_V8_PLATFORM_H_
@@ -0,0 +1,909 @@
1
+ // Copyright 2010 the V8 project authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ #ifndef V8_V8_PROFILER_H_
6
+ #define V8_V8_PROFILER_H_
7
+
8
+ #include <unordered_set>
9
+ #include <vector>
10
+ #include "v8.h" // NOLINT(build/include)
11
+
12
+ /**
13
+ * Profiler support for the V8 JavaScript engine.
14
+ */
15
+ namespace v8 {
16
+
17
+ class HeapGraphNode;
18
+ struct HeapStatsUpdate;
19
+
20
+ typedef uint32_t SnapshotObjectId;
21
+
22
+
23
+ struct CpuProfileDeoptFrame {
24
+ int script_id;
25
+ size_t position;
26
+ };
27
+
28
+ } // namespace v8
29
+
30
+ #ifdef V8_OS_WIN
31
+ template class V8_EXPORT std::vector<v8::CpuProfileDeoptFrame>;
32
+ #endif
33
+
34
+ namespace v8 {
35
+
36
+ struct V8_EXPORT CpuProfileDeoptInfo {
37
+ /** A pointer to a static string owned by v8. */
38
+ const char* deopt_reason;
39
+ std::vector<CpuProfileDeoptFrame> stack;
40
+ };
41
+
42
+ } // namespace v8
43
+
44
+ #ifdef V8_OS_WIN
45
+ template class V8_EXPORT std::vector<v8::CpuProfileDeoptInfo>;
46
+ #endif
47
+
48
+ namespace v8 {
49
+
50
+ /**
51
+ * TracingCpuProfiler monitors tracing being enabled/disabled
52
+ * and emits CpuProfile trace events once v8.cpu_profiler tracing category
53
+ * is enabled. It has no overhead unless the category is enabled.
54
+ */
55
+ class V8_EXPORT TracingCpuProfiler {
56
+ public:
57
+ static std::unique_ptr<TracingCpuProfiler> Create(Isolate*);
58
+ virtual ~TracingCpuProfiler() = default;
59
+
60
+ protected:
61
+ TracingCpuProfiler() = default;
62
+ };
63
+
64
+ // TickSample captures the information collected for each sample.
65
+ struct TickSample {
66
+ // Internal profiling (with --prof + tools/$OS-tick-processor) wants to
67
+ // include the runtime function we're calling. Externally exposed tick
68
+ // samples don't care.
69
+ enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame };
70
+
71
+ TickSample()
72
+ : state(OTHER),
73
+ pc(nullptr),
74
+ external_callback_entry(nullptr),
75
+ frames_count(0),
76
+ has_external_callback(false),
77
+ update_stats(true) {}
78
+
79
+ /**
80
+ * Initialize a tick sample from the isolate.
81
+ * \param isolate The isolate.
82
+ * \param state Execution state.
83
+ * \param record_c_entry_frame Include or skip the runtime function.
84
+ * \param update_stats Whether update the sample to the aggregated stats.
85
+ * \param use_simulator_reg_state When set to true and V8 is running under a
86
+ * simulator, the method will use the simulator
87
+ * register state rather than the one provided
88
+ * with |state| argument. Otherwise the method
89
+ * will use provided register |state| as is.
90
+ */
91
+ void Init(Isolate* isolate, const v8::RegisterState& state,
92
+ RecordCEntryFrame record_c_entry_frame, bool update_stats,
93
+ bool use_simulator_reg_state = true);
94
+ /**
95
+ * Get a call stack sample from the isolate.
96
+ * \param isolate The isolate.
97
+ * \param state Register state.
98
+ * \param record_c_entry_frame Include or skip the runtime function.
99
+ * \param frames Caller allocated buffer to store stack frames.
100
+ * \param frames_limit Maximum number of frames to capture. The buffer must
101
+ * be large enough to hold the number of frames.
102
+ * \param sample_info The sample info is filled up by the function
103
+ * provides number of actual captured stack frames and
104
+ * the current VM state.
105
+ * \param use_simulator_reg_state When set to true and V8 is running under a
106
+ * simulator, the method will use the simulator
107
+ * register state rather than the one provided
108
+ * with |state| argument. Otherwise the method
109
+ * will use provided register |state| as is.
110
+ * \note GetStackSample is thread and signal safe and should only be called
111
+ * when the JS thread is paused or interrupted.
112
+ * Otherwise the behavior is undefined.
113
+ */
114
+ static bool GetStackSample(Isolate* isolate, v8::RegisterState* state,
115
+ RecordCEntryFrame record_c_entry_frame,
116
+ void** frames, size_t frames_limit,
117
+ v8::SampleInfo* sample_info,
118
+ bool use_simulator_reg_state = true);
119
+ StateTag state; // The state of the VM.
120
+ void* pc; // Instruction pointer.
121
+ union {
122
+ void* tos; // Top stack value (*sp).
123
+ void* external_callback_entry;
124
+ };
125
+ static const unsigned kMaxFramesCountLog2 = 8;
126
+ static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1;
127
+ void* stack[kMaxFramesCount]; // Call stack.
128
+ unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames.
129
+ bool has_external_callback : 1;
130
+ bool update_stats : 1; // Whether the sample should update aggregated stats.
131
+ };
132
+
133
+ /**
134
+ * CpuProfileNode represents a node in a call graph.
135
+ */
136
+ class V8_EXPORT CpuProfileNode {
137
+ public:
138
+ struct LineTick {
139
+ /** The 1-based number of the source line where the function originates. */
140
+ int line;
141
+
142
+ /** The count of samples associated with the source line. */
143
+ unsigned int hit_count;
144
+ };
145
+
146
+ /** Returns function name (empty string for anonymous functions.) */
147
+ Local<String> GetFunctionName() const;
148
+
149
+ /**
150
+ * Returns function name (empty string for anonymous functions.)
151
+ * The string ownership is *not* passed to the caller. It stays valid until
152
+ * profile is deleted. The function is thread safe.
153
+ */
154
+ const char* GetFunctionNameStr() const;
155
+
156
+ /** Returns id of the script where function is located. */
157
+ int GetScriptId() const;
158
+
159
+ /** Returns resource name for script from where the function originates. */
160
+ Local<String> GetScriptResourceName() const;
161
+
162
+ /**
163
+ * Returns resource name for script from where the function originates.
164
+ * The string ownership is *not* passed to the caller. It stays valid until
165
+ * profile is deleted. The function is thread safe.
166
+ */
167
+ const char* GetScriptResourceNameStr() const;
168
+
169
+ /**
170
+ * Returns the number, 1-based, of the line where the function originates.
171
+ * kNoLineNumberInfo if no line number information is available.
172
+ */
173
+ int GetLineNumber() const;
174
+
175
+ /**
176
+ * Returns 1-based number of the column where the function originates.
177
+ * kNoColumnNumberInfo if no column number information is available.
178
+ */
179
+ int GetColumnNumber() const;
180
+
181
+ /**
182
+ * Returns the number of the function's source lines that collect the samples.
183
+ */
184
+ unsigned int GetHitLineCount() const;
185
+
186
+ /** Returns the set of source lines that collect the samples.
187
+ * The caller allocates buffer and responsible for releasing it.
188
+ * True if all available entries are copied, otherwise false.
189
+ * The function copies nothing if buffer is not large enough.
190
+ */
191
+ bool GetLineTicks(LineTick* entries, unsigned int length) const;
192
+
193
+ /** Returns bailout reason for the function
194
+ * if the optimization was disabled for it.
195
+ */
196
+ const char* GetBailoutReason() const;
197
+
198
+ /**
199
+ * Returns the count of samples where the function was currently executing.
200
+ */
201
+ unsigned GetHitCount() const;
202
+
203
+ /** Returns function entry UID. */
204
+ V8_DEPRECATE_SOON(
205
+ "Use GetScriptId, GetLineNumber, and GetColumnNumber instead.",
206
+ unsigned GetCallUid() const);
207
+
208
+ /** Returns id of the node. The id is unique within the tree */
209
+ unsigned GetNodeId() const;
210
+
211
+ /** Returns child nodes count of the node. */
212
+ int GetChildrenCount() const;
213
+
214
+ /** Retrieves a child node by index. */
215
+ const CpuProfileNode* GetChild(int index) const;
216
+
217
+ /** Retrieves deopt infos for the node. */
218
+ const std::vector<CpuProfileDeoptInfo>& GetDeoptInfos() const;
219
+
220
+ static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
221
+ static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
222
+ };
223
+
224
+
225
+ /**
226
+ * CpuProfile contains a CPU profile in a form of top-down call tree
227
+ * (from main() down to functions that do all the work).
228
+ */
229
+ class V8_EXPORT CpuProfile {
230
+ public:
231
+ /** Returns CPU profile title. */
232
+ Local<String> GetTitle() const;
233
+
234
+ /** Returns the root node of the top down call tree. */
235
+ const CpuProfileNode* GetTopDownRoot() const;
236
+
237
+ /**
238
+ * Returns number of samples recorded. The samples are not recorded unless
239
+ * |record_samples| parameter of CpuProfiler::StartCpuProfiling is true.
240
+ */
241
+ int GetSamplesCount() const;
242
+
243
+ /**
244
+ * Returns profile node corresponding to the top frame the sample at
245
+ * the given index.
246
+ */
247
+ const CpuProfileNode* GetSample(int index) const;
248
+
249
+ /**
250
+ * Returns the timestamp of the sample. The timestamp is the number of
251
+ * microseconds since some unspecified starting point.
252
+ * The point is equal to the starting point used by GetStartTime.
253
+ */
254
+ int64_t GetSampleTimestamp(int index) const;
255
+
256
+ /**
257
+ * Returns time when the profile recording was started (in microseconds)
258
+ * since some unspecified starting point.
259
+ */
260
+ int64_t GetStartTime() const;
261
+
262
+ /**
263
+ * Returns time when the profile recording was stopped (in microseconds)
264
+ * since some unspecified starting point.
265
+ * The point is equal to the starting point used by GetStartTime.
266
+ */
267
+ int64_t GetEndTime() const;
268
+
269
+ /**
270
+ * Deletes the profile and removes it from CpuProfiler's list.
271
+ * All pointers to nodes previously returned become invalid.
272
+ */
273
+ void Delete();
274
+ };
275
+
276
+ /**
277
+ * Interface for controlling CPU profiling. Instance of the
278
+ * profiler can be created using v8::CpuProfiler::New method.
279
+ */
280
+ class V8_EXPORT CpuProfiler {
281
+ public:
282
+ /**
283
+ * Creates a new CPU profiler for the |isolate|. The isolate must be
284
+ * initialized. The profiler object must be disposed after use by calling
285
+ * |Dispose| method.
286
+ */
287
+ static CpuProfiler* New(Isolate* isolate);
288
+
289
+ /**
290
+ * Disposes the CPU profiler object.
291
+ */
292
+ void Dispose();
293
+
294
+ /**
295
+ * Changes default CPU profiler sampling interval to the specified number
296
+ * of microseconds. Default interval is 1000us. This method must be called
297
+ * when there are no profiles being recorded.
298
+ */
299
+ void SetSamplingInterval(int us);
300
+
301
+ /**
302
+ * Starts collecting CPU profile. Title may be an empty string. It
303
+ * is allowed to have several profiles being collected at
304
+ * once. Attempts to start collecting several profiles with the same
305
+ * title are silently ignored. While collecting a profile, functions
306
+ * from all security contexts are included in it. The token-based
307
+ * filtering is only performed when querying for a profile.
308
+ *
309
+ * |record_samples| parameter controls whether individual samples should
310
+ * be recorded in addition to the aggregated tree.
311
+ */
312
+ void StartProfiling(Local<String> title, bool record_samples = false);
313
+
314
+ /**
315
+ * Stops collecting CPU profile with a given title and returns it.
316
+ * If the title given is empty, finishes the last profile started.
317
+ */
318
+ CpuProfile* StopProfiling(Local<String> title);
319
+
320
+ /**
321
+ * Force collection of a sample. Must be called on the VM thread.
322
+ * Recording the forced sample does not contribute to the aggregated
323
+ * profile statistics.
324
+ */
325
+ void CollectSample();
326
+
327
+ /**
328
+ * Tells the profiler whether the embedder is idle.
329
+ */
330
+ void SetIdle(bool is_idle);
331
+
332
+ private:
333
+ CpuProfiler();
334
+ ~CpuProfiler();
335
+ CpuProfiler(const CpuProfiler&);
336
+ CpuProfiler& operator=(const CpuProfiler&);
337
+ };
338
+
339
+
340
+ /**
341
+ * HeapSnapshotEdge represents a directed connection between heap
342
+ * graph nodes: from retainers to retained nodes.
343
+ */
344
+ class V8_EXPORT HeapGraphEdge {
345
+ public:
346
+ enum Type {
347
+ kContextVariable = 0, // A variable from a function context.
348
+ kElement = 1, // An element of an array.
349
+ kProperty = 2, // A named object property.
350
+ kInternal = 3, // A link that can't be accessed from JS,
351
+ // thus, its name isn't a real property name
352
+ // (e.g. parts of a ConsString).
353
+ kHidden = 4, // A link that is needed for proper sizes
354
+ // calculation, but may be hidden from user.
355
+ kShortcut = 5, // A link that must not be followed during
356
+ // sizes calculation.
357
+ kWeak = 6 // A weak reference (ignored by the GC).
358
+ };
359
+
360
+ /** Returns edge type (see HeapGraphEdge::Type). */
361
+ Type GetType() const;
362
+
363
+ /**
364
+ * Returns edge name. This can be a variable name, an element index, or
365
+ * a property name.
366
+ */
367
+ Local<Value> GetName() const;
368
+
369
+ /** Returns origin node. */
370
+ const HeapGraphNode* GetFromNode() const;
371
+
372
+ /** Returns destination node. */
373
+ const HeapGraphNode* GetToNode() const;
374
+ };
375
+
376
+
377
+ /**
378
+ * HeapGraphNode represents a node in a heap graph.
379
+ */
380
+ class V8_EXPORT HeapGraphNode {
381
+ public:
382
+ enum Type {
383
+ kHidden = 0, // Hidden node, may be filtered when shown to user.
384
+ kArray = 1, // An array of elements.
385
+ kString = 2, // A string.
386
+ kObject = 3, // A JS object (except for arrays and strings).
387
+ kCode = 4, // Compiled code.
388
+ kClosure = 5, // Function closure.
389
+ kRegExp = 6, // RegExp.
390
+ kHeapNumber = 7, // Number stored in the heap.
391
+ kNative = 8, // Native object (not from V8 heap).
392
+ kSynthetic = 9, // Synthetic object, usually used for grouping
393
+ // snapshot items together.
394
+ kConsString = 10, // Concatenated string. A pair of pointers to strings.
395
+ kSlicedString = 11, // Sliced string. A fragment of another string.
396
+ kSymbol = 12 // A Symbol (ES6).
397
+ };
398
+
399
+ /** Returns node type (see HeapGraphNode::Type). */
400
+ Type GetType() const;
401
+
402
+ /**
403
+ * Returns node name. Depending on node's type this can be the name
404
+ * of the constructor (for objects), the name of the function (for
405
+ * closures), string value, or an empty string (for compiled code).
406
+ */
407
+ Local<String> GetName() const;
408
+
409
+ /**
410
+ * Returns node id. For the same heap object, the id remains the same
411
+ * across all snapshots.
412
+ */
413
+ SnapshotObjectId GetId() const;
414
+
415
+ /** Returns node's own size, in bytes. */
416
+ size_t GetShallowSize() const;
417
+
418
+ /** Returns child nodes count of the node. */
419
+ int GetChildrenCount() const;
420
+
421
+ /** Retrieves a child by index. */
422
+ const HeapGraphEdge* GetChild(int index) const;
423
+ };
424
+
425
+
426
+ /**
427
+ * An interface for exporting data from V8, using "push" model.
428
+ */
429
+ class V8_EXPORT OutputStream { // NOLINT
430
+ public:
431
+ enum WriteResult {
432
+ kContinue = 0,
433
+ kAbort = 1
434
+ };
435
+ virtual ~OutputStream() {}
436
+ /** Notify about the end of stream. */
437
+ virtual void EndOfStream() = 0;
438
+ /** Get preferred output chunk size. Called only once. */
439
+ virtual int GetChunkSize() { return 1024; }
440
+ /**
441
+ * Writes the next chunk of snapshot data into the stream. Writing
442
+ * can be stopped by returning kAbort as function result. EndOfStream
443
+ * will not be called in case writing was aborted.
444
+ */
445
+ virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
446
+ /**
447
+ * Writes the next chunk of heap stats data into the stream. Writing
448
+ * can be stopped by returning kAbort as function result. EndOfStream
449
+ * will not be called in case writing was aborted.
450
+ */
451
+ virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
452
+ return kAbort;
453
+ }
454
+ };
455
+
456
+
457
+ /**
458
+ * HeapSnapshots record the state of the JS heap at some moment.
459
+ */
460
+ class V8_EXPORT HeapSnapshot {
461
+ public:
462
+ enum SerializationFormat {
463
+ kJSON = 0 // See format description near 'Serialize' method.
464
+ };
465
+
466
+ /** Returns the root node of the heap graph. */
467
+ const HeapGraphNode* GetRoot() const;
468
+
469
+ /** Returns a node by its id. */
470
+ const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
471
+
472
+ /** Returns total nodes count in the snapshot. */
473
+ int GetNodesCount() const;
474
+
475
+ /** Returns a node by index. */
476
+ const HeapGraphNode* GetNode(int index) const;
477
+
478
+ /** Returns a max seen JS object Id. */
479
+ SnapshotObjectId GetMaxSnapshotJSObjectId() const;
480
+
481
+ /**
482
+ * Deletes the snapshot and removes it from HeapProfiler's list.
483
+ * All pointers to nodes, edges and paths previously returned become
484
+ * invalid.
485
+ */
486
+ void Delete();
487
+
488
+ /**
489
+ * Prepare a serialized representation of the snapshot. The result
490
+ * is written into the stream provided in chunks of specified size.
491
+ * The total length of the serialized snapshot is unknown in
492
+ * advance, it can be roughly equal to JS heap size (that means,
493
+ * it can be really big - tens of megabytes).
494
+ *
495
+ * For the JSON format, heap contents are represented as an object
496
+ * with the following structure:
497
+ *
498
+ * {
499
+ * snapshot: {
500
+ * title: "...",
501
+ * uid: nnn,
502
+ * meta: { meta-info },
503
+ * node_count: nnn,
504
+ * edge_count: nnn
505
+ * },
506
+ * nodes: [nodes array],
507
+ * edges: [edges array],
508
+ * strings: [strings array]
509
+ * }
510
+ *
511
+ * Nodes reference strings, other nodes, and edges by their indexes
512
+ * in corresponding arrays.
513
+ */
514
+ void Serialize(OutputStream* stream,
515
+ SerializationFormat format = kJSON) const;
516
+ };
517
+
518
+
519
+ /**
520
+ * An interface for reporting progress and controlling long-running
521
+ * activities.
522
+ */
523
+ class V8_EXPORT ActivityControl { // NOLINT
524
+ public:
525
+ enum ControlOption {
526
+ kContinue = 0,
527
+ kAbort = 1
528
+ };
529
+ virtual ~ActivityControl() {}
530
+ /**
531
+ * Notify about current progress. The activity can be stopped by
532
+ * returning kAbort as the callback result.
533
+ */
534
+ virtual ControlOption ReportProgressValue(int done, int total) = 0;
535
+ };
536
+
537
+
538
+ /**
539
+ * AllocationProfile is a sampled profile of allocations done by the program.
540
+ * This is structured as a call-graph.
541
+ */
542
+ class V8_EXPORT AllocationProfile {
543
+ public:
544
+ struct Allocation {
545
+ /**
546
+ * Size of the sampled allocation object.
547
+ */
548
+ size_t size;
549
+
550
+ /**
551
+ * The number of objects of such size that were sampled.
552
+ */
553
+ unsigned int count;
554
+ };
555
+
556
+ /**
557
+ * Represents a node in the call-graph.
558
+ */
559
+ struct Node {
560
+ /**
561
+ * Name of the function. May be empty for anonymous functions or if the
562
+ * script corresponding to this function has been unloaded.
563
+ */
564
+ Local<String> name;
565
+
566
+ /**
567
+ * Name of the script containing the function. May be empty if the script
568
+ * name is not available, or if the script has been unloaded.
569
+ */
570
+ Local<String> script_name;
571
+
572
+ /**
573
+ * id of the script where the function is located. May be equal to
574
+ * v8::UnboundScript::kNoScriptId in cases where the script doesn't exist.
575
+ */
576
+ int script_id;
577
+
578
+ /**
579
+ * Start position of the function in the script.
580
+ */
581
+ int start_position;
582
+
583
+ /**
584
+ * 1-indexed line number where the function starts. May be
585
+ * kNoLineNumberInfo if no line number information is available.
586
+ */
587
+ int line_number;
588
+
589
+ /**
590
+ * 1-indexed column number where the function starts. May be
591
+ * kNoColumnNumberInfo if no line number information is available.
592
+ */
593
+ int column_number;
594
+
595
+ /**
596
+ * List of callees called from this node for which we have sampled
597
+ * allocations. The lifetime of the children is scoped to the containing
598
+ * AllocationProfile.
599
+ */
600
+ std::vector<Node*> children;
601
+
602
+ /**
603
+ * List of self allocations done by this node in the call-graph.
604
+ */
605
+ std::vector<Allocation> allocations;
606
+ };
607
+
608
+ /**
609
+ * Returns the root node of the call-graph. The root node corresponds to an
610
+ * empty JS call-stack. The lifetime of the returned Node* is scoped to the
611
+ * containing AllocationProfile.
612
+ */
613
+ virtual Node* GetRootNode() = 0;
614
+
615
+ virtual ~AllocationProfile() {}
616
+
617
+ static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
618
+ static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
619
+ };
620
+
621
+
622
+ /**
623
+ * Interface for controlling heap profiling. Instance of the
624
+ * profiler can be retrieved using v8::Isolate::GetHeapProfiler.
625
+ */
626
+ class V8_EXPORT HeapProfiler {
627
+ public:
628
+ enum SamplingFlags {
629
+ kSamplingNoFlags = 0,
630
+ kSamplingForceGC = 1 << 0,
631
+ };
632
+
633
+ typedef std::unordered_set<const v8::PersistentBase<v8::Value>*>
634
+ RetainerChildren;
635
+ typedef std::vector<std::pair<v8::RetainedObjectInfo*, RetainerChildren>>
636
+ RetainerGroups;
637
+ typedef std::vector<std::pair<const v8::PersistentBase<v8::Value>*,
638
+ const v8::PersistentBase<v8::Value>*>>
639
+ RetainerEdges;
640
+
641
+ struct RetainerInfos {
642
+ RetainerGroups groups;
643
+ RetainerEdges edges;
644
+ };
645
+
646
+ /**
647
+ * Callback function invoked to retrieve all RetainerInfos from the embedder.
648
+ */
649
+ typedef RetainerInfos (*GetRetainerInfosCallback)(v8::Isolate* isolate);
650
+
651
+ /**
652
+ * Callback function invoked for obtaining RetainedObjectInfo for
653
+ * the given JavaScript wrapper object. It is prohibited to enter V8
654
+ * while the callback is running: only getters on the handle and
655
+ * GetPointerFromInternalField on the objects are allowed.
656
+ */
657
+ typedef RetainedObjectInfo* (*WrapperInfoCallback)(uint16_t class_id,
658
+ Local<Value> wrapper);
659
+
660
+ /** Returns the number of snapshots taken. */
661
+ int GetSnapshotCount();
662
+
663
+ /** Returns a snapshot by index. */
664
+ const HeapSnapshot* GetHeapSnapshot(int index);
665
+
666
+ /**
667
+ * Returns SnapshotObjectId for a heap object referenced by |value| if
668
+ * it has been seen by the heap profiler, kUnknownObjectId otherwise.
669
+ */
670
+ SnapshotObjectId GetObjectId(Local<Value> value);
671
+
672
+ /**
673
+ * Returns heap object with given SnapshotObjectId if the object is alive,
674
+ * otherwise empty handle is returned.
675
+ */
676
+ Local<Value> FindObjectById(SnapshotObjectId id);
677
+
678
+ /**
679
+ * Clears internal map from SnapshotObjectId to heap object. The new objects
680
+ * will not be added into it unless a heap snapshot is taken or heap object
681
+ * tracking is kicked off.
682
+ */
683
+ void ClearObjectIds();
684
+
685
+ /**
686
+ * A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
687
+ * it in case heap profiler cannot find id for the object passed as
688
+ * parameter. HeapSnapshot::GetNodeById will always return NULL for such id.
689
+ */
690
+ static const SnapshotObjectId kUnknownObjectId = 0;
691
+
692
+ /**
693
+ * Callback interface for retrieving user friendly names of global objects.
694
+ */
695
+ class ObjectNameResolver {
696
+ public:
697
+ /**
698
+ * Returns name to be used in the heap snapshot for given node. Returned
699
+ * string must stay alive until snapshot collection is completed.
700
+ */
701
+ virtual const char* GetName(Local<Object> object) = 0;
702
+
703
+ protected:
704
+ virtual ~ObjectNameResolver() {}
705
+ };
706
+
707
+ /**
708
+ * Takes a heap snapshot and returns it.
709
+ */
710
+ const HeapSnapshot* TakeHeapSnapshot(
711
+ ActivityControl* control = NULL,
712
+ ObjectNameResolver* global_object_name_resolver = NULL);
713
+
714
+ /**
715
+ * Starts tracking of heap objects population statistics. After calling
716
+ * this method, all heap objects relocations done by the garbage collector
717
+ * are being registered.
718
+ *
719
+ * |track_allocations| parameter controls whether stack trace of each
720
+ * allocation in the heap will be recorded and reported as part of
721
+ * HeapSnapshot.
722
+ */
723
+ void StartTrackingHeapObjects(bool track_allocations = false);
724
+
725
+ /**
726
+ * Adds a new time interval entry to the aggregated statistics array. The
727
+ * time interval entry contains information on the current heap objects
728
+ * population size. The method also updates aggregated statistics and
729
+ * reports updates for all previous time intervals via the OutputStream
730
+ * object. Updates on each time interval are provided as a stream of the
731
+ * HeapStatsUpdate structure instances.
732
+ * If |timestamp_us| is supplied, timestamp of the new entry will be written
733
+ * into it. The return value of the function is the last seen heap object Id.
734
+ *
735
+ * StartTrackingHeapObjects must be called before the first call to this
736
+ * method.
737
+ */
738
+ SnapshotObjectId GetHeapStats(OutputStream* stream,
739
+ int64_t* timestamp_us = NULL);
740
+
741
+ /**
742
+ * Stops tracking of heap objects population statistics, cleans up all
743
+ * collected data. StartHeapObjectsTracking must be called again prior to
744
+ * calling GetHeapStats next time.
745
+ */
746
+ void StopTrackingHeapObjects();
747
+
748
+ /**
749
+ * Starts gathering a sampling heap profile. A sampling heap profile is
750
+ * similar to tcmalloc's heap profiler and Go's mprof. It samples object
751
+ * allocations and builds an online 'sampling' heap profile. At any point in
752
+ * time, this profile is expected to be a representative sample of objects
753
+ * currently live in the system. Each sampled allocation includes the stack
754
+ * trace at the time of allocation, which makes this really useful for memory
755
+ * leak detection.
756
+ *
757
+ * This mechanism is intended to be cheap enough that it can be used in
758
+ * production with minimal performance overhead.
759
+ *
760
+ * Allocations are sampled using a randomized Poisson process. On average, one
761
+ * allocation will be sampled every |sample_interval| bytes allocated. The
762
+ * |stack_depth| parameter controls the maximum number of stack frames to be
763
+ * captured on each allocation.
764
+ *
765
+ * NOTE: This is a proof-of-concept at this point. Right now we only sample
766
+ * newspace allocations. Support for paged space allocation (e.g. pre-tenured
767
+ * objects, large objects, code objects, etc.) and native allocations
768
+ * doesn't exist yet, but is anticipated in the future.
769
+ *
770
+ * Objects allocated before the sampling is started will not be included in
771
+ * the profile.
772
+ *
773
+ * Returns false if a sampling heap profiler is already running.
774
+ */
775
+ bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
776
+ int stack_depth = 16,
777
+ SamplingFlags flags = kSamplingNoFlags);
778
+
779
+ /**
780
+ * Stops the sampling heap profile and discards the current profile.
781
+ */
782
+ void StopSamplingHeapProfiler();
783
+
784
+ /**
785
+ * Returns the sampled profile of allocations allocated (and still live) since
786
+ * StartSamplingHeapProfiler was called. The ownership of the pointer is
787
+ * transferred to the caller. Returns nullptr if sampling heap profiler is not
788
+ * active.
789
+ */
790
+ AllocationProfile* GetAllocationProfile();
791
+
792
+ /**
793
+ * Deletes all snapshots taken. All previously returned pointers to
794
+ * snapshots and their contents become invalid after this call.
795
+ */
796
+ void DeleteAllHeapSnapshots();
797
+
798
+ /** Binds a callback to embedder's class ID. */
799
+ void SetWrapperClassInfoProvider(
800
+ uint16_t class_id,
801
+ WrapperInfoCallback callback);
802
+
803
+ void SetGetRetainerInfosCallback(GetRetainerInfosCallback callback);
804
+
805
+ /**
806
+ * Default value of persistent handle class ID. Must not be used to
807
+ * define a class. Can be used to reset a class of a persistent
808
+ * handle.
809
+ */
810
+ static const uint16_t kPersistentHandleNoClassId = 0;
811
+
812
+ private:
813
+ HeapProfiler();
814
+ ~HeapProfiler();
815
+ HeapProfiler(const HeapProfiler&);
816
+ HeapProfiler& operator=(const HeapProfiler&);
817
+ };
818
+
819
+ /**
820
+ * Interface for providing information about embedder's objects
821
+ * held by global handles. This information is reported in two ways:
822
+ *
823
+ * 1. When calling AddObjectGroup, an embedder may pass
824
+ * RetainedObjectInfo instance describing the group. To collect
825
+ * this information while taking a heap snapshot, V8 calls GC
826
+ * prologue and epilogue callbacks.
827
+ *
828
+ * 2. When a heap snapshot is collected, V8 additionally
829
+ * requests RetainedObjectInfos for persistent handles that
830
+ * were not previously reported via AddObjectGroup.
831
+ *
832
+ * Thus, if an embedder wants to provide information about native
833
+ * objects for heap snapshots, it can do it in a GC prologue
834
+ * handler, and / or by assigning wrapper class ids in the following way:
835
+ *
836
+ * 1. Bind a callback to class id by calling SetWrapperClassInfoProvider.
837
+ * 2. Call SetWrapperClassId on certain persistent handles.
838
+ *
839
+ * V8 takes ownership of RetainedObjectInfo instances passed to it and
840
+ * keeps them alive only during snapshot collection. Afterwards, they
841
+ * are freed by calling the Dispose class function.
842
+ */
843
+ class V8_EXPORT RetainedObjectInfo { // NOLINT
844
+ public:
845
+ /** Called by V8 when it no longer needs an instance. */
846
+ virtual void Dispose() = 0;
847
+
848
+ /** Returns whether two instances are equivalent. */
849
+ virtual bool IsEquivalent(RetainedObjectInfo* other) = 0;
850
+
851
+ /**
852
+ * Returns hash value for the instance. Equivalent instances
853
+ * must have the same hash value.
854
+ */
855
+ virtual intptr_t GetHash() = 0;
856
+
857
+ /**
858
+ * Returns human-readable label. It must be a null-terminated UTF-8
859
+ * encoded string. V8 copies its contents during a call to GetLabel.
860
+ */
861
+ virtual const char* GetLabel() = 0;
862
+
863
+ /**
864
+ * Returns human-readable group label. It must be a null-terminated UTF-8
865
+ * encoded string. V8 copies its contents during a call to GetGroupLabel.
866
+ * Heap snapshot generator will collect all the group names, create
867
+ * top level entries with these names and attach the objects to the
868
+ * corresponding top level group objects. There is a default
869
+ * implementation which is required because embedders don't have their
870
+ * own implementation yet.
871
+ */
872
+ virtual const char* GetGroupLabel() { return GetLabel(); }
873
+
874
+ /**
875
+ * Returns element count in case if a global handle retains
876
+ * a subgraph by holding one of its nodes.
877
+ */
878
+ virtual intptr_t GetElementCount() { return -1; }
879
+
880
+ /** Returns embedder's object size in bytes. */
881
+ virtual intptr_t GetSizeInBytes() { return -1; }
882
+
883
+ protected:
884
+ RetainedObjectInfo() {}
885
+ virtual ~RetainedObjectInfo() {}
886
+
887
+ private:
888
+ RetainedObjectInfo(const RetainedObjectInfo&);
889
+ RetainedObjectInfo& operator=(const RetainedObjectInfo&);
890
+ };
891
+
892
+
893
+ /**
894
+ * A struct for exporting HeapStats data from V8, using "push" model.
895
+ * See HeapProfiler::GetHeapStats.
896
+ */
897
+ struct HeapStatsUpdate {
898
+ HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
899
+ : index(index), count(count), size(size) { }
900
+ uint32_t index; // Index of the time interval that was changed.
901
+ uint32_t count; // New value of count field for the interval with this index.
902
+ uint32_t size; // New value of size field for the interval with this index.
903
+ };
904
+
905
+
906
+ } // namespace v8
907
+
908
+
909
+ #endif // V8_V8_PROFILER_H_