Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
TimeTracing.h
Go to the documentation of this file.
1//===-- TimeTracing.h -- Time tracing helpers -------------------*- C++ -*-===//
2//
3// Part of the Proteus Project, under the Apache License v2.0 with LLVM
4// Exceptions. See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef PROTEUS_TIME_TRACING_H
10#define PROTEUS_TIME_TRACING_H
11
12#include <chrono>
13#include <cstdint>
14#include <memory>
15#include <string_view>
16
17namespace proteus {
18
19struct TimeTraceScopeWrapper;
20
22public:
23 explicit ScopedTimeTrace(std::string_view Name);
25
28
29private:
30 std::unique_ptr<TimeTraceScopeWrapper> Pimpl;
31};
32
33class Timer {
34 using Clock = std::chrono::steady_clock;
35
36public:
37 explicit Timer(bool Enabled = true);
38
39 uint64_t elapsed();
40
41 template <class Duration> uint64_t elapsedAs() const {
42 static_assert(
43 !std::chrono::treat_as_floating_point_v<typename Duration::rep>,
44 "Timer::elapsedAs only supports integral duration reps");
45 if (!Enabled)
46 return 0;
47 return std::chrono::duration_cast<Duration>(Clock::now() - Start).count();
48 }
49
50 void reset();
51
52private:
53 bool Enabled = true;
54 Clock::time_point Start;
55};
56
57// Use TIMESCOPE("Explicit::Label") for explicit phase names and
58// TIMESCOPE(Class, Method) to emit a stable "Class::Method" label.
59#define PROTEUS_TIMESCOPE_VAR(Line) STT_##Line
60#define PROTEUS_TIMESCOPE_VAR_EXPAND(Line) PROTEUS_TIMESCOPE_VAR(Line)
61#define PROTEUS_TIMESCOPE_1(Label) \
62 ::proteus::ScopedTimeTrace PROTEUS_TIMESCOPE_VAR_EXPAND(__LINE__)(Label);
63#define PROTEUS_TIMESCOPE_2(Class, Method) \
64 PROTEUS_TIMESCOPE_1(#Class "::" #Method)
65#define PROTEUS_GET_TIMESCOPE(_1, _2, NAME, ...) NAME
66#define TIMESCOPE(...) \
67 PROTEUS_GET_TIMESCOPE(__VA_ARGS__, PROTEUS_TIMESCOPE_2, \
68 PROTEUS_TIMESCOPE_1)(__VA_ARGS__)
69
70} // namespace proteus
71
72#endif
Definition TimeTracing.h:21
ScopedTimeTrace(ScopedTimeTrace &&)=default
ScopedTimeTrace & operator=(ScopedTimeTrace &&)=default
Definition TimeTracing.h:33
uint64_t elapsedAs() const
Definition TimeTracing.h:41
void reset()
Definition TimeTracing.cpp:68
uint64_t elapsed()
Definition TimeTracing.cpp:66
Definition MemoryCache.h:27