aha-libv8-node 16.0.0.0-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/ext/libv8-node/.location.yml +1 -0
  3. data/ext/libv8-node/location.rb +76 -0
  4. data/ext/libv8-node/paths.rb +30 -0
  5. data/lib/libv8-node.rb +1 -0
  6. data/lib/libv8/node.rb +11 -0
  7. data/lib/libv8/node/version.rb +7 -0
  8. data/vendor/v8/include/cppgc/allocation.h +229 -0
  9. data/vendor/v8/include/cppgc/common.h +29 -0
  10. data/vendor/v8/include/cppgc/cross-thread-persistent.h +345 -0
  11. data/vendor/v8/include/cppgc/custom-space.h +97 -0
  12. data/vendor/v8/include/cppgc/default-platform.h +75 -0
  13. data/vendor/v8/include/cppgc/ephemeron-pair.h +30 -0
  14. data/vendor/v8/include/cppgc/garbage-collected.h +116 -0
  15. data/vendor/v8/include/cppgc/heap-consistency.h +236 -0
  16. data/vendor/v8/include/cppgc/heap-state.h +59 -0
  17. data/vendor/v8/include/cppgc/heap-statistics.h +110 -0
  18. data/vendor/v8/include/cppgc/heap.h +199 -0
  19. data/vendor/v8/include/cppgc/internal/api-constants.h +47 -0
  20. data/vendor/v8/include/cppgc/internal/atomic-entry-flag.h +48 -0
  21. data/vendor/v8/include/cppgc/internal/caged-heap-local-data.h +68 -0
  22. data/vendor/v8/include/cppgc/internal/compiler-specific.h +38 -0
  23. data/vendor/v8/include/cppgc/internal/finalizer-trait.h +90 -0
  24. data/vendor/v8/include/cppgc/internal/gc-info.h +47 -0
  25. data/vendor/v8/include/cppgc/internal/logging.h +50 -0
  26. data/vendor/v8/include/cppgc/internal/name-trait.h +111 -0
  27. data/vendor/v8/include/cppgc/internal/persistent-node.h +132 -0
  28. data/vendor/v8/include/cppgc/internal/pointer-policies.h +143 -0
  29. data/vendor/v8/include/cppgc/internal/prefinalizer-handler.h +30 -0
  30. data/vendor/v8/include/cppgc/internal/write-barrier.h +390 -0
  31. data/vendor/v8/include/cppgc/liveness-broker.h +74 -0
  32. data/vendor/v8/include/cppgc/macros.h +26 -0
  33. data/vendor/v8/include/cppgc/member.h +271 -0
  34. data/vendor/v8/include/cppgc/name-provider.h +65 -0
  35. data/vendor/v8/include/cppgc/object-size-trait.h +58 -0
  36. data/vendor/v8/include/cppgc/persistent.h +365 -0
  37. data/vendor/v8/include/cppgc/platform.h +151 -0
  38. data/vendor/v8/include/cppgc/prefinalizer.h +52 -0
  39. data/vendor/v8/include/cppgc/process-heap-statistics.h +36 -0
  40. data/vendor/v8/include/cppgc/sentinel-pointer.h +32 -0
  41. data/vendor/v8/include/cppgc/source-location.h +91 -0
  42. data/vendor/v8/include/cppgc/testing.h +50 -0
  43. data/vendor/v8/include/cppgc/trace-trait.h +116 -0
  44. data/vendor/v8/include/cppgc/type-traits.h +228 -0
  45. data/vendor/v8/include/cppgc/visitor.h +340 -0
  46. data/vendor/v8/include/libplatform/libplatform-export.h +29 -0
  47. data/vendor/v8/include/libplatform/libplatform.h +117 -0
  48. data/vendor/v8/include/libplatform/v8-tracing.h +334 -0
  49. data/vendor/v8/include/v8-cppgc.h +278 -0
  50. data/vendor/v8/include/v8-fast-api-calls.h +419 -0
  51. data/vendor/v8/include/v8-inspector-protocol.h +13 -0
  52. data/vendor/v8/include/v8-inspector.h +336 -0
  53. data/vendor/v8/include/v8-internal.h +462 -0
  54. data/vendor/v8/include/v8-metrics.h +189 -0
  55. data/vendor/v8/include/v8-platform.h +710 -0
  56. data/vendor/v8/include/v8-profiler.h +1116 -0
  57. data/vendor/v8/include/v8-unwinder-state.h +30 -0
  58. data/vendor/v8/include/v8-util.h +652 -0
  59. data/vendor/v8/include/v8-value-serializer-version.h +24 -0
  60. data/vendor/v8/include/v8-version-string.h +38 -0
  61. data/vendor/v8/include/v8-version.h +20 -0
  62. data/vendor/v8/include/v8-wasm-trap-handler-posix.h +31 -0
  63. data/vendor/v8/include/v8-wasm-trap-handler-win.h +28 -0
  64. data/vendor/v8/include/v8.h +12479 -0
  65. data/vendor/v8/include/v8config.h +521 -0
  66. data/vendor/v8/out.gn/libv8/obj/libv8_monolith.a +0 -0
  67. metadata +137 -0
