LLVM  15.0.0git
TimeProfiler.h
Go to the documentation of this file.
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_TIMEPROFILER_H
10 #define LLVM_SUPPORT_TIMEPROFILER_H
11 
12 #include "llvm/Support/Error.h"
14 
15 namespace llvm {
16 
17 class raw_pwrite_stream;
18 
19 struct TimeTraceProfiler;
20 TimeTraceProfiler *getTimeTraceProfilerInstance();
21 
22 /// Initialize the time trace profiler.
23 /// This sets up the global \p TimeTraceProfilerInstance
24 /// variable to be the profiler instance.
25 void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
26  StringRef ProcName);
27 
28 /// Cleanup the time trace profiler, if it was initialized.
30 
31 /// Finish a time trace profiler running on a worker thread.
33 
34 /// Is the time trace profiler enabled, i.e. initialized?
35 inline bool timeTraceProfilerEnabled() {
36  return getTimeTraceProfilerInstance() != nullptr;
37 }
38 
39 /// Write profiling data to output stream.
40 /// Data produced is JSON, in Chrome "Trace Event" format, see
41 /// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
42 void timeTraceProfilerWrite(raw_pwrite_stream &OS);
43 
44 /// Write profiling data to a file.
45 /// The function will write to \p PreferredFileName if provided, if not
46 /// then will write to \p FallbackFileName appending .time-trace.
47 /// Returns a StringError indicating a failure if the function is
48 /// unable to open the file for writing.
49 Error timeTraceProfilerWrite(StringRef PreferredFileName,
50  StringRef FallbackFileName);
51 
52 /// Manually begin a time section, with the given \p Name and \p Detail.
53 /// Profiler copies the string data, so the pointers can be given into
54 /// temporaries. Time sections can be hierarchical; every Begin must have a
55 /// matching End pair but they can nest.
56 void timeTraceProfilerBegin(StringRef Name, StringRef Detail);
57 void timeTraceProfilerBegin(StringRef Name,
58  llvm::function_ref<std::string()> Detail);
59 
60 /// Manually end the last time section.
62 
63 /// The TimeTraceScope is a helper class to call the begin and end functions
64 /// of the time trace profiler. When the object is constructed, it begins
65 /// the section; and when it is destroyed, it stops it. If the time profiler
66 /// is not initialized, the overhead is a single branch.
68 
69  TimeTraceScope() = delete;
70  TimeTraceScope(const TimeTraceScope &) = delete;
71  TimeTraceScope &operator=(const TimeTraceScope &) = delete;
72  TimeTraceScope(TimeTraceScope &&) = delete;
74 
76  if (getTimeTraceProfilerInstance() != nullptr)
78  }
80  if (getTimeTraceProfilerInstance() != nullptr)
82  }
83  TimeTraceScope(StringRef Name, llvm::function_ref<std::string()> Detail) {
84  if (getTimeTraceProfilerInstance() != nullptr)
86  }
88  if (getTimeTraceProfilerInstance() != nullptr)
90  }
91 };
92 
93 } // end namespace llvm
94 
95 #endif
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::lltok::Error
@ Error
Definition: LLToken.h:21
llvm::TimeTraceScope
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
Definition: TimeProfiler.h:67
llvm::getTimeTraceProfilerInstance
TimeTraceProfiler * getTimeTraceProfilerInstance()
Definition: TimeProfiler.cpp:38
llvm::timeTraceProfilerWrite
void timeTraceProfilerWrite(raw_pwrite_stream &OS)
Write profiling data to output stream.
Definition: TimeProfiler.cpp:289
llvm::timeTraceProfilerCleanup
void timeTraceProfilerCleanup()
Cleanup the time trace profiler, if it was initialized.
Definition: TimeProfiler.cpp:272
Error.h
llvm::TimeTraceScope::~TimeTraceScope
~TimeTraceScope()
Definition: TimeProfiler.h:87
llvm::TimeTraceScope::TimeTraceScope
TimeTraceScope(StringRef Name, StringRef Detail)
Definition: TimeProfiler.h:79
llvm::timeTraceProfilerFinishThread
void timeTraceProfilerFinishThread()
Finish a time trace profiler running on a worker thread.
Definition: TimeProfiler.cpp:283
llvm::timeTraceProfilerInitialize
void timeTraceProfilerInitialize(unsigned TimeTraceGranularity, StringRef ProcName)
Initialize the time trace profiler.
Definition: TimeProfiler.cpp:262
llvm::timeTraceProfilerBegin
void timeTraceProfilerBegin(StringRef Name, StringRef Detail)
Manually begin a time section, with the given Name and Detail.
Definition: TimeProfiler.cpp:315
STLFunctionalExtras.h
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::TimeTraceScope::TimeTraceScope
TimeTraceScope()=delete
llvm::TimeTraceScope::operator=
TimeTraceScope & operator=(const TimeTraceScope &)=delete
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::timeTraceProfilerEnabled
bool timeTraceProfilerEnabled()
Is the time trace profiler enabled, i.e. initialized?
Definition: TimeProfiler.h:35
llvm::TimeTraceScope::TimeTraceScope
TimeTraceScope(StringRef Name)
Definition: TimeProfiler.h:75
llvm::TimeTraceScope::TimeTraceScope
TimeTraceScope(StringRef Name, llvm::function_ref< std::string()> Detail)
Definition: TimeProfiler.h:83
llvm::timeTraceProfilerEnd
void timeTraceProfilerEnd()
Manually end the last time section.
Definition: TimeProfiler.cpp:327