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; 32829a6c0bSDaniel Bristot de Oliveira 33829a6c0bSDaniel Bristot de Oliveira char no_header; 34829a6c0bSDaniel Bristot de Oliveira char no_summary; 35829a6c0bSDaniel Bristot de Oliveira char no_index; 36829a6c0bSDaniel Bristot de Oliveira char with_zeros; 37829a6c0bSDaniel Bristot de Oliveira int bucket_size; 38829a6c0bSDaniel Bristot de Oliveira int entries; 39829a6c0bSDaniel Bristot de Oliveira }; 40829a6c0bSDaniel Bristot de Oliveira 41829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_cpu { 42829a6c0bSDaniel Bristot de Oliveira int *samples; 43829a6c0bSDaniel Bristot de Oliveira int count; 44829a6c0bSDaniel Bristot de Oliveira 45829a6c0bSDaniel Bristot de Oliveira unsigned long long min_sample; 46829a6c0bSDaniel Bristot de Oliveira unsigned long long sum_sample; 47829a6c0bSDaniel Bristot de Oliveira unsigned long long max_sample; 48829a6c0bSDaniel Bristot de Oliveira 49829a6c0bSDaniel Bristot de Oliveira }; 50829a6c0bSDaniel Bristot de Oliveira 51829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data { 52829a6c0bSDaniel Bristot de Oliveira struct tracefs_hist *trace_hist; 53829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_cpu *hist; 54829a6c0bSDaniel Bristot de Oliveira int entries; 55829a6c0bSDaniel Bristot de Oliveira int bucket_size; 56829a6c0bSDaniel Bristot de Oliveira int nr_cpus; 57829a6c0bSDaniel Bristot de Oliveira }; 58829a6c0bSDaniel Bristot de Oliveira 59829a6c0bSDaniel Bristot de Oliveira /* 60829a6c0bSDaniel Bristot de Oliveira * osnoise_free_histogram - free runtime data 61829a6c0bSDaniel Bristot de Oliveira */ 62829a6c0bSDaniel Bristot de Oliveira static void 63829a6c0bSDaniel Bristot de Oliveira osnoise_free_histogram(struct osnoise_hist_data *data) 64829a6c0bSDaniel Bristot de Oliveira { 65829a6c0bSDaniel Bristot de Oliveira int cpu; 66829a6c0bSDaniel Bristot de Oliveira 67829a6c0bSDaniel Bristot de Oliveira /* one histogram for IRQ and one for thread, per CPU */ 68829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 69829a6c0bSDaniel Bristot de Oliveira if (data->hist[cpu].samples) 70829a6c0bSDaniel Bristot de Oliveira free(data->hist[cpu].samples); 71829a6c0bSDaniel Bristot de Oliveira } 72829a6c0bSDaniel Bristot de Oliveira 73829a6c0bSDaniel Bristot de Oliveira /* one set of histograms per CPU */ 74829a6c0bSDaniel Bristot de Oliveira if (data->hist) 75829a6c0bSDaniel Bristot de Oliveira free(data->hist); 76829a6c0bSDaniel Bristot de Oliveira 77829a6c0bSDaniel Bristot de Oliveira free(data); 78829a6c0bSDaniel Bristot de Oliveira } 79829a6c0bSDaniel Bristot de Oliveira 80829a6c0bSDaniel Bristot de Oliveira /* 81829a6c0bSDaniel Bristot de Oliveira * osnoise_alloc_histogram - alloc runtime data 82829a6c0bSDaniel Bristot de Oliveira */ 83829a6c0bSDaniel Bristot de Oliveira static struct osnoise_hist_data 84829a6c0bSDaniel Bristot de Oliveira *osnoise_alloc_histogram(int nr_cpus, int entries, int bucket_size) 85829a6c0bSDaniel Bristot de Oliveira { 86829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data; 87829a6c0bSDaniel Bristot de Oliveira int cpu; 88829a6c0bSDaniel Bristot de Oliveira 89829a6c0bSDaniel Bristot de Oliveira data = calloc(1, sizeof(*data)); 90829a6c0bSDaniel Bristot de Oliveira if (!data) 91829a6c0bSDaniel Bristot de Oliveira return NULL; 92829a6c0bSDaniel Bristot de Oliveira 93829a6c0bSDaniel Bristot de Oliveira data->entries = entries; 94829a6c0bSDaniel Bristot de Oliveira data->bucket_size = bucket_size; 95829a6c0bSDaniel Bristot de Oliveira data->nr_cpus = nr_cpus; 96829a6c0bSDaniel Bristot de Oliveira 97829a6c0bSDaniel Bristot de Oliveira data->hist = calloc(1, sizeof(*data->hist) * nr_cpus); 98829a6c0bSDaniel Bristot de Oliveira if (!data->hist) 99829a6c0bSDaniel Bristot de Oliveira goto cleanup; 100829a6c0bSDaniel Bristot de Oliveira 101829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < nr_cpus; cpu++) { 102829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1)); 103829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].samples) 104829a6c0bSDaniel Bristot de Oliveira goto cleanup; 105829a6c0bSDaniel Bristot de Oliveira } 106829a6c0bSDaniel Bristot de Oliveira 107829a6c0bSDaniel Bristot de Oliveira /* set the min to max */ 108829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < nr_cpus; cpu++) 109829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].min_sample = ~0; 110829a6c0bSDaniel Bristot de Oliveira 111829a6c0bSDaniel Bristot de Oliveira return data; 112829a6c0bSDaniel Bristot de Oliveira 113829a6c0bSDaniel Bristot de Oliveira cleanup: 114829a6c0bSDaniel Bristot de Oliveira osnoise_free_histogram(data); 115829a6c0bSDaniel Bristot de Oliveira return NULL; 116829a6c0bSDaniel Bristot de Oliveira } 117829a6c0bSDaniel Bristot de Oliveira 118829a6c0bSDaniel Bristot de Oliveira static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu, 119829a6c0bSDaniel Bristot de Oliveira unsigned long long duration, int count) 120829a6c0bSDaniel Bristot de Oliveira { 121829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params = tool->params; 122829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 123829a6c0bSDaniel Bristot de Oliveira int entries = data->entries; 124829a6c0bSDaniel Bristot de Oliveira int bucket; 125829a6c0bSDaniel Bristot de Oliveira int *hist; 126829a6c0bSDaniel Bristot de Oliveira 127829a6c0bSDaniel Bristot de Oliveira if (params->output_divisor) 128829a6c0bSDaniel Bristot de Oliveira duration = duration / params->output_divisor; 129829a6c0bSDaniel Bristot de Oliveira 130829a6c0bSDaniel Bristot de Oliveira if (data->bucket_size) 131829a6c0bSDaniel Bristot de Oliveira bucket = duration / data->bucket_size; 132829a6c0bSDaniel Bristot de Oliveira 133829a6c0bSDaniel Bristot de Oliveira hist = data->hist[cpu].samples; 134829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].count += count; 135829a6c0bSDaniel Bristot de Oliveira update_min(&data->hist[cpu].min_sample, &duration); 136829a6c0bSDaniel Bristot de Oliveira update_sum(&data->hist[cpu].sum_sample, &duration); 137829a6c0bSDaniel Bristot de Oliveira update_max(&data->hist[cpu].max_sample, &duration); 138829a6c0bSDaniel Bristot de Oliveira 139829a6c0bSDaniel Bristot de Oliveira if (bucket < entries) 140829a6c0bSDaniel Bristot de Oliveira hist[bucket] += count; 141829a6c0bSDaniel Bristot de Oliveira else 142829a6c0bSDaniel Bristot de Oliveira hist[entries] += count; 143829a6c0bSDaniel Bristot de Oliveira } 144829a6c0bSDaniel Bristot de Oliveira 145829a6c0bSDaniel Bristot de Oliveira /* 146829a6c0bSDaniel Bristot de Oliveira * osnoise_destroy_trace_hist - disable events used to collect histogram 147829a6c0bSDaniel Bristot de Oliveira */ 148829a6c0bSDaniel Bristot de Oliveira static void osnoise_destroy_trace_hist(struct osnoise_tool *tool) 149829a6c0bSDaniel Bristot de Oliveira { 150829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 151829a6c0bSDaniel Bristot de Oliveira 152829a6c0bSDaniel Bristot de Oliveira tracefs_hist_pause(tool->trace.inst, data->trace_hist); 153829a6c0bSDaniel Bristot de Oliveira tracefs_hist_destroy(tool->trace.inst, data->trace_hist); 154829a6c0bSDaniel Bristot de Oliveira } 155829a6c0bSDaniel Bristot de Oliveira 156829a6c0bSDaniel Bristot de Oliveira /* 157829a6c0bSDaniel Bristot de Oliveira * osnoise_init_trace_hist - enable events used to collect histogram 158829a6c0bSDaniel Bristot de Oliveira */ 159829a6c0bSDaniel Bristot de Oliveira static int osnoise_init_trace_hist(struct osnoise_tool *tool) 160829a6c0bSDaniel Bristot de Oliveira { 161829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params = tool->params; 162829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 163829a6c0bSDaniel Bristot de Oliveira int bucket_size; 164829a6c0bSDaniel Bristot de Oliveira char buff[128]; 165829a6c0bSDaniel Bristot de Oliveira int retval = 0; 166829a6c0bSDaniel Bristot de Oliveira 167829a6c0bSDaniel Bristot de Oliveira /* 168829a6c0bSDaniel Bristot de Oliveira * Set the size of the bucket. 169829a6c0bSDaniel Bristot de Oliveira */ 170829a6c0bSDaniel Bristot de Oliveira bucket_size = params->output_divisor * params->bucket_size; 171829a6c0bSDaniel Bristot de Oliveira snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size); 172829a6c0bSDaniel Bristot de Oliveira 173829a6c0bSDaniel Bristot de Oliveira data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold", 174829a6c0bSDaniel Bristot de Oliveira buff, TRACEFS_HIST_KEY_NORMAL); 175829a6c0bSDaniel Bristot de Oliveira if (!data->trace_hist) 176829a6c0bSDaniel Bristot de Oliveira return 1; 177829a6c0bSDaniel Bristot de Oliveira 178829a6c0bSDaniel Bristot de Oliveira retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0); 179829a6c0bSDaniel Bristot de Oliveira if (retval) 180829a6c0bSDaniel Bristot de Oliveira goto out_err; 181829a6c0bSDaniel Bristot de Oliveira 182829a6c0bSDaniel Bristot de Oliveira retval = tracefs_hist_start(tool->trace.inst, data->trace_hist); 183829a6c0bSDaniel Bristot de Oliveira if (retval) 184829a6c0bSDaniel Bristot de Oliveira goto out_err; 185829a6c0bSDaniel Bristot de Oliveira 186829a6c0bSDaniel Bristot de Oliveira return 0; 187829a6c0bSDaniel Bristot de Oliveira 188829a6c0bSDaniel Bristot de Oliveira out_err: 189829a6c0bSDaniel Bristot de Oliveira osnoise_destroy_trace_hist(tool); 190829a6c0bSDaniel Bristot de Oliveira return 1; 191829a6c0bSDaniel Bristot de Oliveira } 192829a6c0bSDaniel Bristot de Oliveira 193829a6c0bSDaniel Bristot de Oliveira /* 194829a6c0bSDaniel Bristot de Oliveira * osnoise_read_trace_hist - parse histogram file and file osnoise histogram 195829a6c0bSDaniel Bristot de Oliveira */ 196829a6c0bSDaniel Bristot de Oliveira static void osnoise_read_trace_hist(struct osnoise_tool *tool) 197829a6c0bSDaniel Bristot de Oliveira { 198829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 199829a6c0bSDaniel Bristot de Oliveira long long cpu, counter, duration; 200829a6c0bSDaniel Bristot de Oliveira char *content, *position; 201829a6c0bSDaniel Bristot de Oliveira 202829a6c0bSDaniel Bristot de Oliveira tracefs_hist_pause(tool->trace.inst, data->trace_hist); 203829a6c0bSDaniel Bristot de Oliveira 204829a6c0bSDaniel Bristot de Oliveira content = tracefs_event_file_read(tool->trace.inst, "osnoise", 205829a6c0bSDaniel Bristot de Oliveira "sample_threshold", 206829a6c0bSDaniel Bristot de Oliveira "hist", NULL); 207829a6c0bSDaniel Bristot de Oliveira if (!content) 208829a6c0bSDaniel Bristot de Oliveira return; 209829a6c0bSDaniel Bristot de Oliveira 210829a6c0bSDaniel Bristot de Oliveira position = content; 211829a6c0bSDaniel Bristot de Oliveira while (true) { 212829a6c0bSDaniel Bristot de Oliveira position = strstr(position, "duration: ~"); 213829a6c0bSDaniel Bristot de Oliveira if (!position) 214829a6c0bSDaniel Bristot de Oliveira break; 215829a6c0bSDaniel Bristot de Oliveira position += strlen("duration: ~"); 216829a6c0bSDaniel Bristot de Oliveira duration = get_llong_from_str(position); 217829a6c0bSDaniel Bristot de Oliveira if (duration == -1) 218829a6c0bSDaniel Bristot de Oliveira err_msg("error reading duration from histogram\n"); 219829a6c0bSDaniel Bristot de Oliveira 220829a6c0bSDaniel Bristot de Oliveira position = strstr(position, "cpu:"); 221829a6c0bSDaniel Bristot de Oliveira if (!position) 222829a6c0bSDaniel Bristot de Oliveira break; 223829a6c0bSDaniel Bristot de Oliveira position += strlen("cpu: "); 224829a6c0bSDaniel Bristot de Oliveira cpu = get_llong_from_str(position); 225829a6c0bSDaniel Bristot de Oliveira if (cpu == -1) 226829a6c0bSDaniel Bristot de Oliveira err_msg("error reading cpu from histogram\n"); 227829a6c0bSDaniel Bristot de Oliveira 228829a6c0bSDaniel Bristot de Oliveira position = strstr(position, "hitcount:"); 229829a6c0bSDaniel Bristot de Oliveira if (!position) 230829a6c0bSDaniel Bristot de Oliveira break; 231829a6c0bSDaniel Bristot de Oliveira position += strlen("hitcount: "); 232829a6c0bSDaniel Bristot de Oliveira counter = get_llong_from_str(position); 233829a6c0bSDaniel Bristot de Oliveira if (counter == -1) 234829a6c0bSDaniel Bristot de Oliveira err_msg("error reading counter from histogram\n"); 235829a6c0bSDaniel Bristot de Oliveira 236829a6c0bSDaniel Bristot de Oliveira osnoise_hist_update_multiple(tool, cpu, duration, counter); 237829a6c0bSDaniel Bristot de Oliveira } 238829a6c0bSDaniel Bristot de Oliveira free(content); 239829a6c0bSDaniel Bristot de Oliveira } 240829a6c0bSDaniel Bristot de Oliveira 241829a6c0bSDaniel Bristot de Oliveira /* 242829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_header - print the header of the tracer to the output 243829a6c0bSDaniel Bristot de Oliveira */ 244829a6c0bSDaniel Bristot de Oliveira static void osnoise_hist_header(struct osnoise_tool *tool) 245829a6c0bSDaniel Bristot de Oliveira { 246829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params = tool->params; 247829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 248829a6c0bSDaniel Bristot de Oliveira struct trace_seq *s = tool->trace.seq; 249829a6c0bSDaniel Bristot de Oliveira char duration[26]; 250829a6c0bSDaniel Bristot de Oliveira int cpu; 251829a6c0bSDaniel Bristot de Oliveira 252829a6c0bSDaniel Bristot de Oliveira if (params->no_header) 253829a6c0bSDaniel Bristot de Oliveira return; 254829a6c0bSDaniel Bristot de Oliveira 255829a6c0bSDaniel Bristot de Oliveira get_duration(tool->start_time, duration, sizeof(duration)); 256829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "# RTLA osnoise histogram\n"); 257829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "# Time unit is %s (%s)\n", 258829a6c0bSDaniel Bristot de Oliveira params->output_divisor == 1 ? "nanoseconds" : "microseconds", 259829a6c0bSDaniel Bristot de Oliveira params->output_divisor == 1 ? "ns" : "us"); 260829a6c0bSDaniel Bristot de Oliveira 261829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "# Duration: %s\n", duration); 262829a6c0bSDaniel Bristot de Oliveira 263829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 264829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "Index"); 265829a6c0bSDaniel Bristot de Oliveira 266829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 267829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 268829a6c0bSDaniel Bristot de Oliveira continue; 269829a6c0bSDaniel Bristot de Oliveira 270829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 271829a6c0bSDaniel Bristot de Oliveira continue; 272829a6c0bSDaniel Bristot de Oliveira 273829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, " CPU-%03d", cpu); 274829a6c0bSDaniel Bristot de Oliveira } 275829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(s, "\n"); 276829a6c0bSDaniel Bristot de Oliveira 277829a6c0bSDaniel Bristot de Oliveira trace_seq_do_printf(s); 278829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(s); 279829a6c0bSDaniel Bristot de Oliveira } 280829a6c0bSDaniel Bristot de Oliveira 281829a6c0bSDaniel Bristot de Oliveira /* 282829a6c0bSDaniel Bristot de Oliveira * osnoise_print_summary - print the summary of the hist data to the output 283829a6c0bSDaniel Bristot de Oliveira */ 284829a6c0bSDaniel Bristot de Oliveira static void 285829a6c0bSDaniel Bristot de Oliveira osnoise_print_summary(struct osnoise_hist_params *params, 286829a6c0bSDaniel Bristot de Oliveira struct trace_instance *trace, 287829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data) 288829a6c0bSDaniel Bristot de Oliveira { 289829a6c0bSDaniel Bristot de Oliveira int cpu; 290829a6c0bSDaniel Bristot de Oliveira 291829a6c0bSDaniel Bristot de Oliveira if (params->no_summary) 292829a6c0bSDaniel Bristot de Oliveira return; 293829a6c0bSDaniel Bristot de Oliveira 294829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 295829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "count:"); 296829a6c0bSDaniel Bristot de Oliveira 297829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 298829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 299829a6c0bSDaniel Bristot de Oliveira continue; 300829a6c0bSDaniel Bristot de Oliveira 301829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 302829a6c0bSDaniel Bristot de Oliveira continue; 303829a6c0bSDaniel Bristot de Oliveira 304829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count); 305829a6c0bSDaniel Bristot de Oliveira } 306829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 307829a6c0bSDaniel Bristot de Oliveira 308829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 309829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "min: "); 310829a6c0bSDaniel Bristot de Oliveira 311829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 312829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 313829a6c0bSDaniel Bristot de Oliveira continue; 314829a6c0bSDaniel Bristot de Oliveira 315829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 316829a6c0bSDaniel Bristot de Oliveira continue; 317829a6c0bSDaniel Bristot de Oliveira 318829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample); 319829a6c0bSDaniel Bristot de Oliveira 320829a6c0bSDaniel Bristot de Oliveira } 321829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 322829a6c0bSDaniel Bristot de Oliveira 323829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 324829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "avg: "); 325829a6c0bSDaniel Bristot de Oliveira 326829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 327829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 328829a6c0bSDaniel Bristot de Oliveira continue; 329829a6c0bSDaniel Bristot de Oliveira 330829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 331829a6c0bSDaniel Bristot de Oliveira continue; 332829a6c0bSDaniel Bristot de Oliveira 333829a6c0bSDaniel Bristot de Oliveira if (data->hist[cpu].count) 334829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9llu ", 335829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].sum_sample / data->hist[cpu].count); 336829a6c0bSDaniel Bristot de Oliveira else 337829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, " - "); 338829a6c0bSDaniel Bristot de Oliveira } 339829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 340829a6c0bSDaniel Bristot de Oliveira 341829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 342829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "max: "); 343829a6c0bSDaniel Bristot de Oliveira 344829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 345829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 346829a6c0bSDaniel Bristot de Oliveira continue; 347829a6c0bSDaniel Bristot de Oliveira 348829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 349829a6c0bSDaniel Bristot de Oliveira continue; 350829a6c0bSDaniel Bristot de Oliveira 351829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample); 352829a6c0bSDaniel Bristot de Oliveira 353829a6c0bSDaniel Bristot de Oliveira } 354829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 355829a6c0bSDaniel Bristot de Oliveira trace_seq_do_printf(trace->seq); 356829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(trace->seq); 357829a6c0bSDaniel Bristot de Oliveira } 358829a6c0bSDaniel Bristot de Oliveira 359829a6c0bSDaniel Bristot de Oliveira /* 360829a6c0bSDaniel Bristot de Oliveira * osnoise_print_stats - print data for all CPUs 361829a6c0bSDaniel Bristot de Oliveira */ 362829a6c0bSDaniel Bristot de Oliveira static void 363829a6c0bSDaniel Bristot de Oliveira osnoise_print_stats(struct osnoise_hist_params *params, struct osnoise_tool *tool) 364829a6c0bSDaniel Bristot de Oliveira { 365829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data *data = tool->data; 366829a6c0bSDaniel Bristot de Oliveira struct trace_instance *trace = &tool->trace; 367829a6c0bSDaniel Bristot de Oliveira int bucket, cpu; 368829a6c0bSDaniel Bristot de Oliveira int total; 369829a6c0bSDaniel Bristot de Oliveira 370829a6c0bSDaniel Bristot de Oliveira osnoise_hist_header(tool); 371829a6c0bSDaniel Bristot de Oliveira 372829a6c0bSDaniel Bristot de Oliveira for (bucket = 0; bucket < data->entries; bucket++) { 373829a6c0bSDaniel Bristot de Oliveira total = 0; 374829a6c0bSDaniel Bristot de Oliveira 375829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 376829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%-6d", 377829a6c0bSDaniel Bristot de Oliveira bucket * data->bucket_size); 378829a6c0bSDaniel Bristot de Oliveira 379829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 380829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 381829a6c0bSDaniel Bristot de Oliveira continue; 382829a6c0bSDaniel Bristot de Oliveira 383829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 384829a6c0bSDaniel Bristot de Oliveira continue; 385829a6c0bSDaniel Bristot de Oliveira 386829a6c0bSDaniel Bristot de Oliveira total += data->hist[cpu].samples[bucket]; 387829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]); 388829a6c0bSDaniel Bristot de Oliveira } 389829a6c0bSDaniel Bristot de Oliveira 390829a6c0bSDaniel Bristot de Oliveira if (total == 0 && !params->with_zeros) { 391829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(trace->seq); 392829a6c0bSDaniel Bristot de Oliveira continue; 393829a6c0bSDaniel Bristot de Oliveira } 394829a6c0bSDaniel Bristot de Oliveira 395829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 396829a6c0bSDaniel Bristot de Oliveira trace_seq_do_printf(trace->seq); 397829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(trace->seq); 398829a6c0bSDaniel Bristot de Oliveira } 399829a6c0bSDaniel Bristot de Oliveira 400829a6c0bSDaniel Bristot de Oliveira if (!params->no_index) 401829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "over: "); 402829a6c0bSDaniel Bristot de Oliveira 403829a6c0bSDaniel Bristot de Oliveira for (cpu = 0; cpu < data->nr_cpus; cpu++) { 404829a6c0bSDaniel Bristot de Oliveira if (params->cpus && !params->monitored_cpus[cpu]) 405829a6c0bSDaniel Bristot de Oliveira continue; 406829a6c0bSDaniel Bristot de Oliveira 407829a6c0bSDaniel Bristot de Oliveira if (!data->hist[cpu].count) 408829a6c0bSDaniel Bristot de Oliveira continue; 409829a6c0bSDaniel Bristot de Oliveira 410829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "%9d ", 411829a6c0bSDaniel Bristot de Oliveira data->hist[cpu].samples[data->entries]); 412829a6c0bSDaniel Bristot de Oliveira } 413829a6c0bSDaniel Bristot de Oliveira trace_seq_printf(trace->seq, "\n"); 414829a6c0bSDaniel Bristot de Oliveira trace_seq_do_printf(trace->seq); 415829a6c0bSDaniel Bristot de Oliveira trace_seq_reset(trace->seq); 416829a6c0bSDaniel Bristot de Oliveira 417829a6c0bSDaniel Bristot de Oliveira osnoise_print_summary(params, trace, data); 418829a6c0bSDaniel Bristot de Oliveira } 419829a6c0bSDaniel Bristot de Oliveira 420829a6c0bSDaniel Bristot de Oliveira /* 421829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_usage - prints osnoise hist usage message 422829a6c0bSDaniel Bristot de Oliveira */ 423829a6c0bSDaniel Bristot de Oliveira static void osnoise_hist_usage(char *usage) 424829a6c0bSDaniel Bristot de Oliveira { 425829a6c0bSDaniel Bristot de Oliveira int i; 426829a6c0bSDaniel Bristot de Oliveira 427829a6c0bSDaniel Bristot de Oliveira static const char * const msg[] = { 428829a6c0bSDaniel Bristot de Oliveira "", 429*2b622eddSDaniel Bristot de Oliveira " usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\", 430*2b622eddSDaniel Bristot de Oliveira " [-T us] [-t[=file]] [-c cpu-list] [-P priority] [-b N] [-E N] [--no-header] \\", 431d635316aSDaniel Bristot de Oliveira " [--no-summary] [--no-index] [--with-zeros]", 432829a6c0bSDaniel Bristot de Oliveira "", 433829a6c0bSDaniel Bristot de Oliveira " -h/--help: print this menu", 434*2b622eddSDaniel Bristot de Oliveira " -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit", 435829a6c0bSDaniel Bristot de Oliveira " -p/--period us: osnoise period in us", 436829a6c0bSDaniel Bristot de Oliveira " -r/--runtime us: osnoise runtime in us", 437829a6c0bSDaniel Bristot de Oliveira " -s/--stop us: stop trace if a single sample is higher than the argument in us", 438829a6c0bSDaniel Bristot de Oliveira " -S/--stop-total us: stop trace if the total sample is higher than the argument in us", 439d635316aSDaniel Bristot de Oliveira " -T/--threshold us: the minimum delta to be considered a noise", 440829a6c0bSDaniel Bristot de Oliveira " -c/--cpus cpu-list: list of cpus to run osnoise threads", 441829a6c0bSDaniel Bristot de Oliveira " -d/--duration time[s|m|h|d]: duration of the session", 442829a6c0bSDaniel Bristot de Oliveira " -D/--debug: print debug info", 443829a6c0bSDaniel Bristot de Oliveira " -t/--trace[=file]: save the stopped trace to [file|osnoise_trace.txt]", 444829a6c0bSDaniel Bristot de Oliveira " -b/--bucket-size N: set the histogram bucket size (default 1)", 445dd48f316SDaniel Bristot de Oliveira " -E/--entries N: set the number of entries of the histogram (default 256)", 446829a6c0bSDaniel Bristot de Oliveira " --no-header: do not print header", 447829a6c0bSDaniel Bristot de Oliveira " --no-summary: do not print summary", 448829a6c0bSDaniel Bristot de Oliveira " --no-index: do not print index", 449829a6c0bSDaniel Bristot de Oliveira " --with-zeros: print zero only entries", 450829a6c0bSDaniel Bristot de Oliveira " -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters", 451829a6c0bSDaniel Bristot de Oliveira " o:prio - use SCHED_OTHER with prio", 452829a6c0bSDaniel Bristot de Oliveira " r:prio - use SCHED_RR with prio", 453829a6c0bSDaniel Bristot de Oliveira " f:prio - use SCHED_FIFO with prio", 454829a6c0bSDaniel Bristot de Oliveira " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period", 455829a6c0bSDaniel Bristot de Oliveira " in nanoseconds", 456829a6c0bSDaniel Bristot de Oliveira NULL, 457829a6c0bSDaniel Bristot de Oliveira }; 458829a6c0bSDaniel Bristot de Oliveira 459829a6c0bSDaniel Bristot de Oliveira if (usage) 460829a6c0bSDaniel Bristot de Oliveira fprintf(stderr, "%s\n", usage); 461829a6c0bSDaniel Bristot de Oliveira 462829a6c0bSDaniel Bristot de Oliveira fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n", 463829a6c0bSDaniel Bristot de Oliveira VERSION); 464829a6c0bSDaniel Bristot de Oliveira 465829a6c0bSDaniel Bristot de Oliveira for (i = 0; msg[i]; i++) 466829a6c0bSDaniel Bristot de Oliveira fprintf(stderr, "%s\n", msg[i]); 467829a6c0bSDaniel Bristot de Oliveira exit(1); 468829a6c0bSDaniel Bristot de Oliveira } 469829a6c0bSDaniel Bristot de Oliveira 470829a6c0bSDaniel Bristot de Oliveira /* 471829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters 472829a6c0bSDaniel Bristot de Oliveira */ 473829a6c0bSDaniel Bristot de Oliveira static struct osnoise_hist_params 474829a6c0bSDaniel Bristot de Oliveira *osnoise_hist_parse_args(int argc, char *argv[]) 475829a6c0bSDaniel Bristot de Oliveira { 476829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params; 477829a6c0bSDaniel Bristot de Oliveira int retval; 478829a6c0bSDaniel Bristot de Oliveira int c; 479829a6c0bSDaniel Bristot de Oliveira 480829a6c0bSDaniel Bristot de Oliveira params = calloc(1, sizeof(*params)); 481829a6c0bSDaniel Bristot de Oliveira if (!params) 482829a6c0bSDaniel Bristot de Oliveira exit(1); 483829a6c0bSDaniel Bristot de Oliveira 484829a6c0bSDaniel Bristot de Oliveira /* display data in microseconds */ 485829a6c0bSDaniel Bristot de Oliveira params->output_divisor = 1000; 486829a6c0bSDaniel Bristot de Oliveira params->bucket_size = 1; 487829a6c0bSDaniel Bristot de Oliveira params->entries = 256; 488829a6c0bSDaniel Bristot de Oliveira 489829a6c0bSDaniel Bristot de Oliveira while (1) { 490829a6c0bSDaniel Bristot de Oliveira static struct option long_options[] = { 491*2b622eddSDaniel Bristot de Oliveira {"auto", required_argument, 0, 'a'}, 492829a6c0bSDaniel Bristot de Oliveira {"bucket-size", required_argument, 0, 'b'}, 493dd48f316SDaniel Bristot de Oliveira {"entries", required_argument, 0, 'E'}, 494829a6c0bSDaniel Bristot de Oliveira {"cpus", required_argument, 0, 'c'}, 495829a6c0bSDaniel Bristot de Oliveira {"debug", no_argument, 0, 'D'}, 496829a6c0bSDaniel Bristot de Oliveira {"duration", required_argument, 0, 'd'}, 497829a6c0bSDaniel Bristot de Oliveira {"help", no_argument, 0, 'h'}, 498829a6c0bSDaniel Bristot de Oliveira {"period", required_argument, 0, 'p'}, 499829a6c0bSDaniel Bristot de Oliveira {"priority", required_argument, 0, 'P'}, 500829a6c0bSDaniel Bristot de Oliveira {"runtime", required_argument, 0, 'r'}, 501829a6c0bSDaniel Bristot de Oliveira {"stop", required_argument, 0, 's'}, 502829a6c0bSDaniel Bristot de Oliveira {"stop-total", required_argument, 0, 'S'}, 503829a6c0bSDaniel Bristot de Oliveira {"trace", optional_argument, 0, 't'}, 504d635316aSDaniel Bristot de Oliveira {"threshold", required_argument, 0, 'T'}, 505829a6c0bSDaniel Bristot de Oliveira {"no-header", no_argument, 0, '0'}, 506829a6c0bSDaniel Bristot de Oliveira {"no-summary", no_argument, 0, '1'}, 507829a6c0bSDaniel Bristot de Oliveira {"no-index", no_argument, 0, '2'}, 508829a6c0bSDaniel Bristot de Oliveira {"with-zeros", no_argument, 0, '3'}, 509829a6c0bSDaniel Bristot de Oliveira {0, 0, 0, 0} 510829a6c0bSDaniel Bristot de Oliveira }; 511829a6c0bSDaniel Bristot de Oliveira 512829a6c0bSDaniel Bristot de Oliveira /* getopt_long stores the option index here. */ 513829a6c0bSDaniel Bristot de Oliveira int option_index = 0; 514829a6c0bSDaniel Bristot de Oliveira 515*2b622eddSDaniel Bristot de Oliveira c = getopt_long(argc, argv, "a:c:b:d:E:Dhp:P:r:s:S:t::T:0123", 516829a6c0bSDaniel Bristot de Oliveira long_options, &option_index); 517829a6c0bSDaniel Bristot de Oliveira 518829a6c0bSDaniel Bristot de Oliveira /* detect the end of the options. */ 519829a6c0bSDaniel Bristot de Oliveira if (c == -1) 520829a6c0bSDaniel Bristot de Oliveira break; 521829a6c0bSDaniel Bristot de Oliveira 522829a6c0bSDaniel Bristot de Oliveira switch (c) { 523*2b622eddSDaniel Bristot de Oliveira case 'a': 524*2b622eddSDaniel Bristot de Oliveira /* set sample stop to auto_thresh */ 525*2b622eddSDaniel Bristot de Oliveira params->stop_us = get_llong_from_str(optarg); 526*2b622eddSDaniel Bristot de Oliveira 527*2b622eddSDaniel Bristot de Oliveira /* set sample threshold to 1 */ 528*2b622eddSDaniel Bristot de Oliveira params->threshold = 1; 529*2b622eddSDaniel Bristot de Oliveira 530*2b622eddSDaniel Bristot de Oliveira /* set trace */ 531*2b622eddSDaniel Bristot de Oliveira params->trace_output = "osnoise_trace.txt"; 532*2b622eddSDaniel Bristot de Oliveira 533*2b622eddSDaniel Bristot de Oliveira break; 534829a6c0bSDaniel Bristot de Oliveira case 'b': 535829a6c0bSDaniel Bristot de Oliveira params->bucket_size = get_llong_from_str(optarg); 536829a6c0bSDaniel Bristot de Oliveira if ((params->bucket_size == 0) || (params->bucket_size >= 1000000)) 537829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Bucket size needs to be > 0 and <= 1000000\n"); 538829a6c0bSDaniel Bristot de Oliveira break; 539829a6c0bSDaniel Bristot de Oliveira case 'c': 540829a6c0bSDaniel Bristot de Oliveira retval = parse_cpu_list(optarg, ¶ms->monitored_cpus); 541829a6c0bSDaniel Bristot de Oliveira if (retval) 542829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("\nInvalid -c cpu list\n"); 543829a6c0bSDaniel Bristot de Oliveira params->cpus = optarg; 544829a6c0bSDaniel Bristot de Oliveira break; 545829a6c0bSDaniel Bristot de Oliveira case 'D': 546829a6c0bSDaniel Bristot de Oliveira config_debug = 1; 547829a6c0bSDaniel Bristot de Oliveira break; 548829a6c0bSDaniel Bristot de Oliveira case 'd': 549829a6c0bSDaniel Bristot de Oliveira params->duration = parse_seconds_duration(optarg); 550829a6c0bSDaniel Bristot de Oliveira if (!params->duration) 551829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Invalid -D duration\n"); 552829a6c0bSDaniel Bristot de Oliveira break; 553dd48f316SDaniel Bristot de Oliveira case 'E': 554829a6c0bSDaniel Bristot de Oliveira params->entries = get_llong_from_str(optarg); 555829a6c0bSDaniel Bristot de Oliveira if ((params->entries < 10) || (params->entries > 9999999)) 556829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Entries must be > 10 and < 9999999\n"); 557829a6c0bSDaniel Bristot de Oliveira break; 558829a6c0bSDaniel Bristot de Oliveira case 'h': 559829a6c0bSDaniel Bristot de Oliveira case '?': 560829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage(NULL); 561829a6c0bSDaniel Bristot de Oliveira break; 562829a6c0bSDaniel Bristot de Oliveira case 'p': 563829a6c0bSDaniel Bristot de Oliveira params->period = get_llong_from_str(optarg); 564829a6c0bSDaniel Bristot de Oliveira if (params->period > 10000000) 565829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Period longer than 10 s\n"); 566829a6c0bSDaniel Bristot de Oliveira break; 567829a6c0bSDaniel Bristot de Oliveira case 'P': 568829a6c0bSDaniel Bristot de Oliveira retval = parse_prio(optarg, ¶ms->sched_param); 569829a6c0bSDaniel Bristot de Oliveira if (retval == -1) 570829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Invalid -P priority"); 571829a6c0bSDaniel Bristot de Oliveira params->set_sched = 1; 572829a6c0bSDaniel Bristot de Oliveira break; 573829a6c0bSDaniel Bristot de Oliveira case 'r': 574829a6c0bSDaniel Bristot de Oliveira params->runtime = get_llong_from_str(optarg); 575829a6c0bSDaniel Bristot de Oliveira if (params->runtime < 100) 576829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Runtime shorter than 100 us\n"); 577829a6c0bSDaniel Bristot de Oliveira break; 578829a6c0bSDaniel Bristot de Oliveira case 's': 579829a6c0bSDaniel Bristot de Oliveira params->stop_us = get_llong_from_str(optarg); 580829a6c0bSDaniel Bristot de Oliveira break; 581829a6c0bSDaniel Bristot de Oliveira case 'S': 582829a6c0bSDaniel Bristot de Oliveira params->stop_total_us = get_llong_from_str(optarg); 583829a6c0bSDaniel Bristot de Oliveira break; 584d635316aSDaniel Bristot de Oliveira case 'T': 585d635316aSDaniel Bristot de Oliveira params->threshold = get_llong_from_str(optarg); 586d635316aSDaniel Bristot de Oliveira break; 587829a6c0bSDaniel Bristot de Oliveira case 't': 588829a6c0bSDaniel Bristot de Oliveira if (optarg) 589829a6c0bSDaniel Bristot de Oliveira /* skip = */ 590829a6c0bSDaniel Bristot de Oliveira params->trace_output = &optarg[1]; 591829a6c0bSDaniel Bristot de Oliveira else 592829a6c0bSDaniel Bristot de Oliveira params->trace_output = "osnoise_trace.txt"; 593829a6c0bSDaniel Bristot de Oliveira break; 594829a6c0bSDaniel Bristot de Oliveira case '0': /* no header */ 595829a6c0bSDaniel Bristot de Oliveira params->no_header = 1; 596829a6c0bSDaniel Bristot de Oliveira break; 597829a6c0bSDaniel Bristot de Oliveira case '1': /* no summary */ 598829a6c0bSDaniel Bristot de Oliveira params->no_summary = 1; 599829a6c0bSDaniel Bristot de Oliveira break; 600829a6c0bSDaniel Bristot de Oliveira case '2': /* no index */ 601829a6c0bSDaniel Bristot de Oliveira params->no_index = 1; 602829a6c0bSDaniel Bristot de Oliveira break; 603829a6c0bSDaniel Bristot de Oliveira case '3': /* with zeros */ 604829a6c0bSDaniel Bristot de Oliveira params->with_zeros = 1; 605829a6c0bSDaniel Bristot de Oliveira break; 606829a6c0bSDaniel Bristot de Oliveira default: 607829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("Invalid option"); 608829a6c0bSDaniel Bristot de Oliveira } 609829a6c0bSDaniel Bristot de Oliveira } 610829a6c0bSDaniel Bristot de Oliveira 611829a6c0bSDaniel Bristot de Oliveira if (geteuid()) { 612829a6c0bSDaniel Bristot de Oliveira err_msg("rtla needs root permission\n"); 613829a6c0bSDaniel Bristot de Oliveira exit(EXIT_FAILURE); 614829a6c0bSDaniel Bristot de Oliveira } 615829a6c0bSDaniel Bristot de Oliveira 616829a6c0bSDaniel Bristot de Oliveira if (params->no_index && !params->with_zeros) 617829a6c0bSDaniel Bristot de Oliveira osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense"); 618829a6c0bSDaniel Bristot de Oliveira 619829a6c0bSDaniel Bristot de Oliveira return params; 620829a6c0bSDaniel Bristot de Oliveira } 621829a6c0bSDaniel Bristot de Oliveira 622829a6c0bSDaniel Bristot de Oliveira /* 623829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_apply_config - apply the hist configs to the initialized tool 624829a6c0bSDaniel Bristot de Oliveira */ 625829a6c0bSDaniel Bristot de Oliveira static int 626829a6c0bSDaniel Bristot de Oliveira osnoise_hist_apply_config(struct osnoise_tool *tool, struct osnoise_hist_params *params) 627829a6c0bSDaniel Bristot de Oliveira { 628829a6c0bSDaniel Bristot de Oliveira int retval; 629829a6c0bSDaniel Bristot de Oliveira 630829a6c0bSDaniel Bristot de Oliveira if (!params->sleep_time) 631829a6c0bSDaniel Bristot de Oliveira params->sleep_time = 1; 632829a6c0bSDaniel Bristot de Oliveira 633829a6c0bSDaniel Bristot de Oliveira if (params->cpus) { 634829a6c0bSDaniel Bristot de Oliveira retval = osnoise_set_cpus(tool->context, params->cpus); 635829a6c0bSDaniel Bristot de Oliveira if (retval) { 636829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to apply CPUs config\n"); 637829a6c0bSDaniel Bristot de Oliveira goto out_err; 638829a6c0bSDaniel Bristot de Oliveira } 639829a6c0bSDaniel Bristot de Oliveira } 640829a6c0bSDaniel Bristot de Oliveira 641829a6c0bSDaniel Bristot de Oliveira if (params->runtime || params->period) { 642829a6c0bSDaniel Bristot de Oliveira retval = osnoise_set_runtime_period(tool->context, 643829a6c0bSDaniel Bristot de Oliveira params->runtime, 644829a6c0bSDaniel Bristot de Oliveira params->period); 645829a6c0bSDaniel Bristot de Oliveira if (retval) { 646829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to set runtime and/or period\n"); 647829a6c0bSDaniel Bristot de Oliveira goto out_err; 648829a6c0bSDaniel Bristot de Oliveira } 649829a6c0bSDaniel Bristot de Oliveira } 650829a6c0bSDaniel Bristot de Oliveira 651829a6c0bSDaniel Bristot de Oliveira if (params->stop_us) { 652829a6c0bSDaniel Bristot de Oliveira retval = osnoise_set_stop_us(tool->context, params->stop_us); 653829a6c0bSDaniel Bristot de Oliveira if (retval) { 654829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to set stop us\n"); 655829a6c0bSDaniel Bristot de Oliveira goto out_err; 656829a6c0bSDaniel Bristot de Oliveira } 657829a6c0bSDaniel Bristot de Oliveira } 658829a6c0bSDaniel Bristot de Oliveira 659829a6c0bSDaniel Bristot de Oliveira if (params->stop_total_us) { 660829a6c0bSDaniel Bristot de Oliveira retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us); 661829a6c0bSDaniel Bristot de Oliveira if (retval) { 662829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to set stop total us\n"); 663829a6c0bSDaniel Bristot de Oliveira goto out_err; 664829a6c0bSDaniel Bristot de Oliveira } 665829a6c0bSDaniel Bristot de Oliveira } 666829a6c0bSDaniel Bristot de Oliveira 667d635316aSDaniel Bristot de Oliveira if (params->threshold) { 668d635316aSDaniel Bristot de Oliveira retval = osnoise_set_tracing_thresh(tool->context, params->threshold); 669d635316aSDaniel Bristot de Oliveira if (retval) { 670d635316aSDaniel Bristot de Oliveira err_msg("Failed to set tracing_thresh\n"); 671d635316aSDaniel Bristot de Oliveira goto out_err; 672d635316aSDaniel Bristot de Oliveira } 673d635316aSDaniel Bristot de Oliveira } 674d635316aSDaniel Bristot de Oliveira 675829a6c0bSDaniel Bristot de Oliveira return 0; 676829a6c0bSDaniel Bristot de Oliveira 677829a6c0bSDaniel Bristot de Oliveira out_err: 678829a6c0bSDaniel Bristot de Oliveira return -1; 679829a6c0bSDaniel Bristot de Oliveira } 680829a6c0bSDaniel Bristot de Oliveira 681829a6c0bSDaniel Bristot de Oliveira /* 682829a6c0bSDaniel Bristot de Oliveira * osnoise_init_hist - initialize a osnoise hist tool with parameters 683829a6c0bSDaniel Bristot de Oliveira */ 684829a6c0bSDaniel Bristot de Oliveira static struct osnoise_tool 685829a6c0bSDaniel Bristot de Oliveira *osnoise_init_hist(struct osnoise_hist_params *params) 686829a6c0bSDaniel Bristot de Oliveira { 687829a6c0bSDaniel Bristot de Oliveira struct osnoise_tool *tool; 688829a6c0bSDaniel Bristot de Oliveira int nr_cpus; 689829a6c0bSDaniel Bristot de Oliveira 690829a6c0bSDaniel Bristot de Oliveira nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 691829a6c0bSDaniel Bristot de Oliveira 692829a6c0bSDaniel Bristot de Oliveira tool = osnoise_init_tool("osnoise_hist"); 693829a6c0bSDaniel Bristot de Oliveira if (!tool) 694829a6c0bSDaniel Bristot de Oliveira return NULL; 695829a6c0bSDaniel Bristot de Oliveira 696829a6c0bSDaniel Bristot de Oliveira tool->data = osnoise_alloc_histogram(nr_cpus, params->entries, params->bucket_size); 697829a6c0bSDaniel Bristot de Oliveira if (!tool->data) 698829a6c0bSDaniel Bristot de Oliveira goto out_err; 699829a6c0bSDaniel Bristot de Oliveira 700829a6c0bSDaniel Bristot de Oliveira tool->params = params; 701829a6c0bSDaniel Bristot de Oliveira 702829a6c0bSDaniel Bristot de Oliveira return tool; 703829a6c0bSDaniel Bristot de Oliveira 704829a6c0bSDaniel Bristot de Oliveira out_err: 705829a6c0bSDaniel Bristot de Oliveira osnoise_destroy_tool(tool); 706829a6c0bSDaniel Bristot de Oliveira return NULL; 707829a6c0bSDaniel Bristot de Oliveira } 708829a6c0bSDaniel Bristot de Oliveira 709829a6c0bSDaniel Bristot de Oliveira static int stop_tracing; 710829a6c0bSDaniel Bristot de Oliveira static void stop_hist(int sig) 711829a6c0bSDaniel Bristot de Oliveira { 712829a6c0bSDaniel Bristot de Oliveira stop_tracing = 1; 713829a6c0bSDaniel Bristot de Oliveira } 714829a6c0bSDaniel Bristot de Oliveira 715829a6c0bSDaniel Bristot de Oliveira /* 716829a6c0bSDaniel Bristot de Oliveira * osnoise_hist_set_signals - handles the signal to stop the tool 717829a6c0bSDaniel Bristot de Oliveira */ 718829a6c0bSDaniel Bristot de Oliveira static void 719829a6c0bSDaniel Bristot de Oliveira osnoise_hist_set_signals(struct osnoise_hist_params *params) 720829a6c0bSDaniel Bristot de Oliveira { 721829a6c0bSDaniel Bristot de Oliveira signal(SIGINT, stop_hist); 722829a6c0bSDaniel Bristot de Oliveira if (params->duration) { 723829a6c0bSDaniel Bristot de Oliveira signal(SIGALRM, stop_hist); 724829a6c0bSDaniel Bristot de Oliveira alarm(params->duration); 725829a6c0bSDaniel Bristot de Oliveira } 726829a6c0bSDaniel Bristot de Oliveira } 727829a6c0bSDaniel Bristot de Oliveira 728829a6c0bSDaniel Bristot de Oliveira int osnoise_hist_main(int argc, char *argv[]) 729829a6c0bSDaniel Bristot de Oliveira { 730829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params *params; 7314bbf59a9SDaniel Bristot de Oliveira struct osnoise_tool *record = NULL; 7324bbf59a9SDaniel Bristot de Oliveira struct osnoise_tool *tool = NULL; 733829a6c0bSDaniel Bristot de Oliveira struct trace_instance *trace; 734829a6c0bSDaniel Bristot de Oliveira int return_value = 1; 735829a6c0bSDaniel Bristot de Oliveira int retval; 736829a6c0bSDaniel Bristot de Oliveira 737829a6c0bSDaniel Bristot de Oliveira params = osnoise_hist_parse_args(argc, argv); 738829a6c0bSDaniel Bristot de Oliveira if (!params) 739829a6c0bSDaniel Bristot de Oliveira exit(1); 740829a6c0bSDaniel Bristot de Oliveira 741829a6c0bSDaniel Bristot de Oliveira tool = osnoise_init_hist(params); 742829a6c0bSDaniel Bristot de Oliveira if (!tool) { 743829a6c0bSDaniel Bristot de Oliveira err_msg("Could not init osnoise hist\n"); 744829a6c0bSDaniel Bristot de Oliveira goto out_exit; 745829a6c0bSDaniel Bristot de Oliveira } 746829a6c0bSDaniel Bristot de Oliveira 747829a6c0bSDaniel Bristot de Oliveira retval = osnoise_hist_apply_config(tool, params); 748829a6c0bSDaniel Bristot de Oliveira if (retval) { 749829a6c0bSDaniel Bristot de Oliveira err_msg("Could not apply config\n"); 750829a6c0bSDaniel Bristot de Oliveira goto out_destroy; 751829a6c0bSDaniel Bristot de Oliveira } 752829a6c0bSDaniel Bristot de Oliveira 753829a6c0bSDaniel Bristot de Oliveira trace = &tool->trace; 754829a6c0bSDaniel Bristot de Oliveira 755829a6c0bSDaniel Bristot de Oliveira retval = enable_osnoise(trace); 756829a6c0bSDaniel Bristot de Oliveira if (retval) { 757829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to enable osnoise tracer\n"); 758829a6c0bSDaniel Bristot de Oliveira goto out_destroy; 759829a6c0bSDaniel Bristot de Oliveira } 760829a6c0bSDaniel Bristot de Oliveira 761829a6c0bSDaniel Bristot de Oliveira retval = osnoise_init_trace_hist(tool); 762829a6c0bSDaniel Bristot de Oliveira if (retval) 763829a6c0bSDaniel Bristot de Oliveira goto out_destroy; 764829a6c0bSDaniel Bristot de Oliveira 765829a6c0bSDaniel Bristot de Oliveira if (params->set_sched) { 766829a6c0bSDaniel Bristot de Oliveira retval = set_comm_sched_attr("osnoise/", ¶ms->sched_param); 767829a6c0bSDaniel Bristot de Oliveira if (retval) { 768829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to set sched parameters\n"); 769829a6c0bSDaniel Bristot de Oliveira goto out_hist; 770829a6c0bSDaniel Bristot de Oliveira } 771829a6c0bSDaniel Bristot de Oliveira } 772829a6c0bSDaniel Bristot de Oliveira 773829a6c0bSDaniel Bristot de Oliveira trace_instance_start(trace); 774829a6c0bSDaniel Bristot de Oliveira 775829a6c0bSDaniel Bristot de Oliveira if (params->trace_output) { 776829a6c0bSDaniel Bristot de Oliveira record = osnoise_init_trace_tool("osnoise"); 777829a6c0bSDaniel Bristot de Oliveira if (!record) { 778829a6c0bSDaniel Bristot de Oliveira err_msg("Failed to enable the trace instance\n"); 779829a6c0bSDaniel Bristot de Oliveira goto out_hist; 780829a6c0bSDaniel Bristot de Oliveira } 781829a6c0bSDaniel Bristot de Oliveira trace_instance_start(&record->trace); 782829a6c0bSDaniel Bristot de Oliveira } 783829a6c0bSDaniel Bristot de Oliveira 784829a6c0bSDaniel Bristot de Oliveira tool->start_time = time(NULL); 785829a6c0bSDaniel Bristot de Oliveira osnoise_hist_set_signals(params); 786829a6c0bSDaniel Bristot de Oliveira 787829a6c0bSDaniel Bristot de Oliveira while (!stop_tracing) { 788829a6c0bSDaniel Bristot de Oliveira sleep(params->sleep_time); 789829a6c0bSDaniel Bristot de Oliveira 790829a6c0bSDaniel Bristot de Oliveira retval = tracefs_iterate_raw_events(trace->tep, 791829a6c0bSDaniel Bristot de Oliveira trace->inst, 792829a6c0bSDaniel Bristot de Oliveira NULL, 793829a6c0bSDaniel Bristot de Oliveira 0, 794829a6c0bSDaniel Bristot de Oliveira collect_registered_events, 795829a6c0bSDaniel Bristot de Oliveira trace); 796829a6c0bSDaniel Bristot de Oliveira if (retval < 0) { 797829a6c0bSDaniel Bristot de Oliveira err_msg("Error iterating on events\n"); 798829a6c0bSDaniel Bristot de Oliveira goto out_hist; 799829a6c0bSDaniel Bristot de Oliveira } 800829a6c0bSDaniel Bristot de Oliveira 801829a6c0bSDaniel Bristot de Oliveira if (!tracefs_trace_is_on(trace->inst)) 802829a6c0bSDaniel Bristot de Oliveira break; 803829a6c0bSDaniel Bristot de Oliveira }; 804829a6c0bSDaniel Bristot de Oliveira 805829a6c0bSDaniel Bristot de Oliveira osnoise_read_trace_hist(tool); 806829a6c0bSDaniel Bristot de Oliveira 807829a6c0bSDaniel Bristot de Oliveira osnoise_print_stats(params, tool); 808829a6c0bSDaniel Bristot de Oliveira 809829a6c0bSDaniel Bristot de Oliveira return_value = 0; 810829a6c0bSDaniel Bristot de Oliveira 811829a6c0bSDaniel Bristot de Oliveira if (!tracefs_trace_is_on(trace->inst)) { 812829a6c0bSDaniel Bristot de Oliveira printf("rtla timelat hit stop tracing\n"); 813829a6c0bSDaniel Bristot de Oliveira if (params->trace_output) { 814829a6c0bSDaniel Bristot de Oliveira printf(" Saving trace to %s\n", params->trace_output); 815829a6c0bSDaniel Bristot de Oliveira save_trace_to_file(record->trace.inst, params->trace_output); 816829a6c0bSDaniel Bristot de Oliveira } 817829a6c0bSDaniel Bristot de Oliveira } 818829a6c0bSDaniel Bristot de Oliveira 819829a6c0bSDaniel Bristot de Oliveira out_hist: 820829a6c0bSDaniel Bristot de Oliveira osnoise_free_histogram(tool->data); 821829a6c0bSDaniel Bristot de Oliveira out_destroy: 822829a6c0bSDaniel Bristot de Oliveira osnoise_destroy_tool(record); 8234bbf59a9SDaniel Bristot de Oliveira osnoise_destroy_tool(tool); 824829a6c0bSDaniel Bristot de Oliveira free(params); 825829a6c0bSDaniel Bristot de Oliveira out_exit: 826829a6c0bSDaniel Bristot de Oliveira exit(return_value); 827829a6c0bSDaniel Bristot de Oliveira } 828