1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/common/lib/tsc.cpp
Steffen Vogel 7d10544d79 Apply clang-format changes
Signed-off-by: Steffen Vogel <steffen.vogel@opal-rt.com>
2024-02-29 18:58:07 +01:00

48 lines
1.2 KiB
C++

/* Measure time and sleep with IA-32 time-stamp counter.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <villas/tsc.hpp>
using namespace villas;
int tsc_init(struct Tsc *t) {
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
// Check if TSC is supported
__get_cpuid(0x1, &eax, &ebx, &ecx, &edx);
if (!(edx & bit_TSC))
return -2;
// Check if RDTSCP instruction is supported
__get_cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
t->rdtscp_supported = edx & bit_RDTSCP;
// Check if TSC is invariant
__get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
t->is_invariant = edx & bit_TSC_INVARIANT;
// Intel SDM Vol 3, Section 18.7.3:
// Nominal TSC frequency = CPUID.15H.ECX[31:0] * CPUID.15H.EBX[31:0] ) ÷ CPUID.15H.EAX[31:0]
__get_cpuid(0x15, &eax, &ebx, &ecx, &edx);
if (ecx != 0)
t->frequency = ecx * ebx / eax;
else {
int ret;
#ifdef __linux__
ret = kernel::get_cpu_frequency(&t->frequency);
if (ret)
return ret;
#endif
}
return 0;
}
uint64_t tsc_rate_to_cycles(struct Tsc *t, double rate) {
return t->frequency / rate;
}