xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Support/TimeProfiler.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 // This provides lightweight and dependency-free machinery to trace execution
10 // time around arbitrary code. Two API flavors are available.
11 //
12 // The primary API uses a RAII object to trigger tracing:
13 //
14 // \code
15 //   {
16 //     TimeTraceScope scope("my_event_name");
17 //     ...my code...
18 //   }
19 // \endcode
20 //
21 // If the code to be profiled does not have a natural lexical scope then
22 // it is also possible to start and end events with respect to an implicit
23 // per-thread stack of profiling entries:
24 //
25 // \code
26 //   timeTraceProfilerBegin("my_event_name");
27 //   ...my code...
28 //   timeTraceProfilerEnd();  // must be called on all control flow paths
29 // \endcode
30 //
31 // Time profiling entries can be given an arbitrary name and, optionally,
32 // an arbitrary 'detail' string. The resulting trace will include 'Total'
33 // entries summing the time spent for each name. Thus, it's best to choose
34 // names to be fairly generic, and rely on the detail field to capture
35 // everything else of interest.
36 //
37 // To avoid lifetime issues name and detail strings are copied into the event
38 // entries at their time of creation. Care should be taken to make string
39 // construction cheap to prevent 'Heisenperf' effects. In particular, the
40 // 'detail' argument may be a string-returning closure:
41 //
42 // \code
43 //   int n;
44 //   {
45 //     TimeTraceScope scope("my_event_name",
46 //                          [n]() { return (Twine("x=") + Twine(n)).str(); });
47 //     ...my code...
48 //   }
49 // \endcode
50 // The closure will not be called if tracing is disabled. Otherwise, the
51 // resulting string will be directly moved into the entry.
52 //
53 // The main process should begin with a timeTraceProfilerInitialize, and
54 // finish with timeTraceProfileWrite and timeTraceProfilerCleanup calls.
55 // Each new thread should begin with a timeTraceProfilerInitialize, and
56 // finish with a timeTraceProfilerFinishThread call.
57 //
58 // Timestamps come from std::chrono::stable_clock. Note that threads need
59 // not see the same time from that clock, and the resolution may not be
60 // the best available.
61 //
62 // Currently, there are a number of compatible viewers:
63 //  - chrome://tracing is the original chromium trace viewer.
64 //  - http://ui.perfetto.dev is the replacement for the above, under active
65 //    development by Google as part of the 'Perfetto' project.
66 //  - https://www.speedscope.app/ has also been reported as an option.
67 //
68 // Future work:
69 //  - Support akin to LLVM_DEBUG for runtime enable/disable of named tracing
70 //    families for non-debug builds which wish to support optional tracing.
71 //  - Evaluate the detail closures at profile write time to avoid
72 //    stringification costs interfering with tracing.
73 //
74 //===----------------------------------------------------------------------===//
75 
76 #ifndef LLVM_SUPPORT_TIMEPROFILER_H
77 #define LLVM_SUPPORT_TIMEPROFILER_H
78 
79 #include "llvm/ADT/STLFunctionalExtras.h"
80 #include "llvm/Support/Error.h"
81 
82 namespace llvm {
83 
84 class raw_pwrite_stream;
85 
86 struct TimeTraceMetadata {
87   std::string Detail;
88   // Source file and line number information for the event.
89   std::string File;
90   int Line = 0;
91 
isEmptyTimeTraceMetadata92   bool isEmpty() const { return Detail.empty() && File.empty(); }
93 };
94 
95 struct TimeTraceProfiler;
96 TimeTraceProfiler *getTimeTraceProfilerInstance();
97 
98 bool isTimeTraceVerbose();
99 
100 struct TimeTraceProfilerEntry;
101 
102 /// Initialize the time trace profiler.
103 /// This sets up the global \p TimeTraceProfilerInstance
104 /// variable to be the profiler instance.
105 void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
106                                  StringRef ProcName,
107                                  bool TimeTraceVerbose = false);
108 
109 /// Cleanup the time trace profiler, if it was initialized.
110 void timeTraceProfilerCleanup();
111 
112 /// Finish a time trace profiler running on a worker thread.
113 void timeTraceProfilerFinishThread();
114 
115 /// Is the time trace profiler enabled, i.e. initialized?
timeTraceProfilerEnabled()116 inline bool timeTraceProfilerEnabled() {
117   return getTimeTraceProfilerInstance() != nullptr;
118 }
119 
120 /// Write profiling data to output stream.
121 /// Data produced is JSON, in Chrome "Trace Event" format, see
122 /// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
123 void timeTraceProfilerWrite(raw_pwrite_stream &OS);
124 
125 /// Write profiling data to a file.
126 /// The function will write to \p PreferredFileName if provided, if not
127 /// then will write to \p FallbackFileName appending .time-trace.
128 /// Returns a StringError indicating a failure if the function is
129 /// unable to open the file for writing.
130 Error timeTraceProfilerWrite(StringRef PreferredFileName,
131                              StringRef FallbackFileName);
132 
133 /// Manually begin a time section, with the given \p Name and \p Detail.
134 /// Profiler copies the string data, so the pointers can be given into
135 /// temporaries. Time sections can be hierarchical; every Begin must have a
136 /// matching End pair but they can nest.
137 TimeTraceProfilerEntry *timeTraceProfilerBegin(StringRef Name,
138                                                StringRef Detail);
139 TimeTraceProfilerEntry *
140 timeTraceProfilerBegin(StringRef Name,
141                        llvm::function_ref<std::string()> Detail);
142 
143 TimeTraceProfilerEntry *
144 timeTraceProfilerBegin(StringRef Name,
145                        llvm::function_ref<TimeTraceMetadata()> MetaData);
146 
147 /// Manually begin a time section, with the given \p Name and \p Detail.
148 /// This starts Async Events having \p Name as a category which is shown
149 /// separately from other traces. See
150 /// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.jh64i9l3vwa1
151 /// for more details.
152 TimeTraceProfilerEntry *timeTraceAsyncProfilerBegin(StringRef Name,
153                                                     StringRef Detail);
154 
155 /// Manually end the last time section.
156 void timeTraceProfilerEnd();
157 void timeTraceProfilerEnd(TimeTraceProfilerEntry *E);
158 
159 /// The TimeTraceScope is a helper class to call the begin and end functions
160 /// of the time trace profiler.  When the object is constructed, it begins
161 /// the section; and when it is destroyed, it stops it. If the time profiler
162 /// is not initialized, the overhead is a single branch.
163 class TimeTraceScope {
164 public:
165   TimeTraceScope() = delete;
166   TimeTraceScope(const TimeTraceScope &) = delete;
167   TimeTraceScope &operator=(const TimeTraceScope &) = delete;
168   TimeTraceScope(TimeTraceScope &&) = delete;
169   TimeTraceScope &operator=(TimeTraceScope &&) = delete;
170 
TimeTraceScope(StringRef Name)171   TimeTraceScope(StringRef Name) {
172     if (getTimeTraceProfilerInstance() != nullptr)
173       Entry = timeTraceProfilerBegin(Name, StringRef(""));
174   }
TimeTraceScope(StringRef Name,StringRef Detail)175   TimeTraceScope(StringRef Name, StringRef Detail) {
176     if (getTimeTraceProfilerInstance() != nullptr)
177       Entry = timeTraceProfilerBegin(Name, Detail);
178   }
TimeTraceScope(StringRef Name,llvm::function_ref<std::string ()> Detail)179   TimeTraceScope(StringRef Name, llvm::function_ref<std::string()> Detail) {
180     if (getTimeTraceProfilerInstance() != nullptr)
181       Entry = timeTraceProfilerBegin(Name, Detail);
182   }
TimeTraceScope(StringRef Name,llvm::function_ref<TimeTraceMetadata ()> Metadata)183   TimeTraceScope(StringRef Name,
184                  llvm::function_ref<TimeTraceMetadata()> Metadata) {
185     if (getTimeTraceProfilerInstance() != nullptr)
186       Entry = timeTraceProfilerBegin(Name, Metadata);
187   }
~TimeTraceScope()188   ~TimeTraceScope() {
189     if (getTimeTraceProfilerInstance() != nullptr)
190       timeTraceProfilerEnd(Entry);
191   }
192 
193 private:
194   TimeTraceProfilerEntry *Entry = nullptr;
195 };
196 
197 } // end namespace llvm
198 
199 #endif
200