forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoarse_steady_clock.hh
43 lines (33 loc) · 1.12 KB
/
coarse_steady_clock.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Copyright (C) 2021-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
// A coarser and faster version of std::steady_clock, using
// CLOCK_MONOTONIC_COARSE instead of CLOCK_MONOTONIC.
//
// Intended for measuring time taken by synchronous code paths (where
// seastar::lowres_clock is not suitable).
#include <chrono>
#include <ctime>
namespace utils {
struct coarse_steady_clock {
using duration = std::chrono::nanoseconds;
using rep = duration::rep;
using period = duration::period;
using time_point = std::chrono::time_point<coarse_steady_clock, duration>;
static constexpr bool is_steady = true;
static time_point now() noexcept {
timespec tp;
clock_gettime(CLOCK_MONOTONIC_COARSE, &tp);
return time_point(std::chrono::seconds(tp.tv_sec) + std::chrono::nanoseconds(tp.tv_nsec));
};
static duration get_resolution() noexcept {
timespec tp;
clock_getres(CLOCK_MONOTONIC_COARSE, &tp);
return std::chrono::seconds(tp.tv_sec) + std::chrono::nanoseconds(tp.tv_nsec);
}
};
};