libtopo/test/test.c

108 lines
2.0 KiB
C
Raw Normal View History

2022-06-10 11:29:01 +00:00
#include <stdint.h>
2022-05-26 16:36:03 +00:00
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "topo.h"
#define S2NS (1000000000UL)
static inline uint64_t
2023-03-03 22:19:19 +00:00
get_uptime(void)
2022-05-26 16:36:03 +00:00
{
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
2022-06-10 11:29:01 +00:00
return ((uint64_t)tp.tv_sec * S2NS + (uint64_t)tp.tv_nsec);
2022-05-26 16:36:03 +00:00
}
void test(const char * case_name, struct timespec * ts)
{
uint64_t slow;
uint64_t fast;
slow = get_uptime();
if (nanosleep(ts, NULL) != 0) {
perror("nanosleep() interrupted!");
exit(-1);
}
slow = get_uptime() - slow;
fast = topo_uptime_ns();
if (nanosleep(ts, NULL) != 0) {
perror("nanosleep() interrupted!");
exit(-1);
}
fast = topo_uptime_ns() - fast;
printf("%s: clock_gettime(): %lu, topo_uptime_ns(): %lu, %% diff: %.2f%%\n", case_name, slow, fast, ((float)fast - (float)slow) / (float)slow * 100);
}
2023-03-03 22:19:19 +00:00
int ts_test(void)
2022-05-26 16:36:03 +00:00
{
struct timespec ts;
// 1s
ts.tv_nsec = 0;
ts.tv_sec = 1;
test("1s", &ts);
// 100ms
ts.tv_nsec = 100000000;
ts.tv_sec = 0;
test("100ms", &ts);
// 10ms
ts.tv_nsec = 10000000;
ts.tv_sec = 0;
test("10ms", &ts);
// 1ms
ts.tv_nsec = 1000000;
ts.tv_sec = 0;
test("1ms", &ts);
// 100us
ts.tv_nsec = 100000;
ts.tv_sec = 0;
test("100us", &ts);
// 10us
ts.tv_nsec = 10000;
ts.tv_sec = 0;
test("10us", &ts);
// 1us
ts.tv_nsec = 1000;
ts.tv_sec = 0;
test("1us", &ts);
// 100ns
ts.tv_nsec = 100;
ts.tv_sec = 0;
test("100ns", &ts);
// 10ns
ts.tv_nsec = 10;
ts.tv_sec = 0;
test("10ns", &ts);
// 1ns
ts.tv_nsec = 1;
ts.tv_sec = 0;
test("1ns", &ts);
return 0;
}
2023-03-03 22:19:19 +00:00
int main(void)
2022-05-26 16:36:03 +00:00
{
2022-06-10 11:29:01 +00:00
topo_init(1);
topo_init(1);
2022-12-14 10:23:58 +00:00
printf("Total cores: %d, numa: %d\n", topo_num_core(), topo_num_numa());
for (int i = 0; i < topo_num_core(); i++) {
printf("Core %d -> NUMA %d\n", i, topo_core_to_numa(i));
}
2022-05-26 16:36:03 +00:00
ts_test();
return 0;
2022-06-10 11:29:01 +00:00
}