Skip to content

Commit 4c5bcd7

Browse files
committed
add guard to profiler
1 parent d553e2f commit 4c5bcd7

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

paddle/fluid/platform/profiler.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ void PopEvent(const std::string& name, const DeviceContext* dev_ctx) {
176176

177177
RecordEvent::RecordEvent(const std::string& name, const DeviceContext* dev_ctx)
178178
: is_enabled_(false), start_ns_(PosixInNsec()) {
179+
std::lock_guard<std::mutex> l(profiler_mu);
179180
if (g_state == ProfilerState::kDisabled) return;
180181
is_enabled_ = true;
181182
dev_ctx_ = dev_ctx;
@@ -186,6 +187,7 @@ RecordEvent::RecordEvent(const std::string& name, const DeviceContext* dev_ctx)
186187
}
187188

188189
RecordEvent::~RecordEvent() {
190+
std::lock_guard<std::mutex> l(profiler_mu);
189191
if (g_state == ProfilerState::kDisabled || !is_enabled_) return;
190192
DeviceTracer* tracer = GetDeviceTracer();
191193
if (tracer) {
@@ -198,13 +200,15 @@ RecordEvent::~RecordEvent() {
198200

199201
RecordBlock::RecordBlock(int block_id)
200202
: is_enabled_(false), start_ns_(PosixInNsec()) {
203+
std::lock_guard<std::mutex> l(profiler_mu);
201204
if (g_state == ProfilerState::kDisabled) return;
202205
is_enabled_ = true;
203206
SetCurBlock(block_id);
204207
name_ = string::Sprintf("block_%d", block_id);
205208
}
206209

207210
RecordBlock::~RecordBlock() {
211+
std::lock_guard<std::mutex> l(profiler_mu);
208212
if (g_state == ProfilerState::kDisabled || !is_enabled_) return;
209213
DeviceTracer* tracer = GetDeviceTracer();
210214
if (tracer) {
@@ -217,11 +221,13 @@ RecordBlock::~RecordBlock() {
217221
}
218222

219223
RecordThread::RecordThread(int thread_id) {
224+
std::lock_guard<std::mutex> l(profiler_mu);
220225
if (g_state == ProfilerState::kDisabled) return;
221226
SetCurThread(thread_id);
222227
}
223228

224229
RecordThread::~RecordThread() {
230+
std::lock_guard<std::mutex> l(profiler_mu);
225231
if (g_state == ProfilerState::kDisabled) return;
226232
ClearCurThread();
227233
}

0 commit comments

Comments
 (0)