1829a6c0bSDaniel Bristot de Oliveira // SPDX-License-Identifier: GPL-2.0 2829a6c0bSDaniel Bristot de Oliveira /* 3829a6c0bSDaniel Bristot de Oliveira * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org> 4829a6c0bSDaniel Bristot de Oliveira */ 5829a6c0bSDaniel Bristot de Oliveira 6829a6c0bSDaniel Bristot de Oliveira #include <getopt.h> 7829a6c0bSDaniel Bristot de Oliveira #include <stdlib.h> 8829a6c0bSDaniel Bristot de Oliveira #include <string.h> 9829a6c0bSDaniel Bristot de Oliveira #include <signal.h> 10829a6c0bSDaniel Bristot de Oliveira #include <unistd.h> 11829a6c0bSDaniel Bristot de Oliveira #include <errno.h> 12829a6c0bSDaniel Bristot de Oliveira #include <stdio.h> 13829a6c0bSDaniel Bristot de Oliveira #include <time.h> 14829a6c0bSDaniel Bristot de Oliveira 15829a6c0bSDaniel Bristot de Oliveira #include "utils.h" 16829a6c0bSDaniel Bristot de Oliveira #include "osnoise.h" 17829a6c0bSDaniel Bristot de Oliveira 18829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params { 19829a6c0bSDaniel Bristot de Oliveira char *cpus; 20829a6c0bSDaniel Bristot de Oliveira char *monitored_cpus; 21829a6c0bSDaniel Bristot de Oliveira char *trace_output; 22829a6c0bSDaniel Bristot de Oliveira unsigned long long runtime; 23829a6c0bSDaniel Bristot de Oliveira unsigned long long period; 24d635316aSDaniel Bristot de Oliveira long long threshold; 25829a6c0bSDaniel Bristot de Oliveira long long stop_us; 26829a6c0bSDaniel Bristot de Oliveira long long stop_total_us; 27829a6c0bSDaniel Bristot de Oliveira int sleep_time; 28829a6c0bSDaniel Bristot de Oliveira int duration; 29829a6c0bSDaniel Bristot de Oliveira int set_sched; 30829a6c0bSDaniel Bristot de Oliveira int output_divisor; 31829a6c0bSDaniel Bristot de Oliveira struct sched_attr sched_param; 3251d64c3aSDaniel Bristot de Oliveira struct trace_events *events; 33829a6c0bSDaniel Bristot de Oliveira 34829a6c0bSDaniel Bristot de Oliveira char no_header; 35829a6c0bSDaniel Bristot de Oliveira char no_summary; 36829a6c0bSDaniel Bristot de Oliveira char no_index; 37829a6c0bSDaniel Bristot de Oliveira char with_zeros; 38829a6c0bSDaniel Bristot de Oliveira int bucket_size; 39829a6c0bSDaniel Bristot de Oliveira int entries; 40829a6c0bSDaniel Bristot de Oliveira }; 41829a6c0bSDaniel Bristot de Oliveira 42829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_cpu { 43829a6c0bSDaniel Bristot de Oliveira int *samples; 44829a6c0bSDaniel Bristot de Oliveira int count; 45829a6c0bSDaniel Bristot de Oliveira 46829a6c0bSDaniel Bristot de Oliveira unsigned long long min_sample; 47829a6c0bSDaniel Bristot de Oliveira unsigned long long sum_sample; 48829a6c0bSDaniel Bristot de Oliveira unsigned long long max_sample; 49829a6c0bSDaniel Bristot de Oliveira 50829a6c0bSDaniel Bristot de Oliveira }; 51829a6c0bSDaniel Bristot de Oliveira 52829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data { 53829a6c0bSDaniel Bristot de Oliveira struct tracefs_hist *trace_hist; 54829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_cpu *hist; 55829a6c0bSDaniel Bristot de Oliveira int entries; 56829a6c0bSDaniel Bristot de Oliveira int bucket_size; 57829a6c0bSDaniel Bristot de Oliveira int nr_cpus; 58829a6c0bSDaniel Bristot de Oliveira }; 59829a6c0bSDaniel Bristot de Oliveira 60829a6c0bSDaniel Bristot de Oliveira /* 61829a6c0bSDaniel Bristot de Oliveira * osnoise_free_histogram - free runtime data 62829a6c0bSDaniel Bristot de Oliveira */ 63829a6c0bSDaniel Bristot de Oliveira static void 64829a6c0bSDaniel Bristot de Oliveira osnoise_free_histogram(struct osnoise_hist_data *data) 65829a6c0bSDaniel Bristot de Oliveira { 66829a6c0bSDaniel Bristot de Oliveira int cpu; 67829a6c0bSDaniel Bristot de Oliveira 68829a6c0bSDaniel Bristot de Oliveira /* one histogram for IRQ and one for thread, per CPU */ 69829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 70829a6c0bSDaniel Bristot de Oliveira if (data->hist[cpu].samples) 71829a6c0bSDaniel Bristot de Oliveira free(data->hist[cpu].samples); 72829a6c0bSDaniel Bristot de Oliveira } 73829a6c0bSDaniel Bristot de Oliveira 74829a6c0bSDaniel Bristot de Oliveira /* one set of histograms per CPU */ 75829a6c0bSDaniel Bristot de Oliveira if (data->hist) 76829a6c0bSDaniel Bristot de Oliveira free(data->hist); 77829a6c0bSDaniel Bristot de Oliveira 78829a6c0bSDaniel Bristot de Oliveira free(data); 79829a6c0bSDaniel Bristot de Oliveira } 80829a6c0bSDaniel Bristot de Oliveira 81829a6c0bSDaniel Bristot de Oliveira /* 82829a6c0bSDaniel Bristot de Oliveira * osnoise_alloc_histogram - alloc runtime data 83829a6c0bSDaniel Bristot de Oliveira */ 84829a6c0bSDaniel Bristot de Oliveira static struct osnoise_hist_data 85829a6c0bSDaniel Bristot de Oliveira *osnoise_alloc_histogram(int nr_cpus, int entries, int bucket_size) 86829a6c0bSDaniel Bristot de Oliveira { 87829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data; 88829a6c0bSDaniel Bristot de Oliveira int cpu; 89829a6c0bSDaniel Bristot de Oliveira 90829a6c0bSDaniel Bristot de Oliveira data = calloc(1, sizeof(*data)); 91829a6c0bSDaniel Bristot de Oliveira if (!data) 92829a6c0bSDaniel Bristot de Oliveira return NULL; 93829a6c0bSDaniel Bristot de Oliveira 94829a6c0bSDaniel Bristot de Oliveira data->entries = entries; 95829a6c0bSDaniel Bristot de Oliveira data->bucket_size = bucket_size; 96829a6c0bSDaniel Bristot de Oliveira data->nr_cpus = nr_cpus; 97829a6c0bSDaniel Bristot de Oliveira 98829a6c0bSDaniel Bristot de Oliveira data->hist = calloc(1, sizeof(*data->hist) * nr_cpus); 99829a6c0bSDaniel Bristot de Oliveira if (!data->hist) 100829a6c0bSDaniel Bristot de Oliveira goto cleanup; 101829a6c0bSDaniel Bristot de Oliveira 102829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < nr_cpus; cpu++) { 103829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1)); 104829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].samples) 105829a6c0bSDaniel Bristot de Oliveira goto cleanup; 106829a6c0bSDaniel Bristot de Oliveira } 107829a6c0bSDaniel Bristot de Oliveira 108829a6c0bSDaniel Bristot de Oliveira /* set the min to max */ 109829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < nr_cpus; cpu++) 110829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].min_sample = ~0; 111829a6c0bSDaniel Bristot de Oliveira 112829a6c0bSDaniel Bristot de Oliveira return data; 113829a6c0bSDaniel Bristot de Oliveira 114829a6c0bSDaniel Bristot de Oliveira cleanup: 115829a6c0bSDaniel Bristot de Oliveira osnoise_free_histogram(data); 116829a6c0bSDaniel Bristot de Oliveira return NULL; 117829a6c0bSDaniel Bristot de Oliveira } 118829a6c0bSDaniel Bristot de Oliveira 119829a6c0bSDaniel Bristot de Oliveira static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu, 120829a6c0bSDaniel Bristot de Oliveira unsigned long long duration, int count) 121829a6c0bSDaniel Bristot de Oliveira { 122829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params = tool->params; 123829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 124*fe137a4fSAndreas Ziegler unsigned long long total_duration; 125829a6c0bSDaniel Bristot de Oliveira int entries = data->entries; 126829a6c0bSDaniel Bristot de Oliveira int bucket; 127829a6c0bSDaniel Bristot de Oliveira int *hist; 128829a6c0bSDaniel Bristot de Oliveira 129829a6c0bSDaniel Bristot de Oliveira if (params->output_divisor) 130829a6c0bSDaniel Bristot de Oliveira duration = duration / params->output_divisor; 131829a6c0bSDaniel Bristot de Oliveira 132829a6c0bSDaniel Bristot de Oliveira if (data->bucket_size) 133829a6c0bSDaniel Bristot de Oliveira bucket = duration / data->bucket_size; 134829a6c0bSDaniel Bristot de Oliveira 135*fe137a4fSAndreas Ziegler total_duration = duration * count; 136*fe137a4fSAndreas Ziegler 137829a6c0bSDaniel Bristot de Oliveira hist = data->hist[cpu].samples; 138829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].count += count; 139829a6c0bSDaniel Bristot de Oliveira update_min(&data->hist[cpu].min_sample, &duration); 140*fe137a4fSAndreas Ziegler update_sum(&data->hist[cpu].sum_sample, &total_duration); 141829a6c0bSDaniel Bristot de Oliveira update_max(&data->hist[cpu].max_sample, &duration); 142829a6c0bSDaniel Bristot de Oliveira 143829a6c0bSDaniel Bristot de Oliveira if (bucket < entries) 144829a6c0bSDaniel Bristot de Oliveira hist[bucket] += count; 145829a6c0bSDaniel Bristot de Oliveira else 146829a6c0bSDaniel Bristot de Oliveira hist[entries] += count; 147829a6c0bSDaniel Bristot de Oliveira } 148829a6c0bSDaniel Bristot de Oliveira 149829a6c0bSDaniel Bristot de Oliveira /* 150829a6c0bSDaniel Bristot de Oliveira * osnoise_destroy_trace_hist - disable events used to collect histogram 151829a6c0bSDaniel Bristot de Oliveira */ 152829a6c0bSDaniel Bristot de Oliveira static void osnoise_destroy_trace_hist(struct osnoise_tool *tool) 153829a6c0bSDaniel Bristot de Oliveira { 154829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 155829a6c0bSDaniel Bristot de Oliveira 156829a6c0bSDaniel Bristot de Oliveira tracefs_hist_pause(tool->trace.inst, data->trace_hist); 157829a6c0bSDaniel Bristot de Oliveira tracefs_hist_destroy(tool->trace.inst, data->trace_hist); 158829a6c0bSDaniel Bristot de Oliveira } 159829a6c0bSDaniel Bristot de Oliveira 160829a6c0bSDaniel Bristot de Oliveira /* 161829a6c0bSDaniel Bristot de Oliveira * osnoise_init_trace_hist - enable events used to collect histogram 162829a6c0bSDaniel Bristot de Oliveira */ 163829a6c0bSDaniel Bristot de Oliveira static int osnoise_init_trace_hist(struct osnoise_tool *tool) 164829a6c0bSDaniel Bristot de Oliveira { 165829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params = tool->params; 166829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 167829a6c0bSDaniel Bristot de Oliveira int bucket_size; 168829a6c0bSDaniel Bristot de Oliveira char buff[128]; 169829a6c0bSDaniel Bristot de Oliveira int retval = 0; 170829a6c0bSDaniel Bristot de Oliveira 171829a6c0bSDaniel Bristot de Oliveira /* 172829a6c0bSDaniel Bristot de Oliveira * Set the size of the bucket. 173829a6c0bSDaniel Bristot de Oliveira */ 174829a6c0bSDaniel Bristot de Oliveira bucket_size = params->output_divisor * params->bucket_size; 175829a6c0bSDaniel Bristot de Oliveira snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size); 176829a6c0bSDaniel Bristot de Oliveira 177829a6c0bSDaniel Bristot de Oliveira data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold", 178829a6c0bSDaniel Bristot de Oliveira buff, TRACEFS_HIST_KEY_NORMAL); 179829a6c0bSDaniel Bristot de Oliveira if (!data->trace_hist) 180829a6c0bSDaniel Bristot de Oliveira return 1; 181829a6c0bSDaniel Bristot de Oliveira 182829a6c0bSDaniel Bristot de Oliveira retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0); 183829a6c0bSDaniel Bristot de Oliveira if (retval) 184829a6c0bSDaniel Bristot de Oliveira goto out_err; 185829a6c0bSDaniel Bristot de Oliveira 186829a6c0bSDaniel Bristot de Oliveira retval = tracefs_hist_start(tool->trace.inst, data->trace_hist); 187829a6c0bSDaniel Bristot de Oliveira if (retval) 188829a6c0bSDaniel Bristot de Oliveira goto out_err; 189829a6c0bSDaniel Bristot de Oliveira 190829a6c0bSDaniel Bristot de Oliveira return 0; 191829a6c0bSDaniel Bristot de Oliveira 192829a6c0bSDaniel Bristot de Oliveira out_err: 193829a6c0bSDaniel Bristot de Oliveira osnoise_destroy_trace_hist(tool); 194829a6c0bSDaniel Bristot de Oliveira return 1; 195829a6c0bSDaniel Bristot de Oliveira } 196829a6c0bSDaniel Bristot de Oliveira 197829a6c0bSDaniel Bristot de Oliveira /* 198829a6c0bSDaniel Bristot de Oliveira * osnoise_read_trace_hist - parse histogram file and file osnoise histogram 199829a6c0bSDaniel Bristot de Oliveira */ 200829a6c0bSDaniel Bristot de Oliveira static void osnoise_read_trace_hist(struct osnoise_tool *tool) 201829a6c0bSDaniel Bristot de Oliveira { 202829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 203829a6c0bSDaniel Bristot de Oliveira long long cpu, counter, duration; 204829a6c0bSDaniel Bristot de Oliveira char *content, *position; 205829a6c0bSDaniel Bristot de Oliveira 206829a6c0bSDaniel Bristot de Oliveira tracefs_hist_pause(tool->trace.inst, data->trace_hist); 207829a6c0bSDaniel Bristot de Oliveira 208829a6c0bSDaniel Bristot de Oliveira content = tracefs_event_file_read(tool->trace.inst, "osnoise", 209829a6c0bSDaniel Bristot de Oliveira "sample_threshold", 210829a6c0bSDaniel Bristot de Oliveira "hist", NULL); 211829a6c0bSDaniel Bristot de Oliveira if (!content) 212829a6c0bSDaniel Bristot de Oliveira return; 213829a6c0bSDaniel Bristot de Oliveira 214829a6c0bSDaniel Bristot de Oliveira position = content; 215829a6c0bSDaniel Bristot de Oliveira while (true) { 216829a6c0bSDaniel Bristot de Oliveira position = strstr(position, "duration: ~"); 217829a6c0bSDaniel Bristot de Oliveira if (!position) 218829a6c0bSDaniel Bristot de Oliveira break; 219829a6c0bSDaniel Bristot de Oliveira position += strlen("duration: ~"); 220829a6c0bSDaniel Bristot de Oliveira duration = get_llong_from_str(position); 221829a6c0bSDaniel Bristot de Oliveira if (duration == -1) 222829a6c0bSDaniel Bristot de Oliveira err_msg("error reading duration from histogram\n"); 223829a6c0bSDaniel Bristot de Oliveira 224829a6c0bSDaniel Bristot de Oliveira position = strstr(position, "cpu:"); 225829a6c0bSDaniel Bristot de Oliveira if (!position) 226829a6c0bSDaniel Bristot de Oliveira break; 227829a6c0bSDaniel Bristot de Oliveira position += strlen("cpu: "); 228829a6c0bSDaniel Bristot de Oliveira cpu = get_llong_from_str(position); 229829a6c0bSDaniel Bristot de Oliveira if (cpu == -1) 230829a6c0bSDaniel Bristot de Oliveira err_msg("error reading cpu from histogram\n"); 231829a6c0bSDaniel Bristot de Oliveira 232829a6c0bSDaniel Bristot de Oliveira position = strstr(position, "hitcount:"); 233829a6c0bSDaniel Bristot de Oliveira if (!position) 234829a6c0bSDaniel Bristot de Oliveira break; 235829a6c0bSDaniel Bristot de Oliveira position += strlen("hitcount: "); 236829a6c0bSDaniel Bristot de Oliveira counter = get_llong_from_str(position); 237829a6c0bSDaniel Bristot de Oliveira if (counter == -1) 238829a6c0bSDaniel Bristot de Oliveira err_msg("error reading counter from histogram\n"); 239829a6c0bSDaniel Bristot de Oliveira 240829a6c0bSDaniel Bristot de Oliveira osnoise_hist_update_multiple(tool, cpu, duration, counter); 241829a6c0bSDaniel Bristot de Oliveira } 242829a6c0bSDaniel Bristot de Oliveira free(content); 243829a6c0bSDaniel Bristot de Oliveira } 244829a6c0bSDaniel Bristot de Oliveira 245829a6c0bSDaniel Bristot de Oliveira /* 246829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_header - print the header of the tracer to the output 247829a6c0bSDaniel Bristot de Oliveira */ 248829a6c0bSDaniel Bristot de Oliveira static void osnoise_hist_header(struct osnoise_tool *tool) 249829a6c0bSDaniel Bristot de Oliveira { 250829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params = tool->params; 251829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 252829a6c0bSDaniel Bristot de Oliveira struct trace_seq *s = tool->trace.seq; 253829a6c0bSDaniel Bristot de Oliveira char duration[26]; 254829a6c0bSDaniel Bristot de Oliveira int cpu; 255829a6c0bSDaniel Bristot de Oliveira 256829a6c0bSDaniel Bristot de Oliveira if (params->no_header) 257829a6c0bSDaniel Bristot de Oliveira return; 258829a6c0bSDaniel Bristot de Oliveira 259829a6c0bSDaniel Bristot de Oliveira get_duration(tool->start_time, duration, sizeof(duration)); 260829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "# RTLA osnoise histogram\n"); 261829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "# Time unit is %s (%s)\n", 262829a6c0bSDaniel Bristot de Oliveira params->output_divisor == 1 ? "nanoseconds" : "microseconds", 263829a6c0bSDaniel Bristot de Oliveira params->output_divisor == 1 ? "ns" : "us"); 264829a6c0bSDaniel Bristot de Oliveira 265829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "# Duration: %s\n", duration); 266829a6c0bSDaniel Bristot de Oliveira 267829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 268829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "Index"); 269829a6c0bSDaniel Bristot de Oliveira 270829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 271829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 272829a6c0bSDaniel Bristot de Oliveira continue; 273829a6c0bSDaniel Bristot de Oliveira 274829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 275829a6c0bSDaniel Bristot de Oliveira continue; 276829a6c0bSDaniel Bristot de Oliveira 277829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, " CPU-%03d", cpu); 278829a6c0bSDaniel Bristot de Oliveira } 279829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "\n"); 280829a6c0bSDaniel Bristot de Oliveira 281829a6c0bSDaniel Bristot de Oliveira trace_seq_do_printf(s); 282829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(s); 283829a6c0bSDaniel Bristot de Oliveira } 284829a6c0bSDaniel Bristot de Oliveira 285829a6c0bSDaniel Bristot de Oliveira /* 286829a6c0bSDaniel Bristot de Oliveira * osnoise_print_summary - print the summary of the hist data to the output 287829a6c0bSDaniel Bristot de Oliveira */ 288829a6c0bSDaniel Bristot de Oliveira static void 289829a6c0bSDaniel Bristot de Oliveira osnoise_print_summary(struct osnoise_hist_params *params, 290829a6c0bSDaniel Bristot de Oliveira struct trace_instance *trace, 291829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data) 292829a6c0bSDaniel Bristot de Oliveira { 293829a6c0bSDaniel Bristot de Oliveira int cpu; 294829a6c0bSDaniel Bristot de Oliveira 295829a6c0bSDaniel Bristot de Oliveira if (params->no_summary) 296829a6c0bSDaniel Bristot de Oliveira return; 297829a6c0bSDaniel Bristot de Oliveira 298829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 299829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "count:"); 300829a6c0bSDaniel Bristot de Oliveira 301829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 302829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 303829a6c0bSDaniel Bristot de Oliveira continue; 304829a6c0bSDaniel Bristot de Oliveira 305829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 306829a6c0bSDaniel Bristot de Oliveira continue; 307829a6c0bSDaniel Bristot de Oliveira 308829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count); 309829a6c0bSDaniel Bristot de Oliveira } 310829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 311829a6c0bSDaniel Bristot de Oliveira 312829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 313829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "min: "); 314829a6c0bSDaniel Bristot de Oliveira 315829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 316829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 317829a6c0bSDaniel Bristot de Oliveira continue; 318829a6c0bSDaniel Bristot de Oliveira 319829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 320829a6c0bSDaniel Bristot de Oliveira continue; 321829a6c0bSDaniel Bristot de Oliveira 322829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample); 323829a6c0bSDaniel Bristot de Oliveira 324829a6c0bSDaniel Bristot de Oliveira } 325829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 326829a6c0bSDaniel Bristot de Oliveira 327829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 328829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "avg: "); 329829a6c0bSDaniel Bristot de Oliveira 330829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 331829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 332829a6c0bSDaniel Bristot de Oliveira continue; 333829a6c0bSDaniel Bristot de Oliveira 334829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 335829a6c0bSDaniel Bristot de Oliveira continue; 336829a6c0bSDaniel Bristot de Oliveira 337829a6c0bSDaniel Bristot de Oliveira if (data->hist[cpu].count) 338829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9llu ", 339829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].sum_sample / data->hist[cpu].count); 340829a6c0bSDaniel Bristot de Oliveira else 341829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, " - "); 342829a6c0bSDaniel Bristot de Oliveira } 343829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 344829a6c0bSDaniel Bristot de Oliveira 345829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 346829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "max: "); 347829a6c0bSDaniel Bristot de Oliveira 348829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 349829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 350829a6c0bSDaniel Bristot de Oliveira continue; 351829a6c0bSDaniel Bristot de Oliveira 352829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 353829a6c0bSDaniel Bristot de Oliveira continue; 354829a6c0bSDaniel Bristot de Oliveira 355829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample); 356829a6c0bSDaniel Bristot de Oliveira 357829a6c0bSDaniel Bristot de Oliveira } 358829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 359829a6c0bSDaniel Bristot de Oliveira trace_seq_do_printf(trace->seq); 360829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(trace->seq); 361829a6c0bSDaniel Bristot de Oliveira } 362829a6c0bSDaniel Bristot de Oliveira 363829a6c0bSDaniel Bristot de Oliveira /* 364829a6c0bSDaniel Bristot de Oliveira * osnoise_print_stats - print data for all CPUs 365829a6c0bSDaniel Bristot de Oliveira */ 366829a6c0bSDaniel Bristot de Oliveira static void 367829a6c0bSDaniel Bristot de Oliveira osnoise_print_stats(struct osnoise_hist_params *params, struct osnoise_tool *tool) 368829a6c0bSDaniel Bristot de Oliveira { 369829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 370829a6c0bSDaniel Bristot de Oliveira struct trace_instance *trace = &tool->trace; 371829a6c0bSDaniel Bristot de Oliveira int bucket, cpu; 372829a6c0bSDaniel Bristot de Oliveira int total; 373829a6c0bSDaniel Bristot de Oliveira 374829a6c0bSDaniel Bristot de Oliveira osnoise_hist_header(tool); 375829a6c0bSDaniel Bristot de Oliveira 376829a6c0bSDaniel Bristot de Oliveira for (bucket = 0; bucket < data->entries; bucket++) { 377829a6c0bSDaniel Bristot de Oliveira total = 0; 378829a6c0bSDaniel Bristot de Oliveira 379829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 380829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%-6d", 381829a6c0bSDaniel Bristot de Oliveira bucket * data->bucket_size); 382829a6c0bSDaniel Bristot de Oliveira 383829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 384829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 385829a6c0bSDaniel Bristot de Oliveira continue; 386829a6c0bSDaniel Bristot de Oliveira 387829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 388829a6c0bSDaniel Bristot de Oliveira continue; 389829a6c0bSDaniel Bristot de Oliveira 390829a6c0bSDaniel Bristot de Oliveira total += data->hist[cpu].samples[bucket]; 391829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]); 392829a6c0bSDaniel Bristot de Oliveira } 393829a6c0bSDaniel Bristot de Oliveira 394829a6c0bSDaniel Bristot de Oliveira if (total == 0 && !params->with_zeros) { 395829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(trace->seq); 396829a6c0bSDaniel Bristot de Oliveira continue; 397829a6c0bSDaniel Bristot de Oliveira } 398829a6c0bSDaniel Bristot de Oliveira 399829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 400829a6c0bSDaniel Bristot de Oliveira trace_seq_do_printf(trace->seq); 401829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(trace->seq); 402829a6c0bSDaniel Bristot de Oliveira } 403829a6c0bSDaniel Bristot de Oliveira 404829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 405829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "over: "); 406829a6c0bSDaniel Bristot de Oliveira 407829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 408829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 409829a6c0bSDaniel Bristot de Oliveira continue; 410829a6c0bSDaniel Bristot de Oliveira 411829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 412829a6c0bSDaniel Bristot de Oliveira continue; 413829a6c0bSDaniel Bristot de Oliveira 414829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9d ", 415829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].samples[data->entries]); 416829a6c0bSDaniel Bristot de Oliveira } 417829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 418829a6c0bSDaniel Bristot de Oliveira trace_seq_do_printf(trace->seq); 419829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(trace->seq); 420829a6c0bSDaniel Bristot de Oliveira 421829a6c0bSDaniel Bristot de Oliveira osnoise_print_summary(params, trace, data); 422829a6c0bSDaniel Bristot de Oliveira } 423829a6c0bSDaniel Bristot de Oliveira 424829a6c0bSDaniel Bristot de Oliveira /* 425829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_usage - prints osnoise hist usage message 426829a6c0bSDaniel Bristot de Oliveira */ 427829a6c0bSDaniel Bristot de Oliveira static void osnoise_hist_usage(char *usage) 428829a6c0bSDaniel Bristot de Oliveira { 429829a6c0bSDaniel Bristot de Oliveira int i; 430829a6c0bSDaniel Bristot de Oliveira 431829a6c0bSDaniel Bristot de Oliveira static const char * const msg[] = { 432829a6c0bSDaniel Bristot de Oliveira "", 4332b622eddSDaniel Bristot de Oliveira " usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\", 43444f3a37dSDaniel Bristot de Oliveira " [-T us] [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\", 43544f3a37dSDaniel Bristot de Oliveira " [-c cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] [--no-index] \\", 43644f3a37dSDaniel Bristot de Oliveira " [--with-zeros]", 437829a6c0bSDaniel Bristot de Oliveira "", 438829a6c0bSDaniel Bristot de Oliveira " -h/--help: print this menu", 4392b622eddSDaniel Bristot de Oliveira " -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit", 440829a6c0bSDaniel Bristot de Oliveira " -p/--period us: osnoise period in us", 441829a6c0bSDaniel Bristot de Oliveira " -r/--runtime us: osnoise runtime in us", 442829a6c0bSDaniel Bristot de Oliveira " -s/--stop us: stop trace if a single sample is higher than the argument in us", 443829a6c0bSDaniel Bristot de Oliveira " -S/--stop-total us: stop trace if the total sample is higher than the argument in us", 444d635316aSDaniel Bristot de Oliveira " -T/--threshold us: the minimum delta to be considered a noise", 445829a6c0bSDaniel Bristot de Oliveira " -c/--cpus cpu-list: list of cpus to run osnoise threads", 446829a6c0bSDaniel Bristot de Oliveira " -d/--duration time[s|m|h|d]: duration of the session", 447829a6c0bSDaniel Bristot de Oliveira " -D/--debug: print debug info", 448829a6c0bSDaniel Bristot de Oliveira " -t/--trace[=file]: save the stopped trace to [file|osnoise_trace.txt]", 44951d64c3aSDaniel Bristot de Oliveira " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 45044f3a37dSDaniel Bristot de Oliveira " --filter <filter>: enable a trace event filter to the previous -e event", 4511a754893SDaniel Bristot de Oliveira " --trigger <trigger>: enable a trace event trigger to the previous -e event", 452829a6c0bSDaniel Bristot de Oliveira " -b/--bucket-size N: set the histogram bucket size (default 1)", 453dd48f316SDaniel Bristot de Oliveira " -E/--entries N: set the number of entries of the histogram (default 256)", 454829a6c0bSDaniel Bristot de Oliveira " --no-header: do not print header", 455829a6c0bSDaniel Bristot de Oliveira " --no-summary: do not print summary", 456829a6c0bSDaniel Bristot de Oliveira " --no-index: do not print index", 457829a6c0bSDaniel Bristot de Oliveira " --with-zeros: print zero only entries", 458829a6c0bSDaniel Bristot de Oliveira " -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters", 459829a6c0bSDaniel Bristot de Oliveira " o:prio - use SCHED_OTHER with prio", 460829a6c0bSDaniel Bristot de Oliveira " r:prio - use SCHED_RR with prio", 461829a6c0bSDaniel Bristot de Oliveira " f:prio - use SCHED_FIFO with prio", 462829a6c0bSDaniel Bristot de Oliveira " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period", 463829a6c0bSDaniel Bristot de Oliveira " in nanoseconds", 464829a6c0bSDaniel Bristot de Oliveira NULL, 465829a6c0bSDaniel Bristot de Oliveira }; 466829a6c0bSDaniel Bristot de Oliveira 467829a6c0bSDaniel Bristot de Oliveira if (usage) 468829a6c0bSDaniel Bristot de Oliveira fprintf(stderr, "%s\n", usage); 469829a6c0bSDaniel Bristot de Oliveira 470829a6c0bSDaniel Bristot de Oliveira fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n", 471829a6c0bSDaniel Bristot de Oliveira VERSION); 472829a6c0bSDaniel Bristot de Oliveira 473829a6c0bSDaniel Bristot de Oliveira for (i = 0; msg[i]; i++) 474829a6c0bSDaniel Bristot de Oliveira fprintf(stderr, "%s\n", msg[i]); 475829a6c0bSDaniel Bristot de Oliveira exit(1); 476829a6c0bSDaniel Bristot de Oliveira } 477829a6c0bSDaniel Bristot de Oliveira 478829a6c0bSDaniel Bristot de Oliveira /* 479829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters 480829a6c0bSDaniel Bristot de Oliveira */ 481829a6c0bSDaniel Bristot de Oliveira static struct osnoise_hist_params 482829a6c0bSDaniel Bristot de Oliveira *osnoise_hist_parse_args(int argc, char *argv[]) 483829a6c0bSDaniel Bristot de Oliveira { 484829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params; 48551d64c3aSDaniel Bristot de Oliveira struct trace_events *tevent; 486829a6c0bSDaniel Bristot de Oliveira int retval; 487829a6c0bSDaniel Bristot de Oliveira int c; 488829a6c0bSDaniel Bristot de Oliveira 489829a6c0bSDaniel Bristot de Oliveira params = calloc(1, sizeof(*params)); 490829a6c0bSDaniel Bristot de Oliveira if (!params) 491829a6c0bSDaniel Bristot de Oliveira exit(1); 492829a6c0bSDaniel Bristot de Oliveira 493829a6c0bSDaniel Bristot de Oliveira /* display data in microseconds */ 494829a6c0bSDaniel Bristot de Oliveira params->output_divisor = 1000; 495829a6c0bSDaniel Bristot de Oliveira params->bucket_size = 1; 496829a6c0bSDaniel Bristot de Oliveira params->entries = 256; 497829a6c0bSDaniel Bristot de Oliveira 498829a6c0bSDaniel Bristot de Oliveira while (1) { 499829a6c0bSDaniel Bristot de Oliveira static struct option long_options[] = { 5002b622eddSDaniel Bristot de Oliveira {"auto", required_argument, 0, 'a'}, 501829a6c0bSDaniel Bristot de Oliveira {"bucket-size", required_argument, 0, 'b'}, 502dd48f316SDaniel Bristot de Oliveira {"entries", required_argument, 0, 'E'}, 503829a6c0bSDaniel Bristot de Oliveira {"cpus", required_argument, 0, 'c'}, 504829a6c0bSDaniel Bristot de Oliveira {"debug", no_argument, 0, 'D'}, 505829a6c0bSDaniel Bristot de Oliveira {"duration", required_argument, 0, 'd'}, 506829a6c0bSDaniel Bristot de Oliveira {"help", no_argument, 0, 'h'}, 507829a6c0bSDaniel Bristot de Oliveira {"period", required_argument, 0, 'p'}, 508829a6c0bSDaniel Bristot de Oliveira {"priority", required_argument, 0, 'P'}, 509829a6c0bSDaniel Bristot de Oliveira {"runtime", required_argument, 0, 'r'}, 510829a6c0bSDaniel Bristot de Oliveira {"stop", required_argument, 0, 's'}, 511829a6c0bSDaniel Bristot de Oliveira {"stop-total", required_argument, 0, 'S'}, 512829a6c0bSDaniel Bristot de Oliveira {"trace", optional_argument, 0, 't'}, 51351d64c3aSDaniel Bristot de Oliveira {"event", required_argument, 0, 'e'}, 514d635316aSDaniel Bristot de Oliveira {"threshold", required_argument, 0, 'T'}, 515829a6c0bSDaniel Bristot de Oliveira {"no-header", no_argument, 0, '0'}, 516829a6c0bSDaniel Bristot de Oliveira {"no-summary", no_argument, 0, '1'}, 517829a6c0bSDaniel Bristot de Oliveira {"no-index", no_argument, 0, '2'}, 518829a6c0bSDaniel Bristot de Oliveira {"with-zeros", no_argument, 0, '3'}, 5191a754893SDaniel Bristot de Oliveira {"trigger", required_argument, 0, '4'}, 52044f3a37dSDaniel Bristot de Oliveira {"filter", required_argument, 0, '5'}, 521829a6c0bSDaniel Bristot de Oliveira {0, 0, 0, 0} 522829a6c0bSDaniel Bristot de Oliveira }; 523829a6c0bSDaniel Bristot de Oliveira 524829a6c0bSDaniel Bristot de Oliveira /* getopt_long stores the option index here. */ 525829a6c0bSDaniel Bristot de Oliveira int option_index = 0; 526829a6c0bSDaniel Bristot de Oliveira 52744f3a37dSDaniel Bristot de Oliveira c = getopt_long(argc, argv, "a:c:b:d:e:E:Dhp:P:r:s:S:t::T:01234:5:", 528829a6c0bSDaniel Bristot de Oliveira long_options, &option_index); 529829a6c0bSDaniel Bristot de Oliveira 530829a6c0bSDaniel Bristot de Oliveira /* detect the end of the options. */ 531829a6c0bSDaniel Bristot de Oliveira if (c == -1) 532829a6c0bSDaniel Bristot de Oliveira break; 533829a6c0bSDaniel Bristot de Oliveira 534829a6c0bSDaniel Bristot de Oliveira switch (c) { 5352b622eddSDaniel Bristot de Oliveira case 'a': 5362b622eddSDaniel Bristot de Oliveira /* set sample stop to auto_thresh */ 5372b622eddSDaniel Bristot de Oliveira params->stop_us = get_llong_from_str(optarg); 5382b622eddSDaniel Bristot de Oliveira 5392b622eddSDaniel Bristot de Oliveira /* set sample threshold to 1 */ 5402b622eddSDaniel Bristot de Oliveira params->threshold = 1; 5412b622eddSDaniel Bristot de Oliveira 5422b622eddSDaniel Bristot de Oliveira /* set trace */ 5432b622eddSDaniel Bristot de Oliveira params->trace_output = "osnoise_trace.txt"; 5442b622eddSDaniel Bristot de Oliveira 5452b622eddSDaniel Bristot de Oliveira break; 546829a6c0bSDaniel Bristot de Oliveira case 'b': 547829a6c0bSDaniel Bristot de Oliveira params->bucket_size = get_llong_from_str(optarg); 548829a6c0bSDaniel Bristot de Oliveira if ((params->bucket_size == 0) || (params->bucket_size >= 1000000)) 549829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Bucket size needs to be > 0 and <= 1000000\n"); 550829a6c0bSDaniel Bristot de Oliveira break; 551829a6c0bSDaniel Bristot de Oliveira case 'c': 552829a6c0bSDaniel Bristot de Oliveira retval = parse_cpu_list(optarg, ¶ms->monitored_cpus); 553829a6c0bSDaniel Bristot de Oliveira if (retval) 554829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("\nInvalid -c cpu list\n"); 555829a6c0bSDaniel Bristot de Oliveira params->cpus = optarg; 556829a6c0bSDaniel Bristot de Oliveira break; 557829a6c0bSDaniel Bristot de Oliveira case 'D': 558829a6c0bSDaniel Bristot de Oliveira config_debug = 1; 559829a6c0bSDaniel Bristot de Oliveira break; 560829a6c0bSDaniel Bristot de Oliveira case 'd': 561829a6c0bSDaniel Bristot de Oliveira params->duration = parse_seconds_duration(optarg); 562829a6c0bSDaniel Bristot de Oliveira if (!params->duration) 563829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Invalid -D duration\n"); 564829a6c0bSDaniel Bristot de Oliveira break; 56551d64c3aSDaniel Bristot de Oliveira case 'e': 56651d64c3aSDaniel Bristot de Oliveira tevent = trace_event_alloc(optarg); 56751d64c3aSDaniel Bristot de Oliveira if (!tevent) { 56851d64c3aSDaniel Bristot de Oliveira err_msg("Error alloc trace event"); 56951d64c3aSDaniel Bristot de Oliveira exit(EXIT_FAILURE); 57051d64c3aSDaniel Bristot de Oliveira } 57151d64c3aSDaniel Bristot de Oliveira 57251d64c3aSDaniel Bristot de Oliveira if (params->events) 57351d64c3aSDaniel Bristot de Oliveira tevent->next = params->events; 57451d64c3aSDaniel Bristot de Oliveira 57551d64c3aSDaniel Bristot de Oliveira params->events = tevent; 57651d64c3aSDaniel Bristot de Oliveira break; 577dd48f316SDaniel Bristot de Oliveira case 'E': 578829a6c0bSDaniel Bristot de Oliveira params->entries = get_llong_from_str(optarg); 579829a6c0bSDaniel Bristot de Oliveira if ((params->entries < 10) || (params->entries > 9999999)) 580829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Entries must be > 10 and < 9999999\n"); 581829a6c0bSDaniel Bristot de Oliveira break; 582829a6c0bSDaniel Bristot de Oliveira case 'h': 583829a6c0bSDaniel Bristot de Oliveira case '?': 584829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage(NULL); 585829a6c0bSDaniel Bristot de Oliveira break; 586829a6c0bSDaniel Bristot de Oliveira case 'p': 587829a6c0bSDaniel Bristot de Oliveira params->period = get_llong_from_str(optarg); 588829a6c0bSDaniel Bristot de Oliveira if (params->period > 10000000) 589829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Period longer than 10 s\n"); 590829a6c0bSDaniel Bristot de Oliveira break; 591829a6c0bSDaniel Bristot de Oliveira case 'P': 592829a6c0bSDaniel Bristot de Oliveira retval = parse_prio(optarg, ¶ms->sched_param); 593829a6c0bSDaniel Bristot de Oliveira if (retval == -1) 594829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Invalid -P priority"); 595829a6c0bSDaniel Bristot de Oliveira params->set_sched = 1; 596829a6c0bSDaniel Bristot de Oliveira break; 597829a6c0bSDaniel Bristot de Oliveira case 'r': 598829a6c0bSDaniel Bristot de Oliveira params->runtime = get_llong_from_str(optarg); 599829a6c0bSDaniel Bristot de Oliveira if (params->runtime < 100) 600829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Runtime shorter than 100 us\n"); 601829a6c0bSDaniel Bristot de Oliveira break; 602829a6c0bSDaniel Bristot de Oliveira case 's': 603829a6c0bSDaniel Bristot de Oliveira params->stop_us = get_llong_from_str(optarg); 604829a6c0bSDaniel Bristot de Oliveira break; 605829a6c0bSDaniel Bristot de Oliveira case 'S': 606829a6c0bSDaniel Bristot de Oliveira params->stop_total_us = get_llong_from_str(optarg); 607829a6c0bSDaniel Bristot de Oliveira break; 608d635316aSDaniel Bristot de Oliveira case 'T': 609d635316aSDaniel Bristot de Oliveira params->threshold = get_llong_from_str(optarg); 610d635316aSDaniel Bristot de Oliveira break; 611829a6c0bSDaniel Bristot de Oliveira case 't': 612829a6c0bSDaniel Bristot de Oliveira if (optarg) 613829a6c0bSDaniel Bristot de Oliveira /* skip = */ 614829a6c0bSDaniel Bristot de Oliveira params->trace_output = &optarg[1]; 615829a6c0bSDaniel Bristot de Oliveira else 616829a6c0bSDaniel Bristot de Oliveira params->trace_output = "osnoise_trace.txt"; 617829a6c0bSDaniel Bristot de Oliveira break; 618829a6c0bSDaniel Bristot de Oliveira case '0': /* no header */ 619829a6c0bSDaniel Bristot de Oliveira params->no_header = 1; 620829a6c0bSDaniel Bristot de Oliveira break; 621829a6c0bSDaniel Bristot de Oliveira case '1': /* no summary */ 622829a6c0bSDaniel Bristot de Oliveira params->no_summary = 1; 623829a6c0bSDaniel Bristot de Oliveira break; 624829a6c0bSDaniel Bristot de Oliveira case '2': /* no index */ 625829a6c0bSDaniel Bristot de Oliveira params->no_index = 1; 626829a6c0bSDaniel Bristot de Oliveira break; 627829a6c0bSDaniel Bristot de Oliveira case '3': /* with zeros */ 628829a6c0bSDaniel Bristot de Oliveira params->with_zeros = 1; 629829a6c0bSDaniel Bristot de Oliveira break; 6301a754893SDaniel Bristot de Oliveira case '4': /* trigger */ 6311a754893SDaniel Bristot de Oliveira if (params->events) { 6321a754893SDaniel Bristot de Oliveira retval = trace_event_add_trigger(params->events, optarg); 6331a754893SDaniel Bristot de Oliveira if (retval) { 6341a754893SDaniel Bristot de Oliveira err_msg("Error adding trigger %s\n", optarg); 6351a754893SDaniel Bristot de Oliveira exit(EXIT_FAILURE); 6361a754893SDaniel Bristot de Oliveira } 6371a754893SDaniel Bristot de Oliveira } else { 6381a754893SDaniel Bristot de Oliveira osnoise_hist_usage("--trigger requires a previous -e\n"); 6391a754893SDaniel Bristot de Oliveira } 6401a754893SDaniel Bristot de Oliveira break; 64144f3a37dSDaniel Bristot de Oliveira case '5': /* filter */ 64244f3a37dSDaniel Bristot de Oliveira if (params->events) { 64344f3a37dSDaniel Bristot de Oliveira retval = trace_event_add_filter(params->events, optarg); 64444f3a37dSDaniel Bristot de Oliveira if (retval) { 64544f3a37dSDaniel Bristot de Oliveira err_msg("Error adding filter %s\n", optarg); 64644f3a37dSDaniel Bristot de Oliveira exit(EXIT_FAILURE); 64744f3a37dSDaniel Bristot de Oliveira } 64844f3a37dSDaniel Bristot de Oliveira } else { 64944f3a37dSDaniel Bristot de Oliveira osnoise_hist_usage("--filter requires a previous -e\n"); 65044f3a37dSDaniel Bristot de Oliveira } 65144f3a37dSDaniel Bristot de Oliveira break; 652829a6c0bSDaniel Bristot de Oliveira default: 653829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Invalid option"); 654829a6c0bSDaniel Bristot de Oliveira } 655829a6c0bSDaniel Bristot de Oliveira } 656829a6c0bSDaniel Bristot de Oliveira 657829a6c0bSDaniel Bristot de Oliveira if (geteuid()) { 658829a6c0bSDaniel Bristot de Oliveira err_msg("rtla needs root permission\n"); 659829a6c0bSDaniel Bristot de Oliveira exit(EXIT_FAILURE); 660829a6c0bSDaniel Bristot de Oliveira } 661829a6c0bSDaniel Bristot de Oliveira 662829a6c0bSDaniel Bristot de Oliveira if (params->no_index && !params->with_zeros) 663829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense"); 664829a6c0bSDaniel Bristot de Oliveira 665829a6c0bSDaniel Bristot de Oliveira return params; 666829a6c0bSDaniel Bristot de Oliveira } 667829a6c0bSDaniel Bristot de Oliveira 668829a6c0bSDaniel Bristot de Oliveira /* 669829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_apply_config - apply the hist configs to the initialized tool 670829a6c0bSDaniel Bristot de Oliveira */ 671829a6c0bSDaniel Bristot de Oliveira static int 672829a6c0bSDaniel Bristot de Oliveira osnoise_hist_apply_config(struct osnoise_tool *tool, struct osnoise_hist_params *params) 673829a6c0bSDaniel Bristot de Oliveira { 674829a6c0bSDaniel Bristot de Oliveira int retval; 675829a6c0bSDaniel Bristot de Oliveira 676829a6c0bSDaniel Bristot de Oliveira if (!params->sleep_time) 677829a6c0bSDaniel Bristot de Oliveira params->sleep_time = 1; 678829a6c0bSDaniel Bristot de Oliveira 679829a6c0bSDaniel Bristot de Oliveira if (params->cpus) { 680829a6c0bSDaniel Bristot de Oliveira retval = osnoise_set_cpus(tool->context, params->cpus); 681829a6c0bSDaniel Bristot de Oliveira if (retval) { 682829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to apply CPUs config\n"); 683829a6c0bSDaniel Bristot de Oliveira goto out_err; 684829a6c0bSDaniel Bristot de Oliveira } 685829a6c0bSDaniel Bristot de Oliveira } 686829a6c0bSDaniel Bristot de Oliveira 687829a6c0bSDaniel Bristot de Oliveira if (params->runtime || params->period) { 688829a6c0bSDaniel Bristot de Oliveira retval = osnoise_set_runtime_period(tool->context, 689829a6c0bSDaniel Bristot de Oliveira params->runtime, 690829a6c0bSDaniel Bristot de Oliveira params->period); 691829a6c0bSDaniel Bristot de Oliveira if (retval) { 692829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to set runtime and/or period\n"); 693829a6c0bSDaniel Bristot de Oliveira goto out_err; 694829a6c0bSDaniel Bristot de Oliveira } 695829a6c0bSDaniel Bristot de Oliveira } 696829a6c0bSDaniel Bristot de Oliveira 697829a6c0bSDaniel Bristot de Oliveira if (params->stop_us) { 698829a6c0bSDaniel Bristot de Oliveira retval = osnoise_set_stop_us(tool->context, params->stop_us); 699829a6c0bSDaniel Bristot de Oliveira if (retval) { 700829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to set stop us\n"); 701829a6c0bSDaniel Bristot de Oliveira goto out_err; 702829a6c0bSDaniel Bristot de Oliveira } 703829a6c0bSDaniel Bristot de Oliveira } 704829a6c0bSDaniel Bristot de Oliveira 705829a6c0bSDaniel Bristot de Oliveira if (params->stop_total_us) { 706829a6c0bSDaniel Bristot de Oliveira retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us); 707829a6c0bSDaniel Bristot de Oliveira if (retval) { 708829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to set stop total us\n"); 709829a6c0bSDaniel Bristot de Oliveira goto out_err; 710829a6c0bSDaniel Bristot de Oliveira } 711829a6c0bSDaniel Bristot de Oliveira } 712829a6c0bSDaniel Bristot de Oliveira 713d635316aSDaniel Bristot de Oliveira if (params->threshold) { 714d635316aSDaniel Bristot de Oliveira retval = osnoise_set_tracing_thresh(tool->context, params->threshold); 715d635316aSDaniel Bristot de Oliveira if (retval) { 716d635316aSDaniel Bristot de Oliveira err_msg("Failed to set tracing_thresh\n"); 717d635316aSDaniel Bristot de Oliveira goto out_err; 718d635316aSDaniel Bristot de Oliveira } 719d635316aSDaniel Bristot de Oliveira } 720d635316aSDaniel Bristot de Oliveira 721829a6c0bSDaniel Bristot de Oliveira return 0; 722829a6c0bSDaniel Bristot de Oliveira 723829a6c0bSDaniel Bristot de Oliveira out_err: 724829a6c0bSDaniel Bristot de Oliveira return -1; 725829a6c0bSDaniel Bristot de Oliveira } 726829a6c0bSDaniel Bristot de Oliveira 727829a6c0bSDaniel Bristot de Oliveira /* 728829a6c0bSDaniel Bristot de Oliveira * osnoise_init_hist - initialize a osnoise hist tool with parameters 729829a6c0bSDaniel Bristot de Oliveira */ 730829a6c0bSDaniel Bristot de Oliveira static struct osnoise_tool 731829a6c0bSDaniel Bristot de Oliveira *osnoise_init_hist(struct osnoise_hist_params *params) 732829a6c0bSDaniel Bristot de Oliveira { 733829a6c0bSDaniel Bristot de Oliveira struct osnoise_tool *tool; 734829a6c0bSDaniel Bristot de Oliveira int nr_cpus; 735829a6c0bSDaniel Bristot de Oliveira 736829a6c0bSDaniel Bristot de Oliveira nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 737829a6c0bSDaniel Bristot de Oliveira 738829a6c0bSDaniel Bristot de Oliveira tool = osnoise_init_tool("osnoise_hist"); 739829a6c0bSDaniel Bristot de Oliveira if (!tool) 740829a6c0bSDaniel Bristot de Oliveira return NULL; 741829a6c0bSDaniel Bristot de Oliveira 742829a6c0bSDaniel Bristot de Oliveira tool->data = osnoise_alloc_histogram(nr_cpus, params->entries, params->bucket_size); 743829a6c0bSDaniel Bristot de Oliveira if (!tool->data) 744829a6c0bSDaniel Bristot de Oliveira goto out_err; 745829a6c0bSDaniel Bristot de Oliveira 746829a6c0bSDaniel Bristot de Oliveira tool->params = params; 747829a6c0bSDaniel Bristot de Oliveira 748829a6c0bSDaniel Bristot de Oliveira return tool; 749829a6c0bSDaniel Bristot de Oliveira 750829a6c0bSDaniel Bristot de Oliveira out_err: 751829a6c0bSDaniel Bristot de Oliveira osnoise_destroy_tool(tool); 752829a6c0bSDaniel Bristot de Oliveira return NULL; 753829a6c0bSDaniel Bristot de Oliveira } 754829a6c0bSDaniel Bristot de Oliveira 755829a6c0bSDaniel Bristot de Oliveira static int stop_tracing; 756829a6c0bSDaniel Bristot de Oliveira static void stop_hist(int sig) 757829a6c0bSDaniel Bristot de Oliveira { 758829a6c0bSDaniel Bristot de Oliveira stop_tracing = 1; 759829a6c0bSDaniel Bristot de Oliveira } 760829a6c0bSDaniel Bristot de Oliveira 761829a6c0bSDaniel Bristot de Oliveira /* 762829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_set_signals - handles the signal to stop the tool 763829a6c0bSDaniel Bristot de Oliveira */ 764829a6c0bSDaniel Bristot de Oliveira static void 765829a6c0bSDaniel Bristot de Oliveira osnoise_hist_set_signals(struct osnoise_hist_params *params) 766829a6c0bSDaniel Bristot de Oliveira { 767829a6c0bSDaniel Bristot de Oliveira signal(SIGINT, stop_hist); 768829a6c0bSDaniel Bristot de Oliveira if (params->duration) { 769829a6c0bSDaniel Bristot de Oliveira signal(SIGALRM, stop_hist); 770829a6c0bSDaniel Bristot de Oliveira alarm(params->duration); 771829a6c0bSDaniel Bristot de Oliveira } 772829a6c0bSDaniel Bristot de Oliveira } 773829a6c0bSDaniel Bristot de Oliveira 774829a6c0bSDaniel Bristot de Oliveira int osnoise_hist_main(int argc, char *argv[]) 775829a6c0bSDaniel Bristot de Oliveira { 776829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params; 7774bbf59a9SDaniel Bristot de Oliveira struct osnoise_tool *record = NULL; 7784bbf59a9SDaniel Bristot de Oliveira struct osnoise_tool *tool = NULL; 779829a6c0bSDaniel Bristot de Oliveira struct trace_instance *trace; 780829a6c0bSDaniel Bristot de Oliveira int return_value = 1; 781829a6c0bSDaniel Bristot de Oliveira int retval; 782829a6c0bSDaniel Bristot de Oliveira 783829a6c0bSDaniel Bristot de Oliveira params = osnoise_hist_parse_args(argc, argv); 784829a6c0bSDaniel Bristot de Oliveira if (!params) 785829a6c0bSDaniel Bristot de Oliveira exit(1); 786829a6c0bSDaniel Bristot de Oliveira 787829a6c0bSDaniel Bristot de Oliveira tool = osnoise_init_hist(params); 788829a6c0bSDaniel Bristot de Oliveira if (!tool) { 789829a6c0bSDaniel Bristot de Oliveira err_msg("Could not init osnoise hist\n"); 790829a6c0bSDaniel Bristot de Oliveira goto out_exit; 791829a6c0bSDaniel Bristot de Oliveira } 792829a6c0bSDaniel Bristot de Oliveira 793829a6c0bSDaniel Bristot de Oliveira retval = osnoise_hist_apply_config(tool, params); 794829a6c0bSDaniel Bristot de Oliveira if (retval) { 795829a6c0bSDaniel Bristot de Oliveira err_msg("Could not apply config\n"); 796829a6c0bSDaniel Bristot de Oliveira goto out_destroy; 797829a6c0bSDaniel Bristot de Oliveira } 798829a6c0bSDaniel Bristot de Oliveira 799829a6c0bSDaniel Bristot de Oliveira trace = &tool->trace; 800829a6c0bSDaniel Bristot de Oliveira 801829a6c0bSDaniel Bristot de Oliveira retval = enable_osnoise(trace); 802829a6c0bSDaniel Bristot de Oliveira if (retval) { 803829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to enable osnoise tracer\n"); 804829a6c0bSDaniel Bristot de Oliveira goto out_destroy; 805829a6c0bSDaniel Bristot de Oliveira } 806829a6c0bSDaniel Bristot de Oliveira 807829a6c0bSDaniel Bristot de Oliveira retval = osnoise_init_trace_hist(tool); 808829a6c0bSDaniel Bristot de Oliveira if (retval) 809829a6c0bSDaniel Bristot de Oliveira goto out_destroy; 810829a6c0bSDaniel Bristot de Oliveira 811829a6c0bSDaniel Bristot de Oliveira if (params->set_sched) { 812829a6c0bSDaniel Bristot de Oliveira retval = set_comm_sched_attr("osnoise/", ¶ms->sched_param); 813829a6c0bSDaniel Bristot de Oliveira if (retval) { 814829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to set sched parameters\n"); 8152a6b52edSWan Jiabing goto out_free; 816829a6c0bSDaniel Bristot de Oliveira } 817829a6c0bSDaniel Bristot de Oliveira } 818829a6c0bSDaniel Bristot de Oliveira 819829a6c0bSDaniel Bristot de Oliveira trace_instance_start(trace); 820829a6c0bSDaniel Bristot de Oliveira 821829a6c0bSDaniel Bristot de Oliveira if (params->trace_output) { 822829a6c0bSDaniel Bristot de Oliveira record = osnoise_init_trace_tool("osnoise"); 823829a6c0bSDaniel Bristot de Oliveira if (!record) { 824829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to enable the trace instance\n"); 8252a6b52edSWan Jiabing goto out_free; 826829a6c0bSDaniel Bristot de Oliveira } 82751d64c3aSDaniel Bristot de Oliveira 82851d64c3aSDaniel Bristot de Oliveira if (params->events) { 82951d64c3aSDaniel Bristot de Oliveira retval = trace_events_enable(&record->trace, params->events); 83051d64c3aSDaniel Bristot de Oliveira if (retval) 83151d64c3aSDaniel Bristot de Oliveira goto out_hist; 83251d64c3aSDaniel Bristot de Oliveira } 83351d64c3aSDaniel Bristot de Oliveira 834829a6c0bSDaniel Bristot de Oliveira trace_instance_start(&record->trace); 835829a6c0bSDaniel Bristot de Oliveira } 836829a6c0bSDaniel Bristot de Oliveira 837829a6c0bSDaniel Bristot de Oliveira tool->start_time = time(NULL); 838829a6c0bSDaniel Bristot de Oliveira osnoise_hist_set_signals(params); 839829a6c0bSDaniel Bristot de Oliveira 840829a6c0bSDaniel Bristot de Oliveira while (!stop_tracing) { 841829a6c0bSDaniel Bristot de Oliveira sleep(params->sleep_time); 842829a6c0bSDaniel Bristot de Oliveira 843829a6c0bSDaniel Bristot de Oliveira retval = tracefs_iterate_raw_events(trace->tep, 844829a6c0bSDaniel Bristot de Oliveira trace->inst, 845829a6c0bSDaniel Bristot de Oliveira NULL, 846829a6c0bSDaniel Bristot de Oliveira 0, 847829a6c0bSDaniel Bristot de Oliveira collect_registered_events, 848829a6c0bSDaniel Bristot de Oliveira trace); 849829a6c0bSDaniel Bristot de Oliveira if (retval < 0) { 850829a6c0bSDaniel Bristot de Oliveira err_msg("Error iterating on events\n"); 851829a6c0bSDaniel Bristot de Oliveira goto out_hist; 852829a6c0bSDaniel Bristot de Oliveira } 853829a6c0bSDaniel Bristot de Oliveira 85428d2160cSDaniel Bristot de Oliveira if (trace_is_off(&tool->trace, &record->trace)) 855829a6c0bSDaniel Bristot de Oliveira break; 85675016ca3SDaniel Bristot de Oliveira } 857829a6c0bSDaniel Bristot de Oliveira 858829a6c0bSDaniel Bristot de Oliveira osnoise_read_trace_hist(tool); 859829a6c0bSDaniel Bristot de Oliveira 860829a6c0bSDaniel Bristot de Oliveira osnoise_print_stats(params, tool); 861829a6c0bSDaniel Bristot de Oliveira 862829a6c0bSDaniel Bristot de Oliveira return_value = 0; 863829a6c0bSDaniel Bristot de Oliveira 86428d2160cSDaniel Bristot de Oliveira if (trace_is_off(&tool->trace, &record->trace)) { 8657d38c351SDaniel Bristot de Oliveira printf("rtla osnoise hit stop tracing\n"); 866829a6c0bSDaniel Bristot de Oliveira if (params->trace_output) { 867829a6c0bSDaniel Bristot de Oliveira printf(" Saving trace to %s\n", params->trace_output); 868829a6c0bSDaniel Bristot de Oliveira save_trace_to_file(record->trace.inst, params->trace_output); 869829a6c0bSDaniel Bristot de Oliveira } 870829a6c0bSDaniel Bristot de Oliveira } 871829a6c0bSDaniel Bristot de Oliveira 872829a6c0bSDaniel Bristot de Oliveira out_hist: 87351d64c3aSDaniel Bristot de Oliveira trace_events_destroy(&record->trace, params->events); 87451d64c3aSDaniel Bristot de Oliveira params->events = NULL; 8752a6b52edSWan Jiabing out_free: 876829a6c0bSDaniel Bristot de Oliveira osnoise_free_histogram(tool->data); 877829a6c0bSDaniel Bristot de Oliveira out_destroy: 878829a6c0bSDaniel Bristot de Oliveira osnoise_destroy_tool(record); 8794bbf59a9SDaniel Bristot de Oliveira osnoise_destroy_tool(tool); 880829a6c0bSDaniel Bristot de Oliveira free(params); 881829a6c0bSDaniel Bristot de Oliveira out_exit: 882829a6c0bSDaniel Bristot de Oliveira exit(return_value); 883829a6c0bSDaniel Bristot de Oliveira } 884