|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
| 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" |
13 | 21 | #include "llvm/Support/TimeProfiler.h"
|
14 | 22 |
|
| 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 | + |
15 | 77 | /// Time spend in the current scope, assigned to the function name.
|
16 | 78 | #define TIMESCOPE() llvm::TimeTraceScope TimeScope(__FUNCTION__)
|
17 | 79 |
|
|
34 | 96 | std::string ProfileLocation = SI.getProfileLocation(); \
|
35 | 97 | std::string RTM = RegionTypeMsg; \
|
36 | 98 | llvm::TimeTraceScope TimeScope(__FUNCTION__, ProfileLocation + RTM)
|
| 99 | + |
| 100 | +#endif // OMPTARGET_SHARED_PROFILE_H |
0 commit comments