Path: blob/master/tools/tracing/rtla/src/timerlat_hist.c
49000 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 <stdio.h>12#include <time.h>13#include <sched.h>14#include <pthread.h>1516#include "timerlat.h"17#include "timerlat_aa.h"18#include "timerlat_bpf.h"1920struct timerlat_hist_cpu {21int *irq;22int *thread;23int *user;2425unsigned long long irq_count;26unsigned long long thread_count;27unsigned long long user_count;2829unsigned long long min_irq;30unsigned long long sum_irq;31unsigned long long max_irq;3233unsigned long long min_thread;34unsigned long long sum_thread;35unsigned long long max_thread;3637unsigned long long min_user;38unsigned long long sum_user;39unsigned long long max_user;40};4142struct timerlat_hist_data {43struct timerlat_hist_cpu *hist;44int entries;45int bucket_size;46int nr_cpus;47};4849/*50* timerlat_free_histogram - free runtime data51*/52static void53timerlat_free_histogram(struct timerlat_hist_data *data)54{55int cpu;5657/* one histogram for IRQ and one for thread, per CPU */58for (cpu = 0; cpu < data->nr_cpus; cpu++) {59if (data->hist[cpu].irq)60free(data->hist[cpu].irq);6162if (data->hist[cpu].thread)63free(data->hist[cpu].thread);6465if (data->hist[cpu].user)66free(data->hist[cpu].user);6768}6970/* one set of histograms per CPU */71if (data->hist)72free(data->hist);73}7475static void timerlat_free_histogram_tool(struct osnoise_tool *tool)76{77timerlat_free_histogram(tool->data);78timerlat_free(tool);79}8081/*82* timerlat_alloc_histogram - alloc runtime data83*/84static struct timerlat_hist_data85*timerlat_alloc_histogram(int nr_cpus, int entries, int bucket_size)86{87struct timerlat_hist_data *data;88int cpu;8990data = calloc(1, sizeof(*data));91if (!data)92return NULL;9394data->entries = entries;95data->bucket_size = bucket_size;96data->nr_cpus = nr_cpus;9798/* one set of histograms per CPU */99data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);100if (!data->hist)101goto cleanup;102103/* one histogram for IRQ and one for thread, per cpu */104for (cpu = 0; cpu < nr_cpus; cpu++) {105data->hist[cpu].irq = calloc(1, sizeof(*data->hist->irq) * (entries + 1));106if (!data->hist[cpu].irq)107goto cleanup;108109data->hist[cpu].thread = calloc(1, sizeof(*data->hist->thread) * (entries + 1));110if (!data->hist[cpu].thread)111goto cleanup;112113data->hist[cpu].user = calloc(1, sizeof(*data->hist->user) * (entries + 1));114if (!data->hist[cpu].user)115goto cleanup;116}117118/* set the min to max */119for (cpu = 0; cpu < nr_cpus; cpu++) {120data->hist[cpu].min_irq = ~0;121data->hist[cpu].min_thread = ~0;122data->hist[cpu].min_user = ~0;123}124125return data;126127cleanup:128timerlat_free_histogram(data);129return NULL;130}131132/*133* timerlat_hist_update - record a new timerlat occurent on cpu, updating data134*/135static void136timerlat_hist_update(struct osnoise_tool *tool, int cpu,137unsigned long long context,138unsigned long long latency)139{140struct timerlat_params *params = to_timerlat_params(tool->params);141struct timerlat_hist_data *data = tool->data;142int entries = data->entries;143int bucket;144int *hist;145146if (params->common.output_divisor)147latency = latency / params->common.output_divisor;148149bucket = latency / data->bucket_size;150151if (!context) {152hist = data->hist[cpu].irq;153data->hist[cpu].irq_count++;154update_min(&data->hist[cpu].min_irq, &latency);155update_sum(&data->hist[cpu].sum_irq, &latency);156update_max(&data->hist[cpu].max_irq, &latency);157} else if (context == 1) {158hist = data->hist[cpu].thread;159data->hist[cpu].thread_count++;160update_min(&data->hist[cpu].min_thread, &latency);161update_sum(&data->hist[cpu].sum_thread, &latency);162update_max(&data->hist[cpu].max_thread, &latency);163} else { /* user */164hist = data->hist[cpu].user;165data->hist[cpu].user_count++;166update_min(&data->hist[cpu].min_user, &latency);167update_sum(&data->hist[cpu].sum_user, &latency);168update_max(&data->hist[cpu].max_user, &latency);169}170171if (bucket < entries)172hist[bucket]++;173else174hist[entries]++;175}176177/*178* timerlat_hist_handler - this is the handler for timerlat tracer events179*/180static int181timerlat_hist_handler(struct trace_seq *s, struct tep_record *record,182struct tep_event *event, void *data)183{184struct trace_instance *trace = data;185unsigned long long context, latency;186struct osnoise_tool *tool;187int cpu = record->cpu;188189tool = container_of(trace, struct osnoise_tool, trace);190191tep_get_field_val(s, event, "context", record, &context, 1);192tep_get_field_val(s, event, "timer_latency", record, &latency, 1);193194timerlat_hist_update(tool, cpu, context, latency);195196return 0;197}198199/*200* timerlat_hist_bpf_pull_data - copy data from BPF maps into userspace201*/202static int timerlat_hist_bpf_pull_data(struct osnoise_tool *tool)203{204struct timerlat_hist_data *data = tool->data;205int i, j, err;206long long value_irq[data->nr_cpus],207value_thread[data->nr_cpus],208value_user[data->nr_cpus];209210/* Pull histogram */211for (i = 0; i < data->entries; i++) {212err = timerlat_bpf_get_hist_value(i, value_irq, value_thread,213value_user, data->nr_cpus);214if (err)215return err;216for (j = 0; j < data->nr_cpus; j++) {217data->hist[j].irq[i] = value_irq[j];218data->hist[j].thread[i] = value_thread[j];219data->hist[j].user[i] = value_user[j];220}221}222223/* Pull summary */224err = timerlat_bpf_get_summary_value(SUMMARY_COUNT,225value_irq, value_thread, value_user,226data->nr_cpus);227if (err)228return err;229for (i = 0; i < data->nr_cpus; i++) {230data->hist[i].irq_count = value_irq[i];231data->hist[i].thread_count = value_thread[i];232data->hist[i].user_count = value_user[i];233}234235err = timerlat_bpf_get_summary_value(SUMMARY_MIN,236value_irq, value_thread, value_user,237data->nr_cpus);238if (err)239return err;240for (i = 0; i < data->nr_cpus; i++) {241data->hist[i].min_irq = value_irq[i];242data->hist[i].min_thread = value_thread[i];243data->hist[i].min_user = value_user[i];244}245246err = timerlat_bpf_get_summary_value(SUMMARY_MAX,247value_irq, value_thread, value_user,248data->nr_cpus);249if (err)250return err;251for (i = 0; i < data->nr_cpus; i++) {252data->hist[i].max_irq = value_irq[i];253data->hist[i].max_thread = value_thread[i];254data->hist[i].max_user = value_user[i];255}256257err = timerlat_bpf_get_summary_value(SUMMARY_SUM,258value_irq, value_thread, value_user,259data->nr_cpus);260if (err)261return err;262for (i = 0; i < data->nr_cpus; i++) {263data->hist[i].sum_irq = value_irq[i];264data->hist[i].sum_thread = value_thread[i];265data->hist[i].sum_user = value_user[i];266}267268err = timerlat_bpf_get_summary_value(SUMMARY_OVERFLOW,269value_irq, value_thread, value_user,270data->nr_cpus);271if (err)272return err;273for (i = 0; i < data->nr_cpus; i++) {274data->hist[i].irq[data->entries] = value_irq[i];275data->hist[i].thread[data->entries] = value_thread[i];276data->hist[i].user[data->entries] = value_user[i];277}278279return 0;280}281282/*283* timerlat_hist_header - print the header of the tracer to the output284*/285static void timerlat_hist_header(struct osnoise_tool *tool)286{287struct timerlat_params *params = to_timerlat_params(tool->params);288struct timerlat_hist_data *data = tool->data;289struct trace_seq *s = tool->trace.seq;290char duration[26];291int cpu;292293if (params->common.hist.no_header)294return;295296get_duration(tool->start_time, duration, sizeof(duration));297trace_seq_printf(s, "# RTLA timerlat histogram\n");298trace_seq_printf(s, "# Time unit is %s (%s)\n",299params->common.output_divisor == 1 ? "nanoseconds" : "microseconds",300params->common.output_divisor == 1 ? "ns" : "us");301302trace_seq_printf(s, "# Duration: %s\n", duration);303304if (!params->common.hist.no_index)305trace_seq_printf(s, "Index");306307for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {308309if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)310continue;311312if (!params->common.hist.no_irq)313trace_seq_printf(s, " IRQ-%03d", cpu);314315if (!params->common.hist.no_thread)316trace_seq_printf(s, " Thr-%03d", cpu);317318if (params->common.user_data)319trace_seq_printf(s, " Usr-%03d", cpu);320}321trace_seq_printf(s, "\n");322323324trace_seq_do_printf(s);325trace_seq_reset(s);326}327328/*329* format_summary_value - format a line of summary value (min, max or avg)330* of hist data331*/332static void format_summary_value(struct trace_seq *seq,333int count,334unsigned long long val,335bool avg)336{337if (count)338trace_seq_printf(seq, "%9llu ", avg ? val / count : val);339else340trace_seq_printf(seq, "%9c ", '-');341}342343/*344* timerlat_print_summary - print the summary of the hist data to the output345*/346static void347timerlat_print_summary(struct timerlat_params *params,348struct trace_instance *trace,349struct timerlat_hist_data *data)350{351int cpu;352353if (params->common.hist.no_summary)354return;355356if (!params->common.hist.no_index)357trace_seq_printf(trace->seq, "count:");358359for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {360361if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)362continue;363364if (!params->common.hist.no_irq)365trace_seq_printf(trace->seq, "%9llu ",366data->hist[cpu].irq_count);367368if (!params->common.hist.no_thread)369trace_seq_printf(trace->seq, "%9llu ",370data->hist[cpu].thread_count);371372if (params->common.user_data)373trace_seq_printf(trace->seq, "%9llu ",374data->hist[cpu].user_count);375}376trace_seq_printf(trace->seq, "\n");377378if (!params->common.hist.no_index)379trace_seq_printf(trace->seq, "min: ");380381for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {382383if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)384continue;385386if (!params->common.hist.no_irq)387format_summary_value(trace->seq,388data->hist[cpu].irq_count,389data->hist[cpu].min_irq,390false);391392if (!params->common.hist.no_thread)393format_summary_value(trace->seq,394data->hist[cpu].thread_count,395data->hist[cpu].min_thread,396false);397398if (params->common.user_data)399format_summary_value(trace->seq,400data->hist[cpu].user_count,401data->hist[cpu].min_user,402false);403}404trace_seq_printf(trace->seq, "\n");405406if (!params->common.hist.no_index)407trace_seq_printf(trace->seq, "avg: ");408409for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {410411if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)412continue;413414if (!params->common.hist.no_irq)415format_summary_value(trace->seq,416data->hist[cpu].irq_count,417data->hist[cpu].sum_irq,418true);419420if (!params->common.hist.no_thread)421format_summary_value(trace->seq,422data->hist[cpu].thread_count,423data->hist[cpu].sum_thread,424true);425426if (params->common.user_data)427format_summary_value(trace->seq,428data->hist[cpu].user_count,429data->hist[cpu].sum_user,430true);431}432trace_seq_printf(trace->seq, "\n");433434if (!params->common.hist.no_index)435trace_seq_printf(trace->seq, "max: ");436437for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {438439if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)440continue;441442if (!params->common.hist.no_irq)443format_summary_value(trace->seq,444data->hist[cpu].irq_count,445data->hist[cpu].max_irq,446false);447448if (!params->common.hist.no_thread)449format_summary_value(trace->seq,450data->hist[cpu].thread_count,451data->hist[cpu].max_thread,452false);453454if (params->common.user_data)455format_summary_value(trace->seq,456data->hist[cpu].user_count,457data->hist[cpu].max_user,458false);459}460trace_seq_printf(trace->seq, "\n");461trace_seq_do_printf(trace->seq);462trace_seq_reset(trace->seq);463}464465static void466timerlat_print_stats_all(struct timerlat_params *params,467struct trace_instance *trace,468struct timerlat_hist_data *data)469{470struct timerlat_hist_cpu *cpu_data;471struct timerlat_hist_cpu sum;472int cpu;473474if (params->common.hist.no_summary)475return;476477memset(&sum, 0, sizeof(sum));478sum.min_irq = ~0;479sum.min_thread = ~0;480sum.min_user = ~0;481482for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {483484if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)485continue;486487cpu_data = &data->hist[cpu];488489sum.irq_count += cpu_data->irq_count;490update_min(&sum.min_irq, &cpu_data->min_irq);491update_sum(&sum.sum_irq, &cpu_data->sum_irq);492update_max(&sum.max_irq, &cpu_data->max_irq);493494sum.thread_count += cpu_data->thread_count;495update_min(&sum.min_thread, &cpu_data->min_thread);496update_sum(&sum.sum_thread, &cpu_data->sum_thread);497update_max(&sum.max_thread, &cpu_data->max_thread);498499sum.user_count += cpu_data->user_count;500update_min(&sum.min_user, &cpu_data->min_user);501update_sum(&sum.sum_user, &cpu_data->sum_user);502update_max(&sum.max_user, &cpu_data->max_user);503}504505if (!params->common.hist.no_index)506trace_seq_printf(trace->seq, "ALL: ");507508if (!params->common.hist.no_irq)509trace_seq_printf(trace->seq, " IRQ");510511if (!params->common.hist.no_thread)512trace_seq_printf(trace->seq, " Thr");513514if (params->common.user_data)515trace_seq_printf(trace->seq, " Usr");516517trace_seq_printf(trace->seq, "\n");518519if (!params->common.hist.no_index)520trace_seq_printf(trace->seq, "count:");521522if (!params->common.hist.no_irq)523trace_seq_printf(trace->seq, "%9llu ",524sum.irq_count);525526if (!params->common.hist.no_thread)527trace_seq_printf(trace->seq, "%9llu ",528sum.thread_count);529530if (params->common.user_data)531trace_seq_printf(trace->seq, "%9llu ",532sum.user_count);533534trace_seq_printf(trace->seq, "\n");535536if (!params->common.hist.no_index)537trace_seq_printf(trace->seq, "min: ");538539if (!params->common.hist.no_irq)540format_summary_value(trace->seq,541sum.irq_count,542sum.min_irq,543false);544545if (!params->common.hist.no_thread)546format_summary_value(trace->seq,547sum.thread_count,548sum.min_thread,549false);550551if (params->common.user_data)552format_summary_value(trace->seq,553sum.user_count,554sum.min_user,555false);556557trace_seq_printf(trace->seq, "\n");558559if (!params->common.hist.no_index)560trace_seq_printf(trace->seq, "avg: ");561562if (!params->common.hist.no_irq)563format_summary_value(trace->seq,564sum.irq_count,565sum.sum_irq,566true);567568if (!params->common.hist.no_thread)569format_summary_value(trace->seq,570sum.thread_count,571sum.sum_thread,572true);573574if (params->common.user_data)575format_summary_value(trace->seq,576sum.user_count,577sum.sum_user,578true);579580trace_seq_printf(trace->seq, "\n");581582if (!params->common.hist.no_index)583trace_seq_printf(trace->seq, "max: ");584585if (!params->common.hist.no_irq)586format_summary_value(trace->seq,587sum.irq_count,588sum.max_irq,589false);590591if (!params->common.hist.no_thread)592format_summary_value(trace->seq,593sum.thread_count,594sum.max_thread,595false);596597if (params->common.user_data)598format_summary_value(trace->seq,599sum.user_count,600sum.max_user,601false);602603trace_seq_printf(trace->seq, "\n");604trace_seq_do_printf(trace->seq);605trace_seq_reset(trace->seq);606}607608/*609* timerlat_print_stats - print data for each CPUs610*/611static void612timerlat_print_stats(struct osnoise_tool *tool)613{614struct timerlat_params *params = to_timerlat_params(tool->params);615struct timerlat_hist_data *data = tool->data;616struct trace_instance *trace = &tool->trace;617int bucket, cpu;618int total;619620timerlat_hist_header(tool);621622for (bucket = 0; bucket < data->entries; bucket++) {623total = 0;624625if (!params->common.hist.no_index)626trace_seq_printf(trace->seq, "%-6d",627bucket * data->bucket_size);628629for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {630631if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)632continue;633634if (!params->common.hist.no_irq) {635total += data->hist[cpu].irq[bucket];636trace_seq_printf(trace->seq, "%9d ",637data->hist[cpu].irq[bucket]);638}639640if (!params->common.hist.no_thread) {641total += data->hist[cpu].thread[bucket];642trace_seq_printf(trace->seq, "%9d ",643data->hist[cpu].thread[bucket]);644}645646if (params->common.user_data) {647total += data->hist[cpu].user[bucket];648trace_seq_printf(trace->seq, "%9d ",649data->hist[cpu].user[bucket]);650}651652}653654if (total == 0 && !params->common.hist.with_zeros) {655trace_seq_reset(trace->seq);656continue;657}658659trace_seq_printf(trace->seq, "\n");660trace_seq_do_printf(trace->seq);661trace_seq_reset(trace->seq);662}663664if (!params->common.hist.no_index)665trace_seq_printf(trace->seq, "over: ");666667for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {668669if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)670continue;671672if (!params->common.hist.no_irq)673trace_seq_printf(trace->seq, "%9d ",674data->hist[cpu].irq[data->entries]);675676if (!params->common.hist.no_thread)677trace_seq_printf(trace->seq, "%9d ",678data->hist[cpu].thread[data->entries]);679680if (params->common.user_data)681trace_seq_printf(trace->seq, "%9d ",682data->hist[cpu].user[data->entries]);683}684trace_seq_printf(trace->seq, "\n");685trace_seq_do_printf(trace->seq);686trace_seq_reset(trace->seq);687688timerlat_print_summary(params, trace, data);689timerlat_print_stats_all(params, trace, data);690osnoise_report_missed_events(tool);691}692693/*694* timerlat_hist_usage - prints timerlat top usage message695*/696static void timerlat_hist_usage(void)697{698int i;699700char *msg[] = {701"",702" usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\",703" [-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",704" [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [--no-summary] \\",705" [--no-index] [--with-zeros] [--dma-latency us] [-C [cgroup_name]] [--no-aa] [--dump-task] [-u|-k]",706" [--warm-up s] [--deepest-idle-state n]",707"",708" -h/--help: print this menu",709" -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",710" -p/--period us: timerlat period in us",711" -i/--irq us: stop trace if the irq latency is higher than the argument in us",712" -T/--thread us: stop trace if the thread latency is higher than the argument in us",713" -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",714" -c/--cpus cpus: run the tracer only on the given cpus",715" -H/--house-keeping cpus: run rtla control threads only on the given cpus",716" -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",717" -d/--duration time[m|h|d]: duration of the session in seconds",718" --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",719" -D/--debug: print debug info",720" -t/--trace [file]: save the stopped trace to [file|timerlat_trace.txt]",721" -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",722" --filter <filter>: enable a trace event filter to the previous -e event",723" --trigger <trigger>: enable a trace event trigger to the previous -e event",724" -n/--nano: display data in nanoseconds",725" --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",726" -b/--bucket-size N: set the histogram bucket size (default 1)",727" -E/--entries N: set the number of entries of the histogram (default 256)",728" --no-irq: ignore IRQ latencies",729" --no-thread: ignore thread latencies",730" --no-header: do not print header",731" --no-summary: do not print summary",732" --no-index: do not print index",733" --with-zeros: print zero only entries",734" --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",735" -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",736" o:prio - use SCHED_OTHER with prio",737" r:prio - use SCHED_RR with prio",738" f:prio - use SCHED_FIFO with prio",739" d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",740" in nanoseconds",741" -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads",742" -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads",743" -U/--user-load: enable timerlat for user-defined user-space workload",744" --warm-up s: let the workload run for s seconds before collecting data",745" --trace-buffer-size kB: set the per-cpu trace buffer size in kB",746" --deepest-idle-state n: only go down to idle state n on cpus used by timerlat to reduce exit from idle latency",747" --on-threshold <action>: define action to be executed at latency threshold, multiple are allowed",748" --on-end <action>: define action to be executed at measurement end, multiple are allowed",749NULL,750};751752fprintf(stderr, "rtla timerlat hist: a per-cpu histogram of the timer latency (version %s)\n",753VERSION);754755for (i = 0; msg[i]; i++)756fprintf(stderr, "%s\n", msg[i]);757758exit(EXIT_SUCCESS);759}760761/*762* timerlat_hist_parse_args - allocs, parse and fill the cmd line parameters763*/764static struct common_params765*timerlat_hist_parse_args(int argc, char *argv[])766{767struct timerlat_params *params;768struct trace_events *tevent;769int auto_thresh;770int retval;771int c;772char *trace_output = NULL;773774params = calloc(1, sizeof(*params));775if (!params)776exit(1);777778actions_init(¶ms->common.threshold_actions);779actions_init(¶ms->common.end_actions);780781/* disabled by default */782params->dma_latency = -1;783784/* disabled by default */785params->deepest_idle_state = -2;786787/* display data in microseconds */788params->common.output_divisor = 1000;789params->common.hist.bucket_size = 1;790params->common.hist.entries = 256;791792/* default to BPF mode */793params->mode = TRACING_MODE_BPF;794795while (1) {796static struct option long_options[] = {797{"auto", required_argument, 0, 'a'},798{"cpus", required_argument, 0, 'c'},799{"cgroup", optional_argument, 0, 'C'},800{"bucket-size", required_argument, 0, 'b'},801{"debug", no_argument, 0, 'D'},802{"entries", required_argument, 0, 'E'},803{"duration", required_argument, 0, 'd'},804{"house-keeping", required_argument, 0, 'H'},805{"help", no_argument, 0, 'h'},806{"irq", required_argument, 0, 'i'},807{"nano", no_argument, 0, 'n'},808{"period", required_argument, 0, 'p'},809{"priority", required_argument, 0, 'P'},810{"stack", required_argument, 0, 's'},811{"thread", required_argument, 0, 'T'},812{"trace", optional_argument, 0, 't'},813{"user-threads", no_argument, 0, 'u'},814{"kernel-threads", no_argument, 0, 'k'},815{"user-load", no_argument, 0, 'U'},816{"event", required_argument, 0, 'e'},817{"no-irq", no_argument, 0, '0'},818{"no-thread", no_argument, 0, '1'},819{"no-header", no_argument, 0, '2'},820{"no-summary", no_argument, 0, '3'},821{"no-index", no_argument, 0, '4'},822{"with-zeros", no_argument, 0, '5'},823{"trigger", required_argument, 0, '6'},824{"filter", required_argument, 0, '7'},825{"dma-latency", required_argument, 0, '8'},826{"no-aa", no_argument, 0, '9'},827{"dump-task", no_argument, 0, '\1'},828{"warm-up", required_argument, 0, '\2'},829{"trace-buffer-size", required_argument, 0, '\3'},830{"deepest-idle-state", required_argument, 0, '\4'},831{"on-threshold", required_argument, 0, '\5'},832{"on-end", required_argument, 0, '\6'},833{0, 0, 0, 0}834};835836c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:i:knp:P:s:t::T:uU0123456:7:8:9\1\2:\3:",837long_options, NULL);838839/* detect the end of the options. */840if (c == -1)841break;842843switch (c) {844case 'a':845auto_thresh = get_llong_from_str(optarg);846847/* set thread stop to auto_thresh */848params->common.stop_total_us = auto_thresh;849params->common.stop_us = auto_thresh;850851/* get stack trace */852params->print_stack = auto_thresh;853854/* set trace */855if (!trace_output)856trace_output = "timerlat_trace.txt";857858break;859case 'c':860retval = parse_cpu_set(optarg, ¶ms->common.monitored_cpus);861if (retval)862fatal("Invalid -c cpu list");863params->common.cpus = optarg;864break;865case 'C':866params->common.cgroup = 1;867params->common.cgroup_name = parse_optional_arg(argc, argv);868break;869case 'b':870params->common.hist.bucket_size = get_llong_from_str(optarg);871if (params->common.hist.bucket_size == 0 ||872params->common.hist.bucket_size >= 1000000)873fatal("Bucket size needs to be > 0 and <= 1000000");874break;875case 'D':876config_debug = 1;877break;878case 'd':879params->common.duration = parse_seconds_duration(optarg);880if (!params->common.duration)881fatal("Invalid -D duration");882break;883case 'e':884tevent = trace_event_alloc(optarg);885if (!tevent)886fatal("Error alloc trace event");887888if (params->common.events)889tevent->next = params->common.events;890891params->common.events = tevent;892break;893case 'E':894params->common.hist.entries = get_llong_from_str(optarg);895if (params->common.hist.entries < 10 ||896params->common.hist.entries > 9999999)897fatal("Entries must be > 10 and < 9999999");898break;899case 'h':900case '?':901timerlat_hist_usage();902break;903case 'H':904params->common.hk_cpus = 1;905retval = parse_cpu_set(optarg, ¶ms->common.hk_cpu_set);906if (retval)907fatal("Error parsing house keeping CPUs");908break;909case 'i':910params->common.stop_us = get_llong_from_str(optarg);911break;912case 'k':913params->common.kernel_workload = 1;914break;915case 'n':916params->common.output_divisor = 1;917break;918case 'p':919params->timerlat_period_us = get_llong_from_str(optarg);920if (params->timerlat_period_us > 1000000)921fatal("Period longer than 1 s");922break;923case 'P':924retval = parse_prio(optarg, ¶ms->common.sched_param);925if (retval == -1)926fatal("Invalid -P priority");927params->common.set_sched = 1;928break;929case 's':930params->print_stack = get_llong_from_str(optarg);931break;932case 'T':933params->common.stop_total_us = get_llong_from_str(optarg);934break;935case 't':936trace_output = parse_optional_arg(argc, argv);937if (!trace_output)938trace_output = "timerlat_trace.txt";939break;940case 'u':941params->common.user_workload = 1;942/* fallback: -u implies in -U */943case 'U':944params->common.user_data = 1;945break;946case '0': /* no irq */947params->common.hist.no_irq = 1;948break;949case '1': /* no thread */950params->common.hist.no_thread = 1;951break;952case '2': /* no header */953params->common.hist.no_header = 1;954break;955case '3': /* no summary */956params->common.hist.no_summary = 1;957break;958case '4': /* no index */959params->common.hist.no_index = 1;960break;961case '5': /* with zeros */962params->common.hist.with_zeros = 1;963break;964case '6': /* trigger */965if (params->common.events) {966retval = trace_event_add_trigger(params->common.events, optarg);967if (retval)968fatal("Error adding trigger %s", optarg);969} else {970fatal("--trigger requires a previous -e");971}972break;973case '7': /* filter */974if (params->common.events) {975retval = trace_event_add_filter(params->common.events, optarg);976if (retval)977fatal("Error adding filter %s", optarg);978} else {979fatal("--filter requires a previous -e");980}981break;982case '8':983params->dma_latency = get_llong_from_str(optarg);984if (params->dma_latency < 0 || params->dma_latency > 10000)985fatal("--dma-latency needs to be >= 0 and < 10000");986break;987case '9':988params->no_aa = 1;989break;990case '\1':991params->dump_tasks = 1;992break;993case '\2':994params->common.warmup = get_llong_from_str(optarg);995break;996case '\3':997params->common.buffer_size = get_llong_from_str(optarg);998break;999case '\4':1000params->deepest_idle_state = get_llong_from_str(optarg);1001break;1002case '\5':1003retval = actions_parse(¶ms->common.threshold_actions, optarg,1004"timerlat_trace.txt");1005if (retval)1006fatal("Invalid action %s", optarg);1007break;1008case '\6':1009retval = actions_parse(¶ms->common.end_actions, optarg,1010"timerlat_trace.txt");1011if (retval)1012fatal("Invalid action %s", optarg);1013break;1014default:1015fatal("Invalid option");1016}1017}10181019if (trace_output)1020actions_add_trace_output(¶ms->common.threshold_actions, trace_output);10211022if (geteuid())1023fatal("rtla needs root permission");10241025if (params->common.hist.no_irq && params->common.hist.no_thread)1026fatal("no-irq and no-thread set, there is nothing to do here");10271028if (params->common.hist.no_index && !params->common.hist.with_zeros)1029fatal("no-index set with with-zeros is not set - it does not make sense");10301031/*1032* Auto analysis only happens if stop tracing, thus:1033*/1034if (!params->common.stop_us && !params->common.stop_total_us)1035params->no_aa = 1;10361037if (params->common.kernel_workload && params->common.user_workload)1038fatal("--kernel-threads and --user-threads are mutually exclusive!");10391040/*1041* If auto-analysis or trace output is enabled, switch from BPF mode to1042* mixed mode1043*/1044if (params->mode == TRACING_MODE_BPF &&1045(params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] ||1046params->common.end_actions.present[ACTION_TRACE_OUTPUT] ||1047!params->no_aa))1048params->mode = TRACING_MODE_MIXED;10491050return ¶ms->common;1051}10521053/*1054* timerlat_hist_apply_config - apply the hist configs to the initialized tool1055*/1056static int1057timerlat_hist_apply_config(struct osnoise_tool *tool)1058{1059struct timerlat_params *params = to_timerlat_params(tool->params);1060int retval;10611062retval = timerlat_apply_config(tool, params);1063if (retval)1064goto out_err;10651066return 0;10671068out_err:1069return -1;1070}10711072/*1073* timerlat_init_hist - initialize a timerlat hist tool with parameters1074*/1075static struct osnoise_tool1076*timerlat_init_hist(struct common_params *params)1077{1078struct osnoise_tool *tool;1079int nr_cpus;10801081nr_cpus = sysconf(_SC_NPROCESSORS_CONF);10821083tool = osnoise_init_tool("timerlat_hist");1084if (!tool)1085return NULL;10861087tool->data = timerlat_alloc_histogram(nr_cpus, params->hist.entries,1088params->hist.bucket_size);1089if (!tool->data)1090goto out_err;10911092tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat",1093timerlat_hist_handler, tool);10941095return tool;10961097out_err:1098osnoise_destroy_tool(tool);1099return NULL;1100}11011102static int timerlat_hist_bpf_main_loop(struct osnoise_tool *tool)1103{1104struct timerlat_params *params = to_timerlat_params(tool->params);1105int retval;11061107while (!stop_tracing) {1108timerlat_bpf_wait(-1);11091110if (!stop_tracing) {1111/* Threshold overflow, perform actions on threshold */1112actions_perform(¶ms->common.threshold_actions);11131114if (!params->common.threshold_actions.continue_flag)1115/* continue flag not set, break */1116break;11171118/* continue action reached, re-enable tracing */1119if (tool->record)1120trace_instance_start(&tool->record->trace);1121if (tool->aa)1122trace_instance_start(&tool->aa->trace);1123timerlat_bpf_restart_tracing();1124}1125}1126timerlat_bpf_detach();11271128retval = timerlat_hist_bpf_pull_data(tool);1129if (retval)1130err_msg("Error pulling BPF data\n");11311132return retval;1133}11341135static int timerlat_hist_main(struct osnoise_tool *tool)1136{1137struct timerlat_params *params = to_timerlat_params(tool->params);1138int retval;11391140if (params->mode == TRACING_MODE_TRACEFS)1141retval = hist_main_loop(tool);1142else1143retval = timerlat_hist_bpf_main_loop(tool);11441145return retval;1146}11471148struct tool_ops timerlat_hist_ops = {1149.tracer = "timerlat",1150.comm_prefix = "timerlat/",1151.parse_args = timerlat_hist_parse_args,1152.init_tool = timerlat_init_hist,1153.apply_config = timerlat_hist_apply_config,1154.enable = timerlat_enable,1155.main = timerlat_hist_main,1156.print_stats = timerlat_print_stats,1157.analyze = timerlat_analyze,1158.free = timerlat_free_histogram_tool,1159};116011611162