Skip to content

Commit b3dac00

Browse files
authored
[libomptarget] [ompt] Added envars controlling buffer flush. (llvm#2693)
2 parents f3128a9 + 1e4eb9f commit b3dac00

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

offload/include/OpenMP/OMPT/OmptTracingBuffer.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#include <omp-tools.h>
3333

34+
#include "Shared/EnvironmentVar.h"
35+
3436
// Maximum number of devices supported in device tracing. No device tracing
3537
// will be performed for any device-id larger than 1023.
3638
#define MAX_NUM_DEVICES 1024
@@ -102,6 +104,12 @@ class OmptTracingBufferMgr {
102104
using BufPtr = std::shared_ptr<Buffer>;
103105

104106
private:
107+
/// Envar to control whether a buffer should be flushed when it gets full.
108+
BoolEnvar OMPX_FlushOnBufferFull;
109+
110+
/// Envar to control whether all buffers should be flushed during shutdown.
111+
BoolEnvar OMPX_FlushOnShutdown;
112+
105113
// Internal variable for tracking threads to wait for flush
106114
uint32_t ThreadFlushTracker;
107115

@@ -216,8 +224,8 @@ class OmptTracingBufferMgr {
216224

217225
/// Called when a buffer \p Buf may be flushed with \p Cursor as the
218226
/// last allocated trace record in the buffer.
219-
/// setComplete should be called without holding any lock.
220-
void setComplete(void *Cursor, BufPtr Buf);
227+
/// triggerFlushOnBufferFull should be called without holding any lock.
228+
void triggerFlushOnBufferFull(void *Cursor, BufPtr Buf);
221229

222230
// Called to dispatch buffer-completion callbacks for the trace records in
223231
// this buffer
@@ -350,7 +358,14 @@ class OmptTracingBufferMgr {
350358
void destroyHelperThreads();
351359

352360
public:
353-
OmptTracingBufferMgr();
361+
OmptTracingBufferMgr()
362+
: OMPX_FlushOnBufferFull("LIBOMPTARGET_OMPT_FLUSH_ON_BUFFER_FULL", true),
363+
OMPX_FlushOnShutdown("LIBOMPTARGET_OMPT_FLUSH_ON_SHUTDOWN", true) {
364+
// no need to hold locks for init() since object is getting constructed
365+
// here.
366+
init();
367+
}
368+
354369
OmptTracingBufferMgr(const OmptTracingBufferMgr &) = delete;
355370
OmptTracingBufferMgr &operator=(const OmptTracingBufferMgr &) = delete;
356371

offload/src/OpenMP/OMPT/OmptTracingBuffer.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ void *OmptTracingBufferMgr::assignCursor(ompt_callbacks_t Type,
129129
lck.unlock();
130130

131131
// Schedule the full buffer for flushing till the corresponding cursor.
132-
if (ToBeFlushedCursor)
133-
setComplete(ToBeFlushedCursor, ToBeFlushedBuf);
132+
if (OMPX_FlushOnBufferFull && ToBeFlushedCursor)
133+
triggerFlushOnBufferFull(ToBeFlushedCursor, ToBeFlushedBuf);
134134

135135
DP("Thread %lu: Assigned %lu bytes at %p in new buffer with id %lu for "
136136
"device %ld\n",
@@ -148,10 +148,10 @@ void *OmptTracingBufferMgr::assignCursor(ompt_callbacks_t Type,
148148
* called without holding any lock.
149149
* Note lock order: buf_lock -> flush_lock
150150
*/
151-
void OmptTracingBufferMgr::setComplete(void *cursor, BufPtr Buf) {
151+
void OmptTracingBufferMgr::triggerFlushOnBufferFull(void *cursor, BufPtr Buf) {
152152
std::unique_lock<std::mutex> buf_lock(BufferMgrMutex);
153153

154-
// Between calling setComplete and this check, a flush-all may have
154+
// Between calling this function and this check, a flush-all may have
155155
// delivered this buffer to the tool and deleted it. So the buffer
156156
// may not exist.
157157
if (Id2BufferMap.find(Buf->Id) == Id2BufferMap.end())
@@ -729,7 +729,8 @@ void OmptTracingBufferMgr::shutdownHelperThreads() {
729729
void OmptTracingBufferMgr::flushAndShutdownHelperThreads() {
730730
std::unique_lock<std::mutex> Lock(llvm::omp::target::ompt::TraceControlMutex);
731731
// Flush buffers for all devices.
732-
flushAllBuffers(MAX_NUM_DEVICES);
732+
if (OMPX_FlushOnShutdown)
733+
flushAllBuffers(MAX_NUM_DEVICES);
733734
shutdownHelperThreads();
734735
}
735736

@@ -747,9 +748,4 @@ void OmptTracingBufferMgr::destroyHelperThreads() {
747748
CompletionThreads.clear();
748749
HelperThreadIdMap.clear();
749750
}
750-
751-
OmptTracingBufferMgr::OmptTracingBufferMgr() {
752-
// no need to hold locks for init() since object is getting constructed here
753-
init();
754-
}
755751
#endif

0 commit comments

Comments
 (0)