#include #include #include #include #include #include "topo.h" #include "topop.h" #define S2NS (1000000000UL) static uint64_t tsc2ns(uint64_t tsc, uint64_t tsc_freq) { return (uint64_t)( (double)tsc / (double)tsc_freq * S2NS); } int topo_ts_init(struct topo_desc * desc, int verbose) { int rc; size_t sz = sizeof(desc->tsc_freq); // init nm_tsc2ns if ((rc = sysctlbyname("machdep.tsc_freq", &desc->tsc_freq, &sz, NULL, 0)) < 0) { fprintf(stderr,"libtopo: failed to query tsc frequency via sysctl (%d)\n", errno); } else { if (verbose) { fprintf(stdout,"libtopo: tsc frequency = %lu\n", desc->tsc_freq); } } return rc; } uint64_t topo_desc_uptime_ns(struct topo_desc * desc) { unsigned int dummy; _mm_lfence(); uint64_t tsc = __rdtscp(&dummy); _mm_lfence(); return tsc2ns(tsc, desc->tsc_freq); }