proxygen
folly::custom_stop_watch< Clock, Duration > Struct Template Reference

#include <stop_watch.h>

Public Types

using clock_type = Clock
 
using duration = Duration
 
using time_point = std::chrono::time_point< clock_type, duration >
 

Public Member Functions

 custom_stop_watch ()
 
 custom_stop_watch (typename clock_type::time_point checkpoint)
 
void reset ()
 
duration elapsed () const
 
template<typename UDuration >
bool elapsed (UDuration &&amount) const
 
duration lap ()
 
template<typename UDuration >
bool lap (UDuration &&amount)
 
clock_type::time_point getCheckpoint () const
 

Private Attributes

clock_type::time_point checkpoint_
 

Detailed Description

template<typename Clock, typename Duration = typename Clock::duration>
struct folly::custom_stop_watch< Clock, Duration >

Calculates the duration of time intervals. Prefer this over directly using monotonic clocks. It is very lightweight and provides convenient facilitles to avoid common pitfalls.

There are two type aliases that should be preferred over instantiating this class directly: coarse_stop_watch and stop_watch.

Arguments:

  • Clock: the monotonic clock to use when calculating time intervals
  • Duration: (optional) the duration to use when reporting elapsed time. Defaults to the clock's duration.

Example 1:

coarse_stop_watch<std::chrono::seconds> watch; do_something(); std::cout << "time elapsed: " << watch.elapsed().count() << std::endl;

auto const ttl = 60_s; if (watch.elapsed(ttl)) { process_expiration(); }

Example 2:

