-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathos_time_linux.cpp
78 lines (60 loc) · 2.02 KB
/
os_time_linux.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/os_interface/linux/os_time_linux.h"
#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/os_interface/os_interface.h"
#include <chrono>
#include <time.h>
namespace NEO {
OSTimeLinux::OSTimeLinux(OSInterface &osInterface, std::unique_ptr<DeviceTime> deviceTime) {
this->osInterface = &osInterface;
auto hwInfo = osInterface.getDriverModel()->getHardwareInfo();
if (hwInfo->capabilityTable.timestampValidBits < 64) {
this->maxGpuTimeStamp = 1ull << hwInfo->capabilityTable.timestampValidBits;
}
resolutionFunc = &clock_getres;
getTimeFunc = &clock_gettime;
this->deviceTime = std::move(deviceTime);
}
bool OSTimeLinux::getCpuTime(uint64_t *timestamp) {
struct timespec ts;
if (getTimeFunc(CLOCK_MONOTONIC_RAW, &ts)) {
return false;
}
*timestamp = (uint64_t)ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
return true;
}
bool OSTime::getCpuTimeHost(uint64_t *timestamp) {
struct timespec ts;
auto ret = clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
*timestamp = (uint64_t)ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
return ret;
}
double OSTimeLinux::getHostTimerResolution() const {
struct timespec ts;
if (resolutionFunc(CLOCK_MONOTONIC_RAW, &ts)) {
return 0;
}
return static_cast<double>(ts.tv_nsec + ts.tv_sec * NSEC_PER_SEC);
}
uint64_t OSTimeLinux::getCpuRawTimestamp() {
uint64_t timesInNsec = 0;
uint64_t ticksInNsec = 0;
if (!getCpuTime(×InNsec)) {
return 0;
}
ticksInNsec = static_cast<uint64_t>(getHostTimerResolution());
if (ticksInNsec == 0) {
return 0;
}
return timesInNsec / ticksInNsec;
}
std::unique_ptr<OSTime> OSTimeLinux::create(OSInterface &osInterface, std::unique_ptr<DeviceTime> deviceTime) {
return std::make_unique<OSTimeLinux>(osInterface, std::move(deviceTime));
}
} // namespace NEO