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/vlib/buffer.c | 6 +----- src/vlib/physmem.c | 1 - src/vlib/threads.c | 24 +++++++---------------- 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 +++++++++ 9 files changed, 77 insertions(+), 30 deletions(-) diff --git a/src/vlib/buffer.c b/src/vlib/buffer.c index b43c1dace97..674f15d5dc6 100644 --- a/src/vlib/buffer.c +++ b/src/vlib/buffer.c @@ -43,7 +43,6 @@ * Allocate/free network buffers. */ -#include #include #include #include @@ -837,10 +836,7 @@ vlib_buffer_main_init (struct vlib_main_t * vm) clib_spinlock_init (&bm->buffer_known_hash_lockp); bmp = os_get_online_cpu_node_bitmap (); - - if ((err = clib_sysfs_read ("/sys/devices/system/node/has_memory", "%U", - unformat_bitmap_list, &bmp_has_memory))) - clib_error_free (err); + bmp_has_memory = os_get_cpu_with_memory_bitmap (); if (bmp && bmp_has_memory) bmp = clib_bitmap_and (bmp, bmp_has_memory); diff --git a/src/vlib/physmem.c b/src/vlib/physmem.c index c6727bb92cb..84c61d2a44f 100644 --- a/src/vlib/physmem.c +++ b/src/vlib/physmem.c @@ -22,7 +22,6 @@ #include #include -#include #include #include #include diff --git a/src/vlib/threads.c b/src/vlib/threads.c index f0cb69b3400..713e1927d1f 100644 --- a/src/vlib/threads.c +++ b/src/vlib/threads.c @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -427,30 +426,21 @@ vlib_worker_thread_bootstrap_fn (void *arg) void vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id) { - const char *sys_cpu_path = "/sys/devices/system/cpu/cpu"; - const char *sys_node_path = "/sys/devices/system/node/node"; clib_bitmap_t *nbmp = 0, *cbmp = 0; - u32 node; - u8 *p = 0; - int core_id = -1, numa_id = -1; - - p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, cpu_id, 0); - clib_sysfs_read ((char *) p, "%d", &core_id); - vec_reset_length (p); + int node, core_id = -1, numa_id = -1; + core_id = os_get_cpu_phys_core_id (cpu_id); nbmp = os_get_online_cpu_node_bitmap (); + clib_bitmap_foreach (node, nbmp) { - p = format (p, "%s%u/cpulist%c", sys_node_path, node, 0); - clib_sysfs_read ((char *) p, "%U", unformat_bitmap_list, &cbmp); - if (clib_bitmap_get (cbmp, cpu_id)) - numa_id = node; - vec_reset_length (cbmp); - vec_reset_length (p); + cbmp = os_get_cpu_on_node_bitmap (node); + if (clib_bitmap_get (cbmp, cpu_id)) + numa_id = node; + vec_reset_length (cbmp); } vec_free (nbmp); vec_free (cbmp); - vec_free (p); w->core_id = core_id; w->numa_id = numa_id; 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