Skip to content

Commit b8b2a27

Browse files
authored
[OpenMP][NFC] Encapsulate profiling logic (#74003)
This simply puts the profiling logic into the `Profiler` class and allows non-RAII profiling via `beginSection` and `endSection`.
1 parent 3dbac2c commit b8b2a27

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

openmp/docs/design/Runtimes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ variables is defined below.
708708

709709
* ``LIBOMPTARGET_DEBUG=<Num>``
710710
* ``LIBOMPTARGET_PROFILE=<Filename>``
711+
* ``LIBOMPTARGET_PROFILE_GRANULARITY=<Num> (default 500, in us)``
711712
* ``LIBOMPTARGET_MEMORY_MANAGER_THRESHOLD=<Num>``
712713
* ``LIBOMPTARGET_INFO=<Num>``
713714
* ``LIBOMPTARGET_HEAP_SIZE=<Num>``
@@ -749,6 +750,12 @@ for time trace output. Note that this will turn ``libomp`` into a C++ library.
749750

750751
.. _`LLVM Support Library`: https://llvm.org/docs/SupportLibrary.html
751752

753+
LIBOMPTARGET_PROFILE_GRANULARITY
754+
""""""""""""""""""""""""""""""""
755+
756+
``LIBOMPTARGET_PROFILE_GRANULARITY`` allows to change the time profile
757+
granularity measured in `us`. Default is 500 (`us`).
758+
752759
LIBOMPTARGET_MEMORY_MANAGER_THRESHOLD
753760
"""""""""""""""""""""""""""""""""""""
754761

openmp/libomptarget/include/Shared/Profile.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,70 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#ifndef OMPTARGET_SHARED_PROFILE_H
14+
#define OMPTARGET_SHARED_PROFILE_H
15+
16+
#include "Shared/Debug.h"
17+
#include "Shared/EnvironmentVar.h"
18+
19+
#include "llvm/ADT/StringRef.h"
20+
#include "llvm/Support/Error.h"
1321
#include "llvm/Support/TimeProfiler.h"
1422

23+
/// Class that holds the singleton profiler and allows to start/end events.
24+
class Profiler {
25+
26+
Profiler() {
27+
if (!ProfileTraceFile.isPresent())
28+
return;
29+
30+
// TODO: Add an alias without LIBOMPTARGET
31+
// Flag to modify the profile granularity (in us).
32+
Int32Envar ProfileGranularity =
33+
Int32Envar("LIBOMPTARGET_PROFILE_GRANULARITY", 500);
34+
35+
llvm::timeTraceProfilerInitialize(ProfileGranularity /* us */,
36+
"libomptarget");
37+
}
38+
39+
~Profiler() {
40+
if (!ProfileTraceFile.isPresent())
41+
return;
42+
43+
if (auto Err = llvm::timeTraceProfilerWrite(ProfileTraceFile.get(), "-"))
44+
REPORT("Error writing out the time trace: %s\n",
45+
llvm::toString(std::move(Err)).c_str());
46+
47+
llvm::timeTraceProfilerCleanup();
48+
}
49+
50+
// TODO: Add an alias without LIBOMPTARGET
51+
/// Flag to enable profiling which also specifies the file profile information
52+
/// is stored in.
53+
StringEnvar ProfileTraceFile = StringEnvar("LIBOMPTARGET_PROFILE");
54+
55+
public:
56+
static Profiler &get() {
57+
static Profiler P;
58+
return P;
59+
}
60+
61+
/// Manually begin a time section, with the given \p Name and \p Detail.
62+
/// Profiler copies the string data, so the pointers can be given into
63+
/// temporaries. Time sections can be hierarchical; every Begin must have a
64+
/// matching End pair but they can nest.
65+
void beginSection(llvm::StringRef Name, llvm::StringRef Detail) {
66+
llvm::timeTraceProfilerBegin(Name, Detail);
67+
}
68+
void beginSection(llvm::StringRef Name,
69+
llvm::function_ref<std::string()> Detail) {
70+
llvm::timeTraceProfilerBegin(Name, Detail);
71+
}
72+
73+
/// Manually end the last time section.
74+
void endSection() { llvm::timeTraceProfilerEnd(); }
75+
};
76+
1577
/// Time spend in the current scope, assigned to the function name.
1678
#define TIMESCOPE() llvm::TimeTraceScope TimeScope(__FUNCTION__)
1779

@@ -34,3 +96,5 @@
3496
std::string ProfileLocation = SI.getProfileLocation(); \
3597
std::string RTM = RegionTypeMsg; \
3698
llvm::TimeTraceScope TimeScope(__FUNCTION__, ProfileLocation + RTM)
99+
100+
#endif // OMPTARGET_SHARED_PROFILE_H

openmp/libomptarget/src/rtl.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ static const char *RTLNames[] = {
4141
/* AMDGPU target */ "libomptarget.rtl.amdgpu",
4242
};
4343

44-
static char *ProfileTraceFile = nullptr;
45-
4644
#ifdef OMPT_SUPPORT
4745
extern void ompt::connectLibrary();
4846
#endif
@@ -64,31 +62,19 @@ __attribute__((constructor(101))) void init() {
6462

6563
PM = new PluginManager(UseEventsForAtomicTransfers);
6664

67-
ProfileTraceFile = getenv("LIBOMPTARGET_PROFILE");
68-
// TODO: add a configuration option for time granularity
69-
if (ProfileTraceFile)
70-
timeTraceProfilerInitialize(500 /* us */, "libomptarget");
71-
7265
#ifdef OMPT_SUPPORT
7366
// Initialize OMPT first
7467
ompt::connectLibrary();
7568
#endif
7669

70+
Profiler::get();
7771
PM->RTLs.loadRTLs();
7872
PM->registerDelayedLibraries();
7973
}
8074

8175
__attribute__((destructor(101))) void deinit() {
8276
DP("Deinit target library!\n");
8377
delete PM;
84-
85-
if (ProfileTraceFile) {
86-
// TODO: add env var for file output
87-
if (auto E = timeTraceProfilerWrite(ProfileTraceFile, "-"))
88-
fprintf(stderr, "Error writing out the time trace\n");
89-
90-
timeTraceProfilerCleanup();
91-
}
9278
}
9379

9480
void PluginAdaptorManagerTy::loadRTLs() {

0 commit comments

Comments
 (0)