| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
#define _GNU_SOURCE |
| 7 |
#include <getopt.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
#include <signal.h> |
| 11 |
#include <unistd.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <time.h> |
| 14 |
|
| 15 |
#include "osnoise.h" |
| 16 |
|
| 17 |
struct osnoise_hist_cpu { |
| 18 |
int *samples; |
| 19 |
int count; |
| 20 |
|
| 21 |
unsigned long long min_sample; |
| 22 |
unsigned long long sum_sample; |
| 23 |
unsigned long long max_sample; |
| 24 |
|
| 25 |
}; |
| 26 |
|
| 27 |
struct osnoise_hist_data { |
| 28 |
struct tracefs_hist *trace_hist; |
| 29 |
struct osnoise_hist_cpu *hist; |
| 30 |
int entries; |
| 31 |
int bucket_size; |
| 32 |
int nr_cpus; |
| 33 |
}; |
| 34 |
|
| 35 |
/* |
| 36 |
* osnoise_free_histogram - free runtime data |
| 37 |
*/ |
| 38 |
static void |
| 39 |
osnoise_free_histogram(struct osnoise_hist_data *data) |
| 40 |
{ |
| 41 |
int cpu; |
| 42 |
|
| 43 |
/* one histogram for IRQ and one for thread, per CPU */ |
| 44 |
for (cpu = 0; cpu < data->nr_cpus; cpu++) { |
| 45 |
if (data->hist[cpu].samples) |
| 46 |
free(data->hist[cpu].samples); |
| 47 |
} |
| 48 |
|
| 49 |
/* one set of histograms per CPU */ |
| 50 |
if (data->hist) |
| 51 |
free(data->hist); |
| 52 |
|
| 53 |
free(data); |
| 54 |
} |
| 55 |
|
| 56 |
static void osnoise_free_hist_tool(struct osnoise_tool *tool) |
| 57 |
{ |
| 58 |
osnoise_free_histogram(tool->data); |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* osnoise_alloc_histogram - alloc runtime data |
| 63 |
*/ |
| 64 |
static struct osnoise_hist_data |
| 65 |
*osnoise_alloc_histogram(int nr_cpus, int entries, int bucket_size) |
| 66 |
{ |
| 67 |
struct osnoise_hist_data *data; |
| 68 |
int cpu; |
| 69 |
|
| 70 |
data = calloc(1, sizeof(*data)); |
| 71 |
if (!data) |
| 72 |
return NULL; |
| 73 |
|
| 74 |
data->entries = entries; |
| 75 |
data->bucket_size = bucket_size; |
| 76 |
data->nr_cpus = nr_cpus; |
| 77 |
|
| 78 |
data->hist = calloc(1, sizeof(*data->hist) * nr_cpus); |
| 79 |
if (!data->hist) |
| 80 |
goto cleanup; |
| 81 |
|
| 82 |
for (cpu = 0; cpu < nr_cpus; cpu++) { |
| 83 |
data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1)); |
| 84 |
if (!data->hist[cpu].samples) |
| 85 |
goto cleanup; |
| 86 |
} |
| 87 |
|
| 88 |
/* set the min to max */ |
| 89 |
for (cpu = 0; cpu < nr_cpus; cpu++) |
| 90 |
data->hist[cpu].min_sample = ~0; |
| 91 |
|
| 92 |
return data; |
| 93 |
|
| 94 |
cleanup: |
| 95 |
osnoise_free_histogram(data); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu, |
| 100 |
unsigned long long duration, int count) |
| 101 |
{ |
| 102 |
struct osnoise_params *params = to_osnoise_params(tool->params); |
| 103 |
struct osnoise_hist_data *data = tool->data; |
| 104 |
unsigned long long total_duration; |
| 105 |
int entries = data->entries; |
| 106 |
int bucket; |
| 107 |
int *hist; |
| 108 |
|
| 109 |
if (params->common.output_divisor) |
| 110 |
duration = duration / params->common.output_divisor; |
| 111 |
|
| 112 |
bucket = duration / data->bucket_size; |
| 113 |
|
| 114 |
total_duration = duration * count; |
| 115 |
|
| 116 |
hist = data->hist[cpu].samples; |
| 117 |
data->hist[cpu].count += count; |
| 118 |
update_min(&data->hist[cpu].min_sample, &duration); |
| 119 |
update_sum(&data->hist[cpu].sum_sample, &total_duration); |
| 120 |
update_max(&data->hist[cpu].max_sample, &duration); |
| 121 |
|
| 122 |
if (bucket < entries) |
| 123 |
hist[bucket] += count; |
| 124 |
else |
| 125 |
hist[entries] += count; |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* osnoise_destroy_trace_hist - disable events used to collect histogram |
| 130 |
*/ |
| 131 |
static void osnoise_destroy_trace_hist(struct osnoise_tool *tool) |
| 132 |
{ |
| 133 |
struct osnoise_hist_data *data = tool->data; |
| 134 |
|
| 135 |
tracefs_hist_pause(tool->trace.inst, data->trace_hist); |
| 136 |
tracefs_hist_destroy(tool->trace.inst, data->trace_hist); |
| 137 |
} |
| 138 |
|
| 139 |
/* |
| 140 |
* osnoise_init_trace_hist - enable events used to collect histogram |
| 141 |
*/ |
| 142 |
static int osnoise_init_trace_hist(struct osnoise_tool *tool) |
| 143 |
{ |
| 144 |
struct osnoise_params *params = to_osnoise_params(tool->params); |
| 145 |
struct osnoise_hist_data *data = tool->data; |
| 146 |
int bucket_size; |
| 147 |
char buff[128]; |
| 148 |
int retval = 0; |
| 149 |
|
| 150 |
/* |
| 151 |
* Set the size of the bucket. |
| 152 |
*/ |
| 153 |
bucket_size = params->common.output_divisor * params->common.hist.bucket_size; |
| 154 |
snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size); |
| 155 |
|
| 156 |
data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold", |
| 157 |
buff, TRACEFS_HIST_KEY_NORMAL); |
| 158 |
if (!data->trace_hist) |
| 159 |
return 1; |
| 160 |
|
| 161 |
retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0); |
| 162 |
if (retval) |
| 163 |
goto out_err; |
| 164 |
|
| 165 |
retval = tracefs_hist_start(tool->trace.inst, data->trace_hist); |
| 166 |
if (retval) |
| 167 |
goto out_err; |
| 168 |
|
| 169 |
return 0; |
| 170 |
|
| 171 |
out_err: |
| 172 |
osnoise_destroy_trace_hist(tool); |
| 173 |
return 1; |
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
* osnoise_read_trace_hist - parse histogram file and file osnoise histogram |
| 178 |
*/ |
| 179 |
static void osnoise_read_trace_hist(struct osnoise_tool *tool) |
| 180 |
{ |
| 181 |
struct osnoise_hist_data *data = tool->data; |
| 182 |
long long cpu, counter, duration; |
| 183 |
char *content, *position; |
| 184 |
|
| 185 |
tracefs_hist_pause(tool->trace.inst, data->trace_hist); |
| 186 |
|
| 187 |
content = tracefs_event_file_read(tool->trace.inst, "osnoise", |
| 188 |
"sample_threshold", |
| 189 |
"hist", NULL); |
| 190 |
if (!content) |
| 191 |
return; |
| 192 |
|
| 193 |
position = content; |
| 194 |
while (true) { |
| 195 |
position = strstr(position, "duration: ~"); |
| 196 |
if (!position) |
| 197 |
break; |
| 198 |
position += strlen("duration: ~"); |
| 199 |
duration = get_llong_from_str(position); |
| 200 |
if (duration == -1) |
| 201 |
err_msg("error reading duration from histogram\n"); |
| 202 |
|
| 203 |
position = strstr(position, "cpu:"); |
| 204 |
if (!position) |
| 205 |
break; |
| 206 |
position += strlen("cpu: "); |
| 207 |
cpu = get_llong_from_str(position); |
| 208 |
if (cpu == -1) |
| 209 |
err_msg("error reading cpu from histogram\n"); |
| 210 |
|
| 211 |
position = strstr(position, "hitcount:"); |
| 212 |
if (!position) |
| 213 |
break; |
| 214 |
position += strlen("hitcount: "); |
| 215 |
counter = get_llong_from_str(position); |
| 216 |
if (counter == -1) |
| 217 |
err_msg("error reading counter from histogram\n"); |
| 218 |
|
| 219 |
osnoise_hist_update_multiple(tool, cpu, duration, counter); |
| 220 |
} |
| 221 |
free(content); |
| 222 |
} |
| 223 |
|
| 224 |
/* |
| 225 |
* osnoise_hist_header - print the header of the tracer to the output |
| 226 |
*/ |
| 227 |
static void osnoise_hist_header(struct osnoise_tool *tool) |
| 228 |
{ |
| 229 |
struct osnoise_params *params = to_osnoise_params(tool->params); |
| 230 |
struct osnoise_hist_data *data = tool->data; |
| 231 |
struct trace_seq *s = tool->trace.seq; |
| 232 |
char duration[26]; |
| 233 |
int cpu; |
| 234 |
|
| 235 |
if (params->common.hist.no_header) |
| 236 |
return; |
| 237 |
|
| 238 |
get_duration(tool->start_time, duration, sizeof(duration)); |
| 239 |
trace_seq_printf(s, "# RTLA osnoise histogram\n"); |
| 240 |
trace_seq_printf(s, "# Time unit is %s (%s)\n", |
| 241 |
params->common.output_divisor == 1 ? "nanoseconds" : "microseconds", |
| 242 |
params->common.output_divisor == 1 ? "ns" : "us"); |
| 243 |
|
| 244 |
trace_seq_printf(s, "# Duration: %s\n", duration); |
| 245 |
|
| 246 |
if (!params->common.hist.no_index) |
| 247 |
trace_seq_printf(s, "Index"); |
| 248 |
|
| 249 |
for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { |
| 250 |
|
| 251 |
if (!data->hist[cpu].count) |
| 252 |
continue; |
| 253 |
|
| 254 |
trace_seq_printf(s, " CPU-%03d", cpu); |
| 255 |
} |
| 256 |
trace_seq_printf(s, "\n"); |
| 257 |
|
| 258 |
trace_seq_do_printf(s); |
| 259 |
trace_seq_reset(s); |
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
* osnoise_print_summary - print the summary of the hist data to the output |
| 264 |
*/ |
| 265 |
static void |
| 266 |
osnoise_print_summary(struct osnoise_params *params, |
| 267 |
struct trace_instance *trace, |
| 268 |
struct osnoise_hist_data *data) |
| 269 |
{ |
| 270 |
int cpu; |
| 271 |
|
| 272 |
if (params->common.hist.no_summary) |
| 273 |
return; |
| 274 |
|
| 275 |
if (!params->common.hist.no_index) |
| 276 |
trace_seq_printf(trace->seq, "count:"); |
| 277 |
|
| 278 |
for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { |
| 279 |
|
| 280 |
if (!data->hist[cpu].count) |
| 281 |
continue; |
| 282 |
|
| 283 |
trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count); |
| 284 |
} |
| 285 |
trace_seq_printf(trace->seq, "\n"); |
| 286 |
|
| 287 |
if (!params->common.hist.no_index) |
| 288 |
trace_seq_printf(trace->seq, "min: "); |
| 289 |
|
| 290 |
for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { |
| 291 |
|
| 292 |
if (!data->hist[cpu].count) |
| 293 |
continue; |
| 294 |
|
| 295 |
trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample); |
| 296 |
|
| 297 |
} |
| 298 |
trace_seq_printf(trace->seq, "\n"); |
| 299 |
|
| 300 |
if (!params->common.hist.no_index) |
| 301 |
trace_seq_printf(trace->seq, "avg: "); |
| 302 |
|
| 303 |
for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { |
| 304 |
|
| 305 |
if (!data->hist[cpu].count) |
| 306 |
continue; |
| 307 |
|
| 308 |
if (data->hist[cpu].count) |
| 309 |
trace_seq_printf(trace->seq, "%9.2f ", |
| 310 |
((double) data->hist[cpu].sum_sample) / data->hist[cpu].count); |
| 311 |
else |
| 312 |
trace_seq_printf(trace->seq, " - "); |
| 313 |
} |
| 314 |
trace_seq_printf(trace->seq, "\n"); |
| 315 |
|
| 316 |
if (!params->common.hist.no_index) |
| 317 |
trace_seq_printf(trace->seq, "max: "); |
| 318 |
|
| 319 |
for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { |
| 320 |
|
| 321 |
if (!data->hist[cpu].count) |
| 322 |
continue; |
| 323 |
|
| 324 |
trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample); |
| 325 |
|
| 326 |
} |
| 327 |
trace_seq_printf(trace->seq, "\n"); |
| 328 |
trace_seq_do_printf(trace->seq); |
| 329 |
trace_seq_reset(trace->seq); |
| 330 |
} |
| 331 |
|
| 332 |
/* |
| 333 |
* osnoise_print_stats - print data for all CPUs |
| 334 |
*/ |
| 335 |
static void |
| 336 |
osnoise_print_stats(struct osnoise_tool *tool) |
| 337 |
{ |
| 338 |
struct osnoise_params *params = to_osnoise_params(tool->params); |
| 339 |
struct osnoise_hist_data *data = tool->data; |
| 340 |
struct trace_instance *trace = &tool->trace; |
| 341 |
int has_samples = 0; |
| 342 |
int bucket, cpu; |
| 343 |
int total; |
| 344 |
|
| 345 |
osnoise_hist_header(tool); |
| 346 |
|
| 347 |
for (bucket = 0; bucket < data->entries; bucket++) { |
| 348 |
total = 0; |
| 349 |
|
| 350 |
if (!params->common.hist.no_index) |
| 351 |
trace_seq_printf(trace->seq, "%-6d", |
| 352 |
bucket * data->bucket_size); |
| 353 |
|
| 354 |
for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { |
| 355 |
|
| 356 |
if (!data->hist[cpu].count) |
| 357 |
continue; |
| 358 |
|
| 359 |
total += data->hist[cpu].samples[bucket]; |
| 360 |
trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]); |
| 361 |
} |
| 362 |
|
| 363 |
if (total == 0 && !params->common.hist.with_zeros) { |
| 364 |
trace_seq_reset(trace->seq); |
| 365 |
continue; |
| 366 |
} |
| 367 |
|
| 368 |
/* There are samples above the threshold */ |
| 369 |
has_samples = 1; |
| 370 |
trace_seq_printf(trace->seq, "\n"); |
| 371 |
trace_seq_do_printf(trace->seq); |
| 372 |
trace_seq_reset(trace->seq); |
| 373 |
} |
| 374 |
|
| 375 |
/* |
| 376 |
* If no samples were recorded, skip calculations, print zeroed statistics |
| 377 |
* and return. |
| 378 |
*/ |
| 379 |
if (!has_samples) { |
| 380 |
trace_seq_reset(trace->seq); |
| 381 |
trace_seq_printf(trace->seq, "over: 0\ncount: 0\nmin: 0\navg: 0\nmax: 0\n"); |
| 382 |
trace_seq_do_printf(trace->seq); |
| 383 |
trace_seq_reset(trace->seq); |
| 384 |
return; |
| 385 |
} |
| 386 |
|
| 387 |
if (!params->common.hist.no_index) |
| 388 |
trace_seq_printf(trace->seq, "over: "); |
| 389 |
|
| 390 |
for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { |
| 391 |
|
| 392 |
if (!data->hist[cpu].count) |
| 393 |
continue; |
| 394 |
|
| 395 |
trace_seq_printf(trace->seq, "%9d ", |
| 396 |
data->hist[cpu].samples[data->entries]); |
| 397 |
} |
| 398 |
trace_seq_printf(trace->seq, "\n"); |
| 399 |
trace_seq_do_printf(trace->seq); |
| 400 |
trace_seq_reset(trace->seq); |
| 401 |
|
| 402 |
osnoise_print_summary(params, trace, data); |
| 403 |
osnoise_report_missed_events(tool); |
| 404 |
} |
| 405 |
|
| 406 |
/* |
| 407 |
* osnoise_hist_usage - prints osnoise hist usage message |
| 408 |
*/ |
| 409 |
static void osnoise_hist_usage(void) |
| 410 |
{ |
| 411 |
static const char * const msg_start[] = { |
| 412 |
"[-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\", |
| 413 |
" [-T us] [-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\", |
| 414 |
" [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\", |
| 415 |
" [--no-index] [--with-zeros] [-C [cgroup_name]] [--warm-up]", |
| 416 |
NULL, |
| 417 |
}; |
| 418 |
|
| 419 |
static const char * const msg_opts[] = { |
| 420 |
" -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit", |
| 421 |
" -p/--period us: osnoise period in us", |
| 422 |
" -r/--runtime us: osnoise runtime in us", |
| 423 |
" -s/--stop us: stop trace if a single sample is higher than the argument in us", |
| 424 |
" -S/--stop-total us: stop trace if the total sample is higher than the argument in us", |
| 425 |
" -T/--threshold us: the minimum delta to be considered a noise", |
| 426 |
" -c/--cpus cpu-list: list of cpus to run osnoise threads", |
| 427 |
" -H/--house-keeping cpus: run rtla control threads only on the given cpus", |
| 428 |
" -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited", |
| 429 |
" -d/--duration time[s|m|h|d]: duration of the session", |
| 430 |
" -D/--debug: print debug info", |
| 431 |
" -t/--trace [file]: save the stopped trace to [file|osnoise_trace.txt]", |
| 432 |
" -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", |
| 433 |
" --filter <filter>: enable a trace event filter to the previous -e event", |
| 434 |
" --trigger <trigger>: enable a trace event trigger to the previous -e event", |
| 435 |
" -b/--bucket-size N: set the histogram bucket size (default 1)", |
| 436 |
" -E/--entries N: set the number of entries of the histogram (default 256)", |
| 437 |
" --no-header: do not print header", |
| 438 |
" --no-summary: do not print summary", |
| 439 |
" --no-index: do not print index", |
| 440 |
" --with-zeros: print zero only entries", |
| 441 |
" -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters", |
| 442 |
" o:prio - use SCHED_OTHER with prio", |
| 443 |
" r:prio - use SCHED_RR with prio", |
| 444 |
" f:prio - use SCHED_FIFO with prio", |
| 445 |
" d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period", |
| 446 |
" in nanoseconds", |
| 447 |
" --warm-up: let the workload run for s seconds before collecting data", |
| 448 |
" --trace-buffer-size kB: set the per-cpu trace buffer size in kB", |
| 449 |
" --on-threshold <action>: define action to be executed at stop-total threshold, multiple are allowed", |
| 450 |
" --on-end <action>: define action to be executed at measurement end, multiple are allowed", |
| 451 |
NULL, |
| 452 |
}; |
| 453 |
|
| 454 |
common_usage("osnoise", "hist", "a per-cpu histogram of the OS noise", |
| 455 |
msg_start, msg_opts); |
| 456 |
} |
| 457 |
|
| 458 |
/* |
| 459 |
* osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters |
| 460 |
*/ |
| 461 |
static struct common_params |
| 462 |
*osnoise_hist_parse_args(int argc, char *argv[]) |
| 463 |
{ |
| 464 |
struct osnoise_params *params; |
| 465 |
int retval; |
| 466 |
int c; |
| 467 |
char *trace_output = NULL; |
| 468 |
|
| 469 |
params = calloc(1, sizeof(*params)); |
| 470 |
if (!params) |
| 471 |
exit(1); |
| 472 |
|
| 473 |
actions_init(¶ms->common.threshold_actions); |
| 474 |
actions_init(¶ms->common.end_actions); |
| 475 |
|
| 476 |
/* display data in microseconds */ |
| 477 |
params->common.output_divisor = 1000; |
| 478 |
params->common.hist.bucket_size = 1; |
| 479 |
params->common.hist.entries = 256; |
| 480 |
|
| 481 |
while (1) { |
| 482 |
static struct option long_options[] = { |
| 483 |
{"auto", required_argument, 0, 'a'}, |
| 484 |
{"bucket-size", required_argument, 0, 'b'}, |
| 485 |
{"entries", required_argument, 0, 'E'}, |
| 486 |
{"help", no_argument, 0, 'h'}, |
| 487 |
{"period", required_argument, 0, 'p'}, |
| 488 |
{"runtime", required_argument, 0, 'r'}, |
| 489 |
{"stop", required_argument, 0, 's'}, |
| 490 |
{"stop-total", required_argument, 0, 'S'}, |
| 491 |
{"trace", optional_argument, 0, 't'}, |
| 492 |
{"threshold", required_argument, 0, 'T'}, |
| 493 |
{"no-header", no_argument, 0, '0'}, |
| 494 |
{"no-summary", no_argument, 0, '1'}, |
| 495 |
{"no-index", no_argument, 0, '2'}, |
| 496 |
{"with-zeros", no_argument, 0, '3'}, |
| 497 |
{"trigger", required_argument, 0, '4'}, |
| 498 |
{"filter", required_argument, 0, '5'}, |
| 499 |
{"warm-up", required_argument, 0, '6'}, |
| 500 |
{"trace-buffer-size", required_argument, 0, '7'}, |
| 501 |
{"on-threshold", required_argument, 0, '8'}, |
| 502 |
{"on-end", required_argument, 0, '9'}, |
| 503 |
{0, 0, 0, 0} |
| 504 |
}; |
| 505 |
|
| 506 |
if (common_parse_options(argc, argv, ¶ms->common)) |
| 507 |
continue; |
| 508 |
|
| 509 |
c = getopt_auto(argc, argv, long_options); |
| 510 |
|
| 511 |
/* detect the end of the options. */ |
| 512 |
if (c == -1) |
| 513 |
break; |
| 514 |
|
| 515 |
switch (c) { |
| 516 |
case 'a': |
| 517 |
/* set sample stop to auto_thresh */ |
| 518 |
params->common.stop_us = get_llong_from_str(optarg); |
| 519 |
|
| 520 |
/* set sample threshold to 1 */ |
| 521 |
params->threshold = 1; |
| 522 |
|
| 523 |
/* set trace */ |
| 524 |
if (!trace_output) |
| 525 |
trace_output = "osnoise_trace.txt"; |
| 526 |
|
| 527 |
break; |
| 528 |
case 'b': |
| 529 |
params->common.hist.bucket_size = get_llong_from_str(optarg); |
| 530 |
if (params->common.hist.bucket_size == 0 || |
| 531 |
params->common.hist.bucket_size >= 1000000) |
| 532 |
fatal("Bucket size needs to be > 0 and <= 1000000"); |
| 533 |
break; |
| 534 |
case 'E': |
| 535 |
params->common.hist.entries = get_llong_from_str(optarg); |
| 536 |
if (params->common.hist.entries < 10 || |
| 537 |
params->common.hist.entries > 9999999) |
| 538 |
fatal("Entries must be > 10 and < 9999999"); |
| 539 |
break; |
| 540 |
case 'h': |
| 541 |
case '?': |
| 542 |
osnoise_hist_usage(); |
| 543 |
break; |
| 544 |
case 'p': |
| 545 |
params->period = get_llong_from_str(optarg); |
| 546 |
if (params->period > 10000000) |
| 547 |
fatal("Period longer than 10 s"); |
| 548 |
break; |
| 549 |
case 'r': |
| 550 |
params->runtime = get_llong_from_str(optarg); |
| 551 |
if (params->runtime < 100) |
| 552 |
fatal("Runtime shorter than 100 us"); |
| 553 |
break; |
| 554 |
case 's': |
| 555 |
params->common.stop_us = get_llong_from_str(optarg); |
| 556 |
break; |
| 557 |
case 'S': |
| 558 |
params->common.stop_total_us = get_llong_from_str(optarg); |
| 559 |
break; |
| 560 |
case 'T': |
| 561 |
params->threshold = get_llong_from_str(optarg); |
| 562 |
break; |
| 563 |
case 't': |
| 564 |
trace_output = parse_optional_arg(argc, argv); |
| 565 |
if (!trace_output) |
| 566 |
trace_output = "osnoise_trace.txt"; |
| 567 |
break; |
| 568 |
case '0': /* no header */ |
| 569 |
params->common.hist.no_header = 1; |
| 570 |
break; |
| 571 |
case '1': /* no summary */ |
| 572 |
params->common.hist.no_summary = 1; |
| 573 |
break; |
| 574 |
case '2': /* no index */ |
| 575 |
params->common.hist.no_index = 1; |
| 576 |
break; |
| 577 |
case '3': /* with zeros */ |
| 578 |
params->common.hist.with_zeros = 1; |
| 579 |
break; |
| 580 |
case '4': /* trigger */ |
| 581 |
if (params->common.events) { |
| 582 |
retval = trace_event_add_trigger(params->common.events, optarg); |
| 583 |
if (retval) |
| 584 |
fatal("Error adding trigger %s", optarg); |
| 585 |
} else { |
| 586 |
fatal("--trigger requires a previous -e"); |
| 587 |
} |
| 588 |
break; |
| 589 |
case '5': /* filter */ |
| 590 |
if (params->common.events) { |
| 591 |
retval = trace_event_add_filter(params->common.events, optarg); |
| 592 |
if (retval) |
| 593 |
fatal("Error adding filter %s", optarg); |
| 594 |
} else { |
| 595 |
fatal("--filter requires a previous -e"); |
| 596 |
} |
| 597 |
break; |
| 598 |
case '6': |
| 599 |
params->common.warmup = get_llong_from_str(optarg); |
| 600 |
break; |
| 601 |
case '7': |
| 602 |
params->common.buffer_size = get_llong_from_str(optarg); |
| 603 |
break; |
| 604 |
case '8': |
| 605 |
retval = actions_parse(¶ms->common.threshold_actions, optarg, |
| 606 |
"osnoise_trace.txt"); |
| 607 |
if (retval) |
| 608 |
fatal("Invalid action %s", optarg); |
| 609 |
break; |
| 610 |
case '9': |
| 611 |
retval = actions_parse(¶ms->common.end_actions, optarg, |
| 612 |
"osnoise_trace.txt"); |
| 613 |
if (retval) |
| 614 |
fatal("Invalid action %s", optarg); |
| 615 |
break; |
| 616 |
default: |
| 617 |
fatal("Invalid option"); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
if (trace_output) |
| 622 |
actions_add_trace_output(¶ms->common.threshold_actions, trace_output); |
| 623 |
|
| 624 |
if (geteuid()) |
| 625 |
fatal("rtla needs root permission"); |
| 626 |
|
| 627 |
if (params->common.hist.no_index && !params->common.hist.with_zeros) |
| 628 |
fatal("no-index set and with-zeros not set - it does not make sense"); |
| 629 |
|
| 630 |
return ¶ms->common; |
| 631 |
} |
| 632 |
|
| 633 |
/* |
| 634 |
* osnoise_hist_apply_config - apply the hist configs to the initialized tool |
| 635 |
*/ |
| 636 |
static int |
| 637 |
osnoise_hist_apply_config(struct osnoise_tool *tool) |
| 638 |
{ |
| 639 |
return osnoise_apply_config(tool, to_osnoise_params(tool->params)); |
| 640 |
} |
| 641 |
|
| 642 |
/* |
| 643 |
* osnoise_init_hist - initialize a osnoise hist tool with parameters |
| 644 |
*/ |
| 645 |
static struct osnoise_tool |
| 646 |
*osnoise_init_hist(struct common_params *params) |
| 647 |
{ |
| 648 |
struct osnoise_tool *tool; |
| 649 |
int nr_cpus; |
| 650 |
|
| 651 |
nr_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 652 |
|
| 653 |
tool = osnoise_init_tool("osnoise_hist"); |
| 654 |
if (!tool) |
| 655 |
return NULL; |
| 656 |
|
| 657 |
tool->data = osnoise_alloc_histogram(nr_cpus, params->hist.entries, |
| 658 |
params->hist.bucket_size); |
| 659 |
if (!tool->data) |
| 660 |
goto out_err; |
| 661 |
|
| 662 |
return tool; |
| 663 |
|
| 664 |
out_err: |
| 665 |
osnoise_destroy_tool(tool); |
| 666 |
return NULL; |
| 667 |
} |
| 668 |
|
| 669 |
static int osnoise_hist_enable(struct osnoise_tool *tool) |
| 670 |
{ |
| 671 |
int retval; |
| 672 |
|
| 673 |
retval = osnoise_init_trace_hist(tool); |
| 674 |
if (retval) |
| 675 |
return retval; |
| 676 |
|
| 677 |
return osnoise_enable(tool); |
| 678 |
} |
| 679 |
|
| 680 |
static int osnoise_hist_main_loop(struct osnoise_tool *tool) |
| 681 |
{ |
| 682 |
int retval; |
| 683 |
|
| 684 |
retval = hist_main_loop(tool); |
| 685 |
osnoise_read_trace_hist(tool); |
| 686 |
|
| 687 |
return retval; |
| 688 |
} |
| 689 |
|
| 690 |
struct tool_ops osnoise_hist_ops = { |
| 691 |
.tracer = "osnoise", |
| 692 |
.comm_prefix = "osnoise/", |
| 693 |
.parse_args = osnoise_hist_parse_args, |
| 694 |
.init_tool = osnoise_init_hist, |
| 695 |
.apply_config = osnoise_hist_apply_config, |
| 696 |
.enable = osnoise_hist_enable, |
| 697 |
.main = osnoise_hist_main_loop, |
| 698 |
.print_stats = osnoise_print_stats, |
| 699 |
.free = osnoise_free_hist_tool, |
| 700 |
}; |