tools/rtla: Consolidate nr_cpus usage across all tools

sysconf(_SC_NPROCESSORS_CONF) (via get_nprocs_conf) reflects
cpu_possible_mask, which is fixed at boot time, so querying it
repeatedly is unnecessary.

Replace multiple calls to sysconf(_SC_NPROCESSORS_CONF) with a single
global nr_cpus variable initialized once at startup.

`#pragma once` in timerlat_u.h is needed for pre-C23 compilers to avoid
redefinition errors.

Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>
Link: https://lore.kernel.org/r/20260306194953.2511960-2-costa.shul@redhat.com
Signed-off-by: Tomas Glozar <tglozar@redhat.com>
This commit is contained in:
Costa Shulyupin 2026-03-06 21:49:49 +02:00 committed by Tomas Glozar
parent e4be7e96d0
commit 115b06a008
12 changed files with 29 additions and 60 deletions

View File

@ -5,12 +5,14 @@
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <sys/sysinfo.h>
#include "common.h" #include "common.h"
struct trace_instance *trace_inst; struct trace_instance *trace_inst;
volatile int stop_tracing; volatile int stop_tracing;
int nr_cpus;
static void stop_trace(int sig) static void stop_trace(int sig)
{ {
@ -165,7 +167,7 @@ common_apply_config(struct osnoise_tool *tool, struct common_params *params)
} }
if (!params->cpus) { if (!params->cpus) {
for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++) for (i = 0; i < nr_cpus; i++)
CPU_SET(i, &params->monitored_cpus); CPU_SET(i, &params->monitored_cpus);
} }
@ -213,6 +215,7 @@ int run_tool(struct tool_ops *ops, int argc, char *argv[])
bool stopped; bool stopped;
int retval; int retval;
nr_cpus = get_nprocs_conf();
params = ops->parse_args(argc, argv); params = ops->parse_args(argc, argv);
if (!params) if (!params)
exit(1); exit(1);

View File

@ -108,6 +108,8 @@ struct common_params {
struct timerlat_u_params user; struct timerlat_u_params user;
}; };
extern int nr_cpus;
#define for_each_monitored_cpu(cpu, nr_cpus, common) \ #define for_each_monitored_cpu(cpu, nr_cpus, common) \
for (cpu = 0; cpu < nr_cpus; cpu++) \ for (cpu = 0; cpu < nr_cpus; cpu++) \
if (!(common)->cpus || CPU_ISSET(cpu, &(common)->monitored_cpus)) if (!(common)->cpus || CPU_ISSET(cpu, &(common)->monitored_cpus))

View File

