11cb0ef41Sopenharmony_ci// Copyright 2010 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_V8_PROFILER_H_
61cb0ef41Sopenharmony_ci#define V8_V8_PROFILER_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <limits.h>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include <memory>
111cb0ef41Sopenharmony_ci#include <unordered_set>
121cb0ef41Sopenharmony_ci#include <vector>
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci#include "v8-local-handle.h"       // NOLINT(build/include_directory)
151cb0ef41Sopenharmony_ci#include "v8-message.h"            // NOLINT(build/include_directory)
161cb0ef41Sopenharmony_ci#include "v8-persistent-handle.h"  // NOLINT(build/include_directory)
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci/**
191cb0ef41Sopenharmony_ci * Profiler support for the V8 JavaScript engine.
201cb0ef41Sopenharmony_ci */
211cb0ef41Sopenharmony_cinamespace v8 {
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cienum class EmbedderStateTag : uint8_t;
241cb0ef41Sopenharmony_ciclass HeapGraphNode;
251cb0ef41Sopenharmony_cistruct HeapStatsUpdate;
261cb0ef41Sopenharmony_ciclass Object;
271cb0ef41Sopenharmony_cienum StateTag : uint16_t;
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciusing NativeObject = void*;
301cb0ef41Sopenharmony_ciusing SnapshotObjectId = uint32_t;
311cb0ef41Sopenharmony_ciusing ProfilerId = uint32_t;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_cistruct CpuProfileDeoptFrame {
341cb0ef41Sopenharmony_ci  int script_id;
351cb0ef41Sopenharmony_ci  size_t position;
361cb0ef41Sopenharmony_ci};
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cinamespace internal {
391cb0ef41Sopenharmony_ciclass CpuProfile;
401cb0ef41Sopenharmony_ci}  // namespace internal
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci}  // namespace v8
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci#ifdef V8_OS_WIN
451cb0ef41Sopenharmony_citemplate class V8_EXPORT std::vector<v8::CpuProfileDeoptFrame>;
461cb0ef41Sopenharmony_ci#endif
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cinamespace v8 {
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_cistruct V8_EXPORT CpuProfileDeoptInfo {
511cb0ef41Sopenharmony_ci  /** A pointer to a static string owned by v8. */
521cb0ef41Sopenharmony_ci  const char* deopt_reason;
531cb0ef41Sopenharmony_ci  std::vector<CpuProfileDeoptFrame> stack;
541cb0ef41Sopenharmony_ci};
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci}  // namespace v8
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci#ifdef V8_OS_WIN
591cb0ef41Sopenharmony_citemplate class V8_EXPORT std::vector<v8::CpuProfileDeoptInfo>;
601cb0ef41Sopenharmony_ci#endif
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cinamespace v8 {
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci/**
651cb0ef41Sopenharmony_ci * CpuProfileNode represents a node in a call graph.
661cb0ef41Sopenharmony_ci */
671cb0ef41Sopenharmony_ciclass V8_EXPORT CpuProfileNode {
681cb0ef41Sopenharmony_ci public:
691cb0ef41Sopenharmony_ci  struct LineTick {
701cb0ef41Sopenharmony_ci    /** The 1-based number of the source line where the function originates. */
711cb0ef41Sopenharmony_ci    int line;
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci    /** The count of samples associated with the source line. */
741cb0ef41Sopenharmony_ci    unsigned int hit_count;
751cb0ef41Sopenharmony_ci  };
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  // An annotation hinting at the source of a CpuProfileNode.
781cb0ef41Sopenharmony_ci  enum SourceType {
791cb0ef41Sopenharmony_ci    // User-supplied script with associated resource information.
801cb0ef41Sopenharmony_ci    kScript = 0,
811cb0ef41Sopenharmony_ci    // Native scripts and provided builtins.
821cb0ef41Sopenharmony_ci    kBuiltin = 1,
831cb0ef41Sopenharmony_ci    // Callbacks into native code.
841cb0ef41Sopenharmony_ci    kCallback = 2,
851cb0ef41Sopenharmony_ci    // VM-internal functions or state.
861cb0ef41Sopenharmony_ci    kInternal = 3,
871cb0ef41Sopenharmony_ci    // A node that failed to symbolize.
881cb0ef41Sopenharmony_ci    kUnresolved = 4,
891cb0ef41Sopenharmony_ci  };
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  /** Returns function name (empty string for anonymous functions.) */
921cb0ef41Sopenharmony_ci  Local<String> GetFunctionName() const;
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  /**
951cb0ef41Sopenharmony_ci   * Returns function name (empty string for anonymous functions.)
961cb0ef41Sopenharmony_ci   * The string ownership is *not* passed to the caller. It stays valid until
971cb0ef41Sopenharmony_ci   * profile is deleted. The function is thread safe.
981cb0ef41Sopenharmony_ci   */
991cb0ef41Sopenharmony_ci  const char* GetFunctionNameStr() const;
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  /** Returns id of the script where function is located. */
1021cb0ef41Sopenharmony_ci  int GetScriptId() const;
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  /** Returns resource name for script from where the function originates. */
1051cb0ef41Sopenharmony_ci  Local<String> GetScriptResourceName() const;
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  /**
1081cb0ef41Sopenharmony_ci   * Returns resource name for script from where the function originates.
1091cb0ef41Sopenharmony_ci   * The string ownership is *not* passed to the caller. It stays valid until
1101cb0ef41Sopenharmony_ci   * profile is deleted. The function is thread safe.
1111cb0ef41Sopenharmony_ci   */
1121cb0ef41Sopenharmony_ci  const char* GetScriptResourceNameStr() const;
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  /**
1151cb0ef41Sopenharmony_ci   * Return true if the script from where the function originates is flagged as
1161cb0ef41Sopenharmony_ci   * being shared cross-origin.
1171cb0ef41Sopenharmony_ci   */
1181cb0ef41Sopenharmony_ci  bool IsScriptSharedCrossOrigin() const;
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  /**
1211cb0ef41Sopenharmony_ci   * Returns the number, 1-based, of the line where the function originates.
1221cb0ef41Sopenharmony_ci   * kNoLineNumberInfo if no line number information is available.
1231cb0ef41Sopenharmony_ci   */
1241cb0ef41Sopenharmony_ci  int GetLineNumber() const;
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  /**
1271cb0ef41Sopenharmony_ci   * Returns 1-based number of the column where the function originates.
1281cb0ef41Sopenharmony_ci   * kNoColumnNumberInfo if no column number information is available.
1291cb0ef41Sopenharmony_ci   */
1301cb0ef41Sopenharmony_ci  int GetColumnNumber() const;
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  /**
1331cb0ef41Sopenharmony_ci   * Returns the number of the function's source lines that collect the samples.
1341cb0ef41Sopenharmony_ci   */
1351cb0ef41Sopenharmony_ci  unsigned int GetHitLineCount() const;
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci  /** Returns the set of source lines that collect the samples.
1381cb0ef41Sopenharmony_ci   *  The caller allocates buffer and responsible for releasing it.
1391cb0ef41Sopenharmony_ci   *  True if all available entries are copied, otherwise false.
1401cb0ef41Sopenharmony_ci   *  The function copies nothing if buffer is not large enough.
1411cb0ef41Sopenharmony_ci   */
1421cb0ef41Sopenharmony_ci  bool GetLineTicks(LineTick* entries, unsigned int length) const;
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  /** Returns bailout reason for the function
1451cb0ef41Sopenharmony_ci    * if the optimization was disabled for it.
1461cb0ef41Sopenharmony_ci    */
1471cb0ef41Sopenharmony_ci  const char* GetBailoutReason() const;
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci  /**
1501cb0ef41Sopenharmony_ci    * Returns the count of samples where the function was currently executing.
1511cb0ef41Sopenharmony_ci    */
1521cb0ef41Sopenharmony_ci  unsigned GetHitCount() const;
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci  /** Returns id of the node. The id is unique within the tree */
1551cb0ef41Sopenharmony_ci  unsigned GetNodeId() const;
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci  /**
1581cb0ef41Sopenharmony_ci   * Gets the type of the source which the node was captured from.
1591cb0ef41Sopenharmony_ci   */
1601cb0ef41Sopenharmony_ci  SourceType GetSourceType() const;
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci  /** Returns child nodes count of the node. */
1631cb0ef41Sopenharmony_ci  int GetChildrenCount() const;
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci  /** Retrieves a child node by index. */
1661cb0ef41Sopenharmony_ci  const CpuProfileNode* GetChild(int index) const;
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci  /** Retrieves the ancestor node, or null if the root. */
1691cb0ef41Sopenharmony_ci  const CpuProfileNode* GetParent() const;
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  /** Retrieves deopt infos for the node. */
1721cb0ef41Sopenharmony_ci  const std::vector<CpuProfileDeoptInfo>& GetDeoptInfos() const;
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
1751cb0ef41Sopenharmony_ci  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
1761cb0ef41Sopenharmony_ci};
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci/**
1791cb0ef41Sopenharmony_ci * An interface for exporting data from V8, using "push" model.
1801cb0ef41Sopenharmony_ci */
1811cb0ef41Sopenharmony_ciclass V8_EXPORT OutputStream {
1821cb0ef41Sopenharmony_ci public:
1831cb0ef41Sopenharmony_ci  enum WriteResult { kContinue = 0, kAbort = 1 };
1841cb0ef41Sopenharmony_ci  virtual ~OutputStream() = default;
1851cb0ef41Sopenharmony_ci  /** Notify about the end of stream. */
1861cb0ef41Sopenharmony_ci  virtual void EndOfStream() = 0;
1871cb0ef41Sopenharmony_ci  /** Get preferred output chunk size. Called only once. */
1881cb0ef41Sopenharmony_ci  virtual int GetChunkSize() { return 1024; }
1891cb0ef41Sopenharmony_ci  /**
1901cb0ef41Sopenharmony_ci   * Writes the next chunk of snapshot data into the stream. Writing
1911cb0ef41Sopenharmony_ci   * can be stopped by returning kAbort as function result. EndOfStream
1921cb0ef41Sopenharmony_ci   * will not be called in case writing was aborted.
1931cb0ef41Sopenharmony_ci   */
1941cb0ef41Sopenharmony_ci  virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
1951cb0ef41Sopenharmony_ci  /**
1961cb0ef41Sopenharmony_ci   * Writes the next chunk of heap stats data into the stream. Writing
1971cb0ef41Sopenharmony_ci   * can be stopped by returning kAbort as function result. EndOfStream
1981cb0ef41Sopenharmony_ci   * will not be called in case writing was aborted.
1991cb0ef41Sopenharmony_ci   */
2001cb0ef41Sopenharmony_ci  virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
2011cb0ef41Sopenharmony_ci    return kAbort;
2021cb0ef41Sopenharmony_ci  }
2031cb0ef41Sopenharmony_ci};
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci/**
2061cb0ef41Sopenharmony_ci * CpuProfile contains a CPU profile in a form of top-down call tree
2071cb0ef41Sopenharmony_ci * (from main() down to functions that do all the work).
2081cb0ef41Sopenharmony_ci */
2091cb0ef41Sopenharmony_ciclass V8_EXPORT CpuProfile {
2101cb0ef41Sopenharmony_ci public:
2111cb0ef41Sopenharmony_ci  enum SerializationFormat {
2121cb0ef41Sopenharmony_ci    kJSON = 0  // See format description near 'Serialize' method.
2131cb0ef41Sopenharmony_ci  };
2141cb0ef41Sopenharmony_ci  /** Returns CPU profile title. */
2151cb0ef41Sopenharmony_ci  Local<String> GetTitle() const;
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci  /** Returns the root node of the top down call tree. */
2181cb0ef41Sopenharmony_ci  const CpuProfileNode* GetTopDownRoot() const;
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci  /**
2211cb0ef41Sopenharmony_ci   * Returns number of samples recorded. The samples are not recorded unless
2221cb0ef41Sopenharmony_ci   * |record_samples| parameter of CpuProfiler::StartCpuProfiling is true.
2231cb0ef41Sopenharmony_ci   */
2241cb0ef41Sopenharmony_ci  int GetSamplesCount() const;
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci  /**
2271cb0ef41Sopenharmony_ci   * Returns profile node corresponding to the top frame the sample at
2281cb0ef41Sopenharmony_ci   * the given index.
2291cb0ef41Sopenharmony_ci   */
2301cb0ef41Sopenharmony_ci  const CpuProfileNode* GetSample(int index) const;
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci  /**
2331cb0ef41Sopenharmony_ci   * Returns the timestamp of the sample. The timestamp is the number of
2341cb0ef41Sopenharmony_ci   * microseconds since some unspecified starting point.
2351cb0ef41Sopenharmony_ci   * The point is equal to the starting point used by GetStartTime.
2361cb0ef41Sopenharmony_ci   */
2371cb0ef41Sopenharmony_ci  int64_t GetSampleTimestamp(int index) const;
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ci  /**
2401cb0ef41Sopenharmony_ci   * Returns time when the profile recording was started (in microseconds)
2411cb0ef41Sopenharmony_ci   * since some unspecified starting point.
2421cb0ef41Sopenharmony_ci   */
2431cb0ef41Sopenharmony_ci  int64_t GetStartTime() const;
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ci  /**
2461cb0ef41Sopenharmony_ci   * Returns state of the vm when sample was captured.
2471cb0ef41Sopenharmony_ci   */
2481cb0ef41Sopenharmony_ci  StateTag GetSampleState(int index) const;
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci  /**
2511cb0ef41Sopenharmony_ci   * Returns state of the embedder when sample was captured.
2521cb0ef41Sopenharmony_ci   */
2531cb0ef41Sopenharmony_ci  EmbedderStateTag GetSampleEmbedderState(int index) const;
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci  /**
2561cb0ef41Sopenharmony_ci   * Returns time when the profile recording was stopped (in microseconds)
2571cb0ef41Sopenharmony_ci   * since some unspecified starting point.
2581cb0ef41Sopenharmony_ci   * The point is equal to the starting point used by GetStartTime.
2591cb0ef41Sopenharmony_ci   */
2601cb0ef41Sopenharmony_ci  int64_t GetEndTime() const;
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_ci  /**
2631cb0ef41Sopenharmony_ci   * Deletes the profile and removes it from CpuProfiler's list.
2641cb0ef41Sopenharmony_ci   * All pointers to nodes previously returned become invalid.
2651cb0ef41Sopenharmony_ci   */
2661cb0ef41Sopenharmony_ci  void Delete();
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ci  /**
2691cb0ef41Sopenharmony_ci   * Prepare a serialized representation of the profile. The result
2701cb0ef41Sopenharmony_ci   * is written into the stream provided in chunks of specified size.
2711cb0ef41Sopenharmony_ci   *
2721cb0ef41Sopenharmony_ci   * For the JSON format, heap contents are represented as an object
2731cb0ef41Sopenharmony_ci   * with the following structure:
2741cb0ef41Sopenharmony_ci   *
2751cb0ef41Sopenharmony_ci   *  {
2761cb0ef41Sopenharmony_ci   *    nodes: [nodes array],
2771cb0ef41Sopenharmony_ci   *    startTime: number,
2781cb0ef41Sopenharmony_ci   *    endTime: number
2791cb0ef41Sopenharmony_ci   *    samples: [strings array]
2801cb0ef41Sopenharmony_ci   *    timeDeltas: [numbers array]
2811cb0ef41Sopenharmony_ci   *  }
2821cb0ef41Sopenharmony_ci   *
2831cb0ef41Sopenharmony_ci   */
2841cb0ef41Sopenharmony_ci  void Serialize(OutputStream* stream,
2851cb0ef41Sopenharmony_ci                 SerializationFormat format = kJSON) const;
2861cb0ef41Sopenharmony_ci};
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_cienum CpuProfilingMode {
2891cb0ef41Sopenharmony_ci  // In the resulting CpuProfile tree, intermediate nodes in a stack trace
2901cb0ef41Sopenharmony_ci  // (from the root to a leaf) will have line numbers that point to the start
2911cb0ef41Sopenharmony_ci  // line of the function, rather than the line of the callsite of the child.
2921cb0ef41Sopenharmony_ci  kLeafNodeLineNumbers,
2931cb0ef41Sopenharmony_ci  // In the resulting CpuProfile tree, nodes are separated based on the line
2941cb0ef41Sopenharmony_ci  // number of their callsite in their parent.
2951cb0ef41Sopenharmony_ci  kCallerLineNumbers,
2961cb0ef41Sopenharmony_ci};
2971cb0ef41Sopenharmony_ci
2981cb0ef41Sopenharmony_ci// Determines how names are derived for functions sampled.
2991cb0ef41Sopenharmony_cienum CpuProfilingNamingMode {
3001cb0ef41Sopenharmony_ci  // Use the immediate name of functions at compilation time.
3011cb0ef41Sopenharmony_ci  kStandardNaming,
3021cb0ef41Sopenharmony_ci  // Use more verbose naming for functions without names, inferred from scope
3031cb0ef41Sopenharmony_ci  // where possible.
3041cb0ef41Sopenharmony_ci  kDebugNaming,
3051cb0ef41Sopenharmony_ci};
3061cb0ef41Sopenharmony_ci
3071cb0ef41Sopenharmony_cienum CpuProfilingLoggingMode {
3081cb0ef41Sopenharmony_ci  // Enables logging when a profile is active, and disables logging when all
3091cb0ef41Sopenharmony_ci  // profiles are detached.
3101cb0ef41Sopenharmony_ci  kLazyLogging,
3111cb0ef41Sopenharmony_ci  // Enables logging for the lifetime of the CpuProfiler. Calls to
3121cb0ef41Sopenharmony_ci  // StartRecording are faster, at the expense of runtime overhead.
3131cb0ef41Sopenharmony_ci  kEagerLogging,
3141cb0ef41Sopenharmony_ci};
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_ci// Enum for returning profiling status. Once StartProfiling is called,
3171cb0ef41Sopenharmony_ci// we want to return to clients whether the profiling was able to start
3181cb0ef41Sopenharmony_ci// correctly, or return a descriptive error.
3191cb0ef41Sopenharmony_cienum class CpuProfilingStatus {
3201cb0ef41Sopenharmony_ci  kStarted,
3211cb0ef41Sopenharmony_ci  kAlreadyStarted,
3221cb0ef41Sopenharmony_ci  kErrorTooManyProfilers
3231cb0ef41Sopenharmony_ci};
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci/**
3261cb0ef41Sopenharmony_ci * Result from StartProfiling returning the Profiling Status, and
3271cb0ef41Sopenharmony_ci * id of the started profiler, or 0 if profiler is not started
3281cb0ef41Sopenharmony_ci */
3291cb0ef41Sopenharmony_cistruct CpuProfilingResult {
3301cb0ef41Sopenharmony_ci  const ProfilerId id;
3311cb0ef41Sopenharmony_ci  const CpuProfilingStatus status;
3321cb0ef41Sopenharmony_ci};
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ci/**
3351cb0ef41Sopenharmony_ci * Delegate for when max samples reached and samples are discarded.
3361cb0ef41Sopenharmony_ci */
3371cb0ef41Sopenharmony_ciclass V8_EXPORT DiscardedSamplesDelegate {
3381cb0ef41Sopenharmony_ci public:
3391cb0ef41Sopenharmony_ci  DiscardedSamplesDelegate() = default;
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ci  virtual ~DiscardedSamplesDelegate() = default;
3421cb0ef41Sopenharmony_ci  virtual void Notify() = 0;
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci  ProfilerId GetId() const { return profiler_id_; }
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci private:
3471cb0ef41Sopenharmony_ci  friend internal::CpuProfile;
3481cb0ef41Sopenharmony_ci
3491cb0ef41Sopenharmony_ci  void SetId(ProfilerId id) { profiler_id_ = id; }
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ci  ProfilerId profiler_id_;
3521cb0ef41Sopenharmony_ci};
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ci/**
3551cb0ef41Sopenharmony_ci * Optional profiling attributes.
3561cb0ef41Sopenharmony_ci */
3571cb0ef41Sopenharmony_ciclass V8_EXPORT CpuProfilingOptions {
3581cb0ef41Sopenharmony_ci public:
3591cb0ef41Sopenharmony_ci  // Indicates that the sample buffer size should not be explicitly limited.
3601cb0ef41Sopenharmony_ci  static const unsigned kNoSampleLimit = UINT_MAX;
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci  /**
3631cb0ef41Sopenharmony_ci   * \param mode Type of computation of stack frame line numbers.
3641cb0ef41Sopenharmony_ci   * \param max_samples The maximum number of samples that should be recorded by
3651cb0ef41Sopenharmony_ci   *                    the profiler. Samples obtained after this limit will be
3661cb0ef41Sopenharmony_ci   *                    discarded.
3671cb0ef41Sopenharmony_ci   * \param sampling_interval_us controls the profile-specific target
3681cb0ef41Sopenharmony_ci   *                             sampling interval. The provided sampling
3691cb0ef41Sopenharmony_ci   *                             interval will be snapped to the next lowest
3701cb0ef41Sopenharmony_ci   *                             non-zero multiple of the profiler's sampling
3711cb0ef41Sopenharmony_ci   *                             interval, set via SetSamplingInterval(). If
3721cb0ef41Sopenharmony_ci   *                             zero, the sampling interval will be equal to
3731cb0ef41Sopenharmony_ci   *                             the profiler's sampling interval.
3741cb0ef41Sopenharmony_ci   * \param filter_context If specified, profiles will only contain frames
3751cb0ef41Sopenharmony_ci   *                       using this context. Other frames will be elided.
3761cb0ef41Sopenharmony_ci   */
3771cb0ef41Sopenharmony_ci  CpuProfilingOptions(
3781cb0ef41Sopenharmony_ci      CpuProfilingMode mode = kLeafNodeLineNumbers,
3791cb0ef41Sopenharmony_ci      unsigned max_samples = kNoSampleLimit, int sampling_interval_us = 0,
3801cb0ef41Sopenharmony_ci      MaybeLocal<Context> filter_context = MaybeLocal<Context>());
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_ci  CpuProfilingOptions(CpuProfilingOptions&&) = default;
3831cb0ef41Sopenharmony_ci  CpuProfilingOptions& operator=(CpuProfilingOptions&&) = default;
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ci  CpuProfilingMode mode() const { return mode_; }
3861cb0ef41Sopenharmony_ci  unsigned max_samples() const { return max_samples_; }
3871cb0ef41Sopenharmony_ci  int sampling_interval_us() const { return sampling_interval_us_; }
3881cb0ef41Sopenharmony_ci
3891cb0ef41Sopenharmony_ci private:
3901cb0ef41Sopenharmony_ci  friend class internal::CpuProfile;
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci  bool has_filter_context() const { return !filter_context_.IsEmpty(); }
3931cb0ef41Sopenharmony_ci  void* raw_filter_context() const;
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ci  CpuProfilingMode mode_;
3961cb0ef41Sopenharmony_ci  unsigned max_samples_;
3971cb0ef41Sopenharmony_ci  int sampling_interval_us_;
3981cb0ef41Sopenharmony_ci  Global<Context> filter_context_;
3991cb0ef41Sopenharmony_ci};
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_ci/**
4021cb0ef41Sopenharmony_ci * Interface for controlling CPU profiling. Instance of the
4031cb0ef41Sopenharmony_ci * profiler can be created using v8::CpuProfiler::New method.
4041cb0ef41Sopenharmony_ci */
4051cb0ef41Sopenharmony_ciclass V8_EXPORT CpuProfiler {
4061cb0ef41Sopenharmony_ci public:
4071cb0ef41Sopenharmony_ci  /**
4081cb0ef41Sopenharmony_ci   * Creates a new CPU profiler for the |isolate|. The isolate must be
4091cb0ef41Sopenharmony_ci   * initialized. The profiler object must be disposed after use by calling
4101cb0ef41Sopenharmony_ci   * |Dispose| method.
4111cb0ef41Sopenharmony_ci   */
4121cb0ef41Sopenharmony_ci  static CpuProfiler* New(Isolate* isolate,
4131cb0ef41Sopenharmony_ci                          CpuProfilingNamingMode = kDebugNaming,
4141cb0ef41Sopenharmony_ci                          CpuProfilingLoggingMode = kLazyLogging);
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ci  /**
4171cb0ef41Sopenharmony_ci   * Synchronously collect current stack sample in all profilers attached to
4181cb0ef41Sopenharmony_ci   * the |isolate|. The call does not affect number of ticks recorded for
4191cb0ef41Sopenharmony_ci   * the current top node.
4201cb0ef41Sopenharmony_ci   */
4211cb0ef41Sopenharmony_ci  static void CollectSample(Isolate* isolate);
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_ci  /**
4241cb0ef41Sopenharmony_ci   * Disposes the CPU profiler object.
4251cb0ef41Sopenharmony_ci   */
4261cb0ef41Sopenharmony_ci  void Dispose();
4271cb0ef41Sopenharmony_ci
4281cb0ef41Sopenharmony_ci  /**
4291cb0ef41Sopenharmony_ci   * Changes default CPU profiler sampling interval to the specified number
4301cb0ef41Sopenharmony_ci   * of microseconds. Default interval is 1000us. This method must be called
4311cb0ef41Sopenharmony_ci   * when there are no profiles being recorded.
4321cb0ef41Sopenharmony_ci   */
4331cb0ef41Sopenharmony_ci  void SetSamplingInterval(int us);
4341cb0ef41Sopenharmony_ci
4351cb0ef41Sopenharmony_ci  /**
4361cb0ef41Sopenharmony_ci   * Sets whether or not the profiler should prioritize consistency of sample
4371cb0ef41Sopenharmony_ci   * periodicity on Windows. Disabling this can greatly reduce CPU usage, but
4381cb0ef41Sopenharmony_ci   * may result in greater variance in sample timings from the platform's
4391cb0ef41Sopenharmony_ci   * scheduler. Defaults to enabled. This method must be called when there are
4401cb0ef41Sopenharmony_ci   * no profiles being recorded.
4411cb0ef41Sopenharmony_ci   */
4421cb0ef41Sopenharmony_ci  void SetUsePreciseSampling(bool);
4431cb0ef41Sopenharmony_ci
4441cb0ef41Sopenharmony_ci  /**
4451cb0ef41Sopenharmony_ci   * Starts collecting a CPU profile. Several profiles may be collected at once.
4461cb0ef41Sopenharmony_ci   * Generates an anonymous profiler, without a String identifier.
4471cb0ef41Sopenharmony_ci   */
4481cb0ef41Sopenharmony_ci  CpuProfilingResult Start(
4491cb0ef41Sopenharmony_ci      CpuProfilingOptions options,
4501cb0ef41Sopenharmony_ci      std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
4511cb0ef41Sopenharmony_ci
4521cb0ef41Sopenharmony_ci  /**
4531cb0ef41Sopenharmony_ci   * Starts collecting a CPU profile. Title may be an empty string. Several
4541cb0ef41Sopenharmony_ci   * profiles may be collected at once. Attempts to start collecting several
4551cb0ef41Sopenharmony_ci   * profiles with the same title are silently ignored.
4561cb0ef41Sopenharmony_ci   */
4571cb0ef41Sopenharmony_ci  CpuProfilingResult Start(
4581cb0ef41Sopenharmony_ci      Local<String> title, CpuProfilingOptions options,
4591cb0ef41Sopenharmony_ci      std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
4601cb0ef41Sopenharmony_ci
4611cb0ef41Sopenharmony_ci  /**
4621cb0ef41Sopenharmony_ci   * Starts profiling with the same semantics as above, except with expanded
4631cb0ef41Sopenharmony_ci   * parameters.
4641cb0ef41Sopenharmony_ci   *
4651cb0ef41Sopenharmony_ci   * |record_samples| parameter controls whether individual samples should
4661cb0ef41Sopenharmony_ci   * be recorded in addition to the aggregated tree.
4671cb0ef41Sopenharmony_ci   *
4681cb0ef41Sopenharmony_ci   * |max_samples| controls the maximum number of samples that should be
4691cb0ef41Sopenharmony_ci   * recorded by the profiler. Samples obtained after this limit will be
4701cb0ef41Sopenharmony_ci   * discarded.
4711cb0ef41Sopenharmony_ci   */
4721cb0ef41Sopenharmony_ci  CpuProfilingResult Start(
4731cb0ef41Sopenharmony_ci      Local<String> title, CpuProfilingMode mode, bool record_samples = false,
4741cb0ef41Sopenharmony_ci      unsigned max_samples = CpuProfilingOptions::kNoSampleLimit);
4751cb0ef41Sopenharmony_ci
4761cb0ef41Sopenharmony_ci  /**
4771cb0ef41Sopenharmony_ci   * The same as StartProfiling above, but the CpuProfilingMode defaults to
4781cb0ef41Sopenharmony_ci   * kLeafNodeLineNumbers mode, which was the previous default behavior of the
4791cb0ef41Sopenharmony_ci   * profiler.
4801cb0ef41Sopenharmony_ci   */
4811cb0ef41Sopenharmony_ci  CpuProfilingResult Start(Local<String> title, bool record_samples = false);
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci  /**
4841cb0ef41Sopenharmony_ci   * Starts collecting a CPU profile. Title may be an empty string. Several
4851cb0ef41Sopenharmony_ci   * profiles may be collected at once. Attempts to start collecting several
4861cb0ef41Sopenharmony_ci   * profiles with the same title are silently ignored.
4871cb0ef41Sopenharmony_ci   */
4881cb0ef41Sopenharmony_ci  CpuProfilingStatus StartProfiling(
4891cb0ef41Sopenharmony_ci      Local<String> title, CpuProfilingOptions options,
4901cb0ef41Sopenharmony_ci      std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
4911cb0ef41Sopenharmony_ci
4921cb0ef41Sopenharmony_ci  /**
4931cb0ef41Sopenharmony_ci   * Starts profiling with the same semantics as above, except with expanded
4941cb0ef41Sopenharmony_ci   * parameters.
4951cb0ef41Sopenharmony_ci   *
4961cb0ef41Sopenharmony_ci   * |record_samples| parameter controls whether individual samples should
4971cb0ef41Sopenharmony_ci   * be recorded in addition to the aggregated tree.
4981cb0ef41Sopenharmony_ci   *
4991cb0ef41Sopenharmony_ci   * |max_samples| controls the maximum number of samples that should be
5001cb0ef41Sopenharmony_ci   * recorded by the profiler. Samples obtained after this limit will be
5011cb0ef41Sopenharmony_ci   * discarded.
5021cb0ef41Sopenharmony_ci   */
5031cb0ef41Sopenharmony_ci  CpuProfilingStatus StartProfiling(
5041cb0ef41Sopenharmony_ci      Local<String> title, CpuProfilingMode mode, bool record_samples = false,
5051cb0ef41Sopenharmony_ci      unsigned max_samples = CpuProfilingOptions::kNoSampleLimit);
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ci  /**
5081cb0ef41Sopenharmony_ci   * The same as StartProfiling above, but the CpuProfilingMode defaults to
5091cb0ef41Sopenharmony_ci   * kLeafNodeLineNumbers mode, which was the previous default behavior of the
5101cb0ef41Sopenharmony_ci   * profiler.
5111cb0ef41Sopenharmony_ci   */
5121cb0ef41Sopenharmony_ci  CpuProfilingStatus StartProfiling(Local<String> title,
5131cb0ef41Sopenharmony_ci                                    bool record_samples = false);
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ci  /**
5161cb0ef41Sopenharmony_ci   * Stops collecting CPU profile with a given id and returns it.
5171cb0ef41Sopenharmony_ci   */
5181cb0ef41Sopenharmony_ci  CpuProfile* Stop(ProfilerId id);
5191cb0ef41Sopenharmony_ci
5201cb0ef41Sopenharmony_ci  /**
5211cb0ef41Sopenharmony_ci   * Stops collecting CPU profile with a given title and returns it.
5221cb0ef41Sopenharmony_ci   * If the title given is empty, finishes the last profile started.
5231cb0ef41Sopenharmony_ci   */
5241cb0ef41Sopenharmony_ci  CpuProfile* StopProfiling(Local<String> title);
5251cb0ef41Sopenharmony_ci
5261cb0ef41Sopenharmony_ci  /**
5271cb0ef41Sopenharmony_ci   * Generate more detailed source positions to code objects. This results in
5281cb0ef41Sopenharmony_ci   * better results when mapping profiling samples to script source.
5291cb0ef41Sopenharmony_ci   */
5301cb0ef41Sopenharmony_ci  static void UseDetailedSourcePositionsForProfiling(Isolate* isolate);
5311cb0ef41Sopenharmony_ci
5321cb0ef41Sopenharmony_ci private:
5331cb0ef41Sopenharmony_ci  CpuProfiler();
5341cb0ef41Sopenharmony_ci  ~CpuProfiler();
5351cb0ef41Sopenharmony_ci  CpuProfiler(const CpuProfiler&);
5361cb0ef41Sopenharmony_ci  CpuProfiler& operator=(const CpuProfiler&);
5371cb0ef41Sopenharmony_ci};
5381cb0ef41Sopenharmony_ci
5391cb0ef41Sopenharmony_ci/**
5401cb0ef41Sopenharmony_ci * HeapSnapshotEdge represents a directed connection between heap
5411cb0ef41Sopenharmony_ci * graph nodes: from retainers to retained nodes.
5421cb0ef41Sopenharmony_ci */
5431cb0ef41Sopenharmony_ciclass V8_EXPORT HeapGraphEdge {
5441cb0ef41Sopenharmony_ci public:
5451cb0ef41Sopenharmony_ci  enum Type {
5461cb0ef41Sopenharmony_ci    kContextVariable = 0,  // A variable from a function context.
5471cb0ef41Sopenharmony_ci    kElement = 1,          // An element of an array.
5481cb0ef41Sopenharmony_ci    kProperty = 2,         // A named object property.
5491cb0ef41Sopenharmony_ci    kInternal = 3,         // A link that can't be accessed from JS,
5501cb0ef41Sopenharmony_ci                           // thus, its name isn't a real property name
5511cb0ef41Sopenharmony_ci                           // (e.g. parts of a ConsString).
5521cb0ef41Sopenharmony_ci    kHidden = 4,           // A link that is needed for proper sizes
5531cb0ef41Sopenharmony_ci                           // calculation, but may be hidden from user.
5541cb0ef41Sopenharmony_ci    kShortcut = 5,         // A link that must not be followed during
5551cb0ef41Sopenharmony_ci                           // sizes calculation.
5561cb0ef41Sopenharmony_ci    kWeak = 6              // A weak reference (ignored by the GC).
5571cb0ef41Sopenharmony_ci  };
5581cb0ef41Sopenharmony_ci
5591cb0ef41Sopenharmony_ci  /** Returns edge type (see HeapGraphEdge::Type). */
5601cb0ef41Sopenharmony_ci  Type GetType() const;
5611cb0ef41Sopenharmony_ci
5621cb0ef41Sopenharmony_ci  /**
5631cb0ef41Sopenharmony_ci   * Returns edge name. This can be a variable name, an element index, or
5641cb0ef41Sopenharmony_ci   * a property name.
5651cb0ef41Sopenharmony_ci   */
5661cb0ef41Sopenharmony_ci  Local<Value> GetName() const;
5671cb0ef41Sopenharmony_ci
5681cb0ef41Sopenharmony_ci  /** Returns origin node. */
5691cb0ef41Sopenharmony_ci  const HeapGraphNode* GetFromNode() const;
5701cb0ef41Sopenharmony_ci
5711cb0ef41Sopenharmony_ci  /** Returns destination node. */
5721cb0ef41Sopenharmony_ci  const HeapGraphNode* GetToNode() const;
5731cb0ef41Sopenharmony_ci};
5741cb0ef41Sopenharmony_ci
5751cb0ef41Sopenharmony_ci
5761cb0ef41Sopenharmony_ci/**
5771cb0ef41Sopenharmony_ci * HeapGraphNode represents a node in a heap graph.
5781cb0ef41Sopenharmony_ci */
5791cb0ef41Sopenharmony_ciclass V8_EXPORT HeapGraphNode {
5801cb0ef41Sopenharmony_ci public:
5811cb0ef41Sopenharmony_ci  enum Type {
5821cb0ef41Sopenharmony_ci    kHidden = 0,         // Hidden node, may be filtered when shown to user.
5831cb0ef41Sopenharmony_ci    kArray = 1,          // An array of elements.
5841cb0ef41Sopenharmony_ci    kString = 2,         // A string.
5851cb0ef41Sopenharmony_ci    kObject = 3,         // A JS object (except for arrays and strings).
5861cb0ef41Sopenharmony_ci    kCode = 4,           // Compiled code.
5871cb0ef41Sopenharmony_ci    kClosure = 5,        // Function closure.
5881cb0ef41Sopenharmony_ci    kRegExp = 6,         // RegExp.
5891cb0ef41Sopenharmony_ci    kHeapNumber = 7,     // Number stored in the heap.
5901cb0ef41Sopenharmony_ci    kNative = 8,         // Native object (not from V8 heap).
5911cb0ef41Sopenharmony_ci    kSynthetic = 9,      // Synthetic object, usually used for grouping
5921cb0ef41Sopenharmony_ci                         // snapshot items together.
5931cb0ef41Sopenharmony_ci    kConsString = 10,    // Concatenated string. A pair of pointers to strings.
5941cb0ef41Sopenharmony_ci    kSlicedString = 11,  // Sliced string. A fragment of another string.
5951cb0ef41Sopenharmony_ci    kSymbol = 12,        // A Symbol (ES6).
5961cb0ef41Sopenharmony_ci    kBigInt = 13,        // BigInt.
5971cb0ef41Sopenharmony_ci    kObjectShape = 14,   // Internal data used for tracking the shapes (or
5981cb0ef41Sopenharmony_ci                         // "hidden classes") of JS objects.
5991cb0ef41Sopenharmony_ci    kWasmObject = 15,    // A WasmGC struct or array.
6001cb0ef41Sopenharmony_ci  };
6011cb0ef41Sopenharmony_ci
6021cb0ef41Sopenharmony_ci  /** Returns node type (see HeapGraphNode::Type). */
6031cb0ef41Sopenharmony_ci  Type GetType() const;
6041cb0ef41Sopenharmony_ci
6051cb0ef41Sopenharmony_ci  /**
6061cb0ef41Sopenharmony_ci   * Returns node name. Depending on node's type this can be the name
6071cb0ef41Sopenharmony_ci   * of the constructor (for objects), the name of the function (for
6081cb0ef41Sopenharmony_ci   * closures), string value, or an empty string (for compiled code).
6091cb0ef41Sopenharmony_ci   */
6101cb0ef41Sopenharmony_ci  Local<String> GetName() const;
6111cb0ef41Sopenharmony_ci
6121cb0ef41Sopenharmony_ci  /**
6131cb0ef41Sopenharmony_ci   * Returns node id. For the same heap object, the id remains the same
6141cb0ef41Sopenharmony_ci   * across all snapshots.
6151cb0ef41Sopenharmony_ci   */
6161cb0ef41Sopenharmony_ci  SnapshotObjectId GetId() const;
6171cb0ef41Sopenharmony_ci
6181cb0ef41Sopenharmony_ci  /** Returns node's own size, in bytes. */
6191cb0ef41Sopenharmony_ci  size_t GetShallowSize() const;
6201cb0ef41Sopenharmony_ci
6211cb0ef41Sopenharmony_ci  /** Returns child nodes count of the node. */
6221cb0ef41Sopenharmony_ci  int GetChildrenCount() const;
6231cb0ef41Sopenharmony_ci
6241cb0ef41Sopenharmony_ci  /** Retrieves a child by index. */
6251cb0ef41Sopenharmony_ci  const HeapGraphEdge* GetChild(int index) const;
6261cb0ef41Sopenharmony_ci};
6271cb0ef41Sopenharmony_ci
6281cb0ef41Sopenharmony_ci/**
6291cb0ef41Sopenharmony_ci * HeapSnapshots record the state of the JS heap at some moment.
6301cb0ef41Sopenharmony_ci */
6311cb0ef41Sopenharmony_ciclass V8_EXPORT HeapSnapshot {
6321cb0ef41Sopenharmony_ci public:
6331cb0ef41Sopenharmony_ci  enum SerializationFormat {
6341cb0ef41Sopenharmony_ci    kJSON = 0  // See format description near 'Serialize' method.
6351cb0ef41Sopenharmony_ci  };
6361cb0ef41Sopenharmony_ci
6371cb0ef41Sopenharmony_ci  /** Returns the root node of the heap graph. */
6381cb0ef41Sopenharmony_ci  const HeapGraphNode* GetRoot() const;
6391cb0ef41Sopenharmony_ci
6401cb0ef41Sopenharmony_ci  /** Returns a node by its id. */
6411cb0ef41Sopenharmony_ci  const HeapGraphNode* GetNodeById(SnapshotObjectId id) const;
6421cb0ef41Sopenharmony_ci
6431cb0ef41Sopenharmony_ci  /** Returns total nodes count in the snapshot. */
6441cb0ef41Sopenharmony_ci  int GetNodesCount() const;
6451cb0ef41Sopenharmony_ci
6461cb0ef41Sopenharmony_ci  /** Returns a node by index. */
6471cb0ef41Sopenharmony_ci  const HeapGraphNode* GetNode(int index) const;
6481cb0ef41Sopenharmony_ci
6491cb0ef41Sopenharmony_ci  /** Returns a max seen JS object Id. */
6501cb0ef41Sopenharmony_ci  SnapshotObjectId GetMaxSnapshotJSObjectId() const;
6511cb0ef41Sopenharmony_ci
6521cb0ef41Sopenharmony_ci  /**
6531cb0ef41Sopenharmony_ci   * Deletes the snapshot and removes it from HeapProfiler's list.
6541cb0ef41Sopenharmony_ci   * All pointers to nodes, edges and paths previously returned become
6551cb0ef41Sopenharmony_ci   * invalid.
6561cb0ef41Sopenharmony_ci   */
6571cb0ef41Sopenharmony_ci  void Delete();
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ci  /**
6601cb0ef41Sopenharmony_ci   * Prepare a serialized representation of the snapshot. The result
6611cb0ef41Sopenharmony_ci   * is written into the stream provided in chunks of specified size.
6621cb0ef41Sopenharmony_ci   * The total length of the serialized snapshot is unknown in
6631cb0ef41Sopenharmony_ci   * advance, it can be roughly equal to JS heap size (that means,
6641cb0ef41Sopenharmony_ci   * it can be really big - tens of megabytes).
6651cb0ef41Sopenharmony_ci   *
6661cb0ef41Sopenharmony_ci   * For the JSON format, heap contents are represented as an object
6671cb0ef41Sopenharmony_ci   * with the following structure:
6681cb0ef41Sopenharmony_ci   *
6691cb0ef41Sopenharmony_ci   *  {
6701cb0ef41Sopenharmony_ci   *    snapshot: {
6711cb0ef41Sopenharmony_ci   *      title: "...",
6721cb0ef41Sopenharmony_ci   *      uid: nnn,
6731cb0ef41Sopenharmony_ci   *      meta: { meta-info },
6741cb0ef41Sopenharmony_ci   *      node_count: nnn,
6751cb0ef41Sopenharmony_ci   *      edge_count: nnn
6761cb0ef41Sopenharmony_ci   *    },
6771cb0ef41Sopenharmony_ci   *    nodes: [nodes array],
6781cb0ef41Sopenharmony_ci   *    edges: [edges array],
6791cb0ef41Sopenharmony_ci   *    strings: [strings array]
6801cb0ef41Sopenharmony_ci   *  }
6811cb0ef41Sopenharmony_ci   *
6821cb0ef41Sopenharmony_ci   * Nodes reference strings, other nodes, and edges by their indexes
6831cb0ef41Sopenharmony_ci   * in corresponding arrays.
6841cb0ef41Sopenharmony_ci   */
6851cb0ef41Sopenharmony_ci  void Serialize(OutputStream* stream,
6861cb0ef41Sopenharmony_ci                 SerializationFormat format = kJSON) const;
6871cb0ef41Sopenharmony_ci};
6881cb0ef41Sopenharmony_ci
6891cb0ef41Sopenharmony_ci
6901cb0ef41Sopenharmony_ci/**
6911cb0ef41Sopenharmony_ci * An interface for reporting progress and controlling long-running
6921cb0ef41Sopenharmony_ci * activities.
6931cb0ef41Sopenharmony_ci */
6941cb0ef41Sopenharmony_ciclass V8_EXPORT ActivityControl {
6951cb0ef41Sopenharmony_ci public:
6961cb0ef41Sopenharmony_ci  enum ControlOption {
6971cb0ef41Sopenharmony_ci    kContinue = 0,
6981cb0ef41Sopenharmony_ci    kAbort = 1
6991cb0ef41Sopenharmony_ci  };
7001cb0ef41Sopenharmony_ci  virtual ~ActivityControl() = default;
7011cb0ef41Sopenharmony_ci  /**
7021cb0ef41Sopenharmony_ci   * Notify about current progress. The activity can be stopped by
7031cb0ef41Sopenharmony_ci   * returning kAbort as the callback result.
7041cb0ef41Sopenharmony_ci   */
7051cb0ef41Sopenharmony_ci  virtual ControlOption ReportProgressValue(uint32_t done, uint32_t total) = 0;
7061cb0ef41Sopenharmony_ci};
7071cb0ef41Sopenharmony_ci
7081cb0ef41Sopenharmony_ci/**
7091cb0ef41Sopenharmony_ci * AllocationProfile is a sampled profile of allocations done by the program.
7101cb0ef41Sopenharmony_ci * This is structured as a call-graph.
7111cb0ef41Sopenharmony_ci */
7121cb0ef41Sopenharmony_ciclass V8_EXPORT AllocationProfile {
7131cb0ef41Sopenharmony_ci public:
7141cb0ef41Sopenharmony_ci  struct Allocation {
7151cb0ef41Sopenharmony_ci    /**
7161cb0ef41Sopenharmony_ci     * Size of the sampled allocation object.
7171cb0ef41Sopenharmony_ci     */
7181cb0ef41Sopenharmony_ci    size_t size;
7191cb0ef41Sopenharmony_ci
7201cb0ef41Sopenharmony_ci    /**
7211cb0ef41Sopenharmony_ci     * The number of objects of such size that were sampled.
7221cb0ef41Sopenharmony_ci     */
7231cb0ef41Sopenharmony_ci    unsigned int count;
7241cb0ef41Sopenharmony_ci  };
7251cb0ef41Sopenharmony_ci
7261cb0ef41Sopenharmony_ci  /**
7271cb0ef41Sopenharmony_ci   * Represents a node in the call-graph.
7281cb0ef41Sopenharmony_ci   */
7291cb0ef41Sopenharmony_ci  struct Node {
7301cb0ef41Sopenharmony_ci    /**
7311cb0ef41Sopenharmony_ci     * Name of the function. May be empty for anonymous functions or if the
7321cb0ef41Sopenharmony_ci     * script corresponding to this function has been unloaded.
7331cb0ef41Sopenharmony_ci     */
7341cb0ef41Sopenharmony_ci    Local<String> name;
7351cb0ef41Sopenharmony_ci
7361cb0ef41Sopenharmony_ci    /**
7371cb0ef41Sopenharmony_ci     * Name of the script containing the function. May be empty if the script
7381cb0ef41Sopenharmony_ci     * name is not available, or if the script has been unloaded.
7391cb0ef41Sopenharmony_ci     */
7401cb0ef41Sopenharmony_ci    Local<String> script_name;
7411cb0ef41Sopenharmony_ci
7421cb0ef41Sopenharmony_ci    /**
7431cb0ef41Sopenharmony_ci     * id of the script where the function is located. May be equal to
7441cb0ef41Sopenharmony_ci     * v8::UnboundScript::kNoScriptId in cases where the script doesn't exist.
7451cb0ef41Sopenharmony_ci     */
7461cb0ef41Sopenharmony_ci    int script_id;
7471cb0ef41Sopenharmony_ci
7481cb0ef41Sopenharmony_ci    /**
7491cb0ef41Sopenharmony_ci     * Start position of the function in the script.
7501cb0ef41Sopenharmony_ci     */
7511cb0ef41Sopenharmony_ci    int start_position;
7521cb0ef41Sopenharmony_ci
7531cb0ef41Sopenharmony_ci    /**
7541cb0ef41Sopenharmony_ci     * 1-indexed line number where the function starts. May be
7551cb0ef41Sopenharmony_ci     * kNoLineNumberInfo if no line number information is available.
7561cb0ef41Sopenharmony_ci     */
7571cb0ef41Sopenharmony_ci    int line_number;
7581cb0ef41Sopenharmony_ci
7591cb0ef41Sopenharmony_ci    /**
7601cb0ef41Sopenharmony_ci     * 1-indexed column number where the function starts. May be
7611cb0ef41Sopenharmony_ci     * kNoColumnNumberInfo if no line number information is available.
7621cb0ef41Sopenharmony_ci     */
7631cb0ef41Sopenharmony_ci    int column_number;
7641cb0ef41Sopenharmony_ci
7651cb0ef41Sopenharmony_ci    /**
7661cb0ef41Sopenharmony_ci     * Unique id of the node.
7671cb0ef41Sopenharmony_ci     */
7681cb0ef41Sopenharmony_ci    uint32_t node_id;
7691cb0ef41Sopenharmony_ci
7701cb0ef41Sopenharmony_ci    /**
7711cb0ef41Sopenharmony_ci     * List of callees called from this node for which we have sampled
7721cb0ef41Sopenharmony_ci     * allocations. The lifetime of the children is scoped to the containing
7731cb0ef41Sopenharmony_ci     * AllocationProfile.
7741cb0ef41Sopenharmony_ci     */
7751cb0ef41Sopenharmony_ci    std::vector<Node*> children;
7761cb0ef41Sopenharmony_ci
7771cb0ef41Sopenharmony_ci    /**
7781cb0ef41Sopenharmony_ci     * List of self allocations done by this node in the call-graph.
7791cb0ef41Sopenharmony_ci     */
7801cb0ef41Sopenharmony_ci    std::vector<Allocation> allocations;
7811cb0ef41Sopenharmony_ci  };
7821cb0ef41Sopenharmony_ci
7831cb0ef41Sopenharmony_ci  /**
7841cb0ef41Sopenharmony_ci   * Represent a single sample recorded for an allocation.
7851cb0ef41Sopenharmony_ci   */
7861cb0ef41Sopenharmony_ci  struct Sample {
7871cb0ef41Sopenharmony_ci    /**
7881cb0ef41Sopenharmony_ci     * id of the node in the profile tree.
7891cb0ef41Sopenharmony_ci     */
7901cb0ef41Sopenharmony_ci    uint32_t node_id;
7911cb0ef41Sopenharmony_ci
7921cb0ef41Sopenharmony_ci    /**
7931cb0ef41Sopenharmony_ci     * Size of the sampled allocation object.
7941cb0ef41Sopenharmony_ci     */
7951cb0ef41Sopenharmony_ci    size_t size;
7961cb0ef41Sopenharmony_ci
7971cb0ef41Sopenharmony_ci    /**
7981cb0ef41Sopenharmony_ci     * The number of objects of such size that were sampled.
7991cb0ef41Sopenharmony_ci     */
8001cb0ef41Sopenharmony_ci    unsigned int count;
8011cb0ef41Sopenharmony_ci
8021cb0ef41Sopenharmony_ci    /**
8031cb0ef41Sopenharmony_ci     * Unique time-ordered id of the allocation sample. Can be used to track
8041cb0ef41Sopenharmony_ci     * what samples were added or removed between two snapshots.
8051cb0ef41Sopenharmony_ci     */
8061cb0ef41Sopenharmony_ci    uint64_t sample_id;
8071cb0ef41Sopenharmony_ci  };
8081cb0ef41Sopenharmony_ci
8091cb0ef41Sopenharmony_ci  /**
8101cb0ef41Sopenharmony_ci   * Returns the root node of the call-graph. The root node corresponds to an
8111cb0ef41Sopenharmony_ci   * empty JS call-stack. The lifetime of the returned Node* is scoped to the
8121cb0ef41Sopenharmony_ci   * containing AllocationProfile.
8131cb0ef41Sopenharmony_ci   */
8141cb0ef41Sopenharmony_ci  virtual Node* GetRootNode() = 0;
8151cb0ef41Sopenharmony_ci  virtual const std::vector<Sample>& GetSamples() = 0;
8161cb0ef41Sopenharmony_ci
8171cb0ef41Sopenharmony_ci  virtual ~AllocationProfile() = default;
8181cb0ef41Sopenharmony_ci
8191cb0ef41Sopenharmony_ci  static const int kNoLineNumberInfo = Message::kNoLineNumberInfo;
8201cb0ef41Sopenharmony_ci  static const int kNoColumnNumberInfo = Message::kNoColumnInfo;
8211cb0ef41Sopenharmony_ci};
8221cb0ef41Sopenharmony_ci
8231cb0ef41Sopenharmony_ci/**
8241cb0ef41Sopenharmony_ci * An object graph consisting of embedder objects and V8 objects.
8251cb0ef41Sopenharmony_ci * Edges of the graph are strong references between the objects.
8261cb0ef41Sopenharmony_ci * The embedder can build this graph during heap snapshot generation
8271cb0ef41Sopenharmony_ci * to include the embedder objects in the heap snapshot.
8281cb0ef41Sopenharmony_ci * Usage:
8291cb0ef41Sopenharmony_ci * 1) Define derived class of EmbedderGraph::Node for embedder objects.
8301cb0ef41Sopenharmony_ci * 2) Set the build embedder graph callback on the heap profiler using
8311cb0ef41Sopenharmony_ci *    HeapProfiler::AddBuildEmbedderGraphCallback.
8321cb0ef41Sopenharmony_ci * 3) In the callback use graph->AddEdge(node1, node2) to add an edge from
8331cb0ef41Sopenharmony_ci *    node1 to node2.
8341cb0ef41Sopenharmony_ci * 4) To represent references from/to V8 object, construct V8 nodes using
8351cb0ef41Sopenharmony_ci *    graph->V8Node(value).
8361cb0ef41Sopenharmony_ci */
8371cb0ef41Sopenharmony_ciclass V8_EXPORT EmbedderGraph {
8381cb0ef41Sopenharmony_ci public:
8391cb0ef41Sopenharmony_ci  class Node {
8401cb0ef41Sopenharmony_ci   public:
8411cb0ef41Sopenharmony_ci    /**
8421cb0ef41Sopenharmony_ci     * Detachedness specifies whether an object is attached or detached from the
8431cb0ef41Sopenharmony_ci     * main application state. While unkown in general, there may be objects
8441cb0ef41Sopenharmony_ci     * that specifically know their state. V8 passes this information along in
8451cb0ef41Sopenharmony_ci     * the snapshot. Users of the snapshot may use it to annotate the object
8461cb0ef41Sopenharmony_ci     * graph.
8471cb0ef41Sopenharmony_ci     */
8481cb0ef41Sopenharmony_ci    enum class Detachedness : uint8_t {
8491cb0ef41Sopenharmony_ci      kUnknown = 0,
8501cb0ef41Sopenharmony_ci      kAttached = 1,
8511cb0ef41Sopenharmony_ci      kDetached = 2,
8521cb0ef41Sopenharmony_ci    };
8531cb0ef41Sopenharmony_ci
8541cb0ef41Sopenharmony_ci    Node() = default;
8551cb0ef41Sopenharmony_ci    virtual ~Node() = default;
8561cb0ef41Sopenharmony_ci    virtual const char* Name() = 0;
8571cb0ef41Sopenharmony_ci    virtual size_t SizeInBytes() = 0;
8581cb0ef41Sopenharmony_ci    /**
8591cb0ef41Sopenharmony_ci     * The corresponding V8 wrapper node if not null.
8601cb0ef41Sopenharmony_ci     * During heap snapshot generation the embedder node and the V8 wrapper
8611cb0ef41Sopenharmony_ci     * node will be merged into one node to simplify retaining paths.
8621cb0ef41Sopenharmony_ci     */
8631cb0ef41Sopenharmony_ci    virtual Node* WrapperNode() { return nullptr; }
8641cb0ef41Sopenharmony_ci    virtual bool IsRootNode() { return false; }
8651cb0ef41Sopenharmony_ci    /** Must return true for non-V8 nodes. */
8661cb0ef41Sopenharmony_ci    virtual bool IsEmbedderNode() { return true; }
8671cb0ef41Sopenharmony_ci    /**
8681cb0ef41Sopenharmony_ci     * Optional name prefix. It is used in Chrome for tagging detached nodes.
8691cb0ef41Sopenharmony_ci     */
8701cb0ef41Sopenharmony_ci    virtual const char* NamePrefix() { return nullptr; }
8711cb0ef41Sopenharmony_ci
8721cb0ef41Sopenharmony_ci    /**
8731cb0ef41Sopenharmony_ci     * Returns the NativeObject that can be used for querying the
8741cb0ef41Sopenharmony_ci     * |HeapSnapshot|.
8751cb0ef41Sopenharmony_ci     */
8761cb0ef41Sopenharmony_ci    virtual NativeObject GetNativeObject() { return nullptr; }
8771cb0ef41Sopenharmony_ci
8781cb0ef41Sopenharmony_ci    /**
8791cb0ef41Sopenharmony_ci     * Detachedness state of a given object. While unkown in general, there may
8801cb0ef41Sopenharmony_ci     * be objects that specifically know their state. V8 passes this information
8811cb0ef41Sopenharmony_ci     * along in the snapshot. Users of the snapshot may use it to annotate the
8821cb0ef41Sopenharmony_ci     * object graph.
8831cb0ef41Sopenharmony_ci     */
8841cb0ef41Sopenharmony_ci    virtual Detachedness GetDetachedness() { return Detachedness::kUnknown; }
8851cb0ef41Sopenharmony_ci
8861cb0ef41Sopenharmony_ci    /**
8871cb0ef41Sopenharmony_ci     * Returns the address of the object in the embedder heap, or nullptr to not
8881cb0ef41Sopenharmony_ci     * specify the address. If this address is provided, then V8 can generate
8891cb0ef41Sopenharmony_ci     * consistent IDs for objects across subsequent heap snapshots, which allows
8901cb0ef41Sopenharmony_ci     * devtools to determine which objects were retained from one snapshot to
8911cb0ef41Sopenharmony_ci     * the next. This value is used only if GetNativeObject returns nullptr.
8921cb0ef41Sopenharmony_ci     */
8931cb0ef41Sopenharmony_ci    virtual const void* GetAddress() { return nullptr; }
8941cb0ef41Sopenharmony_ci
8951cb0ef41Sopenharmony_ci    Node(const Node&) = delete;
8961cb0ef41Sopenharmony_ci    Node& operator=(const Node&) = delete;
8971cb0ef41Sopenharmony_ci  };
8981cb0ef41Sopenharmony_ci
8991cb0ef41Sopenharmony_ci  /**
9001cb0ef41Sopenharmony_ci   * Returns a node corresponding to the given V8 value. Ownership is not
9011cb0ef41Sopenharmony_ci   * transferred. The result pointer is valid while the graph is alive.
9021cb0ef41Sopenharmony_ci   */
9031cb0ef41Sopenharmony_ci  virtual Node* V8Node(const v8::Local<v8::Value>& value) = 0;
9041cb0ef41Sopenharmony_ci
9051cb0ef41Sopenharmony_ci  /**
9061cb0ef41Sopenharmony_ci   * Adds the given node to the graph and takes ownership of the node.
9071cb0ef41Sopenharmony_ci   * Returns a raw pointer to the node that is valid while the graph is alive.
9081cb0ef41Sopenharmony_ci   */
9091cb0ef41Sopenharmony_ci  virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
9101cb0ef41Sopenharmony_ci
9111cb0ef41Sopenharmony_ci  /**
9121cb0ef41Sopenharmony_ci   * Adds an edge that represents a strong reference from the given
9131cb0ef41Sopenharmony_ci   * node |from| to the given node |to|. The nodes must be added to the graph
9141cb0ef41Sopenharmony_ci   * before calling this function.
9151cb0ef41Sopenharmony_ci   *
9161cb0ef41Sopenharmony_ci   * If name is nullptr, the edge will have auto-increment indexes, otherwise
9171cb0ef41Sopenharmony_ci   * it will be named accordingly.
9181cb0ef41Sopenharmony_ci   */
9191cb0ef41Sopenharmony_ci  virtual void AddEdge(Node* from, Node* to, const char* name = nullptr) = 0;
9201cb0ef41Sopenharmony_ci
9211cb0ef41Sopenharmony_ci  virtual ~EmbedderGraph() = default;
9221cb0ef41Sopenharmony_ci};
9231cb0ef41Sopenharmony_ci
9241cb0ef41Sopenharmony_ci/**
9251cb0ef41Sopenharmony_ci * Interface for controlling heap profiling. Instance of the
9261cb0ef41Sopenharmony_ci * profiler can be retrieved using v8::Isolate::GetHeapProfiler.
9271cb0ef41Sopenharmony_ci */
9281cb0ef41Sopenharmony_ciclass V8_EXPORT HeapProfiler {
9291cb0ef41Sopenharmony_ci public:
9301cb0ef41Sopenharmony_ci  enum SamplingFlags {
9311cb0ef41Sopenharmony_ci    kSamplingNoFlags = 0,
9321cb0ef41Sopenharmony_ci    kSamplingForceGC = 1 << 0,
9331cb0ef41Sopenharmony_ci    kSamplingIncludeObjectsCollectedByMajorGC = 1 << 1,
9341cb0ef41Sopenharmony_ci    kSamplingIncludeObjectsCollectedByMinorGC = 1 << 2,
9351cb0ef41Sopenharmony_ci  };
9361cb0ef41Sopenharmony_ci
9371cb0ef41Sopenharmony_ci  /**
9381cb0ef41Sopenharmony_ci   * Callback function invoked during heap snapshot generation to retrieve
9391cb0ef41Sopenharmony_ci   * the embedder object graph. The callback should use graph->AddEdge(..) to
9401cb0ef41Sopenharmony_ci   * add references between the objects.
9411cb0ef41Sopenharmony_ci   * The callback must not trigger garbage collection in V8.
9421cb0ef41Sopenharmony_ci   */
9431cb0ef41Sopenharmony_ci  typedef void (*BuildEmbedderGraphCallback)(v8::Isolate* isolate,
9441cb0ef41Sopenharmony_ci                                             v8::EmbedderGraph* graph,
9451cb0ef41Sopenharmony_ci                                             void* data);
9461cb0ef41Sopenharmony_ci
9471cb0ef41Sopenharmony_ci  /**
9481cb0ef41Sopenharmony_ci   * Callback function invoked during heap snapshot generation to retrieve
9491cb0ef41Sopenharmony_ci   * the detachedness state of an object referenced by a TracedReference.
9501cb0ef41Sopenharmony_ci   *
9511cb0ef41Sopenharmony_ci   * The callback takes Local<Value> as parameter to allow the embedder to
9521cb0ef41Sopenharmony_ci   * unpack the TracedReference into a Local and reuse that Local for different
9531cb0ef41Sopenharmony_ci   * purposes.
9541cb0ef41Sopenharmony_ci   */
9551cb0ef41Sopenharmony_ci  using GetDetachednessCallback = EmbedderGraph::Node::Detachedness (*)(
9561cb0ef41Sopenharmony_ci      v8::Isolate* isolate, const v8::Local<v8::Value>& v8_value,
9571cb0ef41Sopenharmony_ci      uint16_t class_id, void* data);
9581cb0ef41Sopenharmony_ci
9591cb0ef41Sopenharmony_ci  /** Returns the number of snapshots taken. */
9601cb0ef41Sopenharmony_ci  int GetSnapshotCount();
9611cb0ef41Sopenharmony_ci
9621cb0ef41Sopenharmony_ci  /** Returns a snapshot by index. */
9631cb0ef41Sopenharmony_ci  const HeapSnapshot* GetHeapSnapshot(int index);
9641cb0ef41Sopenharmony_ci
9651cb0ef41Sopenharmony_ci  /**
9661cb0ef41Sopenharmony_ci   * Returns SnapshotObjectId for a heap object referenced by |value| if
9671cb0ef41Sopenharmony_ci   * it has been seen by the heap profiler, kUnknownObjectId otherwise.
9681cb0ef41Sopenharmony_ci   */
9691cb0ef41Sopenharmony_ci  SnapshotObjectId GetObjectId(Local<Value> value);
9701cb0ef41Sopenharmony_ci
9711cb0ef41Sopenharmony_ci  /**
9721cb0ef41Sopenharmony_ci   * Returns SnapshotObjectId for a native object referenced by |value| if it
9731cb0ef41Sopenharmony_ci   * has been seen by the heap profiler, kUnknownObjectId otherwise.
9741cb0ef41Sopenharmony_ci   */
9751cb0ef41Sopenharmony_ci  SnapshotObjectId GetObjectId(NativeObject value);
9761cb0ef41Sopenharmony_ci
9771cb0ef41Sopenharmony_ci  /**
9781cb0ef41Sopenharmony_ci   * Returns heap object with given SnapshotObjectId if the object is alive,
9791cb0ef41Sopenharmony_ci   * otherwise empty handle is returned.
9801cb0ef41Sopenharmony_ci   */
9811cb0ef41Sopenharmony_ci  Local<Value> FindObjectById(SnapshotObjectId id);
9821cb0ef41Sopenharmony_ci
9831cb0ef41Sopenharmony_ci  /**
9841cb0ef41Sopenharmony_ci   * Clears internal map from SnapshotObjectId to heap object. The new objects
9851cb0ef41Sopenharmony_ci   * will not be added into it unless a heap snapshot is taken or heap object
9861cb0ef41Sopenharmony_ci   * tracking is kicked off.
9871cb0ef41Sopenharmony_ci   */
9881cb0ef41Sopenharmony_ci  void ClearObjectIds();
9891cb0ef41Sopenharmony_ci
9901cb0ef41Sopenharmony_ci  /**
9911cb0ef41Sopenharmony_ci   * A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
9921cb0ef41Sopenharmony_ci   * it in case heap profiler cannot find id  for the object passed as
9931cb0ef41Sopenharmony_ci   * parameter. HeapSnapshot::GetNodeById will always return NULL for such id.
9941cb0ef41Sopenharmony_ci   */
9951cb0ef41Sopenharmony_ci  static const SnapshotObjectId kUnknownObjectId = 0;
9961cb0ef41Sopenharmony_ci
9971cb0ef41Sopenharmony_ci  /**
9981cb0ef41Sopenharmony_ci   * Callback interface for retrieving user friendly names of global objects.
9991cb0ef41Sopenharmony_ci   */
10001cb0ef41Sopenharmony_ci  class ObjectNameResolver {
10011cb0ef41Sopenharmony_ci   public:
10021cb0ef41Sopenharmony_ci    /**
10031cb0ef41Sopenharmony_ci     * Returns name to be used in the heap snapshot for given node. Returned
10041cb0ef41Sopenharmony_ci     * string must stay alive until snapshot collection is completed.
10051cb0ef41Sopenharmony_ci     */
10061cb0ef41Sopenharmony_ci    virtual const char* GetName(Local<Object> object) = 0;
10071cb0ef41Sopenharmony_ci
10081cb0ef41Sopenharmony_ci   protected:
10091cb0ef41Sopenharmony_ci    virtual ~ObjectNameResolver() = default;
10101cb0ef41Sopenharmony_ci  };
10111cb0ef41Sopenharmony_ci
10121cb0ef41Sopenharmony_ci  enum class HeapSnapshotMode {
10131cb0ef41Sopenharmony_ci    /**
10141cb0ef41Sopenharmony_ci     * Heap snapshot for regular developers.
10151cb0ef41Sopenharmony_ci     */
10161cb0ef41Sopenharmony_ci    kRegular,
10171cb0ef41Sopenharmony_ci    /**
10181cb0ef41Sopenharmony_ci     * Heap snapshot is exposing internals that may be useful for experts.
10191cb0ef41Sopenharmony_ci     */
10201cb0ef41Sopenharmony_ci    kExposeInternals,
10211cb0ef41Sopenharmony_ci  };
10221cb0ef41Sopenharmony_ci
10231cb0ef41Sopenharmony_ci  enum class NumericsMode {
10241cb0ef41Sopenharmony_ci    /**
10251cb0ef41Sopenharmony_ci     * Numeric values are hidden as they are values of the corresponding
10261cb0ef41Sopenharmony_ci     * objects.
10271cb0ef41Sopenharmony_ci     */
10281cb0ef41Sopenharmony_ci    kHideNumericValues,
10291cb0ef41Sopenharmony_ci    /**
10301cb0ef41Sopenharmony_ci     * Numeric values are exposed in artificial fields.
10311cb0ef41Sopenharmony_ci     */
10321cb0ef41Sopenharmony_ci    kExposeNumericValues
10331cb0ef41Sopenharmony_ci  };
10341cb0ef41Sopenharmony_ci
10351cb0ef41Sopenharmony_ci  struct HeapSnapshotOptions final {
10361cb0ef41Sopenharmony_ci    // Manually define default constructor here to be able to use it in
10371cb0ef41Sopenharmony_ci    // `TakeSnapshot()` below.
10381cb0ef41Sopenharmony_ci    // NOLINTNEXTLINE
10391cb0ef41Sopenharmony_ci    HeapSnapshotOptions() {}
10401cb0ef41Sopenharmony_ci
10411cb0ef41Sopenharmony_ci    /**
10421cb0ef41Sopenharmony_ci     * The control used to report intermediate progress to.
10431cb0ef41Sopenharmony_ci     */
10441cb0ef41Sopenharmony_ci    ActivityControl* control = nullptr;
10451cb0ef41Sopenharmony_ci    /**
10461cb0ef41Sopenharmony_ci     * The resolver used by the snapshot generator to get names for V8 objects.
10471cb0ef41Sopenharmony_ci     */
10481cb0ef41Sopenharmony_ci    ObjectNameResolver* global_object_name_resolver = nullptr;
10491cb0ef41Sopenharmony_ci    /**
10501cb0ef41Sopenharmony_ci     * Mode for taking the snapshot, see `HeapSnapshotMode`.
10511cb0ef41Sopenharmony_ci     */
10521cb0ef41Sopenharmony_ci    HeapSnapshotMode snapshot_mode = HeapSnapshotMode::kRegular;
10531cb0ef41Sopenharmony_ci    /**
10541cb0ef41Sopenharmony_ci     * Mode for dealing with numeric values, see `NumericsMode`.
10551cb0ef41Sopenharmony_ci     */
10561cb0ef41Sopenharmony_ci    NumericsMode numerics_mode = NumericsMode::kHideNumericValues;
10571cb0ef41Sopenharmony_ci  };
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ci  /**
10601cb0ef41Sopenharmony_ci   * Takes a heap snapshot.
10611cb0ef41Sopenharmony_ci   *
10621cb0ef41Sopenharmony_ci   * \returns the snapshot.
10631cb0ef41Sopenharmony_ci   */
10641cb0ef41Sopenharmony_ci  const HeapSnapshot* TakeHeapSnapshot(
10651cb0ef41Sopenharmony_ci      const HeapSnapshotOptions& options = HeapSnapshotOptions());
10661cb0ef41Sopenharmony_ci
10671cb0ef41Sopenharmony_ci  /**
10681cb0ef41Sopenharmony_ci   * Takes a heap snapshot. See `HeapSnapshotOptions` for details on the
10691cb0ef41Sopenharmony_ci   * parameters.
10701cb0ef41Sopenharmony_ci   *
10711cb0ef41Sopenharmony_ci   * \returns the snapshot.
10721cb0ef41Sopenharmony_ci   */
10731cb0ef41Sopenharmony_ci  const HeapSnapshot* TakeHeapSnapshot(
10741cb0ef41Sopenharmony_ci      ActivityControl* control,
10751cb0ef41Sopenharmony_ci      ObjectNameResolver* global_object_name_resolver = nullptr,
10761cb0ef41Sopenharmony_ci      bool hide_internals = true, bool capture_numeric_value = false);
10771cb0ef41Sopenharmony_ci
10781cb0ef41Sopenharmony_ci  /**
10791cb0ef41Sopenharmony_ci   * Starts tracking of heap objects population statistics. After calling
10801cb0ef41Sopenharmony_ci   * this method, all heap objects relocations done by the garbage collector
10811cb0ef41Sopenharmony_ci   * are being registered.
10821cb0ef41Sopenharmony_ci   *
10831cb0ef41Sopenharmony_ci   * |track_allocations| parameter controls whether stack trace of each
10841cb0ef41Sopenharmony_ci   * allocation in the heap will be recorded and reported as part of
10851cb0ef41Sopenharmony_ci   * HeapSnapshot.
10861cb0ef41Sopenharmony_ci   */
10871cb0ef41Sopenharmony_ci  void StartTrackingHeapObjects(bool track_allocations = false);
10881cb0ef41Sopenharmony_ci
10891cb0ef41Sopenharmony_ci  /**
10901cb0ef41Sopenharmony_ci   * Adds a new time interval entry to the aggregated statistics array. The
10911cb0ef41Sopenharmony_ci   * time interval entry contains information on the current heap objects
10921cb0ef41Sopenharmony_ci   * population size. The method also updates aggregated statistics and
10931cb0ef41Sopenharmony_ci   * reports updates for all previous time intervals via the OutputStream
10941cb0ef41Sopenharmony_ci   * object. Updates on each time interval are provided as a stream of the
10951cb0ef41Sopenharmony_ci   * HeapStatsUpdate structure instances.
10961cb0ef41Sopenharmony_ci   * If |timestamp_us| is supplied, timestamp of the new entry will be written
10971cb0ef41Sopenharmony_ci   * into it. The return value of the function is the last seen heap object Id.
10981cb0ef41Sopenharmony_ci   *
10991cb0ef41Sopenharmony_ci   * StartTrackingHeapObjects must be called before the first call to this
11001cb0ef41Sopenharmony_ci   * method.
11011cb0ef41Sopenharmony_ci   */
11021cb0ef41Sopenharmony_ci  SnapshotObjectId GetHeapStats(OutputStream* stream,
11031cb0ef41Sopenharmony_ci                                int64_t* timestamp_us = nullptr);
11041cb0ef41Sopenharmony_ci
11051cb0ef41Sopenharmony_ci  /**
11061cb0ef41Sopenharmony_ci   * Stops tracking of heap objects population statistics, cleans up all
11071cb0ef41Sopenharmony_ci   * collected data. StartHeapObjectsTracking must be called again prior to
11081cb0ef41Sopenharmony_ci   * calling GetHeapStats next time.
11091cb0ef41Sopenharmony_ci   */
11101cb0ef41Sopenharmony_ci  void StopTrackingHeapObjects();
11111cb0ef41Sopenharmony_ci
11121cb0ef41Sopenharmony_ci  /**
11131cb0ef41Sopenharmony_ci   * Starts gathering a sampling heap profile. A sampling heap profile is
11141cb0ef41Sopenharmony_ci   * similar to tcmalloc's heap profiler and Go's mprof. It samples object
11151cb0ef41Sopenharmony_ci   * allocations and builds an online 'sampling' heap profile. At any point in
11161cb0ef41Sopenharmony_ci   * time, this profile is expected to be a representative sample of objects
11171cb0ef41Sopenharmony_ci   * currently live in the system. Each sampled allocation includes the stack
11181cb0ef41Sopenharmony_ci   * trace at the time of allocation, which makes this really useful for memory
11191cb0ef41Sopenharmony_ci   * leak detection.
11201cb0ef41Sopenharmony_ci   *
11211cb0ef41Sopenharmony_ci   * This mechanism is intended to be cheap enough that it can be used in
11221cb0ef41Sopenharmony_ci   * production with minimal performance overhead.
11231cb0ef41Sopenharmony_ci   *
11241cb0ef41Sopenharmony_ci   * Allocations are sampled using a randomized Poisson process. On average, one
11251cb0ef41Sopenharmony_ci   * allocation will be sampled every |sample_interval| bytes allocated. The
11261cb0ef41Sopenharmony_ci   * |stack_depth| parameter controls the maximum number of stack frames to be
11271cb0ef41Sopenharmony_ci   * captured on each allocation.
11281cb0ef41Sopenharmony_ci   *
11291cb0ef41Sopenharmony_ci   * NOTE: Support for native allocations doesn't exist yet, but is anticipated
11301cb0ef41Sopenharmony_ci   * in the future.
11311cb0ef41Sopenharmony_ci   *
11321cb0ef41Sopenharmony_ci   * Objects allocated before the sampling is started will not be included in
11331cb0ef41Sopenharmony_ci   * the profile.
11341cb0ef41Sopenharmony_ci   *
11351cb0ef41Sopenharmony_ci   * Returns false if a sampling heap profiler is already running.
11361cb0ef41Sopenharmony_ci   */
11371cb0ef41Sopenharmony_ci  bool StartSamplingHeapProfiler(uint64_t sample_interval = 512 * 1024,
11381cb0ef41Sopenharmony_ci                                 int stack_depth = 16,
11391cb0ef41Sopenharmony_ci                                 SamplingFlags flags = kSamplingNoFlags);
11401cb0ef41Sopenharmony_ci
11411cb0ef41Sopenharmony_ci  /**
11421cb0ef41Sopenharmony_ci   * Stops the sampling heap profile and discards the current profile.
11431cb0ef41Sopenharmony_ci   */
11441cb0ef41Sopenharmony_ci  void StopSamplingHeapProfiler();
11451cb0ef41Sopenharmony_ci
11461cb0ef41Sopenharmony_ci  /**
11471cb0ef41Sopenharmony_ci   * Returns the sampled profile of allocations allocated (and still live) since
11481cb0ef41Sopenharmony_ci   * StartSamplingHeapProfiler was called. The ownership of the pointer is
11491cb0ef41Sopenharmony_ci   * transferred to the caller. Returns nullptr if sampling heap profiler is not
11501cb0ef41Sopenharmony_ci   * active.
11511cb0ef41Sopenharmony_ci   */
11521cb0ef41Sopenharmony_ci  AllocationProfile* GetAllocationProfile();
11531cb0ef41Sopenharmony_ci
11541cb0ef41Sopenharmony_ci  /**
11551cb0ef41Sopenharmony_ci   * Deletes all snapshots taken. All previously returned pointers to
11561cb0ef41Sopenharmony_ci   * snapshots and their contents become invalid after this call.
11571cb0ef41Sopenharmony_ci   */
11581cb0ef41Sopenharmony_ci  void DeleteAllHeapSnapshots();
11591cb0ef41Sopenharmony_ci
11601cb0ef41Sopenharmony_ci  void AddBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
11611cb0ef41Sopenharmony_ci                                     void* data);
11621cb0ef41Sopenharmony_ci  void RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback,
11631cb0ef41Sopenharmony_ci                                        void* data);
11641cb0ef41Sopenharmony_ci
11651cb0ef41Sopenharmony_ci  void SetGetDetachednessCallback(GetDetachednessCallback callback, void* data);
11661cb0ef41Sopenharmony_ci
11671cb0ef41Sopenharmony_ci  /**
11681cb0ef41Sopenharmony_ci   * Default value of persistent handle class ID. Must not be used to
11691cb0ef41Sopenharmony_ci   * define a class. Can be used to reset a class of a persistent
11701cb0ef41Sopenharmony_ci   * handle.
11711cb0ef41Sopenharmony_ci   */
11721cb0ef41Sopenharmony_ci  static const uint16_t kPersistentHandleNoClassId = 0;
11731cb0ef41Sopenharmony_ci
11741cb0ef41Sopenharmony_ci private:
11751cb0ef41Sopenharmony_ci  HeapProfiler();
11761cb0ef41Sopenharmony_ci  ~HeapProfiler();
11771cb0ef41Sopenharmony_ci  HeapProfiler(const HeapProfiler&);
11781cb0ef41Sopenharmony_ci  HeapProfiler& operator=(const HeapProfiler&);
11791cb0ef41Sopenharmony_ci};
11801cb0ef41Sopenharmony_ci
11811cb0ef41Sopenharmony_ci/**
11821cb0ef41Sopenharmony_ci * A struct for exporting HeapStats data from V8, using "push" model.
11831cb0ef41Sopenharmony_ci * See HeapProfiler::GetHeapStats.
11841cb0ef41Sopenharmony_ci */
11851cb0ef41Sopenharmony_cistruct HeapStatsUpdate {
11861cb0ef41Sopenharmony_ci  HeapStatsUpdate(uint32_t index, uint32_t count, uint32_t size)
11871cb0ef41Sopenharmony_ci    : index(index), count(count), size(size) { }
11881cb0ef41Sopenharmony_ci  uint32_t index;  // Index of the time interval that was changed.
11891cb0ef41Sopenharmony_ci  uint32_t count;  // New value of count field for the interval with this index.
11901cb0ef41Sopenharmony_ci  uint32_t size;  // New value of size field for the interval with this index.
11911cb0ef41Sopenharmony_ci};
11921cb0ef41Sopenharmony_ci
11931cb0ef41Sopenharmony_ci#define CODE_EVENTS_LIST(V)                          \
11941cb0ef41Sopenharmony_ci  V(Builtin)                                         \
11951cb0ef41Sopenharmony_ci  V(Callback)                                        \
11961cb0ef41Sopenharmony_ci  V(Eval)                                            \
11971cb0ef41Sopenharmony_ci  V(Function)                                        \
11981cb0ef41Sopenharmony_ci  V(InterpretedFunction)                             \
11991cb0ef41Sopenharmony_ci  V(Handler)                                         \
12001cb0ef41Sopenharmony_ci  V(BytecodeHandler)                                 \
12011cb0ef41Sopenharmony_ci  V(LazyCompile) /* Unused, use kFunction instead */ \
12021cb0ef41Sopenharmony_ci  V(RegExp)                                          \
12031cb0ef41Sopenharmony_ci  V(Script)                                          \
12041cb0ef41Sopenharmony_ci  V(Stub)                                            \
12051cb0ef41Sopenharmony_ci  V(Relocation)
12061cb0ef41Sopenharmony_ci
12071cb0ef41Sopenharmony_ci/**
12081cb0ef41Sopenharmony_ci * Note that this enum may be extended in the future. Please include a default
12091cb0ef41Sopenharmony_ci * case if this enum is used in a switch statement.
12101cb0ef41Sopenharmony_ci */
12111cb0ef41Sopenharmony_cienum CodeEventType {
12121cb0ef41Sopenharmony_ci  kUnknownType = 0
12131cb0ef41Sopenharmony_ci#define V(Name) , k##Name##Type
12141cb0ef41Sopenharmony_ci  CODE_EVENTS_LIST(V)
12151cb0ef41Sopenharmony_ci#undef V
12161cb0ef41Sopenharmony_ci};
12171cb0ef41Sopenharmony_ci
12181cb0ef41Sopenharmony_ci/**
12191cb0ef41Sopenharmony_ci * Representation of a code creation event
12201cb0ef41Sopenharmony_ci */
12211cb0ef41Sopenharmony_ciclass V8_EXPORT CodeEvent {
12221cb0ef41Sopenharmony_ci public:
12231cb0ef41Sopenharmony_ci  uintptr_t GetCodeStartAddress();
12241cb0ef41Sopenharmony_ci  size_t GetCodeSize();
12251cb0ef41Sopenharmony_ci  Local<String> GetFunctionName();
12261cb0ef41Sopenharmony_ci  Local<String> GetScriptName();
12271cb0ef41Sopenharmony_ci  int GetScriptLine();
12281cb0ef41Sopenharmony_ci  int GetScriptColumn();
12291cb0ef41Sopenharmony_ci  /**
12301cb0ef41Sopenharmony_ci   * NOTE (mmarchini): We can't allocate objects in the heap when we collect
12311cb0ef41Sopenharmony_ci   * existing code, and both the code type and the comment are not stored in the
12321cb0ef41Sopenharmony_ci   * heap, so we return those as const char*.
12331cb0ef41Sopenharmony_ci   */
12341cb0ef41Sopenharmony_ci  CodeEventType GetCodeType();
12351cb0ef41Sopenharmony_ci  const char* GetComment();
12361cb0ef41Sopenharmony_ci
12371cb0ef41Sopenharmony_ci  static const char* GetCodeEventTypeName(CodeEventType code_event_type);
12381cb0ef41Sopenharmony_ci
12391cb0ef41Sopenharmony_ci  uintptr_t GetPreviousCodeStartAddress();
12401cb0ef41Sopenharmony_ci};
12411cb0ef41Sopenharmony_ci
12421cb0ef41Sopenharmony_ci/**
12431cb0ef41Sopenharmony_ci * Interface to listen to code creation and code relocation events.
12441cb0ef41Sopenharmony_ci */
12451cb0ef41Sopenharmony_ciclass V8_EXPORT CodeEventHandler {
12461cb0ef41Sopenharmony_ci public:
12471cb0ef41Sopenharmony_ci  /**
12481cb0ef41Sopenharmony_ci   * Creates a new listener for the |isolate|. The isolate must be initialized.
12491cb0ef41Sopenharmony_ci   * The listener object must be disposed after use by calling |Dispose| method.
12501cb0ef41Sopenharmony_ci   * Multiple listeners can be created for the same isolate.
12511cb0ef41Sopenharmony_ci   */
12521cb0ef41Sopenharmony_ci  explicit CodeEventHandler(Isolate* isolate);
12531cb0ef41Sopenharmony_ci  virtual ~CodeEventHandler();
12541cb0ef41Sopenharmony_ci
12551cb0ef41Sopenharmony_ci  /**
12561cb0ef41Sopenharmony_ci   * Handle is called every time a code object is created or moved. Information
12571cb0ef41Sopenharmony_ci   * about each code event will be available through the `code_event`
12581cb0ef41Sopenharmony_ci   * parameter.
12591cb0ef41Sopenharmony_ci   *
12601cb0ef41Sopenharmony_ci   * When the CodeEventType is kRelocationType, the code for this CodeEvent has
12611cb0ef41Sopenharmony_ci   * moved from `GetPreviousCodeStartAddress()` to `GetCodeStartAddress()`.
12621cb0ef41Sopenharmony_ci   */
12631cb0ef41Sopenharmony_ci  virtual void Handle(CodeEvent* code_event) = 0;
12641cb0ef41Sopenharmony_ci
12651cb0ef41Sopenharmony_ci  /**
12661cb0ef41Sopenharmony_ci   * Call `Enable()` to starts listening to code creation and code relocation
12671cb0ef41Sopenharmony_ci   * events. These events will be handled by `Handle()`.
12681cb0ef41Sopenharmony_ci   */
12691cb0ef41Sopenharmony_ci  void Enable();
12701cb0ef41Sopenharmony_ci
12711cb0ef41Sopenharmony_ci  /**
12721cb0ef41Sopenharmony_ci   * Call `Disable()` to stop listening to code creation and code relocation
12731cb0ef41Sopenharmony_ci   * events.
12741cb0ef41Sopenharmony_ci   */
12751cb0ef41Sopenharmony_ci  void Disable();
12761cb0ef41Sopenharmony_ci
12771cb0ef41Sopenharmony_ci private:
12781cb0ef41Sopenharmony_ci  CodeEventHandler();
12791cb0ef41Sopenharmony_ci  CodeEventHandler(const CodeEventHandler&);
12801cb0ef41Sopenharmony_ci  CodeEventHandler& operator=(const CodeEventHandler&);
12811cb0ef41Sopenharmony_ci  void* internal_listener_;
12821cb0ef41Sopenharmony_ci};
12831cb0ef41Sopenharmony_ci
12841cb0ef41Sopenharmony_ci}  // namespace v8
12851cb0ef41Sopenharmony_ci
12861cb0ef41Sopenharmony_ci
12871cb0ef41Sopenharmony_ci#endif  // V8_V8_PROFILER_H_
1288