iceberg-cpp
Loading...
Searching...
No Matches
timer.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <atomic>
23#include <chrono>
24#include <cstdint>
25#include <functional>
26#include <memory>
27#include <string_view>
28#include <type_traits>
29#include <utility>
30
31#include "iceberg/iceberg_export.h"
32
33namespace iceberg {
34
36enum class TimerUnit {
37 kNanoseconds,
38 kMicroseconds,
39 kMilliseconds,
40 kSeconds,
41 kMinutes,
42 kHours,
43 kDays,
44};
45
47ICEBERG_EXPORT constexpr std::string_view ToString(TimerUnit unit) noexcept {
48 switch (unit) {
49 case TimerUnit::kNanoseconds:
50 return "nanoseconds";
51 case TimerUnit::kMicroseconds:
52 return "microseconds";
53 case TimerUnit::kMilliseconds:
54 return "milliseconds";
55 case TimerUnit::kSeconds:
56 return "seconds";
57 case TimerUnit::kMinutes:
58 return "minutes";
59 case TimerUnit::kHours:
60 return "hours";
61 case TimerUnit::kDays:
62 return "days";
63 }
64 std::unreachable();
65}
66
71class ICEBERG_EXPORT Timer {
72 public:
74 class ICEBERG_EXPORT Timed {
75 public:
76 explicit Timed(Timer& timer);
77 ~Timed();
78
79 Timed(const Timed&) = delete;
80 Timed& operator=(const Timed&) = delete;
81 Timed(Timed&& other) noexcept;
82 Timed& operator=(Timed&& other) noexcept;
83
87 void Stop();
88
89 private:
90 Timer* timer_;
91 std::chrono::steady_clock::time_point start_;
92 bool stopped_ = false;
93 };
94
95 virtual ~Timer() = default;
96
98 virtual int64_t Count() const = 0;
99
101 virtual std::chrono::nanoseconds TotalDuration() const = 0;
102
107 virtual void Record(std::chrono::nanoseconds duration) = 0;
108
110 template <typename Rep, typename Period>
111 void Record(std::chrono::duration<Rep, Period> duration) {
112 Record(std::chrono::duration_cast<std::chrono::nanoseconds>(duration));
113 }
114
116 virtual std::string_view Unit() const { return "nanoseconds"; }
117
119 virtual bool IsNoop() const { return false; }
120
126 Timed Start();
127
129 template <typename Callable>
130 decltype(auto) Time(Callable&& fn) {
131 auto timed = Start();
132 using ResultType = std::invoke_result_t<Callable&&>;
133 if constexpr (std::is_void_v<ResultType>) {
134 std::invoke(std::forward<Callable>(fn));
135 } else if constexpr (std::is_reference_v<ResultType>) {
136 return std::invoke(std::forward<Callable>(fn));
137 } else {
138 return std::invoke(std::forward<Callable>(fn));
139 }
140 }
141
143 static std::shared_ptr<Timer> Noop();
144};
145
147class ICEBERG_EXPORT DefaultTimer : public Timer {
148 public:
149 explicit DefaultTimer(TimerUnit unit = TimerUnit::kNanoseconds);
150
151 int64_t Count() const override;
152 std::chrono::nanoseconds TotalDuration() const override;
153 void Record(std::chrono::nanoseconds duration) override;
154 std::string_view Unit() const override { return ToString(unit_); }
155
156 private:
157 std::atomic<int64_t> count_{0};
158 std::atomic<int64_t> total_nanos_{0};
159 TimerUnit unit_;
160};
161
162} // namespace iceberg
Thread-safe timer backed by std::atomic<int64_t>.
Definition timer.h:147
std::string_view Unit() const override
Return the time unit used by this timer.
Definition timer.h:154
RAII guard that records elapsed time into a non-owning Timer on destruction.
Definition timer.h:74
Abstract timer for measuring operation durations.
Definition timer.h:71
decltype(auto) Time(Callable &&fn)
Execute a callable, record its wall-clock duration, and return its result.
Definition timer.h:130
virtual int64_t Count() const =0
Number of timing recordings made so far.
void Record(std::chrono::duration< Rep, Period > duration)
Record a duration of any chrono type, converting to nanoseconds.
Definition timer.h:111
virtual std::string_view Unit() const
Return the time unit used by this timer.
Definition timer.h:116
virtual bool IsNoop() const
Return true if this timer is a no-op.
Definition timer.h:119
virtual std::chrono::nanoseconds TotalDuration() const =0
Total accumulated duration across all recordings.
virtual void Record(std::chrono::nanoseconds duration)=0
Record a nanosecond duration directly.