@ -646,9 +646,6 @@ static struct osnoise_tool
*osnoise_init_hist(struct common_params *params) *osnoise_init_hist(struct common_params *params)
{ {
struct osnoise_tool *tool; struct osnoise_tool *tool;
int nr_cpus;
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
tool = osnoise_init_tool("osnoise_hist"); tool = osnoise_init_tool("osnoise_hist");
if (!tool) if (!tool)

View File

@ -232,12 +232,8 @@ osnoise_print_stats(struct osnoise_tool *top)
{ {
struct osnoise_params *params = to_osnoise_params(top->params); struct osnoise_params *params = to_osnoise_params(top->params);
struct trace_instance *trace = &top->trace; struct trace_instance *trace = &top->trace;
static int nr_cpus = -1;
int i; int i;
if (nr_cpus == -1)
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
if (!params->common.quiet) if (!params->common.quiet)
clear_terminal(trace->seq); clear_terminal(trace->seq);
@ -494,9 +490,6 @@ osnoise_top_apply_config(struct osnoise_tool *tool)
struct osnoise_tool *osnoise_init_top(struct common_params *params) struct osnoise_tool *osnoise_init_top(struct common_params *params)
{ {
struct osnoise_tool *tool; struct osnoise_tool *tool;
int nr_cpus;
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
tool = osnoise_init_tool("osnoise_top"); tool = osnoise_init_tool("osnoise_top");
if (!tool) if (!tool)

View File

@ -99,7 +99,7 @@ timerlat_apply_config(struct osnoise_tool *tool, struct timerlat_params *params)
int timerlat_enable(struct osnoise_tool *tool) int timerlat_enable(struct osnoise_tool *tool)
{ {
struct timerlat_params *params = to_timerlat_params(tool->params); struct timerlat_params *params = to_timerlat_params(tool->params);
int retval, nr_cpus, i; int retval, i;
if (params->dma_latency >= 0) { if (params->dma_latency >= 0) {
dma_latency_fd = set_cpu_dma_latency(params->dma_latency); dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
@ -115,8 +115,6 @@ int timerlat_enable(struct osnoise_tool *tool)
return -1; return -1;
} }
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
for_each_monitored_cpu(i, nr_cpus, &params->common) { for_each_monitored_cpu(i, nr_cpus, &params->common) {
if (save_cpu_idle_disable_state(i) < 0) { if (save_cpu_idle_disable_state(i) < 0) {
err_msg("Could not save cpu idle state.\n"); err_msg("Could not save cpu idle state.\n");
@ -214,7 +212,6 @@ void timerlat_analyze(struct osnoise_tool *tool, bool stopped)
void timerlat_free(struct osnoise_tool *tool) void timerlat_free(struct osnoise_tool *tool)
{ {
struct timerlat_params *params = to_timerlat_params(tool->params); struct timerlat_params *params = to_timerlat_params(tool->params);
int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
int i; int i;
timerlat_aa_destroy(); timerlat_aa_destroy();

View File

@ -1043,7 +1043,6 @@ void timerlat_aa_destroy(void)
*/ */
int timerlat_aa_init(struct osnoise_tool *tool, int dump_tasks, enum stack_format stack_format) int timerlat_aa_init(struct osnoise_tool *tool, int dump_tasks, enum stack_format stack_format)
{ {
int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
struct timerlat_aa_context *taa_ctx; struct timerlat_aa_context *taa_ctx;
int retval; int retval;

View File

@ -1040,9 +1040,6 @@ static struct osnoise_tool
*timerlat_init_hist(struct common_params *params) *timerlat_init_hist(struct common_params *params)
{ {
struct osnoise_tool *tool; struct osnoise_tool *tool;
int nr_cpus;
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
tool = osnoise_init_tool("timerlat_hist"); tool = osnoise_init_tool("timerlat_hist");
if (!tool) if (!tool)

View File

@ -442,15 +442,11 @@ timerlat_print_stats(struct osnoise_tool *top)
struct timerlat_params *params = to_timerlat_params(top->params); struct timerlat_params *params = to_timerlat_params(top->params);
struct trace_instance *trace = &top->trace; struct trace_instance *trace = &top->trace;
struct timerlat_top_cpu summary; struct timerlat_top_cpu summary;
static int nr_cpus = -1;
int i; int i;
if (params->common.aa_only) if (params->common.aa_only)
return; return;
if (nr_cpus == -1)
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
if (!params->common.quiet) if (!params->common.quiet)
clear_terminal(trace->seq); clear_terminal(trace->seq);
@ -790,9 +786,6 @@ static struct osnoise_tool
*timerlat_init_top(struct common_params *params) *timerlat_init_top(struct common_params *params)
{ {
struct osnoise_tool *top; struct osnoise_tool *top;
int nr_cpus;
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
top = osnoise_init_tool("timerlat_top"); top = osnoise_init_tool("timerlat_top");
if (!top) if (!top)

View File

@ -16,7 +16,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include "utils.h" #include "common.h"
#include "timerlat_u.h" #include "timerlat_u.h"
/* /*
@ -131,7 +131,6 @@ static int timerlat_u_send_kill(pid_t *procs, int nr_cpus)
*/ */
void *timerlat_u_dispatcher(void *data) void *timerlat_u_dispatcher(void *data)
{ {
int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
struct timerlat_u_params *params = data; struct timerlat_u_params *params = data;
char proc_name[128]; char proc_name[128];
int procs_count = 0; int procs_count = 0;

View File

@ -1,4 +1,5 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#pragma once
/* /*
* Copyright (C) 2023 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org> * Copyright (C) 2023 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
*/ */

View File

@ -19,7 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <limits.h> #include <limits.h>
#include "utils.h" #include "common.h"
#define MAX_MSG_LENGTH 1024 #define MAX_MSG_LENGTH 1024
int config_debug; int config_debug;
@ -119,14 +119,11 @@ int parse_cpu_set(char *cpu_list, cpu_set_t *set)
{ {
const char *p; const char *p;
int end_cpu; int end_cpu;
int nr_cpus;
int cpu; int cpu;
int i; int i;
CPU_ZERO(set); CPU_ZERO(set);
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
for (p = cpu_list; *p; ) { for (p = cpu_list; *p; ) {
cpu = atoi(p); cpu = atoi(p);
if (cpu < 0 || (!cpu && *p != '0') || cpu >= nr_cpus) if (cpu < 0 || (!cpu && *p != '0') || cpu >= nr_cpus)
@ -577,7 +574,6 @@ int save_cpu_idle_disable_state(unsigned int cpu)
unsigned int nr_states; unsigned int nr_states;
unsigned int state; unsigned int state;
int disabled; int disabled;
int nr_cpus;
nr_states = cpuidle_state_count(cpu); nr_states = cpuidle_state_count(cpu);
@ -585,7 +581,6 @@ int save_cpu_idle_disable_state(unsigned int cpu)
return 0; return 0;
if (saved_cpu_idle_disable_state == NULL) { if (saved_cpu_idle_disable_state == NULL) {
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
saved_cpu_idle_disable_state = calloc(nr_cpus, sizeof(unsigned int *)); saved_cpu_idle_disable_state = calloc(nr_cpus, sizeof(unsigned int *));
if (!saved_cpu_idle_disable_state) if (!saved_cpu_idle_disable_state)
return -1; return -1;
@ -662,13 +657,10 @@ int restore_cpu_idle_disable_state(unsigned int cpu)
void free_cpu_idle_disable_states(void) void free_cpu_idle_disable_states(void)
{ {
int cpu; int cpu;
int nr_cpus;
if (!saved_cpu_idle_disable_state) if (!saved_cpu_idle_disable_state)
return; return;
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
for (cpu = 0; cpu < nr_cpus; cpu++) { for (cpu = 0; cpu < nr_cpus; cpu++) {
free(saved_cpu_idle_disable_state[cpu]); free(saved_cpu_idle_disable_state[cpu]);
saved_cpu_idle_disable_state[cpu] = NULL; saved_cpu_idle_disable_state[cpu] = NULL;

View File

@ -7,8 +7,10 @@
#include <sched.h> #include <sched.h>
#include <limits.h> #include <limits.h>
#include <unistd.h> #include <unistd.h>
#include <sys/sysinfo.h>
#include "../../src/utils.h" #include "../../src/utils.h"
int nr_cpus;
START_TEST(test_strtoi) START_TEST(test_strtoi)
{ {
@ -34,35 +36,29 @@ END_TEST
START_TEST(test_parse_cpu_set) START_TEST(test_parse_cpu_set)
{ {
cpu_set_t set; cpu_set_t set;
int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
nr_cpus = 8;
ck_assert_int_eq(parse_cpu_set("0", &set), 0); ck_assert_int_eq(parse_cpu_set("0", &set), 0);
ck_assert(CPU_ISSET(0, &set)); ck_assert(CPU_ISSET(0, &set));
ck_assert(!CPU_ISSET(1, &set)); ck_assert(!CPU_ISSET(1, &set));
if (nr_cpus > 2) { ck_assert_int_eq(parse_cpu_set("0,2", &set), 0);
ck_assert_int_eq(parse_cpu_set("0,2", &set), 0); ck_assert(CPU_ISSET(0, &set));
ck_assert(CPU_ISSET(0, &set)); ck_assert(CPU_ISSET(2, &set));
ck_assert(CPU_ISSET(2, &set));
}
if (nr_cpus > 3) { ck_assert_int_eq(parse_cpu_set("0-3", &set), 0);
ck_assert_int_eq(parse_cpu_set("0-3", &set), 0); ck_assert(CPU_ISSET(0, &set));
ck_assert(CPU_ISSET(0, &set)); ck_assert(CPU_ISSET(1, &set));
ck_assert(CPU_ISSET(1, &set)); ck_assert(CPU_ISSET(2, &set));
ck_assert(CPU_ISSET(2, &set)); ck_assert(CPU_ISSET(3, &set));
ck_assert(CPU_ISSET(3, &set));
}
if (nr_cpus > 5) { ck_assert_int_eq(parse_cpu_set("1-3,5", &set), 0);
ck_assert_int_eq(parse_cpu_set("1-3,5", &set), 0); ck_assert(!CPU_ISSET(0, &set));
ck_assert(!CPU_ISSET(0, &set)); ck_assert(CPU_ISSET(1, &set));
ck_assert(CPU_ISSET(1, &set)); ck_assert(CPU_ISSET(2, &set));
ck_assert(CPU_ISSET(2, &set)); ck_assert(CPU_ISSET(3, &set));
ck_assert(CPU_ISSET(3, &set)); ck_assert(!CPU_ISSET(4, &set));
ck_assert(!CPU_ISSET(4, &set)); ck_assert(CPU_ISSET(5, &set));
ck_assert(CPU_ISSET(5, &set));
}
ck_assert_int_eq(parse_cpu_set("-1", &set), 1); ck_assert_int_eq(parse_cpu_set("-1", &set), 1);
ck_assert_int_eq(parse_cpu_set("abc", &set), 1); ck_assert_int_eq(parse_cpu_set("abc", &set), 1);