From 3eb6cbec50a5cbe4c3465d60ba6aea7bf2845cd1 Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Mon, 12 Feb 2024 19:44:58 +0000 Subject: vppinfra: os agnostic api for getting CPU information Avoid direct sysfs reads when possible... Type: improvement Change-Id: I2b84cd18f3da47925d068951f24b79b5b6e20bb1 Signed-off-by: Damjan Marion --- src/vppinfra/linux/mem.c | 1 - src/vppinfra/linux/sysfs.c | 14 +++++++++++--- src/vppinfra/linux/sysfs.h | 3 ++- src/vppinfra/pmalloc.c | 2 ++ src/vppinfra/unix-misc.c | 47 ++++++++++++++++++++++++++++++++++++++++++++-- src/vppinfra/unix.h | 9 +++++++++ 6 files changed, 69 insertions(+), 7 deletions(-) (limited to 'src/vppinfra') diff --git a/src/vppinfra/linux/mem.c b/src/vppinfra/linux/mem.c index 8f696a1036a..734f5c4788c 100644 --- a/src/vppinfra/linux/mem.c +++ b/src/vppinfra/linux/mem.c @@ -31,7 +31,6 @@ #include #include #include -#include #ifndef F_LINUX_SPECIFIC_BASE #define F_LINUX_SPECIFIC_BASE 1024 diff --git a/src/vppinfra/linux/sysfs.c b/src/vppinfra/linux/sysfs.c index 6d9f859640c..61ee6378c8c 100644 --- a/src/vppinfra/linux/sysfs.c +++ b/src/vppinfra/linux/sysfs.c @@ -237,13 +237,21 @@ clib_sysfs_prealloc_hugepages (int numa_node, int log2_page_size, int nr) return clib_sysfs_set_nr_hugepages (numa_node, log2_page_size, n + needed); } -__clib_export uword * -clib_sysfs_list_to_bitmap (char *filename) +__clib_export clib_bitmap_t * +clib_sysfs_read_bitmap (char *fmt, ...) { FILE *fp; uword *r = 0; + va_list va; + u8 *filename; + + va_start (va, fmt); + filename = va_format (0, fmt, &va); + va_end (va); + vec_add1 (filename, 0); - fp = fopen (filename, "r"); + fp = fopen ((char *) filename, "r"); + vec_free (filename); if (fp != NULL) { diff --git a/src/vppinfra/linux/sysfs.h b/src/vppinfra/linux/sysfs.h index a616fa422a1..f2f822d9741 100644 --- a/src/vppinfra/linux/sysfs.h +++ b/src/vppinfra/linux/sysfs.h @@ -17,6 +17,7 @@ #define included_linux_sysfs_h #include +#include clib_error_t *clib_sysfs_write (char *file_name, char *fmt, ...); @@ -33,7 +34,7 @@ clib_error_t *clib_sysfs_get_surplus_hugepages (int numa_node, clib_error_t *clib_sysfs_prealloc_hugepages (int numa_node, int log2_page_size, int nr); -uword *clib_sysfs_list_to_bitmap (char *filename); +uword *clib_sysfs_read_bitmap (char *fmt, ...); #endif /* included_linux_sysfs_h */ diff --git a/src/vppinfra/pmalloc.c b/src/vppinfra/pmalloc.c index ff392a1bf22..2a27379b573 100644 --- a/src/vppinfra/pmalloc.c +++ b/src/vppinfra/pmalloc.c @@ -22,7 +22,9 @@ #include #include +#ifdef __linux__ #include +#endif #include #include #include diff --git a/src/vppinfra/unix-misc.c b/src/vppinfra/unix-misc.c index 623b2e98555..e0591ff4604 100644 --- a/src/vppinfra/unix-misc.c +++ b/src/vppinfra/unix-misc.c @@ -268,7 +268,7 @@ __clib_export clib_bitmap_t * os_get_online_cpu_core_bitmap () { #if __linux__ - return clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online"); + return clib_sysfs_read_bitmap ("/sys/devices/system/cpu/online"); #else return 0; #endif @@ -278,11 +278,54 @@ __clib_export clib_bitmap_t * os_get_online_cpu_node_bitmap () { #if __linux__ - return clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online"); + return clib_sysfs_read_bitmap ("/sys/devices/system/node/online"); #else return 0; #endif } +__clib_export clib_bitmap_t * +os_get_cpu_on_node_bitmap (int node) +{ +#if __linux__ + return clib_sysfs_read_bitmap ("/sys/devices/system/node/node%u/cpulist", + node); +#else + return 0; +#endif +} + +__clib_export clib_bitmap_t * +os_get_cpu_with_memory_bitmap () +{ +#if __linux__ + return clib_sysfs_read_bitmap ("/sys/devices/system/node/has_memory"); +#else + return 0; +#endif +} + +__clib_export int +os_get_cpu_phys_core_id (int cpu_id) +{ +#if __linux + int core_id = -1; + clib_error_t *err; + u8 *p; + + p = + format (0, "/sys/devices/system/cpu/cpu%u/topology/core_id%c", cpu_id, 0); + err = clib_sysfs_read ((char *) p, "%d", &core_id); + vec_free (p); + if (err) + { + clib_error_free (err); + return -1; + } + return core_id; +#else + return -1; +#endif +} /* * fd.io coding-style-patch-verification: ON diff --git a/src/vppinfra/unix.h b/src/vppinfra/unix.h index b43043a567b..651f9bb99e0 100644 --- a/src/vppinfra/unix.h +++ b/src/vppinfra/unix.h @@ -59,6 +59,15 @@ clib_bitmap_t *os_get_online_cpu_core_bitmap (); /* Retrieve bitmap of online cpu nodes (sockets) */ clib_bitmap_t *os_get_online_cpu_node_bitmap (); +/* Retrieve bitmap of cpus with memory */ +clib_bitmap_t *os_get_cpu_with_memory_bitmap (); + +/* Retrieve bitmap of cpus on specific node */ +clib_bitmap_t *os_get_cpu_on_node_bitmap (int node); + +/* Retrieve physical core id of specific cpu, -1 if not available */ +int os_get_cpu_phys_core_id (int cpu); + #endif /* included_clib_unix_h */ /* -- cgit 1.2.3-korg