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