Path: blob/master/tools/tracing/rtla/src/osnoise_hist.c
29524 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 (cpu = 0; cpu < data->nr_cpus; cpu++) {250if (params->common.cpus && !CPU_ISSET(cpu, ¶ms->common.monitored_cpus))251continue;252253if (!data->hist[cpu].count)254continue;255256trace_seq_printf(s, " CPU-%03d", cpu);257}258trace_seq_printf(s, "\n");259260trace_seq_do_printf(s);261trace_seq_reset(s);262}263264/*265* osnoise_print_summary - print the summary of the hist data to the output266*/267static void268osnoise_print_summary(struct osnoise_params *params,269struct trace_instance *trace,270struct osnoise_hist_data *data)271{272int cpu;273274if (params->common.hist.no_summary)275return;276277if (!params->common.hist.no_index)278trace_seq_printf(trace->seq, "count:");279280for (cpu = 0; cpu < data->nr_cpus; cpu++) {281if (params->common.cpus && !CPU_ISSET(cpu, ¶ms->common.monitored_cpus))282continue;283284if (!data->hist[cpu].count)285continue;286287trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count);288}289trace_seq_printf(trace->seq, "\n");290291if (!params->common.hist.no_index)292trace_seq_printf(trace->seq, "min: ");293294for (cpu = 0; cpu < data->nr_cpus; cpu++) {295if (params->common.cpus && !CPU_ISSET(cpu, ¶ms->common.monitored_cpus))296continue;297298if (!data->hist[cpu].count)299continue;300301trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample);302303}304trace_seq_printf(trace->seq, "\n");305306if (!params->common.hist.no_index)307trace_seq_printf(trace->seq, "avg: ");308309for (cpu = 0; cpu < data->nr_cpus; cpu++) {310if (params->common.cpus && !CPU_ISSET(cpu, ¶ms->common.monitored_cpus))311continue;312313if (!data->hist[cpu].count)314continue;315316if (data->hist[cpu].count)317trace_seq_printf(trace->seq, "%9.2f ",318((double) data->hist[cpu].sum_sample) / data->hist[cpu].count);319else320trace_seq_printf(trace->seq, " - ");321}322trace_seq_printf(trace->seq, "\n");323324if (!params->common.hist.no_index)325trace_seq_printf(trace->seq, "max: ");326327for (cpu = 0; cpu < data->nr_cpus; cpu++) {328if (params->common.cpus && !CPU_ISSET(cpu, ¶ms->common.monitored_cpus))329continue;330331if (!data->hist[cpu].count)332continue;333334trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample);335336}337trace_seq_printf(trace->seq, "\n");338trace_seq_do_printf(trace->seq);339trace_seq_reset(trace->seq);340}341342/*343* osnoise_print_stats - print data for all CPUs344*/345static void346osnoise_print_stats(struct osnoise_tool *tool)347{348struct osnoise_params *params = to_osnoise_params(tool->params);349struct osnoise_hist_data *data = tool->data;350struct trace_instance *trace = &tool->trace;351int has_samples = 0;352int bucket, cpu;353int total;354355osnoise_hist_header(tool);356357for (bucket = 0; bucket < data->entries; bucket++) {358total = 0;359360if (!params->common.hist.no_index)361trace_seq_printf(trace->seq, "%-6d",362bucket * data->bucket_size);363364for (cpu = 0; cpu < data->nr_cpus; cpu++) {365if (params->common.cpus && !CPU_ISSET(cpu, ¶ms->common.monitored_cpus))366continue;367368if (!data->hist[cpu].count)369continue;370371total += data->hist[cpu].samples[bucket];372trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]);373}374375if (total == 0 && !params->common.hist.with_zeros) {376trace_seq_reset(trace->seq);377continue;378}379380/* There are samples above the threshold */381has_samples = 1;382trace_seq_printf(trace->seq, "\n");383trace_seq_do_printf(trace->seq);384trace_seq_reset(trace->seq);385}386387/*388* If no samples were recorded, skip calculations, print zeroed statistics389* and return.390*/391if (!has_samples) {392trace_seq_reset(trace->seq);393trace_seq_printf(trace->seq, "over: 0\ncount: 0\nmin: 0\navg: 0\nmax: 0\n");394trace_seq_do_printf(trace->seq);395trace_seq_reset(trace->seq);396return;397}398399if (!params->common.hist.no_index)400trace_seq_printf(trace->seq, "over: ");401402for (cpu = 0; cpu < data->nr_cpus; cpu++) {403if (params->common.cpus && !CPU_ISSET(cpu, ¶ms->common.monitored_cpus))404continue;405406if (!data->hist[cpu].count)407continue;408409trace_seq_printf(trace->seq, "%9d ",410data->hist[cpu].samples[data->entries]);411}412trace_seq_printf(trace->seq, "\n");413trace_seq_do_printf(trace->seq);414trace_seq_reset(trace->seq);415416osnoise_print_summary(params, trace, data);417osnoise_report_missed_events(tool);418}419420/*421* osnoise_hist_usage - prints osnoise hist usage message422*/423static void osnoise_hist_usage(char *usage)424{425int i;426427static const char * const msg[] = {428"",429" usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",430" [-T us] [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",431" [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\",432" [--no-index] [--with-zeros] [-C[=cgroup_name]] [--warm-up]",433"",434" -h/--help: print this menu",435" -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",436" -p/--period us: osnoise period in us",437" -r/--runtime us: osnoise runtime in us",438" -s/--stop us: stop trace if a single sample is higher than the argument in us",439" -S/--stop-total us: stop trace if the total sample is higher than the argument in us",440" -T/--threshold us: the minimum delta to be considered a noise",441" -c/--cpus cpu-list: list of cpus to run osnoise threads",442" -H/--house-keeping cpus: run rtla control threads only on the given cpus",443" -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",444" -d/--duration time[s|m|h|d]: duration of the session",445" -D/--debug: print debug info",446" -t/--trace[file]: save the stopped trace to [file|osnoise_trace.txt]",447" -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",448" --filter <filter>: enable a trace event filter to the previous -e event",449" --trigger <trigger>: enable a trace event trigger to the previous -e event",450" -b/--bucket-size N: set the histogram bucket size (default 1)",451" -E/--entries N: set the number of entries of the histogram (default 256)",452" --no-header: do not print header",453" --no-summary: do not print summary",454" --no-index: do not print index",455" --with-zeros: print zero only entries",456" -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters",457" o:prio - use SCHED_OTHER with prio",458" r:prio - use SCHED_RR with prio",459" f:prio - use SCHED_FIFO with prio",460" d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",461" in nanoseconds",462" --warm-up: let the workload run for s seconds before collecting data",463" --trace-buffer-size kB: set the per-cpu trace buffer size in kB",464" --on-threshold <action>: define action to be executed at stop-total threshold, multiple are allowed",465" --on-end <action>: define action to be executed at measurement end, multiple are allowed",466NULL,467};468469if (usage)470fprintf(stderr, "%s\n", usage);471472fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n",473VERSION);474475for (i = 0; msg[i]; i++)476fprintf(stderr, "%s\n", msg[i]);477478if (usage)479exit(EXIT_FAILURE);480481exit(EXIT_SUCCESS);482}483484/*485* osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters486*/487static struct common_params488*osnoise_hist_parse_args(int argc, char *argv[])489{490struct osnoise_params *params;491struct trace_events *tevent;492int retval;493int c;494char *trace_output = NULL;495496params = calloc(1, sizeof(*params));497if (!params)498exit(1);499500actions_init(¶ms->common.threshold_actions);501actions_init(¶ms->common.end_actions);502503/* display data in microseconds */504params->common.output_divisor = 1000;505params->common.hist.bucket_size = 1;506params->common.hist.entries = 256;507508while (1) {509static struct option long_options[] = {510{"auto", required_argument, 0, 'a'},511{"bucket-size", required_argument, 0, 'b'},512{"entries", required_argument, 0, 'E'},513{"cpus", required_argument, 0, 'c'},514{"cgroup", optional_argument, 0, 'C'},515{"debug", no_argument, 0, 'D'},516{"duration", required_argument, 0, 'd'},517{"house-keeping", required_argument, 0, 'H'},518{"help", no_argument, 0, 'h'},519{"period", required_argument, 0, 'p'},520{"priority", required_argument, 0, 'P'},521{"runtime", required_argument, 0, 'r'},522{"stop", required_argument, 0, 's'},523{"stop-total", required_argument, 0, 'S'},524{"trace", optional_argument, 0, 't'},525{"event", required_argument, 0, 'e'},526{"threshold", required_argument, 0, 'T'},527{"no-header", no_argument, 0, '0'},528{"no-summary", no_argument, 0, '1'},529{"no-index", no_argument, 0, '2'},530{"with-zeros", no_argument, 0, '3'},531{"trigger", required_argument, 0, '4'},532{"filter", required_argument, 0, '5'},533{"warm-up", required_argument, 0, '6'},534{"trace-buffer-size", required_argument, 0, '7'},535{"on-threshold", required_argument, 0, '8'},536{"on-end", required_argument, 0, '9'},537{0, 0, 0, 0}538};539540/* getopt_long stores the option index here. */541int option_index = 0;542543c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:p:P:r:s:S:t::T:01234:5:6:7:",544long_options, &option_index);545546/* detect the end of the options. */547if (c == -1)548break;549550switch (c) {551case 'a':552/* set sample stop to auto_thresh */553params->common.stop_us = get_llong_from_str(optarg);554555/* set sample threshold to 1 */556params->threshold = 1;557558/* set trace */559trace_output = "osnoise_trace.txt";560561break;562case 'b':563params->common.hist.bucket_size = get_llong_from_str(optarg);564if (params->common.hist.bucket_size == 0 ||565params->common.hist.bucket_size >= 1000000)566osnoise_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");567break;568case 'c':569retval = parse_cpu_set(optarg, ¶ms->common.monitored_cpus);570if (retval)571osnoise_hist_usage("\nInvalid -c cpu list\n");572params->common.cpus = optarg;573break;574case 'C':575params->common.cgroup = 1;576if (!optarg) {577/* will inherit this cgroup */578params->common.cgroup_name = NULL;579} else if (*optarg == '=') {580/* skip the = */581params->common.cgroup_name = ++optarg;582}583break;584case 'D':585config_debug = 1;586break;587case 'd':588params->common.duration = parse_seconds_duration(optarg);589if (!params->common.duration)590osnoise_hist_usage("Invalid -D duration\n");591break;592case 'e':593tevent = trace_event_alloc(optarg);594if (!tevent) {595err_msg("Error alloc trace event");596exit(EXIT_FAILURE);597}598599if (params->common.events)600tevent->next = params->common.events;601602params->common.events = tevent;603break;604case 'E':605params->common.hist.entries = get_llong_from_str(optarg);606if (params->common.hist.entries < 10 ||607params->common.hist.entries > 9999999)608osnoise_hist_usage("Entries must be > 10 and < 9999999\n");609break;610case 'h':611case '?':612osnoise_hist_usage(NULL);613break;614case 'H':615params->common.hk_cpus = 1;616retval = parse_cpu_set(optarg, ¶ms->common.hk_cpu_set);617if (retval) {618err_msg("Error parsing house keeping CPUs\n");619exit(EXIT_FAILURE);620}621break;622case 'p':623params->period = get_llong_from_str(optarg);624if (params->period > 10000000)625osnoise_hist_usage("Period longer than 10 s\n");626break;627case 'P':628retval = parse_prio(optarg, ¶ms->common.sched_param);629if (retval == -1)630osnoise_hist_usage("Invalid -P priority");631params->common.set_sched = 1;632break;633case 'r':634params->runtime = get_llong_from_str(optarg);635if (params->runtime < 100)636osnoise_hist_usage("Runtime shorter than 100 us\n");637break;638case 's':639params->common.stop_us = get_llong_from_str(optarg);640break;641case 'S':642params->common.stop_total_us = get_llong_from_str(optarg);643break;644case 'T':645params->threshold = get_llong_from_str(optarg);646break;647case 't':648if (optarg) {649if (optarg[0] == '=')650trace_output = &optarg[1];651else652trace_output = &optarg[0];653} else if (optind < argc && argv[optind][0] != '0')654trace_output = argv[optind];655else656trace_output = "osnoise_trace.txt";657break;658case '0': /* no header */659params->common.hist.no_header = 1;660break;661case '1': /* no summary */662params->common.hist.no_summary = 1;663break;664case '2': /* no index */665params->common.hist.no_index = 1;666break;667case '3': /* with zeros */668params->common.hist.with_zeros = 1;669break;670case '4': /* trigger */671if (params->common.events) {672retval = trace_event_add_trigger(params->common.events, optarg);673if (retval) {674err_msg("Error adding trigger %s\n", optarg);675exit(EXIT_FAILURE);676}677} else {678osnoise_hist_usage("--trigger requires a previous -e\n");679}680break;681case '5': /* filter */682if (params->common.events) {683retval = trace_event_add_filter(params->common.events, optarg);684if (retval) {685err_msg("Error adding filter %s\n", optarg);686exit(EXIT_FAILURE);687}688} else {689osnoise_hist_usage("--filter requires a previous -e\n");690}691break;692case '6':693params->common.warmup = get_llong_from_str(optarg);694break;695case '7':696params->common.buffer_size = get_llong_from_str(optarg);697break;698case '8':699retval = actions_parse(¶ms->common.threshold_actions, optarg,700"osnoise_trace.txt");701if (retval) {702err_msg("Invalid action %s\n", optarg);703exit(EXIT_FAILURE);704}705break;706case '9':707retval = actions_parse(¶ms->common.end_actions, optarg,708"osnoise_trace.txt");709if (retval) {710err_msg("Invalid action %s\n", optarg);711exit(EXIT_FAILURE);712}713break;714default:715osnoise_hist_usage("Invalid option");716}717}718719if (trace_output)720actions_add_trace_output(¶ms->common.threshold_actions, trace_output);721722if (geteuid()) {723err_msg("rtla needs root permission\n");724exit(EXIT_FAILURE);725}726727if (params->common.hist.no_index && !params->common.hist.with_zeros)728osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense");729730return ¶ms->common;731}732733/*734* osnoise_hist_apply_config - apply the hist configs to the initialized tool735*/736static int737osnoise_hist_apply_config(struct osnoise_tool *tool)738{739return osnoise_apply_config(tool, to_osnoise_params(tool->params));740}741742/*743* osnoise_init_hist - initialize a osnoise hist tool with parameters744*/745static struct osnoise_tool746*osnoise_init_hist(struct common_params *params)747{748struct osnoise_tool *tool;749int nr_cpus;750751nr_cpus = sysconf(_SC_NPROCESSORS_CONF);752753tool = osnoise_init_tool("osnoise_hist");754if (!tool)755return NULL;756757tool->data = osnoise_alloc_histogram(nr_cpus, params->hist.entries,758params->hist.bucket_size);759if (!tool->data)760goto out_err;761762return tool;763764out_err:765osnoise_destroy_tool(tool);766return NULL;767}768769static int osnoise_hist_enable(struct osnoise_tool *tool)770{771int retval;772773retval = osnoise_init_trace_hist(tool);774if (retval)775return retval;776777return osnoise_enable(tool);778}779780static int osnoise_hist_main_loop(struct osnoise_tool *tool)781{782int retval;783784retval = hist_main_loop(tool);785osnoise_read_trace_hist(tool);786787return retval;788}789790struct tool_ops osnoise_hist_ops = {791.tracer = "osnoise",792.comm_prefix = "osnoise/",793.parse_args = osnoise_hist_parse_args,794.init_tool = osnoise_init_hist,795.apply_config = osnoise_hist_apply_config,796.enable = osnoise_hist_enable,797.main = osnoise_hist_main_loop,798.print_stats = osnoise_print_stats,799.free = osnoise_free_hist_tool,800};801802803