forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpika_monotonic_time.cc
52 lines (38 loc) · 1.3 KB
/
pika_monotonic_time.cc
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
44
45
46
47
48
49
50
51
52
// Copyright (c) 2023-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#ifdef __APPLE__ // Mac
#include <mach/mach_time.h>
#include "include/pika_monotonic_time.h"
monotime getMonotonicUs() {
static mach_timebase_info_data_t timebase;
if (timebase.denom == 0) {
mach_timebase_info(&timebase);
}
uint64_t nanos = mach_absolute_time() * timebase.numer / timebase.denom;
return nanos / 1000;
}
#elif __linux__ // Linux
#ifdef __x86_64__ // x86_64
#include <ctime>
#include "include/pika_monotonic_time.h"
monotime getMonotonicUs() {
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<uint64_t>(ts.tv_sec) * 1000000 + static_cast<uint64_t>(ts.tv_nsec) / 1000;
}
#elif __arm__ // ARM
#include <sys/time.h>
#include "include/pika_monotonic_time.h"
uint64_t getMonotonicUs() {
timeval tv;
gettimeofday(&tv, nullptr);
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
}
#else
#error "Unsupported architecture for Linux"
#endif // __x86_64__, __arm__
#else
#error "Unsupported platform"
#endif // __APPLE__, __linux__