Path: blob/master/tools/tracing/rtla/src/timerlat_top.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 <stdio.h>12#include <time.h>13#include <errno.h>14#include <sched.h>15#include <pthread.h>1617#include "timerlat.h"18#include "timerlat_aa.h"19#include "timerlat_bpf.h"2021struct timerlat_top_cpu {22unsigned long long irq_count;23unsigned long long thread_count;24unsigned long long user_count;2526unsigned long long cur_irq;27unsigned long long min_irq;28unsigned long long sum_irq;29unsigned long long max_irq;3031unsigned long long cur_thread;32unsigned long long min_thread;33unsigned long long sum_thread;34unsigned long long max_thread;3536unsigned long long cur_user;37unsigned long long min_user;38unsigned long long sum_user;39unsigned long long max_user;40};4142struct timerlat_top_data {43struct timerlat_top_cpu *cpu_data;44int nr_cpus;45};4647/*48* timerlat_free_top - free runtime data49*/50static void timerlat_free_top(struct timerlat_top_data *data)51{52free(data->cpu_data);53free(data);54}5556static void timerlat_free_top_tool(struct osnoise_tool *tool)57{58timerlat_free_top(tool->data);59timerlat_free(tool);60}6162/*63* timerlat_alloc_histogram - alloc runtime data64*/65static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)66{67struct timerlat_top_data *data;68int cpu;6970data = calloc(1, sizeof(*data));71if (!data)72return NULL;7374data->nr_cpus = nr_cpus;7576/* one set of histograms per CPU */77data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);78if (!data->cpu_data)79goto cleanup;8081/* set the min to max */82for (cpu = 0; cpu < nr_cpus; cpu++) {83data->cpu_data[cpu].min_irq = ~0;84data->cpu_data[cpu].min_thread = ~0;85data->cpu_data[cpu].min_user = ~0;86}8788return data;8990cleanup:91timerlat_free_top(data);92return NULL;93}9495static void96timerlat_top_reset_sum(struct timerlat_top_cpu *summary)97{98memset(summary, 0, sizeof(*summary));99summary->min_irq = ~0;100summary->min_thread = ~0;101summary->min_user = ~0;102}103104static void105timerlat_top_update_sum(struct osnoise_tool *tool, int cpu, struct timerlat_top_cpu *sum)106{107struct timerlat_top_data *data = tool->data;108struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];109110sum->irq_count += cpu_data->irq_count;111update_min(&sum->min_irq, &cpu_data->min_irq);112update_sum(&sum->sum_irq, &cpu_data->sum_irq);113update_max(&sum->max_irq, &cpu_data->max_irq);114115sum->thread_count += cpu_data->thread_count;116update_min(&sum->min_thread, &cpu_data->min_thread);117update_sum(&sum->sum_thread, &cpu_data->sum_thread);118update_max(&sum->max_thread, &cpu_data->max_thread);119120sum->user_count += cpu_data->user_count;121update_min(&sum->min_user, &cpu_data->min_user);122update_sum(&sum->sum_user, &cpu_data->sum_user);123update_max(&sum->max_user, &cpu_data->max_user);124}125126/*127* timerlat_hist_update - record a new timerlat occurent on cpu, updating data128*/129static void130timerlat_top_update(struct osnoise_tool *tool, int cpu,131unsigned long long thread,132unsigned long long latency)133{134struct timerlat_params *params = to_timerlat_params(tool->params);135struct timerlat_top_data *data = tool->data;136struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];137138if (params->common.output_divisor)139latency = latency / params->common.output_divisor;140141if (!thread) {142cpu_data->irq_count++;143cpu_data->cur_irq = latency;144update_min(&cpu_data->min_irq, &latency);145update_sum(&cpu_data->sum_irq, &latency);146update_max(&cpu_data->max_irq, &latency);147} else if (thread == 1) {148cpu_data->thread_count++;149cpu_data->cur_thread = latency;150update_min(&cpu_data->min_thread, &latency);151update_sum(&cpu_data->sum_thread, &latency);152update_max(&cpu_data->max_thread, &latency);153} else {154cpu_data->user_count++;155cpu_data->cur_user = latency;156update_min(&cpu_data->min_user, &latency);157update_sum(&cpu_data->sum_user, &latency);158update_max(&cpu_data->max_user, &latency);159}160}161162/*163* timerlat_top_handler - this is the handler for timerlat tracer events164*/165static int166timerlat_top_handler(struct trace_seq *s, struct tep_record *record,167struct tep_event *event, void *context)168{169struct trace_instance *trace = context;170unsigned long long latency, thread;171struct osnoise_tool *top;172int cpu = record->cpu;173174top = container_of(trace, struct osnoise_tool, trace);175176if (!top->params->aa_only) {177tep_get_field_val(s, event, "context", record, &thread, 1);178tep_get_field_val(s, event, "timer_latency", record, &latency, 1);179180timerlat_top_update(top, cpu, thread, latency);181}182183return 0;184}185186/*187* timerlat_top_bpf_pull_data - copy data from BPF maps into userspace188*/189static int timerlat_top_bpf_pull_data(struct osnoise_tool *tool)190{191struct timerlat_top_data *data = tool->data;192int i, err;193long long value_irq[data->nr_cpus],194value_thread[data->nr_cpus],195value_user[data->nr_cpus];196197/* Pull summary */198err = timerlat_bpf_get_summary_value(SUMMARY_CURRENT,199value_irq, value_thread, value_user,200data->nr_cpus);201if (err)202return err;203for (i = 0; i < data->nr_cpus; i++) {204data->cpu_data[i].cur_irq = value_irq[i];205data->cpu_data[i].cur_thread = value_thread[i];206data->cpu_data[i].cur_user = value_user[i];207}208209err = timerlat_bpf_get_summary_value(SUMMARY_COUNT,210value_irq, value_thread, value_user,211data->nr_cpus);212if (err)213return err;214for (i = 0; i < data->nr_cpus; i++) {215data->cpu_data[i].irq_count = value_irq[i];216data->cpu_data[i].thread_count = value_thread[i];217data->cpu_data[i].user_count = value_user[i];218}219220err = timerlat_bpf_get_summary_value(SUMMARY_MIN,221value_irq, value_thread, value_user,222data->nr_cpus);223if (err)224return err;225for (i = 0; i < data->nr_cpus; i++) {226data->cpu_data[i].min_irq = value_irq[i];227data->cpu_data[i].min_thread = value_thread[i];228data->cpu_data[i].min_user = value_user[i];229}230231err = timerlat_bpf_get_summary_value(SUMMARY_MAX,232value_irq, value_thread, value_user,233data->nr_cpus);234if (err)235return err;236for (i = 0; i < data->nr_cpus; i++) {237data->cpu_data[i].max_irq = value_irq[i];238data->cpu_data[i].max_thread = value_thread[i];239data->cpu_data[i].max_user = value_user[i];240}241242err = timerlat_bpf_get_summary_value(SUMMARY_SUM,243value_irq, value_thread, value_user,244data->nr_cpus);245if (err)246return err;247for (i = 0; i < data->nr_cpus; i++) {248data->cpu_data[i].sum_irq = value_irq[i];249data->cpu_data[i].sum_thread = value_thread[i];250data->cpu_data[i].sum_user = value_user[i];251}252253return 0;254}255256/*257* timerlat_top_header - print the header of the tool output258*/259static void timerlat_top_header(struct timerlat_params *params, struct osnoise_tool *top)260{261struct trace_seq *s = top->trace.seq;262bool pretty = params->common.pretty_output;263char duration[26];264265get_duration(top->start_time, duration, sizeof(duration));266267if (pretty)268trace_seq_printf(s, "\033[2;37;40m");269270trace_seq_printf(s, " Timer Latency ");271if (params->common.user_data)272trace_seq_printf(s, " ");273274if (pretty)275trace_seq_printf(s, "\033[0;0;0m");276trace_seq_printf(s, "\n");277278trace_seq_printf(s, "%-6s | IRQ Timer Latency (%s) | Thread Timer Latency (%s)", duration,279params->common.output_divisor == 1 ? "ns" : "us",280params->common.output_divisor == 1 ? "ns" : "us");281282if (params->common.user_data) {283trace_seq_printf(s, " | Ret user Timer Latency (%s)",284params->common.output_divisor == 1 ? "ns" : "us");285}286287trace_seq_printf(s, "\n");288if (pretty)289trace_seq_printf(s, "\033[2;30;47m");290291trace_seq_printf(s, "CPU COUNT | cur min avg max | cur min avg max");292if (params->common.user_data)293trace_seq_printf(s, " | cur min avg max");294295if (pretty)296trace_seq_printf(s, "\033[0;0;0m");297trace_seq_printf(s, "\n");298}299300static const char *no_value = " -";301302/*303* timerlat_top_print - prints the output of a given CPU304*/305static void timerlat_top_print(struct osnoise_tool *top, int cpu)306{307struct timerlat_params *params = to_timerlat_params(top->params);308struct timerlat_top_data *data = top->data;309struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];310struct trace_seq *s = top->trace.seq;311312/*313* Skip if no data is available: is this cpu offline?314*/315if (!cpu_data->irq_count && !cpu_data->thread_count)316return;317318/*319* Unless trace is being lost, IRQ counter is always the max.320*/321trace_seq_printf(s, "%3d #%-9llu |", cpu, cpu_data->irq_count);322323if (!cpu_data->irq_count) {324trace_seq_printf(s, "%s %s %s %s |", no_value, no_value, no_value, no_value);325} else {326trace_seq_printf(s, "%9llu ", cpu_data->cur_irq);327trace_seq_printf(s, "%9llu ", cpu_data->min_irq);328trace_seq_printf(s, "%9llu ", cpu_data->sum_irq / cpu_data->irq_count);329trace_seq_printf(s, "%9llu |", cpu_data->max_irq);330}331332if (!cpu_data->thread_count) {333trace_seq_printf(s, "%s %s %s %s", no_value, no_value, no_value, no_value);334} else {335trace_seq_printf(s, "%9llu ", cpu_data->cur_thread);336trace_seq_printf(s, "%9llu ", cpu_data->min_thread);337trace_seq_printf(s, "%9llu ",338cpu_data->sum_thread / cpu_data->thread_count);339trace_seq_printf(s, "%9llu", cpu_data->max_thread);340}341342if (!params->common.user_data) {343trace_seq_printf(s, "\n");344return;345}346347trace_seq_printf(s, " |");348349if (!cpu_data->user_count) {350trace_seq_printf(s, "%s %s %s %s\n", no_value, no_value, no_value, no_value);351} else {352trace_seq_printf(s, "%9llu ", cpu_data->cur_user);353trace_seq_printf(s, "%9llu ", cpu_data->min_user);354trace_seq_printf(s, "%9llu ",355cpu_data->sum_user / cpu_data->user_count);356trace_seq_printf(s, "%9llu\n", cpu_data->max_user);357}358}359360/*361* timerlat_top_print_sum - prints the summary output362*/363static void364timerlat_top_print_sum(struct osnoise_tool *top, struct timerlat_top_cpu *summary)365{366const char *split = "----------------------------------------";367struct timerlat_params *params = to_timerlat_params(top->params);368unsigned long long count = summary->irq_count;369struct trace_seq *s = top->trace.seq;370int e = 0;371372/*373* Skip if no data is available: is this cpu offline?374*/375if (!summary->irq_count && !summary->thread_count)376return;377378while (count > 999999) {379e++;380count /= 10;381}382383trace_seq_printf(s, "%.*s|%.*s|%.*s", 15, split, 40, split, 39, split);384if (params->common.user_data)385trace_seq_printf(s, "-|%.*s", 39, split);386trace_seq_printf(s, "\n");387388trace_seq_printf(s, "ALL #%-6llu e%d |", count, e);389390if (!summary->irq_count) {391trace_seq_printf(s, " %s %s %s |", no_value, no_value, no_value);392} else {393trace_seq_printf(s, " ");394trace_seq_printf(s, "%9llu ", summary->min_irq);395trace_seq_printf(s, "%9llu ", summary->sum_irq / summary->irq_count);396trace_seq_printf(s, "%9llu |", summary->max_irq);397}398399if (!summary->thread_count) {400trace_seq_printf(s, "%s %s %s %s", no_value, no_value, no_value, no_value);401} else {402trace_seq_printf(s, " ");403trace_seq_printf(s, "%9llu ", summary->min_thread);404trace_seq_printf(s, "%9llu ",405summary->sum_thread / summary->thread_count);406trace_seq_printf(s, "%9llu", summary->max_thread);407}408409if (!params->common.user_data) {410trace_seq_printf(s, "\n");411return;412}413414trace_seq_printf(s, " |");415416if (!summary->user_count) {417trace_seq_printf(s, " %s %s %s |", no_value, no_value, no_value);418} else {419trace_seq_printf(s, " ");420trace_seq_printf(s, "%9llu ", summary->min_user);421trace_seq_printf(s, "%9llu ",422summary->sum_user / summary->user_count);423trace_seq_printf(s, "%9llu\n", summary->max_user);424}425}426427/*428* clear_terminal - clears the output terminal429*/430static void clear_terminal(struct trace_seq *seq)431{432if (!config_debug)433trace_seq_printf(seq, "\033c");434}435436/*437* timerlat_print_stats - print data for all cpus438*/439static void440timerlat_print_stats(struct osnoise_tool *top)441{442struct timerlat_params *params = to_timerlat_params(top->params);443struct trace_instance *trace = &top->trace;444struct timerlat_top_cpu summary;445static int nr_cpus = -1;446int i;447448if (params->common.aa_only)449return;450451if (nr_cpus == -1)452nr_cpus = sysconf(_SC_NPROCESSORS_CONF);453454if (!params->common.quiet)455clear_terminal(trace->seq);456457timerlat_top_reset_sum(&summary);458459timerlat_top_header(params, top);460461for (i = 0; i < nr_cpus; i++) {462if (params->common.cpus && !CPU_ISSET(i, ¶ms->common.monitored_cpus))463continue;464timerlat_top_print(top, i);465timerlat_top_update_sum(top, i, &summary);466}467468timerlat_top_print_sum(top, &summary);469470trace_seq_do_printf(trace->seq);471trace_seq_reset(trace->seq);472osnoise_report_missed_events(top);473}474475/*476* timerlat_top_usage - prints timerlat top usage message477*/478static void timerlat_top_usage(char *usage)479{480int i;481482static const char *const msg[] = {483"",484" usage: rtla timerlat [top] [-h] [-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\",485" [[-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",486" [-P priority] [--dma-latency us] [--aa-only us] [-C[=cgroup_name]] [-u|-k] [--warm-up s] [--deepest-idle-state n]",487"",488" -h/--help: print this menu",489" -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",490" --aa-only us: stop if <us> latency is hit, only printing the auto analysis (reduces CPU usage)",491" -p/--period us: timerlat period in us",492" -i/--irq us: stop trace if the irq latency is higher than the argument in us",493" -T/--thread us: stop trace if the thread latency is higher than the argument in us",494" -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",495" -c/--cpus cpus: run the tracer only on the given cpus",496" -H/--house-keeping cpus: run rtla control threads only on the given cpus",497" -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",498" -d/--duration time[s|m|h|d]: duration of the session",499" -D/--debug: print debug info",500" --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",501" -t/--trace[file]: save the stopped trace to [file|timerlat_trace.txt]",502" -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",503" --filter <command>: enable a trace event filter to the previous -e event",504" --trigger <command>: enable a trace event trigger to the previous -e event",505" -n/--nano: display data in nanoseconds",506" --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",507" -q/--quiet print only a summary at the end",508" --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",509" -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",510" o:prio - use SCHED_OTHER with prio",511" r:prio - use SCHED_RR with prio",512" f:prio - use SCHED_FIFO with prio",513" d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",514" in nanoseconds",515" -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads",516" -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads",517" -U/--user-load: enable timerlat for user-defined user-space workload",518" --warm-up s: let the workload run for s seconds before collecting data",519" --trace-buffer-size kB: set the per-cpu trace buffer size in kB",520" --deepest-idle-state n: only go down to idle state n on cpus used by timerlat to reduce exit from idle latency",521" --on-threshold <action>: define action to be executed at latency threshold, multiple are allowed",522" --on-end: define action to be executed at measurement end, multiple are allowed",523NULL,524};525526if (usage)527fprintf(stderr, "%s\n", usage);528529fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",530VERSION);531532for (i = 0; msg[i]; i++)533fprintf(stderr, "%s\n", msg[i]);534535if (usage)536exit(EXIT_FAILURE);537538exit(EXIT_SUCCESS);539}540541/*542* timerlat_top_parse_args - allocs, parse and fill the cmd line parameters543*/544static struct common_params545*timerlat_top_parse_args(int argc, char **argv)546{547struct timerlat_params *params;548struct trace_events *tevent;549long long auto_thresh;550int retval;551int c;552char *trace_output = NULL;553554params = calloc(1, sizeof(*params));555if (!params)556exit(1);557558actions_init(¶ms->common.threshold_actions);559actions_init(¶ms->common.end_actions);560561/* disabled by default */562params->dma_latency = -1;563564/* disabled by default */565params->deepest_idle_state = -2;566567/* display data in microseconds */568params->common.output_divisor = 1000;569570/* default to BPF mode */571params->mode = TRACING_MODE_BPF;572573while (1) {574static struct option long_options[] = {575{"auto", required_argument, 0, 'a'},576{"cpus", required_argument, 0, 'c'},577{"cgroup", optional_argument, 0, 'C'},578{"debug", no_argument, 0, 'D'},579{"duration", required_argument, 0, 'd'},580{"event", required_argument, 0, 'e'},581{"help", no_argument, 0, 'h'},582{"house-keeping", required_argument, 0, 'H'},583{"irq", required_argument, 0, 'i'},584{"nano", no_argument, 0, 'n'},585{"period", required_argument, 0, 'p'},586{"priority", required_argument, 0, 'P'},587{"quiet", no_argument, 0, 'q'},588{"stack", required_argument, 0, 's'},589{"thread", required_argument, 0, 'T'},590{"trace", optional_argument, 0, 't'},591{"user-threads", no_argument, 0, 'u'},592{"kernel-threads", no_argument, 0, 'k'},593{"user-load", no_argument, 0, 'U'},594{"trigger", required_argument, 0, '0'},595{"filter", required_argument, 0, '1'},596{"dma-latency", required_argument, 0, '2'},597{"no-aa", no_argument, 0, '3'},598{"dump-tasks", no_argument, 0, '4'},599{"aa-only", required_argument, 0, '5'},600{"warm-up", required_argument, 0, '6'},601{"trace-buffer-size", required_argument, 0, '7'},602{"deepest-idle-state", required_argument, 0, '8'},603{"on-threshold", required_argument, 0, '9'},604{"on-end", required_argument, 0, '\1'},605{0, 0, 0, 0}606};607608/* getopt_long stores the option index here. */609int option_index = 0;610611c = getopt_long(argc, argv, "a:c:C::d:De:hH:i:knp:P:qs:t::T:uU0:1:2:345:6:7:",612long_options, &option_index);613614/* detect the end of the options. */615if (c == -1)616break;617618switch (c) {619case 'a':620auto_thresh = get_llong_from_str(optarg);621622/* set thread stop to auto_thresh */623params->common.stop_total_us = auto_thresh;624params->common.stop_us = auto_thresh;625626/* get stack trace */627params->print_stack = auto_thresh;628629/* set trace */630trace_output = "timerlat_trace.txt";631632break;633case '5':634/* it is here because it is similar to -a */635auto_thresh = get_llong_from_str(optarg);636637/* set thread stop to auto_thresh */638params->common.stop_total_us = auto_thresh;639params->common.stop_us = auto_thresh;640641/* get stack trace */642params->print_stack = auto_thresh;643644/* set aa_only to avoid parsing the trace */645params->common.aa_only = 1;646break;647case 'c':648retval = parse_cpu_set(optarg, ¶ms->common.monitored_cpus);649if (retval)650timerlat_top_usage("\nInvalid -c cpu list\n");651params->common.cpus = optarg;652break;653case 'C':654params->common.cgroup = 1;655if (!optarg) {656/* will inherit this cgroup */657params->common.cgroup_name = NULL;658} else if (*optarg == '=') {659/* skip the = */660params->common.cgroup_name = ++optarg;661}662break;663case 'D':664config_debug = 1;665break;666case 'd':667params->common.duration = parse_seconds_duration(optarg);668if (!params->common.duration)669timerlat_top_usage("Invalid -d duration\n");670break;671case 'e':672tevent = trace_event_alloc(optarg);673if (!tevent) {674err_msg("Error alloc trace event");675exit(EXIT_FAILURE);676}677678if (params->common.events)679tevent->next = params->common.events;680params->common.events = tevent;681break;682case 'h':683case '?':684timerlat_top_usage(NULL);685break;686case 'H':687params->common.hk_cpus = 1;688retval = parse_cpu_set(optarg, ¶ms->common.hk_cpu_set);689if (retval) {690err_msg("Error parsing house keeping CPUs\n");691exit(EXIT_FAILURE);692}693break;694case 'i':695params->common.stop_us = get_llong_from_str(optarg);696break;697case 'k':698params->common.kernel_workload = true;699break;700case 'n':701params->common.output_divisor = 1;702break;703case 'p':704params->timerlat_period_us = get_llong_from_str(optarg);705if (params->timerlat_period_us > 1000000)706timerlat_top_usage("Period longer than 1 s\n");707break;708case 'P':709retval = parse_prio(optarg, ¶ms->common.sched_param);710if (retval == -1)711timerlat_top_usage("Invalid -P priority");712params->common.set_sched = 1;713break;714case 'q':715params->common.quiet = 1;716break;717case 's':718params->print_stack = get_llong_from_str(optarg);719break;720case 'T':721params->common.stop_total_us = get_llong_from_str(optarg);722break;723case 't':724if (optarg) {725if (optarg[0] == '=')726trace_output = &optarg[1];727else728trace_output = &optarg[0];729} else if (optind < argc && argv[optind][0] != '-')730trace_output = argv[optind];731else732trace_output = "timerlat_trace.txt";733break;734case 'u':735params->common.user_workload = true;736/* fallback: -u implies -U */737case 'U':738params->common.user_data = true;739break;740case '0': /* trigger */741if (params->common.events) {742retval = trace_event_add_trigger(params->common.events, optarg);743if (retval) {744err_msg("Error adding trigger %s\n", optarg);745exit(EXIT_FAILURE);746}747} else {748timerlat_top_usage("--trigger requires a previous -e\n");749}750break;751case '1': /* filter */752if (params->common.events) {753retval = trace_event_add_filter(params->common.events, optarg);754if (retval) {755err_msg("Error adding filter %s\n", optarg);756exit(EXIT_FAILURE);757}758} else {759timerlat_top_usage("--filter requires a previous -e\n");760}761break;762case '2': /* dma-latency */763params->dma_latency = get_llong_from_str(optarg);764if (params->dma_latency < 0 || params->dma_latency > 10000) {765err_msg("--dma-latency needs to be >= 0 and < 10000");766exit(EXIT_FAILURE);767}768break;769case '3': /* no-aa */770params->no_aa = 1;771break;772case '4':773params->dump_tasks = 1;774break;775case '6':776params->common.warmup = get_llong_from_str(optarg);777break;778case '7':779params->common.buffer_size = get_llong_from_str(optarg);780break;781case '8':782params->deepest_idle_state = get_llong_from_str(optarg);783break;784case '9':785retval = actions_parse(¶ms->common.threshold_actions, optarg,786"timerlat_trace.txt");787if (retval) {788err_msg("Invalid action %s\n", optarg);789exit(EXIT_FAILURE);790}791break;792case '\1':793retval = actions_parse(¶ms->common.end_actions, optarg,794"timerlat_trace.txt");795if (retval) {796err_msg("Invalid action %s\n", optarg);797exit(EXIT_FAILURE);798}799break;800default:801timerlat_top_usage("Invalid option");802}803}804805if (trace_output)806actions_add_trace_output(¶ms->common.threshold_actions, trace_output);807808if (geteuid()) {809err_msg("rtla needs root permission\n");810exit(EXIT_FAILURE);811}812813/*814* Auto analysis only happens if stop tracing, thus:815*/816if (!params->common.stop_us && !params->common.stop_total_us)817params->no_aa = 1;818819if (params->no_aa && params->common.aa_only)820timerlat_top_usage("--no-aa and --aa-only are mutually exclusive!");821822if (params->common.kernel_workload && params->common.user_workload)823timerlat_top_usage("--kernel-threads and --user-threads are mutually exclusive!");824825/*826* If auto-analysis or trace output is enabled, switch from BPF mode to827* mixed mode828*/829if (params->mode == TRACING_MODE_BPF &&830(params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] ||831params->common.end_actions.present[ACTION_TRACE_OUTPUT] ||832!params->no_aa))833params->mode = TRACING_MODE_MIXED;834835return ¶ms->common;836}837838/*839* timerlat_top_apply_config - apply the top configs to the initialized tool840*/841static int842timerlat_top_apply_config(struct osnoise_tool *top)843{844struct timerlat_params *params = to_timerlat_params(top->params);845int retval;846847retval = timerlat_apply_config(top, params);848if (retval)849goto out_err;850851if (isatty(STDOUT_FILENO) && !params->common.quiet)852params->common.pretty_output = 1;853854return 0;855856out_err:857return -1;858}859860/*861* timerlat_init_top - initialize a timerlat top tool with parameters862*/863static struct osnoise_tool864*timerlat_init_top(struct common_params *params)865{866struct osnoise_tool *top;867int nr_cpus;868869nr_cpus = sysconf(_SC_NPROCESSORS_CONF);870871top = osnoise_init_tool("timerlat_top");872if (!top)873return NULL;874875top->data = timerlat_alloc_top(nr_cpus);876if (!top->data)877goto out_err;878879tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",880timerlat_top_handler, top);881882return top;883884out_err:885osnoise_destroy_tool(top);886return NULL;887}888889/*890* timerlat_top_bpf_main_loop - main loop to process events (BPF variant)891*/892static int893timerlat_top_bpf_main_loop(struct osnoise_tool *tool)894{895struct timerlat_params *params = to_timerlat_params(tool->params);896int retval, wait_retval;897898if (params->common.aa_only) {899/* Auto-analysis only, just wait for stop tracing */900timerlat_bpf_wait(-1);901return 0;902}903904/* Pull and display data in a loop */905while (!stop_tracing) {906wait_retval = timerlat_bpf_wait(params->common.quiet ? -1 :907params->common.sleep_time);908909retval = timerlat_top_bpf_pull_data(tool);910if (retval) {911err_msg("Error pulling BPF data\n");912return retval;913}914915if (!params->common.quiet)916timerlat_print_stats(tool);917918if (wait_retval == 1) {919/* Stopping requested by tracer */920actions_perform(¶ms->common.threshold_actions);921922if (!params->common.threshold_actions.continue_flag)923/* continue flag not set, break */924break;925926/* continue action reached, re-enable tracing */927if (tool->record)928trace_instance_start(&tool->record->trace);929if (tool->aa)930trace_instance_start(&tool->aa->trace);931timerlat_bpf_restart_tracing();932}933934/* is there still any user-threads ? */935if (params->common.user_workload) {936if (params->common.user.stopped_running) {937debug_msg("timerlat user space threads stopped!\n");938break;939}940}941}942943return 0;944}945946static int timerlat_top_main_loop(struct osnoise_tool *tool)947{948struct timerlat_params *params = to_timerlat_params(tool->params);949int retval;950951if (params->mode == TRACING_MODE_TRACEFS) {952retval = top_main_loop(tool);953} else {954retval = timerlat_top_bpf_main_loop(tool);955timerlat_bpf_detach();956}957958return retval;959}960961struct tool_ops timerlat_top_ops = {962.tracer = "timerlat",963.comm_prefix = "timerlat/",964.parse_args = timerlat_top_parse_args,965.init_tool = timerlat_init_top,966.apply_config = timerlat_top_apply_config,967.enable = timerlat_enable,968.main = timerlat_top_main_loop,969.print_stats = timerlat_print_stats,970.analyze = timerlat_analyze,971.free = timerlat_free_top_tool,972};973974975