#include #include #include #include #include #include "topo.h" #define S2NS (1000000000UL) static inline uint64_t get_uptime() { struct timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); return ((uint64_t)tp.tv_sec * S2NS + (uint64_t)tp.tv_nsec); } 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); } int ts_test() { 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; } int main() { topo_init(1); topo_init(1); 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)); } ts_test(); return 0; }