Path: blob/master/tools/tracing/rtla/src/osnoise_hist.c
50647 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <[email protected]>3*/45#define _GNU_SOURCE6#include <getopt.h>7#include <stdlib.h>8#include <string.h>9#include <signal.h>10#include <unistd.h>11#include <errno.h>12#include <stdio.h>13#include <time.h>1415#include "osnoise.h"1617struct osnoise_hist_cpu {18int *samples;19int count;2021unsigned long long min_sample;22unsigned long long sum_sample;23unsigned long long max_sample;2425};2627struct osnoise_hist_data {28struct tracefs_hist *trace_hist;29struct osnoise_hist_cpu *hist;30int entries;31int bucket_size;32int nr_cpus;33};3435/*36* osnoise_free_histogram - free runtime data37*/38static void39osnoise_free_histogram(struct osnoise_hist_data *data)40{41int cpu;4243/* one histogram for IRQ and one for thread, per CPU */44for (cpu = 0; cpu < data->nr_cpus; cpu++) {45if (data->hist[cpu].samples)46free(data->hist[cpu].samples);47}4849/* one set of histograms per CPU */50if (data->hist)51free(data->hist);5253free(data);54}5556static void osnoise_free_hist_tool(struct osnoise_tool *tool)57{58osnoise_free_histogram(tool->data);59}6061/*62* osnoise_alloc_histogram - alloc runtime data63*/64static struct osnoise_hist_data65*osnoise_alloc_histogram(int nr_cpus, int entries, int bucket_size)66{67struct osnoise_hist_data *data;68int cpu;6970data = calloc(1, sizeof(*data));71if (!data)72return NULL;7374data->entries = entries;75data->bucket_size = bucket_size;76data->nr_cpus = nr_cpus;7778data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);79if (!data->hist)80goto cleanup;8182for (cpu = 0; cpu < nr_cpus; cpu++) {83data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1));84if (!data->hist[cpu].samples)85goto cleanup;86}8788/* set the min to max */89for (cpu = 0; cpu < nr_cpus; cpu++)90data->hist[cpu].min_sample = ~0;9192return data;9394cleanup:95osnoise_free_histogram(data);96return NULL;97}9899static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu,100unsigned long long duration, int count)101{102struct osnoise_params *params = to_osnoise_params(tool->params);103struct osnoise_hist_data *data = tool->data;104unsigned long long total_duration;105int entries = data->entries;106int bucket;107int *hist;108109if (params->common.output_divisor)110duration = duration / params->common.output_divisor;111112bucket = duration / data->bucket_size;113114total_duration = duration * count;115116hist = data->hist[cpu].samples;117data->hist[cpu].count += count;118update_min(&data->hist[cpu].min_sample, &duration);119update_sum(&data->hist[cpu].sum_sample, &total_duration);120update_max(&data->hist[cpu].max_sample, &duration);121122if (bucket < entries)123hist[bucket] += count;124else125hist[entries] += count;126}127128/*129* osnoise_destroy_trace_hist - disable events used to collect histogram130*/131static void osnoise_destroy_trace_hist(struct osnoise_tool *tool)132{133struct osnoise_hist_data *data = tool->data;134135tracefs_hist_pause(tool->trace.inst, data->trace_hist);136tracefs_hist_destroy(tool->trace.inst, data->trace_hist);137}138139/*140* osnoise_init_trace_hist - enable events used to collect histogram141*/142static int osnoise_init_trace_hist(struct osnoise_tool *tool)143{144struct osnoise_params *params = to_osnoise_params(tool->params);145struct osnoise_hist_data *data = tool->data;146int bucket_size;147char buff[128];148int retval = 0;149150/*151* Set the size of the bucket.152*/153bucket_size = params->common.output_divisor * params->common.hist.bucket_size;154snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size);155156data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold",157buff, TRACEFS_HIST_KEY_NORMAL);158if (!data->trace_hist)159return 1;160161retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0);162if (retval)163goto out_err;164165retval = tracefs_hist_start(tool->trace.inst, data->trace_hist);166if (retval)167goto out_err;168169return 0;170171out_err:172osnoise_destroy_trace_hist(tool);173return 1;174}175176/*177* osnoise_read_trace_hist - parse histogram file and file osnoise histogram178*/179static void osnoise_read_trace_hist(struct osnoise_tool *tool)180{181struct osnoise_hist_data *data = tool->data;182long long cpu, counter, duration;183char *content, *position;184185tracefs_hist_pause(tool->trace.inst, data->trace_hist);186187content = tracefs_event_file_read(tool->trace.inst, "osnoise",188"sample_threshold",189"hist", NULL);190if (!content)191return;192193position = content;194while (true) {195position = strstr(position, "duration: ~");196if (!position)197break;198position += strlen("duration: ~");199duration = get_llong_from_str(position);200if (duration == -1)201err_msg("error reading duration from histogram\n");202203position = strstr(position, "cpu:");204if (!position)205break;206position += strlen("cpu: ");207cpu = get_llong_from_str(position);208if (cpu == -1)209err_msg("error reading cpu from histogram\n");210211position = strstr(position, "hitcount:");212if (!position)213break;214position += strlen("hitcount: ");215counter = get_llong_from_str(position);216if (counter == -1)217err_msg("error reading counter from histogram\n");218219osnoise_hist_update_multiple(tool, cpu, duration, counter);220}221free(content);222}223224/*225* osnoise_hist_header - print the header of the tracer to the output226*/227static void osnoise_hist_header(struct osnoise_tool *tool)228{229struct osnoise_params *params = to_osnoise_params(tool->params);230struct osnoise_hist_data *data = tool->data;231struct trace_seq *s = tool->trace.seq;232char duration[26];233int cpu;234235if (params->common.hist.no_header)236return;237238get_duration(tool->start_time, duration, sizeof(duration));239trace_seq_printf(s, "# RTLA osnoise histogram\n");240trace_seq_printf(s, "# Time unit is %s (%s)\n",241params->common.output_divisor == 1 ? "nanoseconds" : "microseconds",242params->common.output_divisor == 1 ? "ns" : "us");243244trace_seq_printf(s, "# Duration: %s\n", duration);245246if (!params->common.hist.no_index)247trace_seq_printf(s, "Index");248249for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {250251if (!data->hist[cpu].count)252continue;253254trace_seq_printf(s, " CPU-%03d", cpu);255}256trace_seq_printf(s, "\n");257258trace_seq_do_printf(s);259trace_seq_reset(s);260}261262/*263* osnoise_print_summary - print the summary of the hist data to the output264*/265static void266osnoise_print_summary(struct osnoise_params *params,267struct trace_instance *trace,268struct osnoise_hist_data *data)269{270int cpu;271272if (params->common.hist.no_summary)273return;274275if (!params->common.hist.no_index)276trace_seq_printf(trace->seq, "count:");277278for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {279280if (!data->hist[cpu].count)281continue;282283trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count);284}285trace_seq_printf(trace->seq, "\n");286287if (!params->common.hist.no_index)288trace_seq_printf(trace->seq, "min: ");289290for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {291292if (!data->hist[cpu].count)293continue;294295trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample);296297}298trace_seq_printf(trace->seq, "\n");299300if (!params->common.hist.no_index)301trace_seq_printf(trace->seq, "avg: ");302303for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {304305if (!data->hist[cpu].count)306continue;307308if (data->hist[cpu].count)309trace_seq_printf(trace->seq, "%9.2f ",310((double) data->hist[cpu].sum_sample) / data->hist[cpu].count);311else312trace_seq_printf(trace->seq, " - ");313}314trace_seq_printf(trace->seq, "\n");315316if (!params->common.hist.no_index)317trace_seq_printf(trace->seq, "max: ");318319for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {320321if (!data->hist[cpu].count)322continue;323324trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample);325326}327trace_seq_printf(trace->seq, "\n");328trace_seq_do_printf(trace->seq);329trace_seq_reset(trace->seq);330}331332/*333* osnoise_print_stats - print data for all CPUs334*/335static void336osnoise_print_stats(struct osnoise_tool *tool)337{338struct osnoise_params *params = to_osnoise_params(tool->params);339struct osnoise_hist_data *data = tool->data;340struct trace_instance *trace = &tool->trace;341int has_samples = 0;342int bucket, cpu;343int total;344345osnoise_hist_header(tool);346347for (bucket = 0; bucket < data->entries; bucket++) {348total = 0;349350if (!params->common.hist.no_index)351trace_seq_printf(trace->seq, "%-6d",352bucket * data->bucket_size);353354for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {355356if (!data->hist[cpu].count)357continue;358359total += data->hist[cpu].samples[bucket];360trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]);361}362363if (total == 0 && !params->common.hist.with_zeros) {364trace_seq_reset(trace->seq);365continue;366}367368/* There are samples above the threshold */369has_samples = 1;370trace_seq_printf(trace->seq, "\n");371trace_seq_do_printf(trace->seq);372trace_seq_reset(trace->seq);373}374375/*376* If no samples were recorded, skip calculations, print zeroed statistics377* and return.378*/379if (!has_samples) {380trace_seq_reset(trace->seq);381trace_seq_printf(trace->seq, "over: 0\ncount: 0\nmin: 0\navg: 0\nmax: 0\n");382trace_seq_do_printf(trace->seq);383trace_seq_reset(trace->seq);384return;385}386387if (!params->common.hist.no_index)388trace_seq_printf(trace->seq, "over: ");389390for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {391392if (!data->hist[cpu].count)393continue;394395trace_seq_printf(trace->seq, "%9d ",396data->hist[cpu].samples[data->entries]);397}398trace_seq_printf(trace->seq, "\n");399trace_seq_do_printf(trace->seq);400trace_seq_reset(trace->seq);401402osnoise_print_summary(params, trace, data);403osnoise_report_missed_events(tool);404}405406/*407* osnoise_hist_usage - prints osnoise hist usage message408*/409static void osnoise_hist_usage(void)410{411int i;412413static const char * const msg[] = {414"",415" usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",416" [-T us] [-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",417" [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\",418" [--no-index] [--with-zeros] [-C [cgroup_name]] [--warm-up]",419"",420" -h/--help: print this menu",421" -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",422" -p/--period us: osnoise period in us",423" -r/--runtime us: osnoise runtime in us",424" -s/--stop us: stop trace if a single sample is higher than the argument in us",425" -S/--stop-total us: stop trace if the total sample is higher than the argument in us",426" -T/--threshold us: the minimum delta to be considered a noise",427" -c/--cpus cpu-list: list of cpus to run osnoise threads",428" -H/--house-keeping cpus: run rtla control threads only on the given cpus",429" -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",430" -d/--duration time[s|m|h|d]: duration of the session",431" -D/--debug: print debug info",432" -t/--trace [file]: save the stopped trace to [file|osnoise_trace.txt]",433" -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",434" --filter <filter>: enable a trace event filter to the previous -e event",435" --trigger <trigger>: enable a trace event trigger to the previous -e event",436" -b/--bucket-size N: set the histogram bucket size (default 1)",437" -E/--entries N: set the number of entries of the histogram (default 256)",438" --no-header: do not print header",439" --no-summary: do not print summary",440" --no-index: do not print index",441" --with-zeros: print zero only entries",442" -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters",443" o:prio - use SCHED_OTHER with prio",444" r:prio - use SCHED_RR with prio",445" f:prio - use SCHED_FIFO with prio",446" d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",447" in nanoseconds",448" --warm-up: let the workload run for s seconds before collecting data",449" --trace-buffer-size kB: set the per-cpu trace buffer size in kB",450" --on-threshold <action>: define action to be executed at stop-total threshold, multiple are allowed",451" --on-end <action>: define action to be executed at measurement end, multiple are allowed",452NULL,453};454455fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n",456VERSION);457458for (i = 0; msg[i]; i++)459fprintf(stderr, "%s\n", msg[i]);460461exit(EXIT_SUCCESS);462}463464/*465* osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters466*/467static struct common_params468*osnoise_hist_parse_args(int argc, char *argv[])469{470struct osnoise_params *params;471struct trace_events *tevent;472int retval;473int c;474char *trace_output = NULL;475476params = calloc(1, sizeof(*params));477if (!params)478exit(1);479480actions_init(¶ms->common.threshold_actions);481actions_init(¶ms->common.end_actions);482483/* display data in microseconds */484params->common.output_divisor = 1000;485params->common.hist.bucket_size = 1;486params->common.hist.entries = 256;487488while (1) {489static struct option long_options[] = {490{"auto", required_argument, 0, 'a'},491{"bucket-size", required_argument, 0, 'b'},492{"entries", required_argument, 0, 'E'},493{"cpus", required_argument, 0, 'c'},494{"cgroup", optional_argument, 0, 'C'},495{"debug", no_argument, 0, 'D'},496{"duration", required_argument, 0, 'd'},497{"house-keeping", required_argument, 0, 'H'},498{"help", no_argument, 0, 'h'},499{"period", required_argument, 0, 'p'},500{"priority", required_argument, 0, 'P'},501{"runtime", required_argument, 0, 'r'},502{"stop", required_argument, 0, 's'},503{"stop-total", required_argument, 0, 'S'},504{"trace", optional_argument, 0, 't'},505{"event", required_argument, 0, 'e'},506{"threshold", required_argument, 0, 'T'},507{"no-header", no_argument, 0, '0'},508{"no-summary", no_argument, 0, '1'},509{"no-index", no_argument, 0, '2'},510{"with-zeros", no_argument, 0, '3'},511{"trigger", required_argument, 0, '4'},512{"filter", required_argument, 0, '5'},513{"warm-up", required_argument, 0, '6'},514{"trace-buffer-size", required_argument, 0, '7'},515{"on-threshold", required_argument, 0, '8'},516{"on-end", required_argument, 0, '9'},517{0, 0, 0, 0}518};519520c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:p:P:r:s:S:t::T:01234:5:6:7:",521long_options, NULL);522523/* detect the end of the options. */524if (c == -1)525break;526527switch (c) {528case 'a':529/* set sample stop to auto_thresh */530params->common.stop_us = get_llong_from_str(optarg);531532/* set sample threshold to 1 */533params->threshold = 1;534535/* set trace */536if (!trace_output)537trace_output = "osnoise_trace.txt";538539break;540case 'b':541params->common.hist.bucket_size = get_llong_from_str(optarg);542if (params->common.hist.bucket_size == 0 ||543params->common.hist.bucket_size >= 1000000)544fatal("Bucket size needs to be > 0 and <= 1000000");545break;546case 'c':547retval = parse_cpu_set(optarg, ¶ms->common.monitored_cpus);548if (retval)549fatal("Invalid -c cpu list");550params->common.cpus = optarg;551break;552case 'C':553params->common.cgroup = 1;554params->common.cgroup_name = parse_optional_arg(argc, argv);555break;556case 'D':557config_debug = 1;558break;559case 'd':560params->common.duration = parse_seconds_duration(optarg);561if (!params->common.duration)562fatal("Invalid -D duration");563break;564case 'e':565tevent = trace_event_alloc(optarg);566if (!tevent)567fatal("Error alloc trace event");568569if (params->common.events)570tevent->next = params->common.events;571572params->common.events = tevent;573break;574case 'E':575params->common.hist.entries = get_llong_from_str(optarg);576if (params->common.hist.entries < 10 ||577params->common.hist.entries > 9999999)578fatal("Entries must be > 10 and < 9999999");579break;580case 'h':581case '?':582osnoise_hist_usage();583break;584case 'H':585params->common.hk_cpus = 1;586retval = parse_cpu_set(optarg, ¶ms->common.hk_cpu_set);587if (retval)588fatal("Error parsing house keeping CPUs");589break;590case 'p':591params->period = get_llong_from_str(optarg);592if (params->period > 10000000)593fatal("Period longer than 10 s");594break;595case 'P':596retval = parse_prio(optarg, ¶ms->common.sched_param);597if (retval == -1)598fatal("Invalid -P priority");599params->common.set_sched = 1;600break;601case 'r':602params->runtime = get_llong_from_str(optarg);603if (params->runtime < 100)604fatal("Runtime shorter than 100 us");605break;606case 's':607params->common.stop_us = get_llong_from_str(optarg);608break;609case 'S':610params->common.stop_total_us = get_llong_from_str(optarg);611break;612case 'T':613params->threshold = get_llong_from_str(optarg);614break;615case 't':616trace_output = parse_optional_arg(argc, argv);617if (!trace_output)618trace_output = "osnoise_trace.txt";619break;620case '0': /* no header */621params->common.hist.no_header = 1;622break;623case '1': /* no summary */624params->common.hist.no_summary = 1;625break;626case '2': /* no index */627params->common.hist.no_index = 1;628break;629case '3': /* with zeros */630params->common.hist.with_zeros = 1;631break;632case '4': /* trigger */633if (params->common.events) {634retval = trace_event_add_trigger(params->common.events, optarg);635if (retval)636fatal("Error adding trigger %s", optarg);637} else {638fatal("--trigger requires a previous -e");639}640break;641case '5': /* filter */642if (params->common.events) {643retval = trace_event_add_filter(params->common.events, optarg);644if (retval)645fatal("Error adding filter %s", optarg);646} else {647fatal("--filter requires a previous -e");648}649break;650case '6':651params->common.warmup = get_llong_from_str(optarg);652break;653case '7':654params->common.buffer_size = get_llong_from_str(optarg);655break;656case '8':657retval = actions_parse(¶ms->common.threshold_actions, optarg,658"osnoise_trace.txt");659if (retval)660fatal("Invalid action %s", optarg);661break;662case '9':663retval = actions_parse(¶ms->common.end_actions, optarg,664"osnoise_trace.txt");665if (retval)666fatal("Invalid action %s", optarg);667break;668default:669fatal("Invalid option");670}671}672673if (trace_output)674actions_add_trace_output(¶ms->common.threshold_actions, trace_output);675676if (geteuid())677fatal("rtla needs root permission");678679if (params->common.hist.no_index && !params->common.hist.with_zeros)680fatal("no-index set and with-zeros not set - it does not make sense");681682return ¶ms->common;683}684685/*686* osnoise_hist_apply_config - apply the hist configs to the initialized tool687*/688static int689osnoise_hist_apply_config(struct osnoise_tool *tool)690{691return osnoise_apply_config(tool, to_osnoise_params(tool->params));692}693694/*695* osnoise_init_hist - initialize a osnoise hist tool with parameters696*/697static struct osnoise_tool698*osnoise_init_hist(struct common_params *params)699{700struct osnoise_tool *tool;701int nr_cpus;702703nr_cpus = sysconf(_SC_NPROCESSORS_CONF);704705tool = osnoise_init_tool("osnoise_hist");706if (!tool)707return NULL;708709tool->data = osnoise_alloc_histogram(nr_cpus, params->hist.entries,710params->hist.bucket_size);711if (!tool->data)712goto out_err;713714return tool;715716out_err:717osnoise_destroy_tool(tool);718return NULL;719}720721static int osnoise_hist_enable(struct osnoise_tool *tool)722{723int retval;724725retval = osnoise_init_trace_hist(tool);726if (retval)727return retval;728729return osnoise_enable(tool);730}731732static int osnoise_hist_main_loop(struct osnoise_tool *tool)733{734int retval;735736retval = hist_main_loop(tool);737osnoise_read_trace_hist(tool);738739return retval;740}741742struct tool_ops osnoise_hist_ops = {743.tracer = "osnoise",744.comm_prefix = "osnoise/",745.parse_args = osnoise_hist_parse_args,746.init_tool = osnoise_init_hist,747.apply_config = osnoise_hist_apply_config,748.enable = osnoise_hist_enable,749.main = osnoise_hist_main_loop,750.print_stats = osnoise_print_stats,751.free = osnoise_free_hist_tool,752};753754755