|
| 1 | +//===- llvm/Support/TimeProfiler.h - Hierarchical Time Profiler -*- C++ -*-===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef LLVM_SUPPORT_TIME_PROFILER_H |
| 11 | +#define LLVM_SUPPORT_TIME_PROFILER_H |
| 12 | + |
| 13 | +#include "llvm/ADT/STLExtras.h" |
| 14 | +#include "llvm/Support/raw_ostream.h" |
| 15 | + |
| 16 | +namespace llvm { |
| 17 | + |
| 18 | +struct TimeTraceProfiler; |
| 19 | +extern TimeTraceProfiler *TimeTraceProfilerInstance; |
| 20 | + |
| 21 | +/// Initialize the time trace profiler. |
| 22 | +/// This sets up the global \p TimeTraceProfilerInstance |
| 23 | +/// variable to be the profiler instance. |
| 24 | +void timeTraceProfilerInitialize(); |
| 25 | + |
| 26 | +/// Cleanup the time trace profiler, if it was initialized. |
| 27 | +void timeTraceProfilerCleanup(); |
| 28 | + |
| 29 | +/// Is the time trace profiler enabled, i.e. initialized? |
| 30 | +inline bool timeTraceProfilerEnabled() { |
| 31 | + return TimeTraceProfilerInstance != nullptr; |
| 32 | +} |
| 33 | + |
| 34 | +/// Write profiling data to output file. |
| 35 | +/// Data produced is JSON, in Chrome "Trace Event" format, see |
| 36 | +/// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview |
| 37 | +void timeTraceProfilerWrite(raw_ostream &OS); |
| 38 | + |
| 39 | +/// Manually begin a time section, with the given \p Name and \p Detail. |
| 40 | +/// Profiler copies the string data, so the pointers can be given into |
| 41 | +/// temporaries. Time sections can be hierarchical; every Begin must have a |
| 42 | +/// matching End pair but they can nest. |
| 43 | +void timeTraceProfilerBegin(StringRef Name, StringRef Detail); |
| 44 | +void timeTraceProfilerBegin(StringRef Name, |
| 45 | + llvm::function_ref<std::string()> Detail); |
| 46 | + |
| 47 | +/// Manually end the last time section. |
| 48 | +void timeTraceProfilerEnd(); |
| 49 | + |
| 50 | +/// The TimeTraceScope is a helper class to call the begin and end functions |
| 51 | +/// of the time trace profiler. When the object is constructed, it begins |
| 52 | +/// the section; and when it is destroyed, it stops it. If the time profiler |
| 53 | +/// is not initialized, the overhead is a single branch. |
| 54 | +struct TimeTraceScope { |
| 55 | + TimeTraceScope(StringRef Name, StringRef Detail) { |
| 56 | + if (TimeTraceProfilerInstance != nullptr) |
| 57 | + timeTraceProfilerBegin(Name, Detail); |
| 58 | + } |
| 59 | + TimeTraceScope(StringRef Name, llvm::function_ref<std::string()> Detail) { |
| 60 | + if (TimeTraceProfilerInstance != nullptr) |
| 61 | + timeTraceProfilerBegin(Name, Detail); |
| 62 | + } |
| 63 | + ~TimeTraceScope() { |
| 64 | + if (TimeTraceProfilerInstance != nullptr) |
| 65 | + timeTraceProfilerEnd(); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +} // end namespace llvm |
| 70 | + |
| 71 | +#endif |
0 commit comments