struct run_every_n_seconds { using callback = std::function<void()>; run_every_n_seconds(std::chrono::seconds period, callback action) period_(period), action_(std::move(action)) { // watch_ is correctly initialized to the current time }

void run() { while (true) { if (watch_.lap(period_)) { action_(); } std::this_thread::yield(); } }

private: stop_watch<> watch_; std::chrono::seconds period_; callback action_; };

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 83 of file stop_watch.h.

Member Typedef Documentation

template<typename Clock, typename Duration = typename Clock::duration>
using folly::custom_stop_watch< Clock, Duration >::clock_type = Clock

Definition at line 84 of file stop_watch.h.

template<typename Clock, typename Duration = typename Clock::duration>
using folly::custom_stop_watch< Clock, Duration >::duration = Duration

Definition at line 85 of file stop_watch.h.

template<typename Clock, typename Duration = typename Clock::duration>
using folly::custom_stop_watch< Clock, Duration >::time_point = std::chrono::time_point<clock_type, duration>

Definition at line 86 of file stop_watch.h.

Constructor & Destructor Documentation

template<typename Clock, typename Duration = typename Clock::duration>
folly::custom_stop_watch< Clock, Duration >::custom_stop_watch ( )
inline

Initializes the stop watch with the current time as its checkpoint.

Example:

stop_watch<> watch; do_something(); std::cout << "time elapsed: " << watch.elapsed() << std::endl;

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 109 of file stop_watch.h.

clock_type::time_point checkpoint_
Definition: stop_watch.h:262
std::chrono::steady_clock::time_point now()
template<typename Clock, typename Duration = typename Clock::duration>
folly::custom_stop_watch< Clock, Duration >::custom_stop_watch ( typename clock_type::time_point  checkpoint)
inlineexplicit

Initializes the stop watch with the given time as its checkpoint.

NOTE: this constructor should be seldomly used. It is only provided so that, in the rare occasions it is needed, one does not have to reimplement the custom_stop_watch class.

Example:

custom_stop_watch<monotonic_clock> watch(monotonic_clock::now()); do_something(); std::cout << "time elapsed: " << watch.elapsed() << std::endl;

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 126 of file stop_watch.h.

127  : checkpoint_(std::move(checkpoint)) {}
clock_type::time_point checkpoint_
Definition: stop_watch.h:262
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567

Member Function Documentation

template<typename Clock, typename Duration = typename Clock::duration>
duration folly::custom_stop_watch< Clock, Duration >::elapsed ( ) const
inline

Tells the elapsed time since the last update.

The stop watch's checkpoint remains unchanged.

Example:

stop_watch<> watch; do_something(); std::cout << "time elapsed: " << watch.elapsed() << std::endl;

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 168 of file stop_watch.h.

References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().

Referenced by testTryReadUntil(), and testTryWriteUntil().

168  {
169  return std::chrono::duration_cast<duration>(
171  }
clock_type::time_point checkpoint_
Definition: stop_watch.h:262
std::chrono::steady_clock::time_point now()
template<typename Clock, typename Duration = typename Clock::duration>
template<typename UDuration >
bool folly::custom_stop_watch< Clock, Duration >::elapsed ( UDuration &&  amount) const
inline

Tells whether the given duration has already elapsed since the last checkpoint.

Example:

auto const ttl = 60_s; stop_watch<> watch;

do_something();

std::cout << "has the TTL expired? " std::boolalpha<< watch.elapsed(ttl);

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 189 of file stop_watch.h.

References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().

189  {
190  return clock_type::now() - checkpoint_ >= amount;
191  }
clock_type::time_point checkpoint_
Definition: stop_watch.h:262
std::chrono::steady_clock::time_point now()
template<typename Clock, typename Duration = typename Clock::duration>
clock_type::time_point folly::custom_stop_watch< Clock, Duration >::getCheckpoint ( ) const
inline

Returns the current checkpoint

Definition at line 257 of file stop_watch.h.

References folly::custom_stop_watch< Clock, Duration >::checkpoint_.

Referenced by testTryReadUntil(), and testTryWriteUntil().

257  {
258  return checkpoint_;
259  }
clock_type::time_point checkpoint_
Definition: stop_watch.h:262
template<typename Clock, typename Duration = typename Clock::duration>
duration folly::custom_stop_watch< Clock, Duration >::lap ( )
inline

Tells the elapsed time since the last update, and updates the checkpoint to the current time.

Example:

struct some_resource { // ...

void on_reloaded() { auto const alive = time_alive.lap(); std::cout << "resource reloaded after being alive for " << alive; }

private: stop_watch<> time_alive; };

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 213 of file stop_watch.h.

References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().

213  {
214  auto lastCheckpoint = checkpoint_;
215 
217 
218  return std::chrono::duration_cast<duration>(checkpoint_ - lastCheckpoint);
219  }
clock_type::time_point checkpoint_
Definition: stop_watch.h:262
std::chrono::steady_clock::time_point now()
template<typename Clock, typename Duration = typename Clock::duration>
template<typename UDuration >
bool folly::custom_stop_watch< Clock, Duration >::lap ( UDuration &&  amount)
inline

Tells whether the given duration has already elapsed since the last checkpoint. If so, update the checkpoint to the current time. If not, the checkpoint remains unchanged.

Example:

void run_every_n_seconds( std::chrono::seconds period, std::function<void()> action ) { for (stop_watch<> watch;; ) { if (watch.lap(period)) { action(); } std::this_thread::yield(); } }

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 243 of file stop_watch.h.

References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().

243  {
244  auto now = clock_type::now();
245 
246  if (now - checkpoint_ < amount) {
247  return false;
248  }
249 
250  checkpoint_ = now;
251  return true;
252  }
clock_type::time_point checkpoint_
Definition: stop_watch.h:262
std::chrono::steady_clock::time_point now()
template<typename Clock, typename Duration = typename Clock::duration>
void folly::custom_stop_watch< Clock, Duration >::reset ( )
inline

Updates the stop watch checkpoint to the current time.

Example:

struct some_resource { // ...

void on_reloaded() { time_alive.reset(); }

void report() { std::cout << "resource has been alive for " << time_alive.elapsed(); }

private: stop_watch<> time_alive; };

Author
: Marcelo Juchem marce.nosp@m.lo@f.nosp@m.b.com

Definition at line 151 of file stop_watch.h.

References folly::custom_stop_watch< Clock, Duration >::checkpoint_, and now().

151  {
153  }
clock_type::time_point checkpoint_
Definition: stop_watch.h:262
std::chrono::steady_clock::time_point now()

Member Data Documentation

template<typename Clock, typename Duration = typename Clock::duration>
clock_type::time_point folly::custom_stop_watch< Clock, Duration >::checkpoint_
private

The documentation for this struct was generated from the following file: