LLVM  14.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 struct TimeTraceProfiler;
18 TimeTraceProfiler *getTimeTraceProfilerInstance();
19 
20 /// Initialize the time trace profiler.
21 /// This sets up the global \p TimeTraceProfilerInstance
22 /// variable to be the profiler instance.
23 void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
24  StringRef ProcName);
25 
26 /// Cleanup the time trace profiler, if it was initialized.
28 
29 /// Finish a time trace profiler running on a worker thread.
31 
32 /// Is the time trace profiler enabled, i.e. initialized?
33 inline bool timeTraceProfilerEnabled() {
34  return getTimeTraceProfilerInstance() != nullptr;
35 }
36 
37 /// Write profiling data to output stream.
38 /// Data produced is JSON, in Chrome "Trace Event" format, see
39 /// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
40 void timeTraceProfilerWrite(raw_pwrite_stream &OS);
41 
42 /// Write profiling data to a file.
43 /// The function will write to \p PreferredFileName if provided, if not
44 /// then will write to \p FallbackFileName appending .time-trace.
45 /// Returns a StringError indicating a failure if the function is
46 /// unable to open the file for writing.
47 Error timeTraceProfilerWrite(StringRef PreferredFileName,
48  StringRef FallbackFileName);
49 
50 /// Manually begin a time section, with the given \p Name and \p Detail.
51 /// Profiler copies the string data, so the pointers can be given into
52 /// temporaries. Time sections can be hierarchical; every Begin must have a
53 /// matching End pair but they can nest.
54 void timeTraceProfilerBegin(StringRef Name, StringRef Detail);
55 void timeTraceProfilerBegin(StringRef Name,
56  llvm::function_ref<std::string()> Detail);
57 
58 /// Manually end the last time section.
60 
61 /// The TimeTraceScope is a helper class to call the begin and end functions
62 /// of the time trace profiler. When the object is constructed, it begins
63 /// the section; and when it is destroyed, it stops it. If the time profiler
64 /// is not initialized, the overhead is a single branch.
66 
67  TimeTraceScope() = delete;
68  TimeTraceScope(const TimeTraceScope &) = delete;
69  TimeTraceScope &operator=(const TimeTraceScope &) = delete;
70  TimeTraceScope(TimeTraceScope &&) = delete;
72 
74  if (getTimeTraceProfilerInstance() != nullptr)
76  }
78  if (getTimeTraceProfilerInstance() != nullptr)
80  }
81  TimeTraceScope(StringRef Name, llvm::function_ref<std::string()> Detail) {
82  if (getTimeTraceProfilerInstance() != nullptr)
84  }
86  if (getTimeTraceProfilerInstance() != nullptr)
88  }
89 };
90 
91 } // end namespace llvm
92 
93 #endif
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
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:65
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:273
Error.h
llvm::TimeTraceScope::~TimeTraceScope
~TimeTraceScope()
Definition: TimeProfiler.h:85
llvm::TimeTraceScope::TimeTraceScope
TimeTraceScope(StringRef Name, StringRef Detail)
Definition: TimeProfiler.h:77
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:263
llvm::timeTraceProfilerBegin
void timeTraceProfilerBegin(StringRef Name, StringRef Detail)
Manually begin a time section, with the given Name and Detail.
Definition: TimeProfiler.cpp:315
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
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:52
llvm::timeTraceProfilerEnabled
bool timeTraceProfilerEnabled()
Is the time trace profiler enabled, i.e. initialized?
Definition: TimeProfiler.h:33
llvm::TimeTraceScope::TimeTraceScope
TimeTraceScope(StringRef Name)
Definition: TimeProfiler.h:73
llvm::TimeTraceScope::TimeTraceScope
TimeTraceScope(StringRef Name, llvm::function_ref< std::string()> Detail)
Definition: TimeProfiler.h:81
llvm::timeTraceProfilerEnd
void timeTraceProfilerEnd()
Manually end the last time section.
Definition: TimeProfiler.cpp:327
raw_ostream.h