Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Costa Shulyupin and committed by
Tomas Glozar
115b06a0 e4be7e96

+29 -60
+5 -2
tools/tracing/rtla/src/common.c
··· 5 5 #include <signal.h> 6 6 #include <stdlib.h> 7 7 #include <string.h> 8 - #include <unistd.h> 9 8 #include <getopt.h> 9 + #include <sys/sysinfo.h> 10 + 10 11 #include "common.h" 11 12 12 13 struct trace_instance *trace_inst; 13 14 volatile int stop_tracing; 15 + int nr_cpus; 14 16 15 17 static void stop_trace(int sig) 16 18 { ··· 167 165 } 168 166 169 167 if (!params->cpus) { 170 - for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++) 168 + for (i = 0; i < nr_cpus; i++) 171 169 CPU_SET(i, &params->monitored_cpus); 172 170 } 173 171 ··· 215 213 bool stopped; 216 214 int retval; 217 215 216 + nr_cpus = get_nprocs_conf(); 218 217 params = ops->parse_args(argc, argv); 219 218 if (!params) 220 219 exit(1);
+2
tools/tracing/rtla/src/common.h
··· 108 108 struct timerlat_u_params user; 109 109 }; 110 110 111 + extern int nr_cpus; 112 + 111 113 #define for_each_monitored_cpu(cpu, nr_cpus, common) \ 112 114 for (cpu = 0; cpu < nr_cpus; cpu++) \ 113 115 if (!(common)->cpus || CPU_ISSET(cpu, &(common)->monitored_cpus))
-3
tools/tracing/rtla/src/osnoise_hist.c
··· 646 646 *osnoise_init_hist(struct common_params *params) 647 647 { 648 648 struct osnoise_tool *tool; 649 - int nr_cpus; 650 - 651 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 652 649 653 650 tool = osnoise_init_tool("osnoise_hist"); 654 651 if (!tool)
-7
tools/tracing/rtla/src/osnoise_top.c
··· 232 232 { 233 233 struct osnoise_params *params = to_osnoise_params(top->params); 234 234 struct trace_instance *trace = &top->trace; 235 - static int nr_cpus = -1; 236 235 int i; 237 - 238 - if (nr_cpus == -1) 239 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 240 236 241 237 if (!params->common.quiet) 242 238 clear_terminal(trace->seq); ··· 490 494 struct osnoise_tool *osnoise_init_top(struct common_params *params) 491 495 { 492 496 struct osnoise_tool *tool; 493 - int nr_cpus; 494 - 495 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 496 497 497 498 tool = osnoise_init_tool("osnoise_top"); 498 499 if (!tool)
+1 -4
tools/tracing/rtla/src/timerlat.c
··· 99 99 int timerlat_enable(struct osnoise_tool *tool) 100 100 { 101 101 struct timerlat_params *params = to_timerlat_params(tool->params); 102 - int retval, nr_cpus, i; 102 + int retval, i; 103 103 104 104 if (params->dma_latency >= 0) { 105 105 dma_latency_fd = set_cpu_dma_latency(params->dma_latency); ··· 114 114 err_msg("rtla built without libcpupower, --deepest-idle-state is not supported\n"); 115 115 return -1; 116 116 } 117 - 118 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 119 117 120 118 for_each_monitored_cpu(i, nr_cpus, &params->common) { 121 119 if (save_cpu_idle_disable_state(i) < 0) { ··· 212 214 void timerlat_free(struct osnoise_tool *tool) 213 215 { 214 216 struct timerlat_params *params = to_timerlat_params(tool->params); 215 - int nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 216 217 int i; 217 218 218 219 timerlat_aa_destroy();
-1
tools/tracing/rtla/src/timerlat_aa.c
··· 1043 1043 */ 1044 1044 int timerlat_aa_init(struct osnoise_tool *tool, int dump_tasks, enum stack_format stack_format) 1045 1045 { 1046 - int nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 1047 1046 struct timerlat_aa_context *taa_ctx; 1048 1047 int retval; 1049 1048
-3
tools/tracing/rtla/src/timerlat_hist.c
··· 1040 1040 *timerlat_init_hist(struct common_params *params) 1041 1041 { 1042 1042 struct osnoise_tool *tool; 1043 - int nr_cpus; 1044 - 1045 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 1046 1043 1047 1044 tool = osnoise_init_tool("timerlat_hist"); 1048 1045 if (!tool)
-7
tools/tracing/rtla/src/timerlat_top.c
··· 442 442 struct timerlat_params *params = to_timerlat_params(top->params); 443 443 struct trace_instance *trace = &top->trace; 444 444 struct timerlat_top_cpu summary; 445 - static int nr_cpus = -1; 446 445 int i; 447 446 448 447 if (params->common.aa_only) 449 448 return; 450 - 451 - if (nr_cpus == -1) 452 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 453 449 454 450 if (!params->common.quiet) 455 451 clear_terminal(trace->seq); ··· 786 790 *timerlat_init_top(struct common_params *params) 787 791 { 788 792 struct osnoise_tool *top; 789 - int nr_cpus; 790 - 791 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 792 793 793 794 top = osnoise_init_tool("timerlat_top"); 794 795 if (!top)
+1 -2
tools/tracing/rtla/src/timerlat_u.c
··· 16 16 #include <sys/wait.h> 17 17 #include <sys/prctl.h> 18 18 19 - #include "utils.h" 19 + #include "common.h" 20 20 #include "timerlat_u.h" 21 21 22 22 /* ··· 131 131 */ 132 132 void *timerlat_u_dispatcher(void *data) 133 133 { 134 - int nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 135 134 struct timerlat_u_params *params = data; 136 135 char proc_name[128]; 137 136 int procs_count = 0;
+1
tools/tracing/rtla/src/timerlat_u.h
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 + #pragma once 2 3 /* 3 4 * Copyright (C) 2023 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org> 4 5 */
+1 -9
tools/tracing/rtla/src/utils.c
··· 19 19 #include <stdio.h> 20 20 #include <limits.h> 21 21 22 - #include "utils.h" 22 + #include "common.h" 23 23 24 24 #define MAX_MSG_LENGTH 1024 25 25 int config_debug; ··· 119 119 { 120 120 const char *p; 121 121 int end_cpu; 122 - int nr_cpus; 123 122 int cpu; 124 123 int i; 125 124 126 125 CPU_ZERO(set); 127 - 128 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 129 126 130 127 for (p = cpu_list; *p; ) { 131 128 cpu = atoi(p); ··· 574 577 unsigned int nr_states; 575 578 unsigned int state; 576 579 int disabled; 577 - int nr_cpus; 578 580 579 581 nr_states = cpuidle_state_count(cpu); 580 582 ··· 581 585 return 0; 582 586 583 587 if (saved_cpu_idle_disable_state == NULL) { 584 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 585 588 saved_cpu_idle_disable_state = calloc(nr_cpus, sizeof(unsigned int *)); 586 589 if (!saved_cpu_idle_disable_state) 587 590 return -1; ··· 657 662 void free_cpu_idle_disable_states(void) 658 663 { 659 664 int cpu; 660 - int nr_cpus; 661 665 662 666 if (!saved_cpu_idle_disable_state) 663 667 return; 664 - 665 - nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 666 668 667 669 for (cpu = 0; cpu < nr_cpus; cpu++) { 668 670 free(saved_cpu_idle_disable_state[cpu]);
+18 -22
tools/tracing/rtla/tests/unit/unit_tests.c
··· 7 7 #include <sched.h> 8 8 #include <limits.h> 9 9 #include <unistd.h> 10 + #include <sys/sysinfo.h> 10 11 11 12 #include "../../src/utils.h" 13 + int nr_cpus; 12 14 13 15 START_TEST(test_strtoi) 14 16 { ··· 36 34 START_TEST(test_parse_cpu_set) 37 35 { 38 36 cpu_set_t set; 39 - int nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 40 37 38 + nr_cpus = 8; 41 39 ck_assert_int_eq(parse_cpu_set("0", &set), 0); 42 40 ck_assert(CPU_ISSET(0, &set)); 43 41 ck_assert(!CPU_ISSET(1, &set)); 44 42 45 - if (nr_cpus > 2) { 46 - ck_assert_int_eq(parse_cpu_set("0,2", &set), 0); 47 - ck_assert(CPU_ISSET(0, &set)); 48 - ck_assert(CPU_ISSET(2, &set)); 49 - } 43 + ck_assert_int_eq(parse_cpu_set("0,2", &set), 0); 44 + ck_assert(CPU_ISSET(0, &set)); 45 + ck_assert(CPU_ISSET(2, &set)); 50 46 51 - if (nr_cpus > 3) { 52 - ck_assert_int_eq(parse_cpu_set("0-3", &set), 0); 53 - ck_assert(CPU_ISSET(0, &set)); 54 - ck_assert(CPU_ISSET(1, &set)); 55 - ck_assert(CPU_ISSET(2, &set)); 56 - ck_assert(CPU_ISSET(3, &set)); 57 - } 47 + ck_assert_int_eq(parse_cpu_set("0-3", &set), 0); 48 + ck_assert(CPU_ISSET(0, &set)); 49 + ck_assert(CPU_ISSET(1, &set)); 50 + ck_assert(CPU_ISSET(2, &set)); 51 + ck_assert(CPU_ISSET(3, &set)); 58 52 59 - if (nr_cpus > 5) { 60 - ck_assert_int_eq(parse_cpu_set("1-3,5", &set), 0); 61 - ck_assert(!CPU_ISSET(0, &set)); 62 - ck_assert(CPU_ISSET(1, &set)); 63 - ck_assert(CPU_ISSET(2, &set)); 64 - ck_assert(CPU_ISSET(3, &set)); 65 - ck_assert(!CPU_ISSET(4, &set)); 66 - ck_assert(CPU_ISSET(5, &set)); 67 - } 53 + ck_assert_int_eq(parse_cpu_set("1-3,5", &set), 0); 54 + ck_assert(!CPU_ISSET(0, &set)); 55 + ck_assert(CPU_ISSET(1, &set)); 56 + ck_assert(CPU_ISSET(2, &set)); 57 + ck_assert(CPU_ISSET(3, &set)); 58 + ck_assert(!CPU_ISSET(4, &set)); 59 + ck_assert(CPU_ISSET(5, &set)); 68 60 69 61 ck_assert_int_eq(parse_cpu_set("-1", &set), 1); 70 62 ck_assert_int_eq(parse_cpu_set("abc", &set), 1);