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

tools/rtla: Create common_apply_config()

Merge the common bits of osnoise_apply_config() and
timerlat_apply_config(). Put the result in a new common.c, and move
enough things to common.h so that common.c does not need to include
osnoise.h.

Cc: John Kacur <jkacur@redhat.com>
Cc: Costa Shulyupin <costa.shul@redhat.com>
Link: https://lore.kernel.org/20250907022325.243930-4-crwood@redhat.com
Reviewed-by: Tomas Glozar <tglozar@redhat.com>
Signed-off-by: Crystal Wood <crwood@redhat.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

authored by

Crystal Wood and committed by
Steven Rostedt (Google)
263d7eac 5742bf62

+142 -150
+1
tools/tracing/rtla/src/Build
··· 1 1 rtla-y += trace.o 2 2 rtla-y += utils.o 3 3 rtla-y += actions.o 4 + rtla-y += common.o 4 5 rtla-y += osnoise.o 5 6 rtla-y += osnoise_top.o 6 7 rtla-y += osnoise_hist.o
+63
tools/tracing/rtla/src/common.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #define _GNU_SOURCE 3 + 4 + #include <unistd.h> 5 + #include "common.h" 6 + 7 + /* 8 + * common_apply_config - apply common configs to the initialized tool 9 + */ 10 + int 11 + common_apply_config(struct osnoise_tool *tool, struct common_params *params) 12 + { 13 + int retval, i; 14 + 15 + if (!params->sleep_time) 16 + params->sleep_time = 1; 17 + 18 + retval = osnoise_set_cpus(tool->context, params->cpus ? params->cpus : "all"); 19 + if (retval) { 20 + err_msg("Failed to apply CPUs config\n"); 21 + goto out_err; 22 + } 23 + 24 + if (!params->cpus) { 25 + for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++) 26 + CPU_SET(i, &params->monitored_cpus); 27 + } 28 + 29 + if (params->hk_cpus) { 30 + retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set), 31 + &params->hk_cpu_set); 32 + if (retval == -1) { 33 + err_msg("Failed to set rtla to the house keeping CPUs\n"); 34 + goto out_err; 35 + } 36 + } else if (params->cpus) { 37 + /* 38 + * Even if the user do not set a house-keeping CPU, try to 39 + * move rtla to a CPU set different to the one where the user 40 + * set the workload to run. 41 + * 42 + * No need to check results as this is an automatic attempt. 43 + */ 44 + auto_house_keeping(&params->monitored_cpus); 45 + } 46 + 47 + /* 48 + * Set workload according to type of thread if the kernel supports it. 49 + * On kernels without support, user threads will have already failed 50 + * on missing fd, and kernel threads do not need it. 51 + */ 52 + retval = osnoise_set_workload(tool->context, params->kernel_workload); 53 + if (retval < -1) { 54 + err_msg("Failed to set OSNOISE_WORKLOAD option\n"); 55 + goto out_err; 56 + } 57 + 58 + return 0; 59 + 60 + out_err: 61 + return -1; 62 + } 63 +
+67
tools/tracing/rtla/src/common.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 #pragma once 3 3 4 + #include "trace.h" 4 5 #include "utils.h" 6 + 7 + /* 8 + * osnoise_context - read, store, write, restore osnoise configs. 9 + */ 10 + struct osnoise_context { 11 + int flags; 12 + int ref; 13 + 14 + char *curr_cpus; 15 + char *orig_cpus; 16 + 17 + /* 0 as init value */ 18 + unsigned long long orig_runtime_us; 19 + unsigned long long runtime_us; 20 + 21 + /* 0 as init value */ 22 + unsigned long long orig_period_us; 23 + unsigned long long period_us; 24 + 25 + /* 0 as init value */ 26 + long long orig_timerlat_period_us; 27 + long long timerlat_period_us; 28 + 29 + /* 0 as init value */ 30 + long long orig_tracing_thresh; 31 + long long tracing_thresh; 32 + 33 + /* -1 as init value because 0 is disabled */ 34 + long long orig_stop_us; 35 + long long stop_us; 36 + 37 + /* -1 as init value because 0 is disabled */ 38 + long long orig_stop_total_us; 39 + long long stop_total_us; 40 + 41 + /* -1 as init value because 0 is disabled */ 42 + long long orig_print_stack; 43 + long long print_stack; 44 + 45 + /* -1 as init value because 0 is off */ 46 + int orig_opt_irq_disable; 47 + int opt_irq_disable; 48 + 49 + /* -1 as init value because 0 is off */ 50 + int orig_opt_workload; 51 + int opt_workload; 52 + }; 53 + 54 + /* 55 + * osnoise_tool - osnoise based tool definition. 56 + */ 57 + struct osnoise_tool { 58 + struct trace_instance trace; 59 + struct osnoise_context *context; 60 + void *data; 61 + void *params; 62 + time_t start_time; 63 + }; 5 64 6 65 struct hist_params { 7 66 char no_irq; ··· 103 44 int output_divisor; 104 45 int pretty_output; 105 46 int quiet; 47 + int kernel_workload; 106 48 }; 49 + 50 + int osnoise_set_cpus(struct osnoise_context *context, char *cpus); 51 + void osnoise_restore_cpus(struct osnoise_context *context); 52 + 53 + int osnoise_set_workload(struct osnoise_context *context, bool onoff); 54 + 55 + int common_apply_config(struct osnoise_tool *tool, struct common_params *params);
+3 -34
tools/tracing/rtla/src/osnoise.c
··· 1120 1120 } 1121 1121 1122 1122 /* 1123 - * osnoise_apply_config - apply common configs to the initialized tool 1123 + * osnoise_apply_config - apply osnoise configs to the initialized tool 1124 1124 */ 1125 1125 int 1126 1126 osnoise_apply_config(struct osnoise_tool *tool, struct osnoise_params *params) 1127 1127 { 1128 1128 int retval; 1129 1129 1130 - if (!params->common.sleep_time) 1131 - params->common.sleep_time = 1; 1132 - 1133 - retval = osnoise_set_cpus(tool->context, params->common.cpus ? params->common.cpus : "all"); 1134 - if (retval) { 1135 - err_msg("Failed to apply CPUs config\n"); 1136 - goto out_err; 1137 - } 1130 + params->common.kernel_workload = true; 1138 1131 1139 1132 if (params->runtime || params->period) { 1140 1133 retval = osnoise_set_runtime_period(tool->context, ··· 1162 1169 goto out_err; 1163 1170 } 1164 1171 1165 - if (params->common.hk_cpus) { 1166 - retval = sched_setaffinity(getpid(), sizeof(params->common.hk_cpu_set), 1167 - &params->common.hk_cpu_set); 1168 - if (retval == -1) { 1169 - err_msg("Failed to set rtla to the house keeping CPUs\n"); 1170 - goto out_err; 1171 - } 1172 - } else if (params->common.cpus) { 1173 - /* 1174 - * Even if the user do not set a house-keeping CPU, try to 1175 - * move rtla to a CPU set different to the one where the user 1176 - * set the workload to run. 1177 - * 1178 - * No need to check results as this is an automatic attempt. 1179 - */ 1180 - auto_house_keeping(&params->common.monitored_cpus); 1181 - } 1182 - 1183 - retval = osnoise_set_workload(tool->context, true); 1184 - if (retval < -1) { 1185 - err_msg("Failed to set OSNOISE_WORKLOAD option\n"); 1186 - goto out_err; 1187 - } 1188 - 1189 - return 0; 1172 + return common_apply_config(tool, &params->common); 1190 1173 1191 1174 out_err: 1192 1175 return -1;
-64
tools/tracing/rtla/src/osnoise.h
··· 2 2 #pragma once 3 3 4 4 #include "common.h" 5 - #include "trace.h" 6 5 7 6 enum osnoise_mode { 8 7 MODE_OSNOISE = 0, ··· 18 19 }; 19 20 20 21 /* 21 - * osnoise_context - read, store, write, restore osnoise configs. 22 - */ 23 - struct osnoise_context { 24 - int flags; 25 - int ref; 26 - 27 - char *curr_cpus; 28 - char *orig_cpus; 29 - 30 - /* 0 as init value */ 31 - unsigned long long orig_runtime_us; 32 - unsigned long long runtime_us; 33 - 34 - /* 0 as init value */ 35 - unsigned long long orig_period_us; 36 - unsigned long long period_us; 37 - 38 - /* 0 as init value */ 39 - long long orig_timerlat_period_us; 40 - long long timerlat_period_us; 41 - 42 - /* 0 as init value */ 43 - long long orig_tracing_thresh; 44 - long long tracing_thresh; 45 - 46 - /* -1 as init value because 0 is disabled */ 47 - long long orig_stop_us; 48 - long long stop_us; 49 - 50 - /* -1 as init value because 0 is disabled */ 51 - long long orig_stop_total_us; 52 - long long stop_total_us; 53 - 54 - /* -1 as init value because 0 is disabled */ 55 - long long orig_print_stack; 56 - long long print_stack; 57 - 58 - /* -1 as init value because 0 is off */ 59 - int orig_opt_irq_disable; 60 - int opt_irq_disable; 61 - 62 - /* -1 as init value because 0 is off */ 63 - int orig_opt_workload; 64 - int opt_workload; 65 - }; 66 - 67 - /* 68 22 * *_INIT_VALs are also invalid values, they are used to 69 23 * communicate errors. 70 24 */ ··· 27 75 struct osnoise_context *osnoise_context_alloc(void); 28 76 int osnoise_get_context(struct osnoise_context *context); 29 77 void osnoise_put_context(struct osnoise_context *context); 30 - 31 - int osnoise_set_cpus(struct osnoise_context *context, char *cpus); 32 - void osnoise_restore_cpus(struct osnoise_context *context); 33 78 34 79 int osnoise_set_runtime_period(struct osnoise_context *context, 35 80 unsigned long long runtime, ··· 54 105 long long print_stack); 55 106 56 107 int osnoise_set_irq_disable(struct osnoise_context *context, bool onoff); 57 - int osnoise_set_workload(struct osnoise_context *context, bool onoff); 58 - 59 - /* 60 - * osnoise_tool - osnoise based tool definition. 61 - */ 62 - struct osnoise_tool { 63 - struct trace_instance trace; 64 - struct osnoise_context *context; 65 - void *data; 66 - void *params; 67 - time_t start_time; 68 - }; 69 - 70 108 void osnoise_destroy_tool(struct osnoise_tool *top); 71 109 struct osnoise_tool *osnoise_init_tool(char *tool_name); 72 110 struct osnoise_tool *osnoise_init_trace_tool(char *tracer);
+4 -47
tools/tracing/rtla/src/timerlat.c
··· 24 24 int 25 25 timerlat_apply_config(struct osnoise_tool *tool, struct timerlat_params *params) 26 26 { 27 - int retval, i; 28 - 29 - if (!params->common.sleep_time) 30 - params->common.sleep_time = 1; 31 - 32 - retval = osnoise_set_cpus(tool->context, params->common.cpus ? params->common.cpus : "all"); 33 - if (retval) { 34 - err_msg("Failed to apply CPUs config\n"); 35 - goto out_err; 36 - } 37 - 38 - if (!params->common.cpus) { 39 - for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++) 40 - CPU_SET(i, &params->common.monitored_cpus); 41 - } 27 + int retval; 42 28 43 29 if (params->mode != TRACING_MODE_BPF) { 44 30 /* ··· 61 75 goto out_err; 62 76 } 63 77 64 - if (params->common.hk_cpus) { 65 - retval = sched_setaffinity(getpid(), sizeof(params->common.hk_cpu_set), 66 - &params->common.hk_cpu_set); 67 - if (retval == -1) { 68 - err_msg("Failed to set rtla to the house keeping CPUs\n"); 69 - goto out_err; 70 - } 71 - } else if (params->common.cpus) { 72 - /* 73 - * Even if the user do not set a house-keeping CPU, try to 74 - * move rtla to a CPU set different to the one where the user 75 - * set the workload to run. 76 - * 77 - * No need to check results as this is an automatic attempt. 78 - */ 79 - auto_house_keeping(&params->common.monitored_cpus); 80 - } 81 - 82 78 /* 83 79 * If the user did not specify a type of thread, try user-threads first. 84 80 * Fall back to kernel threads otherwise. 85 81 */ 86 - if (!params->kernel_workload && !params->user_data) { 82 + if (!params->common.kernel_workload && !params->user_data) { 87 83 retval = tracefs_file_exists(NULL, "osnoise/per_cpu/cpu0/timerlat_fd"); 88 84 if (retval) { 89 85 debug_msg("User-space interface detected, setting user-threads\n"); ··· 73 105 params->user_data = 1; 74 106 } else { 75 107 debug_msg("User-space interface not detected, setting kernel-threads\n"); 76 - params->kernel_workload = 1; 108 + params->common.kernel_workload = 1; 77 109 } 78 110 } 79 111 80 - /* 81 - * Set workload according to type of thread if the kernel supports it. 82 - * On kernels without support, user threads will have already failed 83 - * on missing timerlat_fd, and kernel threads do not need it. 84 - */ 85 - retval = osnoise_set_workload(tool->context, params->kernel_workload); 86 - if (retval < -1) { 87 - err_msg("Failed to set OSNOISE_WORKLOAD option\n"); 88 - goto out_err; 89 - } 90 - 91 - return 0; 112 + return common_apply_config(tool, &params->common); 92 113 93 114 out_err: 94 115 return -1;
-1
tools/tracing/rtla/src/timerlat.h
··· 27 27 int no_aa; 28 28 int dump_tasks; 29 29 int user_workload; 30 - int kernel_workload; 31 30 int user_data; 32 31 int deepest_idle_state; 33 32 int aa_only;
+2 -2
tools/tracing/rtla/src/timerlat_hist.c
··· 941 941 params->common.stop_us = get_llong_from_str(optarg); 942 942 break; 943 943 case 'k': 944 - params->kernel_workload = 1; 944 + params->common.kernel_workload = 1; 945 945 break; 946 946 case 'n': 947 947 params->common.output_divisor = 1; ··· 1081 1081 if (!params->common.stop_us && !params->common.stop_total_us) 1082 1082 params->no_aa = 1; 1083 1083 1084 - if (params->kernel_workload && params->user_workload) 1084 + if (params->common.kernel_workload && params->user_workload) 1085 1085 timerlat_hist_usage("--kernel-threads and --user-threads are mutually exclusive!"); 1086 1086 1087 1087 /*
+2 -2
tools/tracing/rtla/src/timerlat_top.c
··· 694 694 params->common.stop_us = get_llong_from_str(optarg); 695 695 break; 696 696 case 'k': 697 - params->kernel_workload = true; 697 + params->common.kernel_workload = true; 698 698 break; 699 699 case 'n': 700 700 params->common.output_divisor = 1; ··· 816 816 if (params->no_aa && params->aa_only) 817 817 timerlat_top_usage("--no-aa and --aa-only are mutually exclusive!"); 818 818 819 - if (params->kernel_workload && params->user_workload) 819 + if (params->common.kernel_workload && params->user_workload) 820 820 timerlat_top_usage("--kernel-threads and --user-threads are mutually exclusive!"); 821 821 822 822 /*