File 04-Resolve-max-count-of-CPUs-at-runtime.patch of Package numatop
From ebe98ffcc2517fa7e0a251914c52274e8a378b1c Mon Sep 17 00:00:00 2001
From: Sandipan Das <sandipan.das@amd.com>
Date: Fri, 27 Jun 2025 15:47:59 +0530
Subject: [PATCH] common: Resolve max count of CPUs at runtime
Replace statically defined NCPUS_MAX with a variable which is set at
runtime. Use libnuma helpers to determine the OS-defined limits for the
maximum possible number of CPUs in the the system.
Signed-off-by: Sandipan Das <sandipan.das@amd.com>
---
common/include/types.h | 2 +-
common/numatop.c | 2 +-
common/os/node.c | 2 ++
common/os/os_util.c | 16 ++++++++++------
4 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/common/include/types.h b/common/include/types.h
index e688225..eb64fb1 100644
--- a/common/include/types.h
+++ b/common/include/types.h
@@ -116,12 +116,12 @@ typedef enum {
#define UI_COUNT_NUM 5
#define NCPUS_NODE_MAX 256
-#define NCPUS_MAX (nnodes_max * NCPUS_NODE_MAX)
#define NPROCS_NAX 4096
#define LL_THRESH 128
#define LL_PERIOD 1000
extern int nnodes_max;
+extern int ncpus_max;
typedef struct _count_value {
uint64_t counts[PERF_COUNT_NUM];
diff --git a/common/numatop.c b/common/numatop.c
index c5c0580..2e95378 100644
--- a/common/numatop.c
+++ b/common/numatop.c
@@ -236,7 +236,7 @@ main(int argc, char *argv[])
if (node_group_init() != 0) {
stderr_print("The node/cpu number is out of range, \n"
"numatop supports up to %d nodes and %d CPUs\n",
- nnodes_max, NCPUS_MAX);
+ nnodes_max, ncpus_max);
goto L_EXIT5;
}
diff --git a/common/os/node.c b/common/os/node.c
index c2ca7a7..f79bcdf 100644
--- a/common/os/node.c
+++ b/common/os/node.c
@@ -48,6 +48,7 @@ static node_group_t s_node_group;
int g_ncpus;
int nnodes_max;
+int ncpus_max;
static void
node_init(node_t *node, int nid, boolean_t hotadd)
@@ -86,6 +87,7 @@ node_group_init(void)
return (-1);
nnodes_max = numa_num_possible_nodes();
+ ncpus_max = numa_num_possible_cpus();
(void) memset(&s_node_group, 0, sizeof (node_group_t));
if (pthread_mutex_init(&s_node_group.mutex, NULL) != 0) {
diff --git a/common/os/os_util.c b/common/os/os_util.c
index ec3f6e5..c836e89 100644
--- a/common/os/os_util.c
+++ b/common/os/os_util.c
@@ -470,19 +470,23 @@ os_sysfs_cpu_enum(int nid, int *cpu_arr, int arr_size, int *num)
int
os_sysfs_online_ncpus(void)
{
- int cpu_arr[NCPUS_MAX], num;
+ int *cpu_arr, num, ret = -1;
char path[PATH_MAX];
- if (sysconf(_SC_NPROCESSORS_CONF) > NCPUS_MAX) {
+ cpu_arr = (int *) malloc(ncpus_max * sizeof(int));
+ if (!cpu_arr)
return (-1);
- }
snprintf(path, PATH_MAX, "/sys/devices/system/cpu/online");
- if (!file_int_extract(path, cpu_arr, NCPUS_MAX, &num)) {
- return (-1);
+ if (!file_int_extract(path, cpu_arr, ncpus_max, &num)) {
+ goto L_EXIT;
}
- return (num);
+ ret = num;
+
+L_EXIT:
+ free(cpu_arr);
+ return (ret);
}
static boolean_t