2019-03-30 09:42:48 +01:00
|
|
|
//===- llvm/Support/TimeProfiler.h - Hierarchical Time Profiler -*- C++ -*-===//
|
|
|
|
//
|
2019-04-15 23:02:47 +02:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-03-30 09:42:48 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-02-06 06:02:06 +01:00
|
|
|
#ifndef LLVM_SUPPORT_TIMEPROFILER_H
|
|
|
|
#define LLVM_SUPPORT_TIMEPROFILER_H
|
2019-03-30 09:42:48 +01:00
|
|
|
|
2020-03-06 23:16:17 +01:00
|
|
|
#include "llvm/Support/Error.h"
|
2019-03-30 09:42:48 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
struct TimeTraceProfiler;
|
2020-01-31 10:27:21 +01:00
|
|
|
TimeTraceProfiler *getTimeTraceProfilerInstance();
|
2019-03-30 09:42:48 +01:00
|
|
|
|
|
|
|
/// Initialize the time trace profiler.
|
|
|
|
/// This sets up the global \p TimeTraceProfilerInstance
|
|
|
|
/// variable to be the profiler instance.
|
2019-12-02 14:10:44 +01:00
|
|
|
void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
|
|
|
|
StringRef ProcName);
|
2019-03-30 09:42:48 +01:00
|
|
|
|
|
|
|
/// Cleanup the time trace profiler, if it was initialized.
|
|
|
|
void timeTraceProfilerCleanup();
|
|
|
|
|
2020-01-03 17:04:19 +01:00
|
|
|
/// Finish a time trace profiler running on a worker thread.
|
|
|
|
void timeTraceProfilerFinishThread();
|
|
|
|
|
2019-03-30 09:42:48 +01:00
|
|
|
/// Is the time trace profiler enabled, i.e. initialized?
|
|
|
|
inline bool timeTraceProfilerEnabled() {
|
2020-01-31 10:27:21 +01:00
|
|
|
return getTimeTraceProfilerInstance() != nullptr;
|
2019-03-30 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 23:16:17 +01:00
|
|
|
/// Write profiling data to output stream.
|
2019-03-30 09:42:48 +01:00
|
|
|
/// Data produced is JSON, in Chrome "Trace Event" format, see
|
|
|
|
/// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
|
2019-04-15 23:02:47 +02:00
|
|
|
void timeTraceProfilerWrite(raw_pwrite_stream &OS);
|
2019-03-30 09:42:48 +01:00
|
|
|
|
2020-03-06 23:16:17 +01:00
|
|
|
/// Write profiling data to a file.
|
|
|
|
/// The function will write to \p PreferredFileName if provided, if not
|
|
|
|
/// then will write to \p FallbackFileName appending .time-trace.
|
|
|
|
/// Returns a StringError indicating a failure if the function is
|
|
|
|
/// unable to open the file for writing.
|
|
|
|
Error timeTraceProfilerWrite(StringRef PreferredFileName,
|
|
|
|
StringRef FallbackFileName);
|
|
|
|
|
2019-03-30 09:42:48 +01:00
|
|
|
/// Manually begin a time section, with the given \p Name and \p Detail.
|
|
|
|
/// Profiler copies the string data, so the pointers can be given into
|
|
|
|
/// temporaries. Time sections can be hierarchical; every Begin must have a
|
|
|
|
/// matching End pair but they can nest.
|
|
|
|
void timeTraceProfilerBegin(StringRef Name, StringRef Detail);
|
|
|
|
void timeTraceProfilerBegin(StringRef Name,
|
|
|
|
llvm::function_ref<std::string()> Detail);
|
|
|
|
|
|
|
|
/// Manually end the last time section.
|
|
|
|
void timeTraceProfilerEnd();
|
|
|
|
|
|
|
|
/// The TimeTraceScope is a helper class to call the begin and end functions
|
|
|
|
/// of the time trace profiler. When the object is constructed, it begins
|
|
|
|
/// the section; and when it is destroyed, it stops it. If the time profiler
|
|
|
|
/// is not initialized, the overhead is a single branch.
|
|
|
|
struct TimeTraceScope {
|
2019-04-15 23:02:47 +02:00
|
|
|
|
|
|
|
TimeTraceScope() = delete;
|
|
|
|
TimeTraceScope(const TimeTraceScope &) = delete;
|
|
|
|
TimeTraceScope &operator=(const TimeTraceScope &) = delete;
|
|
|
|
TimeTraceScope(TimeTraceScope &&) = delete;
|
|
|
|
TimeTraceScope &operator=(TimeTraceScope &&) = delete;
|
|
|
|
|
2019-12-11 12:49:42 +01:00
|
|
|
TimeTraceScope(StringRef Name) {
|
2020-01-31 10:27:21 +01:00
|
|
|
if (getTimeTraceProfilerInstance() != nullptr)
|
2019-12-11 12:49:42 +01:00
|
|
|
timeTraceProfilerBegin(Name, StringRef(""));
|
|
|
|
}
|
2019-03-30 09:42:48 +01:00
|
|
|
TimeTraceScope(StringRef Name, StringRef Detail) {
|
2020-01-31 10:27:21 +01:00
|
|
|
if (getTimeTraceProfilerInstance() != nullptr)
|
2019-03-30 09:42:48 +01:00
|
|
|
timeTraceProfilerBegin(Name, Detail);
|
|
|
|
}
|
|
|
|
TimeTraceScope(StringRef Name, llvm::function_ref<std::string()> Detail) {
|
2020-01-31 10:27:21 +01:00
|
|
|
if (getTimeTraceProfilerInstance() != nullptr)
|
2019-03-30 09:42:48 +01:00
|
|
|
timeTraceProfilerBegin(Name, Detail);
|
|
|
|
}
|
|
|
|
~TimeTraceScope() {
|
2020-01-31 10:27:21 +01:00
|
|
|
if (getTimeTraceProfilerInstance() != nullptr)
|
2019-03-30 09:42:48 +01:00
|
|
|
timeTraceProfilerEnd();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|