libv8-node 16.10.0.0-x86_64-darwin

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) 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 +34 -0
  5. data/lib/libv8/node/version.rb +7 -0
  6. data/lib/libv8/node.rb +11 -0
  7. data/lib/libv8-node.rb +1 -0
  8. data/vendor/v8/include/cppgc/allocation.h +232 -0
  9. data/vendor/v8/include/cppgc/common.h +29 -0
  10. data/vendor/v8/include/cppgc/cross-thread-persistent.h +384 -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/explicit-management.h +82 -0
  15. data/vendor/v8/include/cppgc/garbage-collected.h +117 -0
  16. data/vendor/v8/include/cppgc/heap-consistency.h +236 -0
  17. data/vendor/v8/include/cppgc/heap-state.h +70 -0
  18. data/vendor/v8/include/cppgc/heap-statistics.h +120 -0
  19. data/vendor/v8/include/cppgc/heap.h +201 -0
  20. data/vendor/v8/include/cppgc/internal/api-constants.h +47 -0
  21. data/vendor/v8/include/cppgc/internal/atomic-entry-flag.h +48 -0
  22. data/vendor/v8/include/cppgc/internal/caged-heap-local-data.h +68 -0
  23. data/vendor/v8/include/cppgc/internal/compiler-specific.h +38 -0
  24. data/vendor/v8/include/cppgc/internal/finalizer-trait.h +90 -0
  25. data/vendor/v8/include/cppgc/internal/gc-info.h +76 -0
  26. data/vendor/v8/include/cppgc/internal/logging.h +50 -0
  27. data/vendor/v8/include/cppgc/internal/name-trait.h +111 -0
  28. data/vendor/v8/include/cppgc/internal/persistent-node.h +172 -0
  29. data/vendor/v8/include/cppgc/internal/pointer-policies.h +175 -0
  30. data/vendor/v8/include/cppgc/internal/prefinalizer-handler.h +30 -0
  31. data/vendor/v8/include/cppgc/internal/write-barrier.h +396 -0
  32. data/vendor/v8/include/cppgc/liveness-broker.h +74 -0
  33. data/vendor/v8/include/cppgc/macros.h +26 -0
  34. data/vendor/v8/include/cppgc/member.h +286 -0
  35. data/vendor/v8/include/cppgc/name-provider.h +65 -0
  36. data/vendor/v8/include/cppgc/object-size-trait.h +58 -0
  37. data/vendor/v8/include/cppgc/persistent.h +365 -0
  38. data/vendor/v8/include/cppgc/platform.h +153 -0
  39. data/vendor/v8/include/cppgc/prefinalizer.h +52 -0
  40. data/vendor/v8/include/cppgc/process-heap-statistics.h +36 -0
  41. data/vendor/v8/include/cppgc/sentinel-pointer.h +32 -0
  42. data/vendor/v8/include/cppgc/source-location.h +92 -0
  43. data/vendor/v8/include/cppgc/testing.h +99 -0
  44. data/vendor/v8/include/cppgc/trace-trait.h +116 -0
  45. data/vendor/v8/include/cppgc/type-traits.h +247 -0
  46. data/vendor/v8/include/cppgc/visitor.h +377 -0
  47. data/vendor/v8/include/libplatform/libplatform-export.h +29 -0
  48. data/vendor/v8/include/libplatform/libplatform.h +117 -0
  49. data/vendor/v8/include/libplatform/v8-tracing.h +334 -0
  50. data/vendor/v8/include/v8-cppgc.h +325 -0
  51. data/vendor/v8/include/v8-fast-api-calls.h +791 -0
  52. data/vendor/v8/include/v8-inspector-protocol.h +13 -0
  53. data/vendor/v8/include/v8-inspector.h +348 -0
  54. data/vendor/v8/include/v8-internal.h +499 -0
  55. data/vendor/v8/include/v8-metrics.h +202 -0
  56. data/vendor/v8/include/v8-platform.h +709 -0
  57. data/vendor/v8/include/v8-profiler.h +1123 -0
  58. data/vendor/v8/include/v8-unwinder-state.h +30 -0
  59. data/vendor/v8/include/v8-util.h +652 -0
  60. data/vendor/v8/include/v8-value-serializer-version.h +24 -0
  61. data/vendor/v8/include/v8-version-string.h +38 -0
  62. data/vendor/v8/include/v8-version.h +20 -0
  63. data/vendor/v8/include/v8-wasm-trap-handler-posix.h +31 -0
  64. data/vendor/v8/include/v8-wasm-trap-handler-win.h +28 -0
  65. data/vendor/v8/include/v8.h +12648 -0
  66. data/vendor/v8/include/v8config.h +515 -0
  67. data/vendor/v8/x86_64-darwin/libv8/obj/libv8_monolith.a +0 -0
  68. metadata +138 -0
