iceberg-cpp
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
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
24
25#include <atomic>
26#include <chrono>
27#include <cstdint>
28#include <functional>
29#include <memory>
30#include <string_view>
31#include <type_traits>
32#include <utility>
33
36
37namespace iceberg {
38
40enum class TimerUnit {
41 kNanoseconds,
42 kMicroseconds,
43 kMilliseconds,
44 kSeconds,
45 kMinutes,
46 kHours,
47 kDays,
48};
49
51ICEBERG_EXPORT constexpr std::string_view ToString(TimerUnit unit) noexcept {
52 switch (unit) {
53 case TimerUnit::kNanoseconds:
54 return "nanoseconds";
55 case TimerUnit::kMicroseconds:
56 return "microseconds";
57 case TimerUnit::kMilliseconds:
58 return "milliseconds";
59 case TimerUnit::kSeconds:
60 return "seconds";
61 case TimerUnit::kMinutes:
62 return "minutes";
63 case TimerUnit::kHours:
64 return "hours";
65 case TimerUnit::kDays:
66 return "days";
67 }
69}
70
75class ICEBERG_EXPORT Timer {
76 public:
78 class ICEBERG_EXPORT Timed {
79 public:
80 explicit Timed(Timer& timer);
81 ~Timed();
82
83 Timed(const Timed&) = delete;
84 Timed& operator=(const Timed&) = delete;
85 Timed(Timed&& other) noexcept;
86 Timed& operator=(Timed&& other) noexcept;
87
91 void Stop();
92
93 private:
94 Timer* timer_;
95 std::chrono::steady_clock::time_point start_;
96 bool stopped_ = false;
97 };
98
99 virtual ~Timer() = default;
100
102 virtual int64_t Count() const = 0;
103
105 virtual std::chrono::nanoseconds TotalDuration() const = 0;
106
111 virtual void Record(std::chrono::nanoseconds duration) = 0;
112
114 template <typename Rep, typename Period>
115 void Record(std::chrono::duration<Rep, Period> duration) {
116 Record(std::chrono::duration_cast<std::chrono::nanoseconds>(duration));
117 }
118
120 virtual std::string_view Unit() const { return "nanoseconds"; }
121
123 virtual bool IsNoop() const { return false; }
124
131
133 template <typename Callable>
134 decltype(auto) Time(Callable&& fn) {
135 auto timed = Start();
136 using ResultType = std::invoke_result_t<Callable&&>;
137 if constexpr (std::is_void_v<ResultType>) {
138 std::invoke(std::forward<Callable>(fn));
139 } else if constexpr (std::is_reference_v<ResultType>) {
140 return std::invoke(std::forward<Callable>(fn));
141 } else {
142 return std::invoke(std::forward<Callable>(fn));
143 }
144 }
145
147 static std::shared_ptr<Timer> Noop();
148};
149
151class ICEBERG_EXPORT DefaultTimer : public Timer {
152 public:
153 explicit DefaultTimer(TimerUnit unit = TimerUnit::kNanoseconds);
154
155 int64_t Count() const override;
156 std::chrono::nanoseconds TotalDuration() const override;
157 void Record(std::chrono::nanoseconds duration) override;
158 std::string_view Unit() const override { return ToString(unit_); }
159
160 private:
161 std::atomic<int64_t> count_{0};
162 std::atomic<int64_t> total_nanos_{0};
163 TimerUnit unit_;
164};
165
166} // namespace iceberg
Thread-safe timer backed by std::atomic<int64_t>.
Definition timer.h:151
int64_t Count() const override
Number of timing recordings made so far.
std::chrono::nanoseconds TotalDuration() const override
Total accumulated duration across all recordings.
void Record(std::chrono::nanoseconds duration) override
Record a nanosecond duration directly.
std::string_view Unit() const override
Return the time unit used by this timer.
Definition timer.h:158
RAII guard that records elapsed time into a non-owning Timer on destruction.
Definition timer.h:78
void Stop()
Explicitly stop timing and record the duration.
Abstract timer for measuring operation durations.
Definition timer.h:75
decltype(auto) Time(Callable &&fn)
Execute a callable, record its wall-clock duration, and return its result.
Definition timer.h:134
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:115
virtual std::string_view Unit() const
Return the time unit used by this timer.
Definition timer.h:120
virtual bool IsNoop() const
Return true if this timer is a no-op.
Definition timer.h:123
virtual std::chrono::nanoseconds TotalDuration() const =0
Total accumulated duration across all recordings.
static std::shared_ptr< Timer > Noop()
Return a shared no-op timer singleton.
virtual void Record(std::chrono::nanoseconds duration)=0
Record a nanosecond duration directly.
Timed Start()
Start timing and return a RAII Timed guard.
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
TimerUnit
Time units supported by Timer.
Definition timer.h:40
ICEBERG_EXPORT std::string_view ToString(Expression::Operation op)
Returns a string representation of an expression operation.
void unreachable() noexcept
Marks a branch the program can never take.
Definition unreachable.h:35
Portable stand-in for std::unreachable.