@@ -0,0 +1,1116 @@
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 <limits.h>
9
+
10
+ #include <memory>
11
+ #include <unordered_set>
12
+ #include <vector>
13
+
14
+ #include "v8.h" // NOLINT(build/include_directory)
15
+
16
+ /**
17
+ * Profiler support for the V8 JavaScript engine.
18
+ */
19
+ namespace v8 {
20
+
21
+ class HeapGraphNode;
22
+ struct HeapStatsUpdate;
23
+
24
+ using NativeObject = void*;
25
+ using SnapshotObjectId = uint32_t;
26
+
27
+ struct CpuProfileDeoptFrame {
28
+ int script_id;
29
+ size_t position;
30
+ };
31
+
32
+ namespace internal {
33
+ class CpuProfile;
34
+ } // namespace internal
35
+
36
+ } // namespace v8
37
+
38
+ #ifdef V8_OS_WIN
39
+ template class V8_EXPORT std::vector<v8::CpuProfileDeoptFrame>;
40
+ #endif
41
+
42
+ namespace v8 {
43
+
44
+ struct V8_EXPORT CpuProfileDeoptInfo {
45
+ /** A pointer to a static string owned by v8. */
46
+ const char* deopt_reason;
47
+ std::vector<CpuProfileDeoptFrame> stack;
48
+ };
49
+
50
+ } // namespace v8
51
+
52
+ #ifdef V8_OS_WIN
53
+ template class V8_EXPORT std::vector<v8::CpuProfileDeoptInfo>;
54
+ #endif
55
+
56
+ namespace v8 {
57
+
58
+ /**
59
+ * CpuProfileNode represents a node in a call graph.
60
+ */
61
+ class V8_EXPORT CpuProfileNode {
62
+ public:
63
+ struct LineTick {
64
+ /** The 1-based number of the source line where the function originates. */
65
+ int line;
66
+
67
+ /** The count of samples associated with the source line. */
68
+ unsigned int hit_count;
69
+ };
70
+
71
+ // An annotation hinting at the source of a CpuProfileNode.
72
+ enum SourceType {
73
+ // User-supplied script with associated resource information.
74
+ kScript = 0,
75
+ // Native scripts and provided builtins.
76
+ kBuiltin = 1,
77
+ // Callbacks into native code.
78
+ kCallback = 2,
79
+ // VM-internal functions or state.
80
+ kInternal = 3,
81
+ // A node that failed to symbolize.
82
+ kUnresolved = 4,
83
+ };
84
+
85
+ /** Returns function name (empty string for anonymous functions.) */
86
+ Local<String> GetFunctionName() const;
87
+
88
+ /**
89
+ * Returns function name (empty string for anonymous functions.)
90
+ * The string ownership is *not* passed to the caller. It stays valid until
91
+ * profile is deleted. The function is thread safe.
92
+ */
93
+ const char* GetFunctionNameStr() const;
94
+
95
+ /** Returns id of the script where function is located. */
96
+ int GetScriptId() const;
97
+
98
+ /** Returns resource name for script from where the function originates. */
99
+ Local<String> GetScriptResourceName() const;
100
+
101
+ /**
102
+ * Returns resource name for script from where the function originates.
103
+ * The string ownership is *not* passed to the caller. It stays valid until
104
+ * profile is deleted. The function is thread safe.
105
+ */
106
+ const char* GetScriptResourceNameStr() const;
107
+
108
+ /**
109
+ * Return true if the script from where the function originates is flagged as
110
+ * being shared cross-origin.
111
+ */
112
+ bool IsScriptSharedCrossOrigin() const;
113
+
114
+ /**
115
+ * Returns the number, 1-based, of the line where the function originates.
116
+ * kNoLineNumberInfo if no line number information is available.
117
+ */
118
+ int GetLineNumber() const;
119
+
120
+ /**
121
+ * Returns 1-based number of the column where the function originates.
122
+ * kNoColumnNumberInfo if no column number information is available.
123
+ */
124
+ int GetColumnNumber() const;
125
+
126
+ /**
127
+ * Returns the number of the function's source lines that collect the samples.
128
+ */
129
+ unsigned int GetHitLineCount() const;
130
+
131
+ /** Returns the set of source lines that collect the samples.
132
+ * The caller allocates buffer and responsible for releasing it.
133
+ * True if all available entries are copied, otherwise false.
134
+ * The function copies nothing if buffer is not large enough.
135
+ */
136
+ bool GetLineTicks(LineTick* entries, unsigned int length) const;
137
+
138
+ /** Returns bailout reason for the function
139
+ * if the optimization was disabled for it.
140
+ */
141
+ const char* GetBailoutReason() const;
142
+
143
+ /**
144
+ * Returns the count of samples where the function was currently executing.
145
+ */
146
+ unsigned GetHitCount() const;
147
+
148
+ /** Returns id of the node. The id is unique within the tree */
149
+ unsigned GetNodeId() const;
150
+
151
+ /**
152
+ * Gets the type of the source which the node was captured from.
153
+ */
154
+ SourceType GetSourceType() const;
155
+
156
+ /** Returns child nodes count of the node. */
157
+ int GetChildrenCount() const;
158
+
159
+ /** Retrieves a child node by index. */
160
+ const CpuProfileNode* GetChild(int index) const;
161
+
162
+ /** Retrieves the ancestor node, or null if the root. */
163
+ const CpuProfileNode* GetParent() const;
164
+
165
+ /** Retrieves deopt infos for the node. */
166
+ const std::vector<CpuProfileDeoptInfo>& GetDeoptInfos() const;
167
+
168
+ static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
169
+ static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
170
+ };
171
+
172
+
173
+ /**
174
+ * CpuProfile contains a CPU profile in a form of top-down call tree
175
+ * (from main() down to functions that do all the work).
176
+ */
177
+ class V8_EXPORT CpuProfile {
178
+ public:
179
+ /** Returns CPU profile title. */
180
+ Local<String> GetTitle() const;
181
+
182
+ /** Returns the root node of the top down call tree. */
183
+ const CpuProfileNode* GetTopDownRoot() const;
184
+
185
+ /**
186
+ * Returns number of samples recorded. The samples are not recorded unless
187
+ * |record_samples| parameter of CpuProfiler::StartCpuProfiling is true.
188
+ */
189
+ int GetSamplesCount() const;
190
+
191
+ /**
192
+ * Returns profile node corresponding to the top frame the sample at
193
+ * the given index.
194
+ */
195
+ const CpuProfileNode* GetSample(int index) const;
196
+
197
+ /**
198
+ * Returns the timestamp of the sample. The timestamp is the number of
199
+ * microseconds since some unspecified starting point.
200
+ * The point is equal to the starting point used by GetStartTime.
201
+ */
202
+ int64_t GetSampleTimestamp(int index) const;
203
+
204
+ /**
205
+ * Returns time when the profile recording was started (in microseconds)
206
+ * since some unspecified starting point.
207
+ */
208
+ int64_t GetStartTime() const;
209
+
210
+ /**
211
+ * Returns time when the profile recording was stopped (in microseconds)
212
+ * since some unspecified starting point.
213
+ * The point is equal to the starting point used by GetStartTime.
214
+ */
215
+ int64_t GetEndTime() const;
216
+
217
+ /**
218
+ * Deletes the profile and removes it from CpuProfiler's list.
219
+ * All pointers to nodes previously returned become invalid.
220
+ */
221
+ void Delete();
222
+ };
223
+
224
+ enum CpuProfilingMode {
225
+ // In the resulting CpuProfile tree, intermediate nodes in a stack trace
226
+ // (from the root to a leaf) will have line numbers that point to the start
227
+ // line of the function, rather than the line of the callsite of the child.
228
+ kLeafNodeLineNumbers,
229
+ // In the resulting CpuProfile tree, nodes are separated based on the line
230
+ // number of their callsite in their parent.
231
+ kCallerLineNumbers,
232
+ };
233
+
234
+ // Determines how names are derived for functions sampled.
235
+ enum CpuProfilingNamingMode {
236
+ // Use the immediate name of functions at compilation time.
237
+ kStandardNaming,
238
+ // Use more verbose naming for functions without names, inferred from scope
239
+ // where possible.
240
+ kDebugNaming,
241
+ };
242
+
243
+ enum CpuProfilingLoggingMode {
244
+ // Enables logging when a profile is active, and disables logging when all
245
+ // profiles are detached.
246
+ kLazyLogging,
247
+ // Enables logging for the lifetime of the CpuProfiler. Calls to
248
+ // StartRecording are faster, at the expense of runtime overhead.
249
+ kEagerLogging,
250
+ };
251
+
252
+ // Enum for returning profiling status. Once StartProfiling is called,
253
+ // we want to return to clients whether the profiling was able to start
254
+ // correctly, or return a descriptive error.
255
+ enum class CpuProfilingStatus {
256
+ kStarted,
257
+ kAlreadyStarted,
258
+ kErrorTooManyProfilers
259
+ };
260
+
261
+ /**
262
+ * Delegate for when max samples reached and samples are discarded.
263
+ */
264
+ class V8_EXPORT DiscardedSamplesDelegate {
265
+ public:
266
+ DiscardedSamplesDelegate() {}
267
+
268
+ virtual ~DiscardedSamplesDelegate() = default;
269
+ virtual void Notify() = 0;
270
+ };
271
+
272
+ /**
273
+ * Optional profiling attributes.
274
+ */
275
+ class V8_EXPORT CpuProfilingOptions {
276
+ public:
277
+ // Indicates that the sample buffer size should not be explicitly limited.
278
+ static const unsigned kNoSampleLimit = UINT_MAX;
279
+
280
+ /**
281
+ * \param mode Type of computation of stack frame line numbers.
282
+ * \param max_samples The maximum number of samples that should be recorded by
283
+ * the profiler. Samples obtained after this limit will be
284
+ * discarded.
285
+ * \param sampling_interval_us controls the profile-specific target
286
+ * sampling interval. The provided sampling
287
+ * interval will be snapped to the next lowest
288
+ * non-zero multiple of the profiler's sampling
289
+ * interval, set via SetSamplingInterval(). If
290
+ * zero, the sampling interval will be equal to
291
+ * the profiler's sampling interval.
292
+ * \param filter_context Deprecated option to filter by context, currently a
293
+ * no-op.
294
+ */
295
+ CpuProfilingOptions(
296
+ CpuProfilingMode mode = kLeafNodeLineNumbers,
297
+ unsigned max_samples = kNoSampleLimit, int sampling_interval_us = 0,
298
+ MaybeLocal<Context> filter_context = MaybeLocal<Context>());
299
+
300
+ CpuProfilingMode mode() const { return mode_; }
301
+ unsigned max_samples() const { return max_samples_; }
302
+ int sampling_interval_us() const { return sampling_interval_us_; }
303
+
304
+ private:
305
+ friend class internal::CpuProfile;
306
+
307
+ CpuProfilingMode mode_;
308
+ unsigned max_samples_;
309
+ int sampling_interval_us_;
310
+ };
311
+
312
+ /**
313
+ * Interface for controlling CPU profiling. Instance of the
314
+ * profiler can be created using v8::CpuProfiler::New method.
315
+ */
316
+ class V8_EXPORT CpuProfiler {
317
+ public:
318
+ /**
319
+ * Creates a new CPU profiler for the |isolate|. The isolate must be
320
+ * initialized. The profiler object must be disposed after use by calling
321
+ * |Dispose| method.
322
+ */
323
+ static CpuProfiler* New(Isolate* isolate,
324
+ CpuProfilingNamingMode = kDebugNaming,
325
+ CpuProfilingLoggingMode = kLazyLogging);
326
+
327
+ /**
328
+ * Synchronously collect current stack sample in all profilers attached to
329
+ * the |isolate|. The call does not affect number of ticks recorded for
330
+ * the current top node.
331
+ */
332
+ static void CollectSample(Isolate* isolate);
333
+
334
+ /**
335
+ * Disposes the CPU profiler object.
336
+ */
337
+ void Dispose();
338
+
339
+ /**
340
+ * Changes default CPU profiler sampling interval to the specified number
341
+ * of microseconds. Default interval is 1000us. This method must be called
342
+ * when there are no profiles being recorded.
343
+ */
344
+ void SetSamplingInterval(int us);
345
+
346
+ /**
347
+ * Sets whether or not the profiler should prioritize consistency of sample
348
+ * periodicity on Windows. Disabling this can greatly reduce CPU usage, but
349
+ * may result in greater variance in sample timings from the platform's
350
+ * scheduler. Defaults to enabled. This method must be called when there are
351
+ * no profiles being recorded.
352
+ */
353
+ void SetUsePreciseSampling(bool);
354
+
355
+ /**
356
+ * Starts collecting a CPU profile. Title may be an empty string. Several
357
+ * profiles may be collected at once. Attempts to start collecting several
358
+ * profiles with the same title are silently ignored.
359
+ */
360
+ CpuProfilingStatus StartProfiling(
361
+ Local<String> title, CpuProfilingOptions options,
362
+ std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
363
+
364
+ /**
365
+ * Starts profiling with the same semantics as above, except with expanded
366
+ * parameters.
367
+ *
368
+ * |record_samples| parameter controls whether individual samples should
369
+ * be recorded in addition to the aggregated tree.
370
+ *
371
+ * |max_samples| controls the maximum number of samples that should be
372
+ * recorded by the profiler. Samples obtained after this limit will be
373
+ * discarded.
374
+ */
375
+ CpuProfilingStatus StartProfiling(
376
+ Local<String> title, CpuProfilingMode mode, bool record_samples = false,
377
+ unsigned max_samples = CpuProfilingOptions::kNoSampleLimit);
378
+ /**
379
+ * The same as StartProfiling above, but the CpuProfilingMode defaults to
380
+ * kLeafNodeLineNumbers mode, which was the previous default behavior of the
381
+ * profiler.
382
+ */
383
+ CpuProfilingStatus StartProfiling(Local<String> title,
384
+ bool record_samples = false);
385
+
386
+ /**
387
+ * Stops collecting CPU profile with a given title and returns it.
388
+ * If the title given is empty, finishes the last profile started.
389
+ */
390
+ CpuProfile* StopProfiling(Local<String> title);
391
+
392
+ /**
393
+ * Generate more detailed source positions to code objects. This results in
394
+ * better results when mapping profiling samples to script source.
395
+ */
396
+ static void UseDetailedSourcePositionsForProfiling(Isolate* isolate);
397
+
398
+ private:
399
+ CpuProfiler();
400
+ ~CpuProfiler();
401
+ CpuProfiler(const CpuProfiler&);
402
+ CpuProfiler& operator=(const CpuProfiler&);
403
+ };
404
+
405
+ /**
406
+ * HeapSnapshotEdge represents a directed connection between heap
407
+ * graph nodes: from retainers to retained nodes.
408
+ */
409
+ class V8_EXPORT HeapGraphEdge {
410
+ public:
411
+ enum Type {
412
+ kContextVariable = 0, // A variable from a function context.
413
+ kElement = 1, // An element of an array.
414
+ kProperty = 2, // A named object property.
415
+ kInternal = 3, // A link that can't be accessed from JS,
416
+ // thus, its name isn't a real property name
417
+ // (e.g. parts of a ConsString).
418
+ kHidden = 4, // A link that is needed for proper sizes
419
+ // calculation, but may be hidden from user.
420
+ kShortcut = 5, // A link that must not be followed during
421
+ // sizes calculation.
422
+ kWeak = 6 // A weak reference (ignored by the GC).
423
+ };
424
+
425
+ /** Returns edge type (see HeapGraphEdge::Type). */
426
+ Type GetType() const;
427
+
428
+ /**
429
+ * Returns edge name. This can be a variable name, an element index, or
430
+ * a property name.
431
+ */
432
+ Local<Value> GetName() const;
433
+
434
+ /** Returns origin node. */
435
+ const HeapGraphNode* GetFromNode() const;
436
+
437
+ /** Returns destination node. */
438
+ const HeapGraphNode* GetToNode() const;
439
+ };
440
+
441
+
442
+ /**
443
+ * HeapGraphNode represents a node in a heap graph.
444
+ */
445
+ class V8_EXPORT HeapGraphNode {
446
+ public:
447
+ enum Type {
448
+ kHidden = 0, // Hidden node, may be filtered when shown to user.
449
+ kArray = 1, // An array of elements.
450
+ kString = 2, // A string.
451
+ kObject = 3, // A JS object (except for arrays and strings).
452
+ kCode = 4, // Compiled code.
453
+ kClosure = 5, // Function closure.
454
+ kRegExp = 6, // RegExp.
455
+ kHeapNumber = 7, // Number stored in the heap.
456
+ kNative = 8, // Native object (not from V8 heap).
457
+ kSynthetic = 9, // Synthetic object, usually used for grouping
458
+ // snapshot items together.
459
+ kConsString = 10, // Concatenated string. A pair of pointers to strings.
460
+ kSlicedString = 11, // Sliced string. A fragment of another string.
461
+ kSymbol = 12, // A Symbol (ES6).
462
+ kBigInt = 13 // BigInt.
463
+ };
464
+
465
+ /** Returns node type (see HeapGraphNode::Type). */
466
+ Type GetType() const;
467
+
468
+ /**
469
+ * Returns node name. Depending on node's type this can be the name
470
+ * of the constructor (for objects), the name of the function (for
471
+ * closures), string value, or an empty string (for compiled code).
472
+ */
473
+ Local<String> GetName() const;
474
+
475
+ /**
476
+ * Returns node id. For the same heap object, the id remains the same
477
+ * across all snapshots.
478
+ */
479
+ SnapshotObjectId GetId() const;
480
+
481
+ /** Returns node's own size, in bytes. */
482
+ size_t GetShallowSize() const;
483
+
484
+ /** Returns child nodes count of the node. */
485
+ int GetChildrenCount() const;
486
+
487
+ /** Retrieves a child by index. */
488
+ const HeapGraphEdge* GetChild(int index) const;
489
+ };
490
+
491
+
492
+ /**
493
+ * An interface for exporting data from V8, using "push" model.
494
+ */
495
+ class V8_EXPORT OutputStream { // NOLINT
496
+ public:
497
+ enum WriteResult {
498
+ kContinue = 0,
499
+ kAbort = 1
500
+ };
501
+ virtual ~OutputStream() = default;
502
+ /** Notify about the end of stream. */
503
+ virtual void EndOfStream() = 0;
504
+ /** Get preferred output chunk size. Called only once. */
505
+ virtual int GetChunkSize() { return 1024; }
506
+ /**
507
+ * Writes the next chunk of snapshot data into the stream. Writing
508
+ * can be stopped by returning kAbort as function result. EndOfStream
509
+ * will not be called in case writing was aborted.
510
+ */
511
+ virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
512
+ /**
513
+ * Writes the next chunk of heap stats data into the stream. Writing
514
+ * can be stopped by returning kAbort as function result. EndOfStream
515
+ * will not be called in case writing was aborted.
516
+ */
517
+ virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
518
+ return kAbort;
519
+ }
520
+ };
521
+
522
+
523
+ /**
524
+ * HeapSnapshots record the state of the JS heap at some moment.
525
+ */
526
+ class V8_EXPORT HeapSnapshot {
527
+ public:
528
+ enum SerializationFormat {
529
+ kJSON = 0 // See format description near 'Serialize' method.
530
+ };
531
+
532
+ /** Returns the root node of the heap graph. */
533
+ const HeapGraphNode* GetRoot() const;
534
+
535
+ /** Returns a node by its id. */
536
+ const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
537
+
538
+ /** Returns total nodes count in the snapshot. */
539
+ int GetNodesCount() const;
540
+
541
+ /** Returns a node by index. */
542
+ const HeapGraphNode* GetNode(int index) const;
543
+
544
+ /** Returns a max seen JS object Id. */
545
+ SnapshotObjectId GetMaxSnapshotJSObjectId() const;
546
+
547
+ /**
548
+ * Deletes the snapshot and removes it from HeapProfiler's list.
549
+ * All pointers to nodes, edges and paths previously returned become
550
+ * invalid.
551
+ */
552
+ void Delete();
553
+
554
+ /**
555
+ * Prepare a serialized representation of the snapshot. The result
556
+ * is written into the stream provided in chunks of specified size.
557
+ * The total length of the serialized snapshot is unknown in
558
+ * advance, it can be roughly equal to JS heap size (that means,
559
+ * it can be really big - tens of megabytes).
560
+ *
561
+ * For the JSON format, heap contents are represented as an object
562
+ * with the following structure:
563
+ *
564
+ * {
565
+ * snapshot: {
566
+ * title: "...",
567
+ * uid: nnn,
568
+ * meta: { meta-info },
569
+ * node_count: nnn,
570
+ * edge_count: nnn
571
+ * },
572
+ * nodes: [nodes array],
573
+ * edges: [edges array],
574
+ * strings: [strings array]
575
+ * }
576
+ *
577
+ * Nodes reference strings, other nodes, and edges by their indexes
578
+ * in corresponding arrays.
579
+ */
580
+ void Serialize(OutputStream* stream,
581
+ SerializationFormat format = kJSON) const;
582
+ };
583
+
584
+
585
+ /**
586
+ * An interface for reporting progress and controlling long-running
587
+ * activities.
588
+ */
589
+ class V8_EXPORT ActivityControl { // NOLINT
590
+ public:
591
+ enum ControlOption {
592
+ kContinue = 0,
593
+ kAbort = 1
594
+ };
595
+ virtual ~ActivityControl() = default;
596
+ /**
597
+ * Notify about current progress. The activity can be stopped by
598
+ * returning kAbort as the callback result.
599
+ */
600
+ virtual ControlOption ReportProgressValue(int done, int total) = 0;
601
+ };
602
+
603
+
604
+ /**
605
+ * AllocationProfile is a sampled profile of allocations done by the program.
606
+ * This is structured as a call-graph.
607
+ */
608
+ class V8_EXPORT AllocationProfile {
609
+ public:
610
+ struct Allocation {
611
+ /**
612
+ * Size of the sampled allocation object.
613
+ */
614
+ size_t size;
615
+
616
+ /**
617
+ * The number of objects of such size that were sampled.
618
+ */
619
+ unsigned int count;
620
+ };
621
+
622
+ /**
623
+ * Represents a node in the call-graph.
624
+ */
625
+ struct Node {
626
+ /**
627
+ * Name of the function. May be empty for anonymous functions or if the
628
+ * script corresponding to this function has been unloaded.
629
+ */
630
+ Local<String> name;
631
+
632
+ /**
633
+ * Name of the script containing the function. May be empty if the script
634
+ * name is not available, or if the script has been unloaded.
635
+ */
636
+ Local<String> script_name;
637
+
638
+ /**
639
+ * id of the script where the function is located. May be equal to
640
+ * v8::UnboundScript::kNoScriptId in cases where the script doesn't exist.
641
+ */
642
+ int script_id;
643
+
644
+ /**
645
+ * Start position of the function in the script.
646
+ */
647
+ int start_position;
648
+
649
+ /**
650
+ * 1-indexed line number where the function starts. May be
651
+ * kNoLineNumberInfo if no line number information is available.
652
+ */
653
+ int line_number;
654
+
655
+ /**
656
+ * 1-indexed column number where the function starts. May be
657
+ * kNoColumnNumberInfo if no line number information is available.
658
+ */
659
+ int column_number;
660
+
661
+ /**
662
+ * Unique id of the node.
663
+ */
664
+ uint32_t node_id;
665
+
666
+ /**
667
+ * List of callees called from this node for which we have sampled
668
+ * allocations. The lifetime of the children is scoped to the containing
669
+ * AllocationProfile.
670
+ */
671
+ std::vector<Node*> children;
672
+
673
+ /**
674
+ * List of self allocations done by this node in the call-graph.
675
+ */
676
+ std::vector<Allocation> allocations;
677
+ };
678
+
679
+ /**
680
+ * Represent a single sample recorded for an allocation.
681
+ */
682
+ struct Sample {
683
+ /**
684
+ * id of the node in the profile tree.
685
+ */
686
+ uint32_t node_id;
687
+
688
+ /**
689
+ * Size of the sampled allocation object.
690
+ */
691
+ size_t size;
692
+
693
+ /**
694
+ * The number of objects of such size that were sampled.
695
+ */
696
+ unsigned int count;
697
+
698
+ /**
699
+ * Unique time-ordered id of the allocation sample. Can be used to track
700
+ * what samples were added or removed between two snapshots.
701
+ */
702
+ uint64_t sample_id;
703
+ };
704
+
705
+ /**
706
+ * Returns the root node of the call-graph. The root node corresponds to an
707
+ * empty JS call-stack. The lifetime of the returned Node* is scoped to the
708
+ * containing AllocationProfile.
709
+ */
710
+ virtual Node* GetRootNode() = 0;
711
+ virtual const std::vector<Sample>& GetSamples() = 0;
712
+
713
+ virtual ~AllocationProfile() = default;
714
+
715
+ static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
716
+ static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
717
+ };
718
+
719
+ /**
720
+ * An object graph consisting of embedder objects and V8 objects.
721
+ * Edges of the graph are strong references between the objects.
722
+ * The embedder can build this graph during heap snapshot generation
723
+ * to include the embedder objects in the heap snapshot.
724
+ * Usage:
725
+ * 1) Define derived class of EmbedderGraph::Node for embedder objects.
726
+ * 2) Set the build embedder graph callback on the heap profiler using
727
+ * HeapProfiler::AddBuildEmbedderGraphCallback.
728
+ * 3) In the callback use graph->AddEdge(node1, node2) to add an edge from
729
+ * node1 to node2.
730
+ * 4) To represent references from/to V8 object, construct V8 nodes using
731
+ * graph->V8Node(value).
732
+ */
733
+ class V8_EXPORT EmbedderGraph {
734
+ public:
735
+ class Node {
736
+ public:
737
+ /**
738
+ * Detachedness specifies whether an object is attached or detached from the
739
+ * main application state. While unkown in general, there may be objects
740
+ * that specifically know their state. V8 passes this information along in
741
+ * the snapshot. Users of the snapshot may use it to annotate the object
742
+ * graph.
743
+ */
744
+ enum class Detachedness : uint8_t {
745
+ kUnknown = 0,
746
+ kAttached = 1,
747
+ kDetached = 2,
748
+ };
749
+
750
+ Node() = default;
751
+ virtual ~Node() = default;
752
+ virtual const char* Name() = 0;
753
+ virtual size_t SizeInBytes() = 0;
754
+ /**
755
+ * The corresponding V8 wrapper node if not null.
756
+ * During heap snapshot generation the embedder node and the V8 wrapper
757
+ * node will be merged into one node to simplify retaining paths.
758
+ */
759
+ virtual Node* WrapperNode() { return nullptr; }
760
+ virtual bool IsRootNode() { return false; }
761
+ /** Must return true for non-V8 nodes. */
762
+ virtual bool IsEmbedderNode() { return true; }
763
+ /**
764
+ * Optional name prefix. It is used in Chrome for tagging detached nodes.
765
+ */
766
+ virtual const char* NamePrefix() { return nullptr; }
767
+
768
+ /**
769
+ * Returns the NativeObject that can be used for querying the
770
+ * |HeapSnapshot|.
771
+ */
772
+ virtual NativeObject GetNativeObject() { return nullptr; }
773
+
774
+ /**
775
+ * Detachedness state of a given object. While unkown in general, there may
776
+ * be objects that specifically know their state. V8 passes this information
777
+ * along in the snapshot. Users of the snapshot may use it to annotate the
778
+ * object graph.
779
+ */
780
+ virtual Detachedness GetDetachedness() { return Detachedness::kUnknown; }
781
+
782
+ Node(const Node&) = delete;
783
+ Node& operator=(const Node&) = delete;
784
+ };
785
+
786
+ /**
787
+ * Returns a node corresponding to the given V8 value. Ownership is not
788
+ * transferred. The result pointer is valid while the graph is alive.
789
+ */
790
+ virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
791
+
792
+ /**
793
+ * Adds the given node to the graph and takes ownership of the node.
794
+ * Returns a raw pointer to the node that is valid while the graph is alive.
795
+ */
796
+ virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
797
+
798
+ /**
799
+ * Adds an edge that represents a strong reference from the given
800
+ * node |from| to the given node |to|. The nodes must be added to the graph
801
+ * before calling this function.
802
+ *
803
+ * If name is nullptr, the edge will have auto-increment indexes, otherwise
804
+ * it will be named accordingly.
805
+ */
806
+ virtual void AddEdge(Node* from, Node* to, const char* name = nullptr) = 0;
807
+
808
+ virtual ~EmbedderGraph() = default;
809
+ };
810
+
811
+ /**
812
+ * Interface for controlling heap profiling. Instance of the
813
+ * profiler can be retrieved using v8::Isolate::GetHeapProfiler.
814
+ */
815
+ class V8_EXPORT HeapProfiler {
816
+ public:
817
+ enum SamplingFlags {
818
+ kSamplingNoFlags = 0,
819
+ kSamplingForceGC = 1 << 0,
820
+ };
821
+
822
+ /**
823
+ * Callback function invoked during heap snapshot generation to retrieve
824
+ * the embedder object graph. The callback should use graph->AddEdge(..) to
825
+ * add references between the objects.
826
+ * The callback must not trigger garbage collection in V8.
827
+ */
828
+ typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
829
+ v8::EmbedderGraph* graph,
830
+ void* data);
831
+
832
+ /**
833
+ * Callback function invoked during heap snapshot generation to retrieve
834
+ * the detachedness state of an object referenced by a TracedReference.
835
+ *
836
+ * The callback takes Local<Value> as parameter to allow the embedder to
837
+ * unpack the TracedReference into a Local and reuse that Local for different
838
+ * purposes.
839
+ */
840
+ using GetDetachednessCallback = EmbedderGraph::Node::Detachedness (*)(
841
+ v8::Isolate* isolate, const v8::Local<v8::Value>& v8_value,
842
+ uint16_t class_id, void* data);
843
+
844
+ /** Returns the number of snapshots taken. */
845
+ int GetSnapshotCount();
846
+
847
+ /** Returns a snapshot by index. */
848
+ const HeapSnapshot* GetHeapSnapshot(int index);
849
+
850
+ /**
851
+ * Returns SnapshotObjectId for a heap object referenced by |value| if
852
+ * it has been seen by the heap profiler, kUnknownObjectId otherwise.
853
+ */
854
+ SnapshotObjectId GetObjectId(Local<Value> value);
855
+
856
+ /**
857
+ * Returns SnapshotObjectId for a native object referenced by |value| if it
858
+ * has been seen by the heap profiler, kUnknownObjectId otherwise.
859
+ */
860
+ SnapshotObjectId GetObjectId(NativeObject value);
861
+
862
+ /**
863
+ * Returns heap object with given SnapshotObjectId if the object is alive,
864
+ * otherwise empty handle is returned.
865
+ */
866
+ Local<Value> FindObjectById(SnapshotObjectId id);
867
+
868
+ /**
869
+ * Clears internal map from SnapshotObjectId to heap object. The new objects
870
+ * will not be added into it unless a heap snapshot is taken or heap object
871
+ * tracking is kicked off.
872
+ */
873
+ void ClearObjectIds();
874
+
875
+ /**
876
+ * A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
877
+ * it in case heap profiler cannot find id for the object passed as
878
+ * parameter. HeapSnapshot::GetNodeById will always return NULL for such id.
879
+ */
880
+ static const SnapshotObjectId kUnknownObjectId = 0;
881
+
882
+ /**
883
+ * Callback interface for retrieving user friendly names of global objects.
884
+ */
885
+ class ObjectNameResolver {
886
+ public:
887
+ /**
888
+ * Returns name to be used in the heap snapshot for given node. Returned
889
+ * string must stay alive until snapshot collection is completed.
890
+ */
891
+ virtual const char* GetName(Local<Object> object) = 0;
892
+
893
+ protected:
894
+ virtual ~ObjectNameResolver() = default;
895
+ };
896
+
897
+ /**
898
+ * Takes a heap snapshot and returns it.
899
+ */
900
+ const HeapSnapshot* TakeHeapSnapshot(
901
+ ActivityControl* control = nullptr,
902
+ ObjectNameResolver* global_object_name_resolver = nullptr,
903
+ bool treat_global_objects_as_roots = true);
904
+
905
+ /**
906
+ * Starts tracking of heap objects population statistics. After calling
907
+ * this method, all heap objects relocations done by the garbage collector
908
+ * are being registered.
909
+ *
910
+ * |track_allocations| parameter controls whether stack trace of each
911
+ * allocation in the heap will be recorded and reported as part of
912
+ * HeapSnapshot.
913
+ */
914
+ void StartTrackingHeapObjects(bool track_allocations = false);
915
+
916
+ /**
917
+ * Adds a new time interval entry to the aggregated statistics array. The
918
+ * time interval entry contains information on the current heap objects
919
+ * population size. The method also updates aggregated statistics and
920
+ * reports updates for all previous time intervals via the OutputStream
921
+ * object. Updates on each time interval are provided as a stream of the
922
+ * HeapStatsUpdate structure instances.
923
+ * If |timestamp_us| is supplied, timestamp of the new entry will be written
924
+ * into it. The return value of the function is the last seen heap object Id.
925
+ *
926
+ * StartTrackingHeapObjects must be called before the first call to this
927
+ * method.
928
+ */
929
+ SnapshotObjectId GetHeapStats(OutputStream* stream,
930
+ int64_t* timestamp_us = nullptr);
931
+
932
+ /**
933
+ * Stops tracking of heap objects population statistics, cleans up all
934
+ * collected data. StartHeapObjectsTracking must be called again prior to
935
+ * calling GetHeapStats next time.
936
+ */
937
+ void StopTrackingHeapObjects();
938
+
939
+ /**
940
+ * Starts gathering a sampling heap profile. A sampling heap profile is
941
+ * similar to tcmalloc's heap profiler and Go's mprof. It samples object
942
+ * allocations and builds an online 'sampling' heap profile. At any point in
943
+ * time, this profile is expected to be a representative sample of objects
944
+ * currently live in the system. Each sampled allocation includes the stack
945
+ * trace at the time of allocation, which makes this really useful for memory
946
+ * leak detection.
947
+ *
948
+ * This mechanism is intended to be cheap enough that it can be used in
949
+ * production with minimal performance overhead.
950
+ *
951
+ * Allocations are sampled using a randomized Poisson process. On average, one
952
+ * allocation will be sampled every |sample_interval| bytes allocated. The
953
+ * |stack_depth| parameter controls the maximum number of stack frames to be
954
+ * captured on each allocation.
955
+ *
956
+ * NOTE: This is a proof-of-concept at this point. Right now we only sample
957
+ * newspace allocations. Support for paged space allocation (e.g. pre-tenured
958
+ * objects, large objects, code objects, etc.) and native allocations
959
+ * doesn't exist yet, but is anticipated in the future.
960
+ *
961
+ * Objects allocated before the sampling is started will not be included in
962
+ * the profile.
963
+ *
964
+ * Returns false if a sampling heap profiler is already running.
965
+ */
966
+ bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
967
+ int stack_depth = 16,
968
+ SamplingFlags flags = kSamplingNoFlags);
969
+
970
+ /**
971
+ * Stops the sampling heap profile and discards the current profile.
972
+ */
973
+ void StopSamplingHeapProfiler();
974
+
975
+ /**
976
+ * Returns the sampled profile of allocations allocated (and still live) since
977
+ * StartSamplingHeapProfiler was called. The ownership of the pointer is
978
+ * transferred to the caller. Returns nullptr if sampling heap profiler is not
979
+ * active.
980
+ */
981
+ AllocationProfile* GetAllocationProfile();
982
+
983
+ /**
984
+ * Deletes all snapshots taken. All previously returned pointers to
985
+ * snapshots and their contents become invalid after this call.
986
+ */
987
+ void DeleteAllHeapSnapshots();
988
+
989
+ void AddBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
990
+ void* data);
991
+ void RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
992
+ void* data);
993
+
994
+ void SetGetDetachednessCallback(GetDetachednessCallback callback, void* data);
995
+
996
+ /**
997
+ * Default value of persistent handle class ID. Must not be used to
998
+ * define a class. Can be used to reset a class of a persistent
999
+ * handle.
1000
+ */
1001
+ static const uint16_t kPersistentHandleNoClassId = 0;
1002
+
1003
+ private:
1004
+ HeapProfiler();
1005
+ ~HeapProfiler();
1006
+ HeapProfiler(const HeapProfiler&);
1007
+ HeapProfiler& operator=(const HeapProfiler&);
1008
+ };
1009
+
1010
+ /**
1011
+ * A struct for exporting HeapStats data from V8, using "push" model.
1012
+ * See HeapProfiler::GetHeapStats.
1013
+ */
1014
+ struct HeapStatsUpdate {
1015
+ HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
1016
+ : index(index), count(count), size(size) { }
1017
+ uint32_t index; // Index of the time interval that was changed.
1018
+ uint32_t count; // New value of count field for the interval with this index.
1019
+ uint32_t size; // New value of size field for the interval with this index.
1020
+ };
1021
+
1022
+ #define CODE_EVENTS_LIST(V) \
1023
+ V(Builtin) \
1024
+ V(Callback) \
1025
+ V(Eval) \
1026
+ V(Function) \
1027
+ V(InterpretedFunction) \
1028
+ V(Handler) \
1029
+ V(BytecodeHandler) \
1030
+ V(LazyCompile) \
1031
+ V(RegExp) \
1032
+ V(Script) \
1033
+ V(Stub) \
1034
+ V(Relocation)
1035
+
1036
+ /**
1037
+ * Note that this enum may be extended in the future. Please include a default
1038
+ * case if this enum is used in a switch statement.
1039
+ */
1040
+ enum CodeEventType {
1041
+ kUnknownType = 0
1042
+ #define V(Name) , k##Name##Type
1043
+ CODE_EVENTS_LIST(V)
1044
+ #undef V
1045
+ };
1046
+
1047
+ /**
1048
+ * Representation of a code creation event
1049
+ */
1050
+ class V8_EXPORT CodeEvent {
1051
+ public:
1052
+ uintptr_t GetCodeStartAddress();
1053
+ size_t GetCodeSize();
1054
+ Local<String> GetFunctionName();
1055
+ Local<String> GetScriptName();
1056
+ int GetScriptLine();
1057
+ int GetScriptColumn();
1058
+ /**
1059
+ * NOTE (mmarchini): We can't allocate objects in the heap when we collect
1060
+ * existing code, and both the code type and the comment are not stored in the
1061
+ * heap, so we return those as const char*.
1062
+ */
1063
+ CodeEventType GetCodeType();
1064
+ const char* GetComment();
1065
+
1066
+ static const char* GetCodeEventTypeName(CodeEventType code_event_type);
1067
+
1068
+ uintptr_t GetPreviousCodeStartAddress();
1069
+ };
1070
+
1071
+ /**
1072
+ * Interface to listen to code creation and code relocation events.
1073
+ */
1074
+ class V8_EXPORT CodeEventHandler {
1075
+ public:
1076
+ /**
1077
+ * Creates a new listener for the |isolate|. The isolate must be initialized.
1078
+ * The listener object must be disposed after use by calling |Dispose| method.
1079
+ * Multiple listeners can be created for the same isolate.
1080
+ */
1081
+ explicit CodeEventHandler(Isolate* isolate);
1082
+ virtual ~CodeEventHandler();
1083
+
1084
+ /**
1085
+ * Handle is called every time a code object is created or moved. Information
1086
+ * about each code event will be available through the `code_event`
1087
+ * parameter.
1088
+ *
1089
+ * When the CodeEventType is kRelocationType, the code for this CodeEvent has
1090
+ * moved from `GetPreviousCodeStartAddress()` to `GetCodeStartAddress()`.
1091
+ */
1092
+ virtual void Handle(CodeEvent* code_event) = 0;
1093
+
1094
+ /**
1095
+ * Call `Enable()` to starts listening to code creation and code relocation
1096
+ * events. These events will be handled by `Handle()`.
1097
+ */
1098
+ void Enable();
1099
+
1100
+ /**
1101
+ * Call `Disable()` to stop listening to code creation and code relocation
1102
+ * events.
1103
+ */
1104
+ void Disable();
1105
+
1106
+ private:
1107
+ CodeEventHandler();
1108
+ CodeEventHandler(const CodeEventHandler&);
1109
+ CodeEventHandler& operator=(const CodeEventHandler&);
1110
+ void* internal_listener_;
1111
+ };
1112
+
1113
+ } // namespace v8
1114
+
1115
+
1116
+ #endif // V8_V8_PROFILER_H_