@@ -0,0 +1,1123 @@
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 {
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
+ * HeapSnapshots record the state of the JS heap at some moment.
524
+ */
525
+ class V8_EXPORT HeapSnapshot {
526
+ public:
527
+ enum SerializationFormat {
528
+ kJSON = 0 // See format description near 'Serialize' method.
529
+ };
530
+
531
+ /** Returns the root node of the heap graph. */
532
+ const HeapGraphNode* GetRoot() const;
533
+
534
+ /** Returns a node by its id. */
535
+ const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
536
+
537
+ /** Returns total nodes count in the snapshot. */
538
+ int GetNodesCount() const;
539
+
540
+ /** Returns a node by index. */
541
+ const HeapGraphNode* GetNode(int index) const;
542
+
543
+ /** Returns a max seen JS object Id. */
544
+ SnapshotObjectId GetMaxSnapshotJSObjectId() const;
545
+
546
+ /**
547
+ * Deletes the snapshot and removes it from HeapProfiler's list.
548
+ * All pointers to nodes, edges and paths previously returned become
549
+ * invalid.
550
+ */
551
+ void Delete();
552
+
553
+ /**
554
+ * Prepare a serialized representation of the snapshot. The result
555
+ * is written into the stream provided in chunks of specified size.
556
+ * The total length of the serialized snapshot is unknown in
557
+ * advance, it can be roughly equal to JS heap size (that means,
558
+ * it can be really big - tens of megabytes).
559
+ *
560
+ * For the JSON format, heap contents are represented as an object
561
+ * with the following structure:
562
+ *
563
+ * {
564
+ * snapshot: {
565
+ * title: "...",
566
+ * uid: nnn,
567
+ * meta: { meta-info },
568
+ * node_count: nnn,
569
+ * edge_count: nnn
570
+ * },
571
+ * nodes: [nodes array],
572
+ * edges: [edges array],
573
+ * strings: [strings array]
574
+ * }
575
+ *
576
+ * Nodes reference strings, other nodes, and edges by their indexes
577
+ * in corresponding arrays.
578
+ */
579
+ void Serialize(OutputStream* stream,
580
+ SerializationFormat format = kJSON) const;
581
+ };
582
+
583
+
584
+ /**
585
+ * An interface for reporting progress and controlling long-running
586
+ * activities.
587
+ */
588
+ class V8_EXPORT ActivityControl {
589
+ public:
590
+ enum ControlOption {
591
+ kContinue = 0,
592
+ kAbort = 1
593
+ };
594
+ virtual ~ActivityControl() = default;
595
+ /**
596
+ * Notify about current progress. The activity can be stopped by
597
+ * returning kAbort as the callback result.
598
+ */
599
+ virtual ControlOption ReportProgressValue(int done, int total) = 0;
600
+ };
601
+
602
+ /**
603
+ * AllocationProfile is a sampled profile of allocations done by the program.
604
+ * This is structured as a call-graph.
605
+ */
606
+ class V8_EXPORT AllocationProfile {
607
+ public:
608
+ struct Allocation {
609
+ /**
610
+ * Size of the sampled allocation object.
611
+ */
612
+ size_t size;
613
+
614
+ /**
615
+ * The number of objects of such size that were sampled.
616
+ */
617
+ unsigned int count;
618
+ };
619
+
620
+ /**
621
+ * Represents a node in the call-graph.
622
+ */
623
+ struct Node {
624
+ /**
625
+ * Name of the function. May be empty for anonymous functions or if the
626
+ * script corresponding to this function has been unloaded.
627
+ */
628
+ Local<String> name;
629
+
630
+ /**
631
+ * Name of the script containing the function. May be empty if the script
632
+ * name is not available, or if the script has been unloaded.
633
+ */
634
+ Local<String> script_name;
635
+
636
+ /**
637
+ * id of the script where the function is located. May be equal to
638
+ * v8::UnboundScript::kNoScriptId in cases where the script doesn't exist.
639
+ */
640
+ int script_id;
641
+
642
+ /**
643
+ * Start position of the function in the script.
644
+ */
645
+ int start_position;
646
+
647
+ /**
648
+ * 1-indexed line number where the function starts. May be
649
+ * kNoLineNumberInfo if no line number information is available.
650
+ */
651
+ int line_number;
652
+
653
+ /**
654
+ * 1-indexed column number where the function starts. May be
655
+ * kNoColumnNumberInfo if no line number information is available.
656
+ */
657
+ int column_number;
658
+
659
+ /**
660
+ * Unique id of the node.
661
+ */
662
+ uint32_t node_id;
663
+
664
+ /**
665
+ * List of callees called from this node for which we have sampled
666
+ * allocations. The lifetime of the children is scoped to the containing
667
+ * AllocationProfile.
668
+ */
669
+ std::vector<Node*> children;
670
+
671
+ /**
672
+ * List of self allocations done by this node in the call-graph.
673
+ */
674
+ std::vector<Allocation> allocations;
675
+ };
676
+
677
+ /**
678
+ * Represent a single sample recorded for an allocation.
679
+ */
680
+ struct Sample {
681
+ /**
682
+ * id of the node in the profile tree.
683
+ */
684
+ uint32_t node_id;
685
+
686
+ /**
687
+ * Size of the sampled allocation object.
688
+ */
689
+ size_t size;
690
+
691
+ /**
692
+ * The number of objects of such size that were sampled.
693
+ */
694
+ unsigned int count;
695
+
696
+ /**
697
+ * Unique time-ordered id of the allocation sample. Can be used to track
698
+ * what samples were added or removed between two snapshots.
699
+ */
700
+ uint64_t sample_id;
701
+ };
702
+
703
+ /**
704
+ * Returns the root node of the call-graph. The root node corresponds to an
705
+ * empty JS call-stack. The lifetime of the returned Node* is scoped to the
706
+ * containing AllocationProfile.
707
+ */
708
+ virtual Node* GetRootNode() = 0;
709
+ virtual const std::vector<Sample>& GetSamples() = 0;
710
+
711
+ virtual ~AllocationProfile() = default;
712
+
713
+ static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
714
+ static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
715
+ };
716
+
717
+ /**
718
+ * An object graph consisting of embedder objects and V8 objects.
719
+ * Edges of the graph are strong references between the objects.
720
+ * The embedder can build this graph during heap snapshot generation
721
+ * to include the embedder objects in the heap snapshot.
722
+ * Usage:
723
+ * 1) Define derived class of EmbedderGraph::Node for embedder objects.
724
+ * 2) Set the build embedder graph callback on the heap profiler using
725
+ * HeapProfiler::AddBuildEmbedderGraphCallback.
726
+ * 3) In the callback use graph->AddEdge(node1, node2) to add an edge from
727
+ * node1 to node2.
728
+ * 4) To represent references from/to V8 object, construct V8 nodes using
729
+ * graph->V8Node(value).
730
+ */
731
+ class V8_EXPORT EmbedderGraph {
732
+ public:
733
+ class Node {
734
+ public:
735
+ /**
736
+ * Detachedness specifies whether an object is attached or detached from the
737
+ * main application state. While unkown in general, there may be objects
738
+ * that specifically know their state. V8 passes this information along in
739
+ * the snapshot. Users of the snapshot may use it to annotate the object
740
+ * graph.
741
+ */
742
+ enum class Detachedness : uint8_t {
743
+ kUnknown = 0,
744
+ kAttached = 1,
745
+ kDetached = 2,
746
+ };
747
+
748
+ Node() = default;
749
+ virtual ~Node() = default;
750
+ virtual const char* Name() = 0;
751
+ virtual size_t SizeInBytes() = 0;
752
+ /**
753
+ * The corresponding V8 wrapper node if not null.
754
+ * During heap snapshot generation the embedder node and the V8 wrapper
755
+ * node will be merged into one node to simplify retaining paths.
756
+ */
757
+ virtual Node* WrapperNode() { return nullptr; }
758
+ virtual bool IsRootNode() { return false; }
759
+ /** Must return true for non-V8 nodes. */
760
+ virtual bool IsEmbedderNode() { return true; }
761
+ /**
762
+ * Optional name prefix. It is used in Chrome for tagging detached nodes.
763
+ */
764
+ virtual const char* NamePrefix() { return nullptr; }
765
+
766
+ /**
767
+ * Returns the NativeObject that can be used for querying the
768
+ * |HeapSnapshot|.
769
+ */
770
+ virtual NativeObject GetNativeObject() { return nullptr; }
771
+
772
+ /**
773
+ * Detachedness state of a given object. While unkown in general, there may
774
+ * be objects that specifically know their state. V8 passes this information
775
+ * along in the snapshot. Users of the snapshot may use it to annotate the
776
+ * object graph.
777
+ */
778
+ virtual Detachedness GetDetachedness() { return Detachedness::kUnknown; }
779
+
780
+ Node(const Node&) = delete;
781
+ Node& operator=(const Node&) = delete;
782
+ };
783
+
784
+ /**
785
+ * Returns a node corresponding to the given V8 value. Ownership is not
786
+ * transferred. The result pointer is valid while the graph is alive.
787
+ */
788
+ virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
789
+
790
+ /**
791
+ * Adds the given node to the graph and takes ownership of the node.
792
+ * Returns a raw pointer to the node that is valid while the graph is alive.
793
+ */
794
+ virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
795
+
796
+ /**
797
+ * Adds an edge that represents a strong reference from the given
798
+ * node |from| to the given node |to|. The nodes must be added to the graph
799
+ * before calling this function.
800
+ *
801
+ * If name is nullptr, the edge will have auto-increment indexes, otherwise
802
+ * it will be named accordingly.
803
+ */
804
+ virtual void AddEdge(Node* from, Node* to, const char* name = nullptr) = 0;
805
+
806
+ virtual ~EmbedderGraph() = default;
807
+ };
808
+
809
+ /**
810
+ * Interface for controlling heap profiling. Instance of the
811
+ * profiler can be retrieved using v8::Isolate::GetHeapProfiler.
812
+ */
813
+ class V8_EXPORT HeapProfiler {
814
+ public:
815
+ enum SamplingFlags {
816
+ kSamplingNoFlags = 0,
817
+ kSamplingForceGC = 1 << 0,
818
+ };
819
+
820
+ /**
821
+ * Callback function invoked during heap snapshot generation to retrieve
822
+ * the embedder object graph. The callback should use graph->AddEdge(..) to
823
+ * add references between the objects.
824
+ * The callback must not trigger garbage collection in V8.
825
+ */
826
+ typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
827
+ v8::EmbedderGraph* graph,
828
+ void* data);
829
+
830
+ /**
831
+ * Callback function invoked during heap snapshot generation to retrieve
832
+ * the detachedness state of an object referenced by a TracedReference.
833
+ *
834
+ * The callback takes Local<Value> as parameter to allow the embedder to
835
+ * unpack the TracedReference into a Local and reuse that Local for different
836
+ * purposes.
837
+ */
838
+ using GetDetachednessCallback = EmbedderGraph::Node::Detachedness (*)(
839
+ v8::Isolate* isolate, const v8::Local<v8::Value>& v8_value,
840
+ uint16_t class_id, void* data);
841
+
842
+ /** Returns the number of snapshots taken. */
843
+ int GetSnapshotCount();
844
+
845
+ /** Returns a snapshot by index. */
846
+ const HeapSnapshot* GetHeapSnapshot(int index);
847
+
848
+ /**
849
+ * Returns SnapshotObjectId for a heap object referenced by |value| if
850
+ * it has been seen by the heap profiler, kUnknownObjectId otherwise.
851
+ */
852
+ SnapshotObjectId GetObjectId(Local<Value> value);
853
+
854
+ /**
855
+ * Returns SnapshotObjectId for a native object referenced by |value| if it
856
+ * has been seen by the heap profiler, kUnknownObjectId otherwise.
857
+ */
858
+ SnapshotObjectId GetObjectId(NativeObject value);
859
+
860
+ /**
861
+ * Returns heap object with given SnapshotObjectId if the object is alive,
862
+ * otherwise empty handle is returned.
863
+ */
864
+ Local<Value> FindObjectById(SnapshotObjectId id);
865
+
866
+ /**
867
+ * Clears internal map from SnapshotObjectId to heap object. The new objects
868
+ * will not be added into it unless a heap snapshot is taken or heap object
869
+ * tracking is kicked off.
870
+ */
871
+ void ClearObjectIds();
872
+
873
+ /**
874
+ * A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
875
+ * it in case heap profiler cannot find id for the object passed as
876
+ * parameter. HeapSnapshot::GetNodeById will always return NULL for such id.
877
+ */
878
+ static const SnapshotObjectId kUnknownObjectId = 0;
879
+
880
+ /**
881
+ * Callback interface for retrieving user friendly names of global objects.
882
+ */
883
+ class ObjectNameResolver {
884
+ public:
885
+ /**
886
+ * Returns name to be used in the heap snapshot for given node. Returned
887
+ * string must stay alive until snapshot collection is completed.
888
+ */
889
+ virtual const char* GetName(Local<Object> object) = 0;
890
+
891
+ protected:
892
+ virtual ~ObjectNameResolver() = default;
893
+ };
894
+
895
+ /**
896
+ * Takes a heap snapshot and returns it.
897
+ */
898
+ const HeapSnapshot* TakeHeapSnapshot(
899
+ ActivityControl* control = nullptr,
900
+ ObjectNameResolver* global_object_name_resolver = nullptr,
901
+ bool treat_global_objects_as_roots = true);
902
+
903
+ /**
904
+ * Takes a heap snapshot and returns it.
905
+ */
906
+ const HeapSnapshot* TakeHeapSnapshotV8_92(
907
+ ActivityControl* control = nullptr,
908
+ ObjectNameResolver* global_object_name_resolver = nullptr,
909
+ bool treat_global_objects_as_roots = true,
910
+ bool capture_numeric_value = false);
911
+
912
+ /**
913
+ * Starts tracking of heap objects population statistics. After calling
914
+ * this method, all heap objects relocations done by the garbage collector
915
+ * are being registered.
916
+ *
917
+ * |track_allocations| parameter controls whether stack trace of each
918
+ * allocation in the heap will be recorded and reported as part of
919
+ * HeapSnapshot.
920
+ */
921
+ void StartTrackingHeapObjects(bool track_allocations = false);
922
+
923
+ /**
924
+ * Adds a new time interval entry to the aggregated statistics array. The
925
+ * time interval entry contains information on the current heap objects
926
+ * population size. The method also updates aggregated statistics and
927
+ * reports updates for all previous time intervals via the OutputStream
928
+ * object. Updates on each time interval are provided as a stream of the
929
+ * HeapStatsUpdate structure instances.
930
+ * If |timestamp_us| is supplied, timestamp of the new entry will be written
931
+ * into it. The return value of the function is the last seen heap object Id.
932
+ *
933
+ * StartTrackingHeapObjects must be called before the first call to this
934
+ * method.
935
+ */
936
+ SnapshotObjectId GetHeapStats(OutputStream* stream,
937
+ int64_t* timestamp_us = nullptr);
938
+
939
+ /**
940
+ * Stops tracking of heap objects population statistics, cleans up all
941
+ * collected data. StartHeapObjectsTracking must be called again prior to
942
+ * calling GetHeapStats next time.
943
+ */
944
+ void StopTrackingHeapObjects();
945
+
946
+ /**
947
+ * Starts gathering a sampling heap profile. A sampling heap profile is
948
+ * similar to tcmalloc's heap profiler and Go's mprof. It samples object
949
+ * allocations and builds an online 'sampling' heap profile. At any point in
950
+ * time, this profile is expected to be a representative sample of objects
951
+ * currently live in the system. Each sampled allocation includes the stack
952
+ * trace at the time of allocation, which makes this really useful for memory
953
+ * leak detection.
954
+ *
955
+ * This mechanism is intended to be cheap enough that it can be used in
956
+ * production with minimal performance overhead.
957
+ *
958
+ * Allocations are sampled using a randomized Poisson process. On average, one
959
+ * allocation will be sampled every |sample_interval| bytes allocated. The
960
+ * |stack_depth| parameter controls the maximum number of stack frames to be
961
+ * captured on each allocation.
962
+ *
963
+ * NOTE: This is a proof-of-concept at this point. Right now we only sample
964
+ * newspace allocations. Support for paged space allocation (e.g. pre-tenured
965
+ * objects, large objects, code objects, etc.) and native allocations
966
+ * doesn't exist yet, but is anticipated in the future.
967
+ *
968
+ * Objects allocated before the sampling is started will not be included in
969
+ * the profile.
970
+ *
971
+ * Returns false if a sampling heap profiler is already running.
972
+ */
973
+ bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
974
+ int stack_depth = 16,
975
+ SamplingFlags flags = kSamplingNoFlags);
976
+
977
+ /**
978
+ * Stops the sampling heap profile and discards the current profile.
979
+ */
980
+ void StopSamplingHeapProfiler();
981
+
982
+ /**
983
+ * Returns the sampled profile of allocations allocated (and still live) since
984
+ * StartSamplingHeapProfiler was called. The ownership of the pointer is
985
+ * transferred to the caller. Returns nullptr if sampling heap profiler is not
986
+ * active.
987
+ */
988
+ AllocationProfile* GetAllocationProfile();
989
+
990
+ /**
991
+ * Deletes all snapshots taken. All previously returned pointers to
992
+ * snapshots and their contents become invalid after this call.
993
+ */
994
+ void DeleteAllHeapSnapshots();
995
+
996
+ void AddBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
997
+ void* data);
998
+ void RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
999
+ void* data);
1000
+
1001
+ void SetGetDetachednessCallback(GetDetachednessCallback callback, void* data);
1002
+
1003
+ /**
1004
+ * Default value of persistent handle class ID. Must not be used to
1005
+ * define a class. Can be used to reset a class of a persistent
1006
+ * handle.
1007
+ */
1008
+ static const uint16_t kPersistentHandleNoClassId = 0;
1009
+
1010
+ private:
1011
+ HeapProfiler();
1012
+ ~HeapProfiler();
1013
+ HeapProfiler(const HeapProfiler&);
1014
+ HeapProfiler& operator=(const HeapProfiler&);
1015
+ };
1016
+
1017
+ /**
1018
+ * A struct for exporting HeapStats data from V8, using "push" model.
1019
+ * See HeapProfiler::GetHeapStats.
1020
+ */
1021
+ struct HeapStatsUpdate {
1022
+ HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
1023
+ : index(index), count(count), size(size) { }
1024
+ uint32_t index; // Index of the time interval that was changed.
1025
+ uint32_t count; // New value of count field for the interval with this index.
1026
+ uint32_t size; // New value of size field for the interval with this index.
1027
+ };
1028
+
1029
+ #define CODE_EVENTS_LIST(V) \
1030
+ V(Builtin) \
1031
+ V(Callback) \
1032
+ V(Eval) \
1033
+ V(Function) \
1034
+ V(InterpretedFunction) \
1035
+ V(Handler) \
1036
+ V(BytecodeHandler) \
1037
+ V(LazyCompile) \
1038
+ V(RegExp) \
1039
+ V(Script) \
1040
+ V(Stub) \
1041
+ V(Relocation)
1042
+
1043
+ /**
1044
+ * Note that this enum may be extended in the future. Please include a default
1045
+ * case if this enum is used in a switch statement.
1046
+ */
1047
+ enum CodeEventType {
1048
+ kUnknownType = 0
1049
+ #define V(Name) , k##Name##Type
1050
+ CODE_EVENTS_LIST(V)
1051
+ #undef V
1052
+ };
1053
+
1054
+ /**
1055
+ * Representation of a code creation event
1056
+ */
1057
+ class V8_EXPORT CodeEvent {
1058
+ public:
1059
+ uintptr_t GetCodeStartAddress();
1060
+ size_t GetCodeSize();
1061
+ Local<String> GetFunctionName();
1062
+ Local<String> GetScriptName();
1063
+ int GetScriptLine();
1064
+ int GetScriptColumn();
1065
+ /**
1066
+ * NOTE (mmarchini): We can't allocate objects in the heap when we collect
1067
+ * existing code, and both the code type and the comment are not stored in the
1068
+ * heap, so we return those as const char*.
1069
+ */
1070
+ CodeEventType GetCodeType();
1071
+ const char* GetComment();
1072
+
1073
+ static const char* GetCodeEventTypeName(CodeEventType code_event_type);
1074
+
1075
+ uintptr_t GetPreviousCodeStartAddress();
1076
+ };
1077
+
1078
+ /**
1079
+ * Interface to listen to code creation and code relocation events.
1080
+ */
1081
+ class V8_EXPORT CodeEventHandler {
1082
+ public:
1083
+ /**
1084
+ * Creates a new listener for the |isolate|. The isolate must be initialized.
1085
+ * The listener object must be disposed after use by calling |Dispose| method.
1086
+ * Multiple listeners can be created for the same isolate.
1087
+ */
1088
+ explicit CodeEventHandler(Isolate* isolate);
1089
+ virtual ~CodeEventHandler();
1090
+
1091
+ /**
1092
+ * Handle is called every time a code object is created or moved. Information
1093
+ * about each code event will be available through the `code_event`
1094
+ * parameter.
1095
+ *
1096
+ * When the CodeEventType is kRelocationType, the code for this CodeEvent has
1097
+ * moved from `GetPreviousCodeStartAddress()` to `GetCodeStartAddress()`.
1098
+ */
1099
+ virtual void Handle(CodeEvent* code_event) = 0;
1100
+
1101
+ /**
1102
+ * Call `Enable()` to starts listening to code creation and code relocation
1103
+ * events. These events will be handled by `Handle()`.
1104
+ */
1105
+ void Enable();
1106
+
1107
+ /**
1108
+ * Call `Disable()` to stop listening to code creation and code relocation
1109
+ * events.
1110
+ */
1111
+ void Disable();
1112
+
1113
+ private:
1114
+ CodeEventHandler();
1115
+ CodeEventHandler(const CodeEventHandler&);
1116
+ CodeEventHandler& operator=(const CodeEventHandler&);
1117
+ void* internal_listener_;
1118
+ };
1119
+
1120
+ } // namespace v8
1121
+
1122
+
1123
+ #endif // V8_V8_PROFILER_H_