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