Skip to content

Commit 94c0a64

Browse files
committed
Fix a profiler race condition
In multi-thread condition, EnableProfiler can be called after RecordEvent is constructed. In this case, RecordEvent constructor will not init anything, but RecordEvent destructor will do something since EnableProfiler was called. This PR fixes it.
1 parent ca5ea65 commit 94c0a64

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

paddle/fluid/platform/profiler.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ void PopEvent(const std::string& name, const DeviceContext* dev_ctx) {
173173
}
174174

175175
RecordEvent::RecordEvent(const std::string& name, const DeviceContext* dev_ctx)
176-
: start_ns_(PosixInNsec()) {
176+
: is_enabled_(false), start_ns_(PosixInNsec()) {
177177
if (g_state == ProfilerState::kDisabled) return;
178+
is_enabled_ = true;
178179
dev_ctx_ = dev_ctx;
179180
name_ = name;
180181
PushEvent(name_, dev_ctx_);
@@ -183,7 +184,7 @@ RecordEvent::RecordEvent(const std::string& name, const DeviceContext* dev_ctx)
183184
}
184185

185186
RecordEvent::~RecordEvent() {
186-
if (g_state == ProfilerState::kDisabled) return;
187+
if (g_state == ProfilerState::kDisabled || !is_enabled_) return;
187188
DeviceTracer* tracer = GetDeviceTracer();
188189
if (tracer) {
189190
tracer->AddCPURecords(CurAnnotation(), start_ns_, PosixInNsec(),
@@ -193,14 +194,16 @@ RecordEvent::~RecordEvent() {
193194
PopEvent(name_, dev_ctx_);
194195
}
195196

196-
RecordBlock::RecordBlock(int block_id) : start_ns_(PosixInNsec()) {
197+
RecordBlock::RecordBlock(int block_id)
198+
: is_enabled_(false), start_ns_(PosixInNsec()) {
197199
if (g_state == ProfilerState::kDisabled) return;
200+
is_enabled_ = true;
198201
SetCurBlock(block_id);
199202
name_ = string::Sprintf("block_%d", block_id);
200203
}
201204

202205
RecordBlock::~RecordBlock() {
203-
if (g_state == ProfilerState::kDisabled) return;
206+
if (g_state == ProfilerState::kDisabled || !is_enabled_) return;
204207
DeviceTracer* tracer = GetDeviceTracer();
205208
if (tracer) {
206209
// We try to put all blocks at the same nested depth in the

paddle/fluid/platform/profiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct RecordEvent {
7474

7575
~RecordEvent();
7676

77+
bool is_enabled_;
7778
uint64_t start_ns_;
7879
// The device context is used by Event to get the current cuda stream.
7980
const DeviceContext* dev_ctx_;
@@ -89,6 +90,7 @@ struct RecordBlock {
8990
~RecordBlock();
9091

9192
private:
93+
bool is_enabled_;
9294
std::string name_;
9395
uint64_t start_ns_;
9496
};

0 commit comments

Comments
 (0)