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
35
36namespace iceberg {
37
39enum class TimerUnit {
40 kNanoseconds,
41 kMicroseconds,
42 kMilliseconds,
43 kSeconds,
44 kMinutes,
45 kHours,
46 kDays,
47};
48
50ICEBERG_EXPORT constexpr std::string_view ToString(TimerUnit unit) noexcept {
51 switch (unit) {
52 case TimerUnit::kNanoseconds:
53 return "nanoseconds";
54 case TimerUnit::kMicroseconds:
55 return "microseconds";
56 case TimerUnit::kMilliseconds:
57 return "milliseconds";
58 case TimerUnit::kSeconds:
59 return "seconds";
60 case TimerUnit::kMinutes:
61 return "minutes";
62 case TimerUnit::kHours:
63 return "hours";
64 case TimerUnit::kDays:
65 return "days";
66 }
67 std::unreachable();
68}
69
74class ICEBERG_EXPORT Timer {
75 public:
77 class ICEBERG_EXPORT Timed {
78 public:
79 explicit Timed(Timer& timer);
80 ~Timed();
81
82 Timed(const Timed&) = delete;
83 Timed& operator=(const Timed&) = delete;
84 Timed(Timed&& other) noexcept;
85 Timed& operator=(Timed&& other) noexcept;
86
90 void Stop();
91
92 private:
93 Timer* timer_;
94 std::chrono::steady_clock::time_point start_;
95 bool stopped_ = false;
96 };
97
98 virtual ~Timer() = default;
99
101 virtual int64_t Count() const = 0;
102
104 virtual std::chrono::nanoseconds TotalDuration() const = 0;
105
110 virtual void Record(std::chrono::nanoseconds duration) = 0;
111
113 template <typename Rep, typename Period>
114 void Record(std::chrono::duration<Rep, Period> duration) {
115 Record(std::chrono::duration_cast<std::chrono::nanoseconds>(duration));
116 }
117
119 virtual std::string_view Unit() const { return "nanoseconds"; }
120
122 virtual bool IsNoop() const { return false; }
123
130
132 template <typename Callable>
133 decltype(auto) Time(Callable&& fn) {
134 auto timed = Start();
135 using ResultType = std::invoke_result_t<Callable&&>;
136 if constexpr (std::is_void_v<ResultType>) {
137 std::invoke(std::forward<Callable>(fn));
138 } else if constexpr (std::is_reference_v<ResultType>) {
139 return std::invoke(std::forward<Callable>(fn));
140 } else {
141 return std::invoke(std::forward<Callable>(fn));
142 }
143 }
144
146 static std::shared_ptr<Timer> Noop();
147};
148
150class ICEBERG_EXPORT DefaultTimer : public Timer {
151 public:
152 explicit DefaultTimer(TimerUnit unit = TimerUnit::kNanoseconds);
153
154 int64_t Count() const override;
155 std::chrono::nanoseconds TotalDuration() const override;
156 void Record(std::chrono::nanoseconds duration) override;
157 std::string_view Unit() const override { return ToString(unit_); }
158
159 private:
160 std::atomic<int64_t> count_{0};
161 std::atomic<int64_t> total_nanos_{0};
162 TimerUnit unit_;
163};
164
165} // namespace iceberg
Thread-safe timer backed by std::atomic<int64_t>.
Definition timer.h:150
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:157
RAII guard that records elapsed time into a non-owning Timer on destruction.
Definition timer.h:77
void Stop()
Explicitly stop timing and record the duration.
Abstract timer for measuring operation durations.
Definition timer.h:74
decltype(auto) Time(Callable &&fn)
Execute a callable, record its wall-clock duration, and return its result.
Definition timer.h:133
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:114
virtual std::string_view Unit() const
Return the time unit used by this timer.
Definition timer.h:119
virtual bool IsNoop() const
Return true if this timer is a no-op.
Definition timer.h:122
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:33
TimerUnit
Time units supported by Timer.
Definition timer.h:39
ICEBERG_EXPORT std::string_view ToString(Expression::Operation op)
Returns a string representation of an expression operation.