1// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_INSPECTOR_V8_PROFILER_AGENT_IMPL_H_
6#define V8_INSPECTOR_V8_PROFILER_AGENT_IMPL_H_
7
8#include <memory>
9#include <vector>
10
11#include "src/base/macros.h"
12#include "src/inspector/protocol/Forward.h"
13#include "src/inspector/protocol/Profiler.h"
14
15namespace v8 {
16class CpuProfiler;
17class Isolate;
18}  // namespace v8
19
20namespace v8_inspector {
21
22class V8InspectorSessionImpl;
23
24using protocol::Maybe;
25using protocol::Response;
26
27class V8ProfilerAgentImpl : public protocol::Profiler::Backend {
28 public:
29  V8ProfilerAgentImpl(V8InspectorSessionImpl*, protocol::FrontendChannel*,
30                      protocol::DictionaryValue* state);
31  ~V8ProfilerAgentImpl() override;
32  V8ProfilerAgentImpl(const V8ProfilerAgentImpl&) = delete;
33  V8ProfilerAgentImpl& operator=(const V8ProfilerAgentImpl&) = delete;
34
35  bool enabled() const { return m_enabled; }
36  void restore();
37
38  Response enable() override;
39  Response disable() override;
40  Response setSamplingInterval(int) override;
41  Response start() override;
42  Response stop(std::unique_ptr<protocol::Profiler::Profile>*) override;
43
44  Response startPreciseCoverage(Maybe<bool> binary, Maybe<bool> detailed,
45                                Maybe<bool> allow_triggered_updates,
46                                double* out_timestamp) override;
47  Response stopPreciseCoverage() override;
48  Response takePreciseCoverage(
49      std::unique_ptr<protocol::Array<protocol::Profiler::ScriptCoverage>>*
50          out_result,
51      double* out_timestamp) override;
52  Response getBestEffortCoverage(
53      std::unique_ptr<protocol::Array<protocol::Profiler::ScriptCoverage>>*
54          out_result) override;
55
56  Response startTypeProfile() override;
57  Response stopTypeProfile() override;
58  Response takeTypeProfile(
59      std::unique_ptr<protocol::Array<protocol::Profiler::ScriptTypeProfile>>*
60          out_result) override;
61
62  void consoleProfile(const String16& title);
63  void consoleProfileEnd(const String16& title);
64
65  void triggerPreciseCoverageDeltaUpdate(const String16& occasion);
66
67 private:
68  String16 nextProfileId();
69
70  void startProfiling(const String16& title);
71  std::unique_ptr<protocol::Profiler::Profile> stopProfiling(
72      const String16& title, bool serialize);
73
74  V8InspectorSessionImpl* m_session;
75  v8::Isolate* m_isolate;
76  v8::CpuProfiler* m_profiler = nullptr;
77  protocol::DictionaryValue* m_state;
78  protocol::Profiler::Frontend m_frontend;
79  bool m_enabled = false;
80  bool m_recordingCPUProfile = false;
81  class ProfileDescriptor;
82  std::vector<ProfileDescriptor> m_startedProfiles;
83  String16 m_frontendInitiatedProfileId;
84  int m_startedProfilesCount = 0;
85};
86
87}  // namespace v8_inspector
88
89#endif  // V8_INSPECTOR_V8_PROFILER_AGENT_IMPL_H_
90