diff options
author | Damjan Marion <damarion@cisco.com> | 2024-02-12 19:44:58 +0000 |
---|---|---|
committer | Mohammed HAWARI <momohawari@gmail.com> | 2024-03-18 16:01:35 +0000 |
commit | 3eb6cbec50a5cbe4c3465d60ba6aea7bf2845cd1 (patch) | |
tree | 0f45044087400911e213048c1967ac96a9f8ea29 /src/vppinfra | |
parent | 8799bf6ca6b78321049bbc397256dd8b3884d829 (diff) |
vppinfra: os agnostic api for getting CPU information
Avoid direct sysfs reads when possible...
Type: improvement
Change-Id: I2b84cd18f3da47925d068951f24b79b5b6e20bb1
Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vppinfra')
-rw-r--r-- | src/vppinfra/linux/mem.c | 1 | ||||
-rw-r--r-- | src/vppinfra/linux/sysfs.c | 14 | ||||
-rw-r--r-- | src/vppinfra/linux/sysfs.h | 3 | ||||
-rw-r--r-- | src/vppinfra/pmalloc.c | 2 | ||||
-rw-r--r-- | src/vppinfra/unix-misc.c | 47 | ||||
-rw-r--r-- | src/vppinfra/unix.h | 9 |
6 files changed, 69 insertions, 7 deletions
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 <vppinfra/bitmap.h> #include <vppinfra/format.h> #include <vppinfra/clib_error.h> -#include <vppinfra/linux/sysfs.h> #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 <vppinfra/error.h> +#include <vppinfra/bitmap.h> 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 <sched.h> #include <vppinfra/format.h> +#ifdef __linux__ #include <vppinfra/linux/sysfs.h> +#endif #include <vppinfra/mem.h> #include <vppinfra/hash.h> #include <vppinfra/pmalloc.h> 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 */ /* |