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>
This commit is contained in:
Damjan Marion
2024-02-12 19:44:58 +00:00
committed by Mohammed HAWARI
parent 8799bf6ca6
commit 3eb6cbec50
9 changed files with 77 additions and 30 deletions

View File

@ -43,7 +43,6 @@
* Allocate/free network buffers.
*/
#include <vppinfra/linux/sysfs.h>
#include <vppinfra/bitmap.h>
#include <vppinfra/unix.h>
#include <vlib/vlib.h>
@ -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);

View File

@ -22,7 +22,6 @@
#include <unistd.h>
#include <vppinfra/clib.h>
#include <vppinfra/linux/sysfs.h>
#include <vlib/vlib.h>
#include <vlib/physmem.h>
#include <vlib/unix/unix.h>

View File

@ -19,7 +19,6 @@
#include <vppinfra/format.h>
#include <vppinfra/time_range.h>
#include <vppinfra/interrupt.h>
#include <vppinfra/linux/sysfs.h>
#include <vppinfra/bitmap.h>
#include <vppinfra/unix.h>
#include <vlib/vlib.h>
@ -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;

View File

@ -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

View File

@ -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;
fp = fopen (filename, "r");
va_start (va, fmt);
filename = va_format (0, fmt, &va);
va_end (va);
vec_add1 (filename, 0);
fp = fopen ((char *) filename, "r");
vec_free (filename);
if (fp != NULL)
{

View File

@ -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 */

View File

@ -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>

View File

@ -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

View File

@ -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 */
/*