11cb0ef41Sopenharmony_ci// Copyright 2012 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_PROFILER_CPU_PROFILER_H_
61cb0ef41Sopenharmony_ci#define V8_PROFILER_CPU_PROFILER_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <atomic>
91cb0ef41Sopenharmony_ci#include <memory>
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include "src/base/platform/condition-variable.h"
121cb0ef41Sopenharmony_ci#include "src/base/platform/mutex.h"
131cb0ef41Sopenharmony_ci#include "src/base/platform/time.h"
141cb0ef41Sopenharmony_ci#include "src/profiler/circular-queue.h"
151cb0ef41Sopenharmony_ci#include "src/profiler/profiler-listener.h"
161cb0ef41Sopenharmony_ci#include "src/profiler/tick-sample.h"
171cb0ef41Sopenharmony_ci#include "src/utils/locked-queue.h"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cinamespace v8 {
201cb0ef41Sopenharmony_cinamespace sampler {
211cb0ef41Sopenharmony_ciclass Sampler;
221cb0ef41Sopenharmony_ci}  // namespace sampler
231cb0ef41Sopenharmony_cinamespace internal {
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// Forward declarations.
261cb0ef41Sopenharmony_ciclass CodeEntry;
271cb0ef41Sopenharmony_ciclass CodeMap;
281cb0ef41Sopenharmony_ciclass CpuProfilesCollection;
291cb0ef41Sopenharmony_ciclass Isolate;
301cb0ef41Sopenharmony_ciclass Symbolizer;
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci#define CODE_EVENTS_TYPE_LIST(V)                \
331cb0ef41Sopenharmony_ci  V(kCodeCreation, CodeCreateEventRecord)       \
341cb0ef41Sopenharmony_ci  V(kCodeMove, CodeMoveEventRecord)             \
351cb0ef41Sopenharmony_ci  V(kCodeDisableOpt, CodeDisableOptEventRecord) \
361cb0ef41Sopenharmony_ci  V(kCodeDeopt, CodeDeoptEventRecord)           \
371cb0ef41Sopenharmony_ci  V(kReportBuiltin, ReportBuiltinEventRecord)   \
381cb0ef41Sopenharmony_ci  V(kCodeDelete, CodeDeleteEventRecord)
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci#define VM_EVENTS_TYPE_LIST(V) \
411cb0ef41Sopenharmony_ci  CODE_EVENTS_TYPE_LIST(V)     \
421cb0ef41Sopenharmony_ci  V(kNativeContextMove, NativeContextMoveEventRecord)
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciclass CodeEventRecord {
451cb0ef41Sopenharmony_ci public:
461cb0ef41Sopenharmony_ci#define DECLARE_TYPE(type, ignore) type,
471cb0ef41Sopenharmony_ci  enum class Type { kNoEvent = 0, VM_EVENTS_TYPE_LIST(DECLARE_TYPE) };
481cb0ef41Sopenharmony_ci#undef DECLARE_TYPE
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  Type type;
511cb0ef41Sopenharmony_ci  mutable unsigned order;
521cb0ef41Sopenharmony_ci};
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciclass CodeCreateEventRecord : public CodeEventRecord {
561cb0ef41Sopenharmony_ci public:
571cb0ef41Sopenharmony_ci  Address instruction_start;
581cb0ef41Sopenharmony_ci  CodeEntry* entry;
591cb0ef41Sopenharmony_ci  unsigned instruction_size;
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  V8_INLINE void UpdateCodeMap(CodeMap* code_map);
621cb0ef41Sopenharmony_ci};
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciclass CodeMoveEventRecord : public CodeEventRecord {
661cb0ef41Sopenharmony_ci public:
671cb0ef41Sopenharmony_ci  Address from_instruction_start;
681cb0ef41Sopenharmony_ci  Address to_instruction_start;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  V8_INLINE void UpdateCodeMap(CodeMap* code_map);
711cb0ef41Sopenharmony_ci};
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciclass CodeDisableOptEventRecord : public CodeEventRecord {
751cb0ef41Sopenharmony_ci public:
761cb0ef41Sopenharmony_ci  Address instruction_start;
771cb0ef41Sopenharmony_ci  const char* bailout_reason;
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  V8_INLINE void UpdateCodeMap(CodeMap* code_map);
801cb0ef41Sopenharmony_ci};
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ciclass CodeDeoptEventRecord : public CodeEventRecord {
841cb0ef41Sopenharmony_ci public:
851cb0ef41Sopenharmony_ci  Address instruction_start;
861cb0ef41Sopenharmony_ci  const char* deopt_reason;
871cb0ef41Sopenharmony_ci  int deopt_id;
881cb0ef41Sopenharmony_ci  Address pc;
891cb0ef41Sopenharmony_ci  int fp_to_sp_delta;
901cb0ef41Sopenharmony_ci  CpuProfileDeoptFrame* deopt_frames;
911cb0ef41Sopenharmony_ci  int deopt_frame_count;
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  V8_INLINE void UpdateCodeMap(CodeMap* code_map);
941cb0ef41Sopenharmony_ci};
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ciclass ReportBuiltinEventRecord : public CodeEventRecord {
981cb0ef41Sopenharmony_ci public:
991cb0ef41Sopenharmony_ci  Address instruction_start;
1001cb0ef41Sopenharmony_ci  unsigned instruction_size;
1011cb0ef41Sopenharmony_ci  Builtin builtin;
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  V8_INLINE void UpdateCodeMap(CodeMap* code_map);
1041cb0ef41Sopenharmony_ci};
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci// Signals that a native context's address has changed.
1071cb0ef41Sopenharmony_ciclass NativeContextMoveEventRecord : public CodeEventRecord {
1081cb0ef41Sopenharmony_ci public:
1091cb0ef41Sopenharmony_ci  Address from_address;
1101cb0ef41Sopenharmony_ci  Address to_address;
1111cb0ef41Sopenharmony_ci};
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci// A record type for sending samples from the main thread/signal handler to the
1141cb0ef41Sopenharmony_ci// profiling thread.
1151cb0ef41Sopenharmony_ciclass TickSampleEventRecord {
1161cb0ef41Sopenharmony_ci public:
1171cb0ef41Sopenharmony_ci  // The parameterless constructor is used when we dequeue data from
1181cb0ef41Sopenharmony_ci  // the ticks buffer.
1191cb0ef41Sopenharmony_ci  TickSampleEventRecord() = default;
1201cb0ef41Sopenharmony_ci  explicit TickSampleEventRecord(unsigned order) : order(order) { }
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  unsigned order;
1231cb0ef41Sopenharmony_ci  TickSample sample;
1241cb0ef41Sopenharmony_ci};
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ciclass CodeDeleteEventRecord : public CodeEventRecord {
1271cb0ef41Sopenharmony_ci public:
1281cb0ef41Sopenharmony_ci  CodeEntry* entry;
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  V8_INLINE void UpdateCodeMap(CodeMap* code_map);
1311cb0ef41Sopenharmony_ci};
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci// A record type for sending code events (e.g. create, move, delete) to the
1341cb0ef41Sopenharmony_ci// profiling thread.
1351cb0ef41Sopenharmony_ciclass CodeEventsContainer {
1361cb0ef41Sopenharmony_ci public:
1371cb0ef41Sopenharmony_ci  explicit CodeEventsContainer(
1381cb0ef41Sopenharmony_ci      CodeEventRecord::Type type = CodeEventRecord::Type::kNoEvent) {
1391cb0ef41Sopenharmony_ci    generic.type = type;
1401cb0ef41Sopenharmony_ci  }
1411cb0ef41Sopenharmony_ci  union  {
1421cb0ef41Sopenharmony_ci    CodeEventRecord generic;
1431cb0ef41Sopenharmony_ci#define DECLARE_CLASS(ignore, type) type type##_;
1441cb0ef41Sopenharmony_ci    VM_EVENTS_TYPE_LIST(DECLARE_CLASS)
1451cb0ef41Sopenharmony_ci#undef DECLARE_CLASS
1461cb0ef41Sopenharmony_ci  };
1471cb0ef41Sopenharmony_ci};
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci// Maintains the number of active CPU profilers in an isolate, and routes
1501cb0ef41Sopenharmony_ci// logging to a given ProfilerListener.
1511cb0ef41Sopenharmony_ciclass V8_NODISCARD ProfilingScope {
1521cb0ef41Sopenharmony_ci public:
1531cb0ef41Sopenharmony_ci  ProfilingScope(Isolate* isolate, ProfilerListener* listener);
1541cb0ef41Sopenharmony_ci  ~ProfilingScope();
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci private:
1571cb0ef41Sopenharmony_ci  Isolate* const isolate_;
1581cb0ef41Sopenharmony_ci  ProfilerListener* const listener_;
1591cb0ef41Sopenharmony_ci};
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ciclass ProfilerCodeObserver;
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci// This class implements both the profile events processor thread and
1641cb0ef41Sopenharmony_ci// methods called by event producers: VM and stack sampler threads.
1651cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE ProfilerEventsProcessor : public base::Thread,
1661cb0ef41Sopenharmony_ci                                                  public CodeEventObserver {
1671cb0ef41Sopenharmony_ci public:
1681cb0ef41Sopenharmony_ci  ~ProfilerEventsProcessor() override;
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci  void CodeEventHandler(const CodeEventsContainer& evt_rec) override;
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  // Thread control.
1731cb0ef41Sopenharmony_ci  void Run() override = 0;
1741cb0ef41Sopenharmony_ci  void StopSynchronously();
1751cb0ef41Sopenharmony_ci  bool running() { return running_.load(std::memory_order_relaxed); }
1761cb0ef41Sopenharmony_ci  void Enqueue(const CodeEventsContainer& event);
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci  // Puts current stack into the tick sample events buffer.
1791cb0ef41Sopenharmony_ci  void AddCurrentStack(bool update_stats = false);
1801cb0ef41Sopenharmony_ci  void AddDeoptStack(Address from, int fp_to_sp_delta);
1811cb0ef41Sopenharmony_ci  // Add a sample into the tick sample events buffer. Used for testing.
1821cb0ef41Sopenharmony_ci  void AddSample(TickSample sample);
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci  virtual void SetSamplingInterval(base::TimeDelta) {}
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci protected:
1871cb0ef41Sopenharmony_ci  ProfilerEventsProcessor(Isolate* isolate, Symbolizer* symbolizer,
1881cb0ef41Sopenharmony_ci                          ProfilerCodeObserver* code_observer,
1891cb0ef41Sopenharmony_ci                          CpuProfilesCollection* profiles);
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci  // Called from events processing thread (Run() method.)
1921cb0ef41Sopenharmony_ci  bool ProcessCodeEvent();
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci  enum SampleProcessingResult {
1951cb0ef41Sopenharmony_ci    OneSampleProcessed,
1961cb0ef41Sopenharmony_ci    FoundSampleForNextCodeEvent,
1971cb0ef41Sopenharmony_ci    NoSamplesInQueue
1981cb0ef41Sopenharmony_ci  };
1991cb0ef41Sopenharmony_ci  virtual SampleProcessingResult ProcessOneSample() = 0;
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci  Symbolizer* symbolizer_;
2021cb0ef41Sopenharmony_ci  ProfilerCodeObserver* code_observer_;
2031cb0ef41Sopenharmony_ci  CpuProfilesCollection* profiles_;
2041cb0ef41Sopenharmony_ci  std::atomic_bool running_{true};
2051cb0ef41Sopenharmony_ci  base::ConditionVariable running_cond_;
2061cb0ef41Sopenharmony_ci  base::Mutex running_mutex_;
2071cb0ef41Sopenharmony_ci  LockedQueue<CodeEventsContainer> events_buffer_;
2081cb0ef41Sopenharmony_ci  LockedQueue<TickSampleEventRecord> ticks_from_vm_buffer_;
2091cb0ef41Sopenharmony_ci  std::atomic<unsigned> last_code_event_id_;
2101cb0ef41Sopenharmony_ci  unsigned last_processed_code_event_id_;
2111cb0ef41Sopenharmony_ci  Isolate* isolate_;
2121cb0ef41Sopenharmony_ci};
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE SamplingEventsProcessor
2151cb0ef41Sopenharmony_ci    : public ProfilerEventsProcessor {
2161cb0ef41Sopenharmony_ci public:
2171cb0ef41Sopenharmony_ci  SamplingEventsProcessor(Isolate* isolate, Symbolizer* symbolizer,
2181cb0ef41Sopenharmony_ci                          ProfilerCodeObserver* code_observer,
2191cb0ef41Sopenharmony_ci                          CpuProfilesCollection* profiles,
2201cb0ef41Sopenharmony_ci                          base::TimeDelta period, bool use_precise_sampling);
2211cb0ef41Sopenharmony_ci  ~SamplingEventsProcessor() override;
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci  // SamplingCircularQueue has stricter alignment requirements than a normal new
2241cb0ef41Sopenharmony_ci  // can fulfil, so we need to provide our own new/delete here.
2251cb0ef41Sopenharmony_ci  void* operator new(size_t size);
2261cb0ef41Sopenharmony_ci  void operator delete(void* ptr);
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ci  void Run() override;
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci  void SetSamplingInterval(base::TimeDelta period) override;
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci  // Tick sample events are filled directly in the buffer of the circular
2331cb0ef41Sopenharmony_ci  // queue (because the structure is of fixed width, but usually not all
2341cb0ef41Sopenharmony_ci  // stack frame entries are filled.) This method returns a pointer to the
2351cb0ef41Sopenharmony_ci  // next record of the buffer.
2361cb0ef41Sopenharmony_ci  // These methods are not thread-safe and should only ever be called by one
2371cb0ef41Sopenharmony_ci  // producer (from CpuSampler::SampleStack()). For testing, use AddSample.
2381cb0ef41Sopenharmony_ci  inline TickSample* StartTickSample();
2391cb0ef41Sopenharmony_ci  inline void FinishTickSample();
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci  sampler::Sampler* sampler() { return sampler_.get(); }
2421cb0ef41Sopenharmony_ci  base::TimeDelta period() const { return period_; }
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci private:
2451cb0ef41Sopenharmony_ci  SampleProcessingResult ProcessOneSample() override;
2461cb0ef41Sopenharmony_ci  void SymbolizeAndAddToProfiles(const TickSampleEventRecord* record);
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci  static const size_t kTickSampleBufferSize = 512 * KB;
2491cb0ef41Sopenharmony_ci  static const size_t kTickSampleQueueLength =
2501cb0ef41Sopenharmony_ci      kTickSampleBufferSize / sizeof(TickSampleEventRecord);
2511cb0ef41Sopenharmony_ci  SamplingCircularQueue<TickSampleEventRecord,
2521cb0ef41Sopenharmony_ci                        kTickSampleQueueLength> ticks_buffer_;
2531cb0ef41Sopenharmony_ci  std::unique_ptr<sampler::Sampler> sampler_;
2541cb0ef41Sopenharmony_ci  base::TimeDelta period_;           // Samples & code events processing period.
2551cb0ef41Sopenharmony_ci  const bool use_precise_sampling_;  // Whether or not busy-waiting is used for
2561cb0ef41Sopenharmony_ci                                     // low sampling intervals on Windows.
2571cb0ef41Sopenharmony_ci};
2581cb0ef41Sopenharmony_ci
2591cb0ef41Sopenharmony_ci// Builds and maintains a CodeMap tracking code objects on the VM heap. While
2601cb0ef41Sopenharmony_ci// alive, logs generated code, callbacks, and builtins from the isolate.
2611cb0ef41Sopenharmony_ci// Redirects events to the profiler events processor when present. CodeEntry
2621cb0ef41Sopenharmony_ci// lifetime is associated with the given CodeEntryStorage.
2631cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE ProfilerCodeObserver : public CodeEventObserver {
2641cb0ef41Sopenharmony_ci public:
2651cb0ef41Sopenharmony_ci  explicit ProfilerCodeObserver(Isolate*, CodeEntryStorage&);
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci  void CodeEventHandler(const CodeEventsContainer& evt_rec) override;
2681cb0ef41Sopenharmony_ci  CodeEntryStorage* code_entries() { return &code_entries_; }
2691cb0ef41Sopenharmony_ci  CodeMap* code_map() { return &code_map_; }
2701cb0ef41Sopenharmony_ci  WeakCodeRegistry* weak_code_registry() { return &weak_code_registry_; }
2711cb0ef41Sopenharmony_ci  size_t GetEstimatedMemoryUsage() const;
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_ci  void ClearCodeMap();
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci private:
2761cb0ef41Sopenharmony_ci  friend class ProfilerEventsProcessor;
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci  void CodeEventHandlerInternal(const CodeEventsContainer& evt_rec);
2791cb0ef41Sopenharmony_ci
2801cb0ef41Sopenharmony_ci  void CreateEntriesForRuntimeCallStats();
2811cb0ef41Sopenharmony_ci  void LogBuiltins();
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ci  ProfilerEventsProcessor* processor() { return processor_; }
2841cb0ef41Sopenharmony_ci
2851cb0ef41Sopenharmony_ci  // Redirects code events to be enqueued on the given events processor.
2861cb0ef41Sopenharmony_ci  void set_processor(ProfilerEventsProcessor* processor) {
2871cb0ef41Sopenharmony_ci    processor_ = processor;
2881cb0ef41Sopenharmony_ci  }
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci  // Stops redirection of code events onto an events processor.
2911cb0ef41Sopenharmony_ci  void clear_processor() { processor_ = nullptr; }
2921cb0ef41Sopenharmony_ci
2931cb0ef41Sopenharmony_ci  Isolate* const isolate_;
2941cb0ef41Sopenharmony_ci  CodeEntryStorage& code_entries_;
2951cb0ef41Sopenharmony_ci  CodeMap code_map_;
2961cb0ef41Sopenharmony_ci  WeakCodeRegistry weak_code_registry_;
2971cb0ef41Sopenharmony_ci  ProfilerEventsProcessor* processor_;
2981cb0ef41Sopenharmony_ci};
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_ci// The CpuProfiler is a sampling CPU profiler for JS frames. It corresponds to
3011cb0ef41Sopenharmony_ci// v8::CpuProfiler at the API level. It spawns an additional thread which is
3021cb0ef41Sopenharmony_ci// responsible for triggering samples and then symbolizing the samples with
3031cb0ef41Sopenharmony_ci// function names. To symbolize on a background thread, the profiler copies
3041cb0ef41Sopenharmony_ci// metadata about generated code off-heap.
3051cb0ef41Sopenharmony_ci//
3061cb0ef41Sopenharmony_ci// Sampling is done using posix signals (except on Windows). The profiling
3071cb0ef41Sopenharmony_ci// thread sends a signal to the main thread, based on a timer. The signal
3081cb0ef41Sopenharmony_ci// handler can interrupt the main thread between any abitrary instructions.
3091cb0ef41Sopenharmony_ci// This means we are very careful about reading stack values during the signal
3101cb0ef41Sopenharmony_ci// handler as we could be in the middle of an operation that is modifying the
3111cb0ef41Sopenharmony_ci// stack.
3121cb0ef41Sopenharmony_ci//
3131cb0ef41Sopenharmony_ci// The story on Windows is similar except we use thread suspend and resume.
3141cb0ef41Sopenharmony_ci//
3151cb0ef41Sopenharmony_ci// Samples are passed to the profiling thread via a circular buffer. The
3161cb0ef41Sopenharmony_ci// profiling thread symbolizes the samples by looking up the code pointers
3171cb0ef41Sopenharmony_ci// against its own list of code objects. The profiling thread also listens for
3181cb0ef41Sopenharmony_ci// code creation/move/deletion events (from the GC), to maintain its list of
3191cb0ef41Sopenharmony_ci// code objects accurately.
3201cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE CpuProfiler {
3211cb0ef41Sopenharmony_ci public:
3221cb0ef41Sopenharmony_ci  explicit CpuProfiler(Isolate* isolate, CpuProfilingNamingMode = kDebugNaming,
3231cb0ef41Sopenharmony_ci                       CpuProfilingLoggingMode = kLazyLogging);
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci  CpuProfiler(Isolate* isolate, CpuProfilingNamingMode naming_mode,
3261cb0ef41Sopenharmony_ci              CpuProfilingLoggingMode logging_mode,
3271cb0ef41Sopenharmony_ci              CpuProfilesCollection* profiles, Symbolizer* test_symbolizer,
3281cb0ef41Sopenharmony_ci              ProfilerEventsProcessor* test_processor,
3291cb0ef41Sopenharmony_ci              ProfilerCodeObserver* test_code_observer);
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci  ~CpuProfiler();
3321cb0ef41Sopenharmony_ci  CpuProfiler(const CpuProfiler&) = delete;
3331cb0ef41Sopenharmony_ci  CpuProfiler& operator=(const CpuProfiler&) = delete;
3341cb0ef41Sopenharmony_ci
3351cb0ef41Sopenharmony_ci  static void CollectSample(Isolate* isolate);
3361cb0ef41Sopenharmony_ci  static size_t GetAllProfilersMemorySize(Isolate* isolate);
3371cb0ef41Sopenharmony_ci
3381cb0ef41Sopenharmony_ci  using ProfilingMode = v8::CpuProfilingMode;
3391cb0ef41Sopenharmony_ci  using CpuProfilingResult = v8::CpuProfilingResult;
3401cb0ef41Sopenharmony_ci  using NamingMode = v8::CpuProfilingNamingMode;
3411cb0ef41Sopenharmony_ci  using LoggingMode = v8::CpuProfilingLoggingMode;
3421cb0ef41Sopenharmony_ci  using StartProfilingStatus = CpuProfilingStatus;
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci  base::TimeDelta sampling_interval() const { return base_sampling_interval_; }
3451cb0ef41Sopenharmony_ci  void set_sampling_interval(base::TimeDelta value);
3461cb0ef41Sopenharmony_ci  void set_use_precise_sampling(bool);
3471cb0ef41Sopenharmony_ci  void CollectSample();
3481cb0ef41Sopenharmony_ci  size_t GetEstimatedMemoryUsage() const;
3491cb0ef41Sopenharmony_ci  CpuProfilingResult StartProfiling(
3501cb0ef41Sopenharmony_ci      CpuProfilingOptions options = {},
3511cb0ef41Sopenharmony_ci      std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
3521cb0ef41Sopenharmony_ci  CpuProfilingResult StartProfiling(
3531cb0ef41Sopenharmony_ci      const char* title, CpuProfilingOptions options = {},
3541cb0ef41Sopenharmony_ci      std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
3551cb0ef41Sopenharmony_ci  CpuProfilingResult StartProfiling(
3561cb0ef41Sopenharmony_ci      String title, CpuProfilingOptions options = {},
3571cb0ef41Sopenharmony_ci      std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ci  CpuProfile* StopProfiling(const char* title);
3601cb0ef41Sopenharmony_ci  CpuProfile* StopProfiling(String title);
3611cb0ef41Sopenharmony_ci  CpuProfile* StopProfiling(ProfilerId id);
3621cb0ef41Sopenharmony_ci
3631cb0ef41Sopenharmony_ci  int GetProfilesCount();
3641cb0ef41Sopenharmony_ci  CpuProfile* GetProfile(int index);
3651cb0ef41Sopenharmony_ci  void DeleteAllProfiles();
3661cb0ef41Sopenharmony_ci  void DeleteProfile(CpuProfile* profile);
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ci  bool is_profiling() const { return is_profiling_; }
3691cb0ef41Sopenharmony_ci
3701cb0ef41Sopenharmony_ci  Symbolizer* symbolizer() const { return symbolizer_.get(); }
3711cb0ef41Sopenharmony_ci  ProfilerEventsProcessor* processor() const { return processor_.get(); }
3721cb0ef41Sopenharmony_ci  Isolate* isolate() const { return isolate_; }
3731cb0ef41Sopenharmony_ci  CodeEntryStorage* code_entries() { return &code_entries_; }
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ci  ProfilerListener* profiler_listener_for_test() const {
3761cb0ef41Sopenharmony_ci    return profiler_listener_.get();
3771cb0ef41Sopenharmony_ci  }
3781cb0ef41Sopenharmony_ci  CodeMap* code_map_for_test() { return code_observer_->code_map(); }
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_ci private:
3811cb0ef41Sopenharmony_ci  void StartProcessorIfNotStarted();
3821cb0ef41Sopenharmony_ci  void StopProcessor();
3831cb0ef41Sopenharmony_ci  void ResetProfiles();
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ci  void EnableLogging();
3861cb0ef41Sopenharmony_ci  void DisableLogging();
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ci  // Computes a sampling interval sufficient to accomodate attached profiles.
3891cb0ef41Sopenharmony_ci  base::TimeDelta ComputeSamplingInterval() const;
3901cb0ef41Sopenharmony_ci  // Dynamically updates the sampler to use a sampling interval sufficient for
3911cb0ef41Sopenharmony_ci  // child profiles.
3921cb0ef41Sopenharmony_ci  void AdjustSamplingInterval();
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_ci  Isolate* const isolate_;
3951cb0ef41Sopenharmony_ci  const NamingMode naming_mode_;
3961cb0ef41Sopenharmony_ci  const LoggingMode logging_mode_;
3971cb0ef41Sopenharmony_ci  bool use_precise_sampling_ = true;
3981cb0ef41Sopenharmony_ci  // Sampling interval to which per-profile sampling intervals will be clamped
3991cb0ef41Sopenharmony_ci  // to a multiple of, or used as the default if unspecified.
4001cb0ef41Sopenharmony_ci  base::TimeDelta base_sampling_interval_;
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ci  // Storage for CodeEntry objects allocated by the profiler. May live for
4031cb0ef41Sopenharmony_ci  // multiple profiling sessions, independent of heap listener state.
4041cb0ef41Sopenharmony_ci  CodeEntryStorage code_entries_;
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci  std::unique_ptr<ProfilerCodeObserver> code_observer_;
4071cb0ef41Sopenharmony_ci  std::unique_ptr<CpuProfilesCollection> profiles_;
4081cb0ef41Sopenharmony_ci  std::unique_ptr<Symbolizer> symbolizer_;
4091cb0ef41Sopenharmony_ci  std::unique_ptr<ProfilerEventsProcessor> processor_;
4101cb0ef41Sopenharmony_ci  std::unique_ptr<ProfilerListener> profiler_listener_;
4111cb0ef41Sopenharmony_ci  std::unique_ptr<ProfilingScope> profiling_scope_;
4121cb0ef41Sopenharmony_ci  bool is_profiling_;
4131cb0ef41Sopenharmony_ci};
4141cb0ef41Sopenharmony_ci
4151cb0ef41Sopenharmony_ci}  // namespace internal
4161cb0ef41Sopenharmony_ci}  // namespace v8
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_ci#endif  // V8_PROFILER_CPU_PROFILER_H_
419