2023-08-31 17:35:12 +02:00
|
|
|
/* Unit tests for rdtsc.
|
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
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
|
|
|
|
2021-08-11 12:40:19 -04:00
|
|
|
#include <villas/timing.hpp>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <villas/tsc.h>
|
|
|
|
#include <villas/utils.hpp>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#define CNT (1 << 18)
|
|
|
|
|
2021-08-11 12:40:19 -04:00
|
|
|
// cppcheck-suppress unknownMacro
|
2018-10-19 14:33:10 +02:00
|
|
|
TestSuite(tsc, .description = "Timestamp counters");
|
2018-08-23 13:14:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Test(tsc, increasing) {
|
|
|
|
int ret;
|
|
|
|
struct Tsc tsc;
|
|
|
|
uint64_t *cntrs;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = tsc_init(&tsc);
|
|
|
|
cr_assert_eq(ret, 0);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cntrs = new uint64_t[CNT];
|
|
|
|
cr_assert_not_null(cntrs);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
for (unsigned i = 0; i < CNT; i++)
|
|
|
|
cntrs[i] = tsc_now(&tsc);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
for (unsigned i = 1; i < CNT; i++)
|
|
|
|
cr_assert_lt(cntrs[i - 1], cntrs[i]);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
delete cntrs;
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Test(tsc, sleep) {
|
|
|
|
int ret;
|
|
|
|
double delta, duration = 1;
|
|
|
|
struct timespec start, stop;
|
|
|
|
struct Tsc tsc;
|
|
|
|
uint64_t start_cycles, end_cycles;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = tsc_init(&tsc);
|
|
|
|
cr_assert_eq(ret, 0);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
start_cycles = tsc_now(&tsc);
|
|
|
|
end_cycles = start_cycles + duration * tsc.frequency;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
while (tsc_now(&tsc) < end_cycles)
|
|
|
|
;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &stop);
|
|
|
|
delta = time_delta(&start, &stop);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_float_eq(delta, duration, 1e-4, "Error: %f, Delta: %lf, Freq: %llu",
|
|
|
|
delta - duration, delta, tsc.frequency);
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|