1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org> 4 */ 5 #define _GNU_SOURCE 6 #include <sys/types.h> 7 #include <sys/stat.h> 8 #include <pthread.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <unistd.h> 12 #include <errno.h> 13 #include <fcntl.h> 14 #include <stdio.h> 15 #include <sched.h> 16 17 #include "timerlat.h" 18 #include "timerlat_aa.h" 19 #include "timerlat_bpf.h" 20 21 #define DEFAULT_TIMERLAT_PERIOD 1000 /* 1ms */ 22 23 static int dma_latency_fd = -1; 24 25 /* 26 * timerlat_apply_config - apply common configs to the initialized tool 27 */ 28 int 29 timerlat_apply_config(struct osnoise_tool *tool, struct timerlat_params *params) 30 { 31 int retval; 32 33 /* 34 * Try to enable BPF, unless disabled explicitly. 35 * If BPF enablement fails, fall back to tracefs mode. 36 */ 37 if (getenv("RTLA_NO_BPF") && strncmp(getenv("RTLA_NO_BPF"), "1", 2) == 0) { 38 debug_msg("RTLA_NO_BPF set, disabling BPF\n"); 39 params->mode = TRACING_MODE_TRACEFS; 40 } else if (!tep_find_event_by_name(tool->trace.tep, "osnoise", "timerlat_sample")) { 41 debug_msg("osnoise:timerlat_sample missing, disabling BPF\n"); 42 params->mode = TRACING_MODE_TRACEFS; 43 } else { 44 retval = timerlat_bpf_init(params); 45 if (retval) { 46 debug_msg("Could not enable BPF\n"); 47 params->mode = TRACING_MODE_TRACEFS; 48 } 49 } 50 51 /* Check if BPF action program is requested but BPF is not available */ 52 if (params->bpf_action_program) { 53 if (params->mode == TRACING_MODE_TRACEFS) { 54 err_msg("BPF actions are not supported in tracefs-only mode\n"); 55 goto out_err; 56 } 57 58 if (timerlat_load_bpf_action_program(params->bpf_action_program)) 59 goto out_err; 60 } 61 62 retval = osnoise_set_timerlat_period_us(tool->context, 63 params->timerlat_period_us ? 64 params->timerlat_period_us : 65 DEFAULT_TIMERLAT_PERIOD); 66 if (retval) { 67 err_msg("Failed to set timerlat period\n"); 68 goto out_err; 69 } 70 71 72 retval = osnoise_set_print_stack(tool->context, params->print_stack); 73 if (retval) { 74 err_msg("Failed to set print stack\n"); 75 goto out_err; 76 } 77 78 /* 79 * If the user did not specify a type of thread, try user-threads first. 80 * Fall back to kernel threads otherwise. 81 */ 82 if (!params->common.kernel_workload && !params->common.user_data) { 83 retval = tracefs_file_exists(NULL, "osnoise/per_cpu/cpu0/timerlat_fd"); 84 if (retval) { 85 debug_msg("User-space interface detected, setting user-threads\n"); 86 params->common.user_workload = 1; 87 params->common.user_data = 1; 88 } else { 89 debug_msg("User-space interface not detected, setting kernel-threads\n"); 90 params->common.kernel_workload = 1; 91 } 92 } 93 94 return common_apply_config(tool, ¶ms->common); 95 96 out_err: 97 return -1; 98 } 99 100 int timerlat_enable(struct osnoise_tool *tool) 101 { 102 struct timerlat_params *params = to_timerlat_params(tool->params); 103 int retval, nr_cpus, i; 104 105 if (params->dma_latency >= 0) { 106 dma_latency_fd = set_cpu_dma_latency(params->dma_latency); 107 if (dma_latency_fd < 0) { 108 err_msg("Could not set /dev/cpu_dma_latency.\n"); 109 return -1; 110 } 111 } 112 113 if (params->deepest_idle_state >= -1) { 114 if (!have_libcpupower_support()) { 115 err_msg("rtla built without libcpupower, --deepest-idle-state is not supported\n"); 116 return -1; 117 } 118 119 nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 120 121 for_each_monitored_cpu(i, nr_cpus, ¶ms->common) { 122 if (save_cpu_idle_disable_state(i) < 0) { 123 err_msg("Could not save cpu idle state.\n"); 124 return -1; 125 } 126 if (set_deepest_cpu_idle_state(i, params->deepest_idle_state) < 0) { 127 err_msg("Could not set deepest cpu idle state.\n"); 128 return -1; 129 } 130 } 131 } 132 133 if (!params->no_aa) { 134 tool->aa = osnoise_init_tool("timerlat_aa"); 135 if (!tool->aa) 136 return -1; 137 138 retval = timerlat_aa_init(tool->aa, params->dump_tasks); 139 if (retval) { 140 err_msg("Failed to enable the auto analysis instance\n"); 141 return retval; 142 } 143 144 retval = enable_tracer_by_name(tool->aa->trace.inst, "timerlat"); 145 if (retval) { 146 err_msg("Failed to enable aa tracer\n"); 147 return retval; 148 } 149 } 150 151 if (params->common.warmup > 0) { 152 debug_msg("Warming up for %d seconds\n", params->common.warmup); 153 sleep(params->common.warmup); 154 if (stop_tracing) 155 return -1; 156 } 157 158 /* 159 * Start the tracers here, after having set all instances. 160 * 161 * Let the trace instance start first for the case of hitting a stop 162 * tracing while enabling other instances. The trace instance is the 163 * one with most valuable information. 164 */ 165 if (tool->record) 166 trace_instance_start(&tool->record->trace); 167 if (!params->no_aa) 168 trace_instance_start(&tool->aa->trace); 169 if (params->mode == TRACING_MODE_TRACEFS) { 170 trace_instance_start(&tool->trace); 171 } else { 172 retval = timerlat_bpf_attach(); 173 if (retval) { 174 err_msg("Error attaching BPF program\n"); 175 return retval; 176 } 177 } 178 179 /* 180 * In tracefs and mixed mode, timerlat tracer handles stopping 181 * on threshold 182 */ 183 if (params->mode != TRACING_MODE_BPF) { 184 retval = osn_set_stop(tool); 185 if (retval) 186 return retval; 187 } 188 189 return 0; 190 } 191 192 void timerlat_analyze(struct osnoise_tool *tool, bool stopped) 193 { 194 struct timerlat_params *params = to_timerlat_params(tool->params); 195 196 if (stopped) { 197 if (!params->no_aa) 198 timerlat_auto_analysis(params->common.stop_us, 199 params->common.stop_total_us); 200 } else if (params->common.aa_only) { 201 char *max_lat; 202 203 /* 204 * If the trace did not stop with --aa-only, at least print 205 * the max known latency. 206 */ 207 max_lat = tracefs_instance_file_read(trace_inst->inst, "tracing_max_latency", NULL); 208 if (max_lat) { 209 printf(" Max latency was %s\n", max_lat); 210 free(max_lat); 211 } 212 } 213 } 214 215 void timerlat_free(struct osnoise_tool *tool) 216 { 217 struct timerlat_params *params = to_timerlat_params(tool->params); 218 int nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 219 int i; 220 221 timerlat_aa_destroy(); 222 if (dma_latency_fd >= 0) 223 close(dma_latency_fd); 224 if (params->deepest_idle_state >= -1) { 225 for_each_monitored_cpu(i, nr_cpus, ¶ms->common) { 226 restore_cpu_idle_disable_state(i); 227 } 228 } 229 230 osnoise_destroy_tool(tool->aa); 231 232 if (params->mode != TRACING_MODE_TRACEFS) 233 timerlat_bpf_destroy(); 234 free_cpu_idle_disable_states(); 235 } 236 237 static void timerlat_usage(int err) 238 { 239 int i; 240 241 static const char * const msg[] = { 242 "", 243 "timerlat version " VERSION, 244 "", 245 " usage: [rtla] timerlat [MODE] ...", 246 "", 247 " modes:", 248 " top - prints the summary from timerlat tracer", 249 " hist - prints a histogram of timer latencies", 250 "", 251 "if no MODE is given, the top mode is called, passing the arguments", 252 NULL, 253 }; 254 255 for (i = 0; msg[i]; i++) 256 fprintf(stderr, "%s\n", msg[i]); 257 exit(err); 258 } 259 260 int timerlat_main(int argc, char *argv[]) 261 { 262 if (argc == 0) 263 goto usage; 264 265 /* 266 * if timerlat was called without any argument, run the 267 * default cmdline. 268 */ 269 if (argc == 1) { 270 run_tool(&timerlat_top_ops, argc, argv); 271 exit(0); 272 } 273 274 if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0)) { 275 timerlat_usage(0); 276 } else if (strncmp(argv[1], "-", 1) == 0) { 277 /* the user skipped the tool, call the default one */ 278 run_tool(&timerlat_top_ops, argc, argv); 279 exit(0); 280 } else if (strcmp(argv[1], "top") == 0) { 281 run_tool(&timerlat_top_ops, argc-1, &argv[1]); 282 exit(0); 283 } else if (strcmp(argv[1], "hist") == 0) { 284 run_tool(&timerlat_hist_ops, argc-1, &argv[1]); 285 exit(0); 286 } 287 288 usage: 289 timerlat_usage(1); 290 exit(1); 291 } 292