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/tests/unit/tsc.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.4 KiB
C++
Raw Permalink Normal View History

/* Unit tests for rdtsc.
*
* 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 <criterion/criterion.h>
2021-08-11 12:40:19 -04:00
#include <villas/timing.hpp>
#include <villas/tsc.h>
#include <villas/utils.hpp>
#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
Test(tsc, increasing) {
int ret;
struct Tsc tsc;
uint64_t *cntrs;
ret = tsc_init(&tsc);
cr_assert_eq(ret, 0);
cntrs = new uint64_t[CNT];
cr_assert_not_null(cntrs);
for (unsigned i = 0; i < CNT; i++)
cntrs[i] = tsc_now(&tsc);
for (unsigned i = 1; i < CNT; i++)
cr_assert_lt(cntrs[i - 1], cntrs[i]);
delete cntrs;
}
Test(tsc, sleep) {
int ret;
double delta, duration = 1;
struct timespec start, stop;
struct Tsc tsc;
uint64_t start_cycles, end_cycles;
ret = tsc_init(&tsc);
cr_assert_eq(ret, 0);
clock_gettime(CLOCK_MONOTONIC, &start);
start_cycles = tsc_now(&tsc);
end_cycles = start_cycles + duration * tsc.frequency;
while (tsc_now(&tsc) < end_cycles)
;
clock_gettime(CLOCK_MONOTONIC, &stop);
delta = time_delta(&start, &stop);
cr_assert_float_eq(delta, duration, 1e-4, "Error: %f, Delta: %lf, Freq: %llu",
delta - duration, delta, tsc.frequency);
}