On Sat, May 7, 2022 at 11:16 PM Duncan Bellamy <d...@denkimushi.com> wrote: > > musl lacks __ppc_get_timebase() but has __builtin_ppc_get_timebase() > > the __ppc_get_timebase_freq() is taken from: > https://git.alpinelinux.org/aports/commit/?id=06b03f70fb94972286c0c9f6278df89e53903833 > > Signed-off-by: Duncan Bellamy <d...@denkimushi.com>
- A patch title does not need lib/ prefix. Here, "eal/ppc: " is enough. - Code in lib/eal/linux won't be used for FreeBSD/Windows. On the other hand, arch-specific code (here, lib/eal/ppc/) can be used for the various OS. Besides, as far as I can see in the Linux kernel sources, powerpc is the only architecture that exports a "timebase" entry in /proc/cpuinfo. So, I see no reason to put any code out of lib/eal/ppc. - In the end, unless I missed some point, the patch could probably look like (untested): diff --git a/lib/eal/ppc/include/rte_cycles.h b/lib/eal/ppc/include/rte_cycles.h index 5585f9273c..666fc9b0bf 100644 --- a/lib/eal/ppc/include/rte_cycles.h +++ b/lib/eal/ppc/include/rte_cycles.h @@ -10,7 +10,10 @@ extern "C" { #endif +#include <features.h> +#ifdef __GLIBC__ #include <sys/platform/ppc.h> +#endif #include "generic/rte_cycles.h" @@ -26,7 +29,11 @@ extern "C" { static inline uint64_t rte_rdtsc(void) { +#ifdef __GLIBC__ return __ppc_get_timebase(); +#else + return __builtin_ppc_get_timebase(); +#endif } static inline uint64_t diff --git a/lib/eal/ppc/rte_cycles.c b/lib/eal/ppc/rte_cycles.c index 3180adb0ff..99d36b2f7e 100644 --- a/lib/eal/ppc/rte_cycles.c +++ b/lib/eal/ppc/rte_cycles.c @@ -2,12 +2,50 @@ * Copyright (C) IBM Corporation 2019. */ +#include <features.h> +#ifdef __GLIBC__ #include <sys/platform/ppc.h> +#elif RTE_EXEC_ENV_LINUX +#include <string.h> +#include <stdio.h> +#endif #include "eal_private.h" uint64_t get_tsc_freq_arch(void) { +#ifdef __GLIBC__ return __ppc_get_timebase_freq(); +#elif RTE_EXEC_ENV_LINUX + static unsigned long base; + char buf[512]; + ssize_t nr; + FILE *f; + + if (base != 0) + goto out; + + f = fopen("/proc/cpuinfo", "rb"); + if (f == NULL) + goto out; + + while (fgets(buf, sizeof(buf), f) != NULL) { + char *ret = strstr(buf, "timebase"); + + if (ret == NULL) + continue; + ret += sizeof("timebase") - 1; + ret = strchr(ret, ':'); + if (ret == NULL) + continue; + base = strtoul(ret + 1, NULL, 10); + break; + } + fclose(f); +out: + return (uint64_t) base; +#else + return 0; +#endif } -- David Marchand