proxygen
stop_watch.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <chrono>
20 #include <stdexcept>
21 #include <utility>
22 
23 #include <folly/Chrono.h>
24 #include <folly/portability/Time.h>
25 
26 namespace folly {
27 
28 using monotonic_clock = std::chrono::steady_clock;
29 
82 template <typename Clock, typename Duration = typename Clock::duration>
84  using clock_type = Clock;
85  using duration = Duration;
86  using time_point = std::chrono::time_point<clock_type, duration>;
87 
88  static_assert(
89  std::ratio_less_equal<
90  typename clock_type::duration::period,
91  typename duration::period>::value,
92  "clock must be at least as precise as the requested duration");
93 
94  static_assert(
95  Clock::is_steady,
96  "only monotonic clocks should be used to track time intervals");
97 
110 
126  explicit custom_stop_watch(typename clock_type::time_point checkpoint)
127  : checkpoint_(std::move(checkpoint)) {}
128 
151  void reset() {
153  }
154 
168  duration elapsed() const {
169  return std::chrono::duration_cast<duration>(
171  }
172 
188  template <typename UDuration>
189  bool elapsed(UDuration&& amount) const {
190  return clock_type::now() - checkpoint_ >= amount;
191  }
192 
214  auto lastCheckpoint = checkpoint_;
215 
217 
218  return std::chrono::duration_cast<duration>(checkpoint_ - lastCheckpoint);
219  }
220 
242  template <typename UDuration>
243  bool lap(UDuration&& amount) {
244  auto now = clock_type::now();
245 
246  if (now - checkpoint_ < amount) {
247  return false;
248  }
249 
250  checkpoint_ = now;
251  return true;
252  }
253 
257  typename clock_type::time_point getCheckpoint() const {
258  return checkpoint_;
259  }
260 
261  private:
262  typename clock_type::time_point checkpoint_;
263 };
264 
282 template <typename Duration = folly::chrono::coarse_steady_clock::duration>
283 using coarse_stop_watch =
285 
303 template <typename Duration = std::chrono::steady_clock::duration>
305 } // namespace folly
clock_type::time_point checkpoint_
Definition: stop_watch.h:262
bool lap(UDuration &&amount)
Definition: stop_watch.h:243
duration elapsed() const
Definition: stop_watch.h:168
std::chrono::steady_clock monotonic_clock
Definition: stop_watch.h:28
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::steady_clock::time_point now()
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::chrono::milliseconds Duration
Definition: Types.h:36
std::chrono::time_point< clock_type, duration > time_point
Definition: stop_watch.h:86
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
custom_stop_watch(typename clock_type::time_point checkpoint)
Definition: stop_watch.h:126
clock_type::time_point getCheckpoint() const
Definition: stop_watch.h:257
bool elapsed(UDuration &&amount) const
Definition: stop_watch.h:189