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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/*
* Copyright (C) 2017-2020 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/os_interface/linux/os_time_linux.h"
#include "shared/source/os_interface/linux/drm_neo.h"
#include "shared/source/os_interface/linux/os_interface.h"
#include "drm/i915_drm.h"
#include <time.h>
namespace NEO {
OSTimeLinux::OSTimeLinux(OSInterface *osInterface) {
this->osInterface = osInterface;
resolutionFunc = &clock_getres;
getTimeFunc = &clock_gettime;
if (osInterface) {
pDrm = osInterface->get()->getDrm();
}
timestampTypeDetect();
}
OSTimeLinux::~OSTimeLinux(){};
void OSTimeLinux::timestampTypeDetect() {
struct drm_i915_reg_read reg = {};
int err;
if (pDrm == nullptr)
return;
reg.offset = (TIMESTAMP_LOW_REG | 1);
err = pDrm->ioctl(DRM_IOCTL_I915_REG_READ, ®);
if (err) {
reg.offset = TIMESTAMP_HIGH_REG;
err = pDrm->ioctl(DRM_IOCTL_I915_REG_READ, ®);
if (err) {
getGpuTime = &OSTimeLinux::getGpuTime32;
timestampSizeInBits = OCLRT_NUM_TIMESTAMP_BITS_FALLBACK;
} else {
getGpuTime = &OSTimeLinux::getGpuTimeSplitted;
timestampSizeInBits = OCLRT_NUM_TIMESTAMP_BITS;
}
} else {
getGpuTime = &OSTimeLinux::getGpuTime36;
timestampSizeInBits = OCLRT_NUM_TIMESTAMP_BITS;
}
}
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 OSTimeLinux::getGpuTime32(uint64_t *timestamp) {
struct drm_i915_reg_read reg = {};
reg.offset = TIMESTAMP_LOW_REG;
if (pDrm->ioctl(DRM_IOCTL_I915_REG_READ, ®)) {
return false;
}
*timestamp = reg.val >> 32;
return true;
}
bool OSTimeLinux::getGpuTime36(uint64_t *timestamp) {
struct drm_i915_reg_read reg = {};
reg.offset = TIMESTAMP_LOW_REG | 1;
if (pDrm->ioctl(DRM_IOCTL_I915_REG_READ, ®)) {
return false;
}
*timestamp = reg.val;
return true;
}
bool OSTimeLinux::getGpuTimeSplitted(uint64_t *timestamp) {
struct drm_i915_reg_read reg_hi = {};
struct drm_i915_reg_read reg_lo = {};
uint64_t tmp_hi;
int err = 0, loop = 3;
reg_hi.offset = TIMESTAMP_HIGH_REG;
reg_lo.offset = TIMESTAMP_LOW_REG;
err += pDrm->ioctl(DRM_IOCTL_I915_REG_READ, ®_hi);
do {
tmp_hi = reg_hi.val;
err += pDrm->ioctl(DRM_IOCTL_I915_REG_READ, ®_lo);
err += pDrm->ioctl(DRM_IOCTL_I915_REG_READ, ®_hi);
} while (err == 0 && reg_hi.val != tmp_hi && --loop);
if (err) {
return false;
}
*timestamp = reg_lo.val | (reg_hi.val << 32);
return true;
}
bool OSTimeLinux::getCpuGpuTime(TimeStampData *pGpuCpuTime) {
if (nullptr == this->getGpuTime) {
return false;
}
if (!(this->*getGpuTime)(&pGpuCpuTime->GPUTimeStamp)) {
return false;
}
if (!getCpuTime(&pGpuCpuTime->CPUTimeinNS)) {
return false;
}
return true;
}
std::unique_ptr<OSTime> OSTime::create(OSInterface *osInterface) {
return std::unique_ptr<OSTime>(new OSTimeLinux(osInterface));
}
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);
}
double OSTimeLinux::getDynamicDeviceTimerResolution(HardwareInfo const &hwInfo) const {
if (pDrm) {
drm_i915_getparam_t getParam = {};
int frequency = 0;
getParam.param = I915_PARAM_CS_TIMESTAMP_FREQUENCY;
getParam.value = &frequency;
auto error = pDrm->ioctl(DRM_IOCTL_I915_GETPARAM, &getParam);
if (!error) {
return 1000000000.0 / frequency;
}
}
return OSTime::getDeviceTimerResolution(hwInfo);
}
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;
}
} // namespace NEO
|