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