2023-08-28 12:31:18 +02:00
|
|
|
/* Measure time and sleep with IA-32 time-stamp counter.
|
2018-08-22 11:29:39 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* 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
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-06-27 01:37:56 +02:00
|
|
|
#if !(__x86_64__ || __i386__)
|
2023-09-07 13:19:19 +02:00
|
|
|
#error this header is for x86 only
|
2019-06-27 01:37:56 +02:00
|
|
|
#endif
|
|
|
|
|
2019-06-23 16:26:44 +02:00
|
|
|
#include <cinttypes>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <cpuid.h>
|
2021-09-13 15:11:51 +02:00
|
|
|
#include <x86intrin.h>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2018-08-23 13:12:41 +02:00
|
|
|
|
2018-08-22 11:29:39 +02:00
|
|
|
#ifndef bit_TSC
|
2023-09-07 13:19:19 +02:00
|
|
|
#define bit_TSC (1 << 4)
|
2018-08-22 11:29:39 +02:00
|
|
|
#endif
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
#define bit_TSC_INVARIANT (1 << 8)
|
|
|
|
#define bit_RDTSCP (1 << 27)
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2021-08-11 12:40:19 -04:00
|
|
|
struct Tsc {
|
2023-09-07 13:19:19 +02:00
|
|
|
uint64_t frequency;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
bool rdtscp_supported;
|
|
|
|
bool is_invariant;
|
2018-08-22 11:29:39 +02:00
|
|
|
};
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
__attribute__((unused)) static uint64_t tsc_now(struct Tsc *t) {
|
|
|
|
uint32_t tsc_aux;
|
|
|
|
return t->rdtscp_supported ? __rdtscp(&tsc_aux) : __rdtsc();
|
2018-10-21 20:24:47 +01:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int tsc_init(struct Tsc *t) __attribute__((warn_unused_result));
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2021-08-11 12:40:19 -04:00
|
|
|
uint64_t tsc_rate_to_cycles(struct Tsc *t, double rate);
|