forked from google/pprof-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwall.hh
More file actions
179 lines (148 loc) · 5.45 KB
/
Copy pathwall.hh
File metadata and controls
179 lines (148 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright 2023 Datadog, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "contexts.hh"
#include "thread-cpu-clock.hh"
#include <nan.h>
#include <v8-profiler.h>
#include <atomic>
#include <memory>
#include <unordered_map>
#include <utility>
namespace dd {
struct Result {
Result() = default;
explicit Result(const char* msg) : success{false}, msg{msg} {};
bool success = true;
std::string msg;
};
class WallProfiler : public Nan::ObjectWrap {
public:
enum class CollectionMode { kNoCollect, kPassThrough, kCollectContexts };
private:
enum Fields { kSampleCount, kFieldCount };
using ContextPtr = std::shared_ptr<v8::Global<v8::Value>>;
std::chrono::microseconds samplingPeriod_{0};
v8::CpuProfiler* cpuProfiler_ = nullptr;
v8::Isolate* isolate_ = nullptr;
// TODO: Investigate use of v8::Persistent instead of shared_ptr<Global> to
// avoid heap allocation. Need to figure out the right move/copy semantics in
// and out of the ring buffer.
// We're using a pair of shared pointers and an atomic pointer-to-current as
// a way to ensure signal safety on update.
ContextPtr context1_;
ContextPtr context2_;
std::atomic<ContextPtr*> curContext_;
std::atomic<int> gcCount = 0;
double gcAsyncId;
std::atomic<CollectionMode> collectionMode_;
std::atomic<uint64_t> noCollectCallCount_;
v8::ProfilerId profileId_;
uint64_t profileIdx_ = 0;
bool includeLines_ = false;
bool withContexts_ = false;
bool started_ = false;
bool workaroundV8Bug_;
static inline constexpr bool detectV8Bug_ = true;
bool collectCpuTime_;
bool collectAsyncId_;
bool isMainThread_;
int v8ProfilerStuckEventLoopDetected_ = 0;
ProcessCpuClock::time_point startProcessCpuTime_{};
int64_t startThreadCpuTime_ = 0;
/* threadCpuStopWatch_ is used to measure CPU consumed by JS thread owning the
* WallProfiler object during profiling period of main worker thread. */
ThreadCpuStopWatch threadCpuStopWatch_;
uint32_t* fields_;
v8::Global<v8::Uint32Array> jsArray_;
struct SampleContext {
ContextPtr context;
int64_t time_from;
int64_t time_to;
int64_t cpu_time;
double async_id;
};
using ContextBuffer = std::vector<SampleContext>;
ContextBuffer contexts_;
~WallProfiler() = default;
void Dispose(bool removeFromMap);
// A new CPU profiler object will be created each time profiling is started
// to work around https://bugs.chromium.org/p/v8/issues/detail?id=11051.
v8::CpuProfiler* CreateV8CpuProfiler();
ContextsByNode GetContextsByNode(v8::CpuProfile* profile,
ContextBuffer& contexts,
int64_t startCpuTime);
bool waitForSignal(uint64_t targetCallCount = 0);
static void CleanupHook(void* data);
void Cleanup();
v8::Local<v8::Uint32Array> GetSharedArray() const {
return jsArray_.Get(isolate_);
}
public:
/**
* @param samplingPeriodMicros sampling interval, in microseconds
* @param durationMicros the duration of sampling, in microseconds. This
* parameter is informative; it is up to the caller to call the Stop method
* every period. The parameter is used to preallocate data structures that
* should not be reallocated in async signal safe code.
*/
explicit WallProfiler(std::chrono::microseconds samplingPeriod,
std::chrono::microseconds duration,
bool includeLines,
bool withContexts,
bool workaroundV8bug,
bool collectCpuTime,
bool collectAsyncId,
bool isMainThread);
v8::Local<v8::Value> GetContext();
void SetContext(v8::Local<v8::Value>);
void PushContext(int64_t time_from,
int64_t time_to,
int64_t cpu_time,
double async_id);
Result StartImpl();
v8::ProfilerId StartInternal();
Result StopImpl(bool restart, v8::Local<v8::Value>& profile);
CollectionMode collectionMode() {
auto res = collectionMode_.load(std::memory_order_relaxed);
if (res == CollectionMode::kNoCollect) {
noCollectCallCount_.fetch_add(1, std::memory_order_relaxed);
}
std::atomic_signal_fence(std::memory_order_acquire);
return res;
}
bool collectCpuTime() const { return collectCpuTime_; }
bool interceptSignal() const { return withContexts_ || workaroundV8Bug_; }
int v8ProfilerStuckEventLoopDetected() const {
return v8ProfilerStuckEventLoopDetected_;
}
ThreadCpuClock::duration GetAndResetThreadCpu() {
return threadCpuStopWatch_.GetAndReset();
}
double GetAsyncId();
void OnGCStart();
void OnGCEnd();
static NAN_METHOD(New);
static NAN_METHOD(Start);
static NAN_METHOD(Stop);
static NAN_METHOD(V8ProfilerStuckEventLoopDetected);
static NAN_METHOD(Dispose);
static NAN_MODULE_INIT(Init);
static NAN_GETTER(GetContext);
static NAN_SETTER(SetContext);
static NAN_GETTER(SharedArrayGetter);
};
} // namespace dd