Path: blob/master/tools/tracing/rtla/src/timerlat_top.c
26285 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_u.h"20#include "timerlat_bpf.h"2122struct timerlat_top_cpu {23unsigned long long irq_count;24unsigned long long thread_count;25unsigned long long user_count;2627unsigned long long cur_irq;28unsigned long long min_irq;29unsigned long long sum_irq;30unsigned long long max_irq;3132unsigned long long cur_thread;33unsigned long long min_thread;34unsigned long long sum_thread;35unsigned long long max_thread;3637unsigned long long cur_user;38unsigned long long min_user;39unsigned long long sum_user;40unsigned long long max_user;41};4243struct timerlat_top_data {44struct timerlat_top_cpu *cpu_data;45int nr_cpus;46};4748/*49* timerlat_free_top - free runtime data50*/51static void52timerlat_free_top(struct timerlat_top_data *data)53{54free(data->cpu_data);55free(data);56}5758/*59* timerlat_alloc_histogram - alloc runtime data60*/61static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)62{63struct timerlat_top_data *data;64int cpu;6566data = calloc(1, sizeof(*data));67if (!data)68return NULL;6970data->nr_cpus = nr_cpus;7172/* one set of histograms per CPU */73data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);74if (!data->cpu_data)75goto cleanup;7677/* set the min to max */78for (cpu = 0; cpu < nr_cpus; cpu++) {79data->cpu_data[cpu].min_irq = ~0;80data->cpu_data[cpu].min_thread = ~0;81data->cpu_data[cpu].min_user = ~0;82}8384return data;8586cleanup:87timerlat_free_top(data);88return NULL;89}9091static void92timerlat_top_reset_sum(struct timerlat_top_cpu *summary)93{94memset(summary, 0, sizeof(*summary));95summary->min_irq = ~0;96summary->min_thread = ~0;97summary->min_user = ~0;98}99100static void101timerlat_top_update_sum(struct osnoise_tool *tool, int cpu, struct timerlat_top_cpu *sum)102{103struct timerlat_top_data *data = tool->data;104struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];105106sum->irq_count += cpu_data->irq_count;107update_min(&sum->min_irq, &cpu_data->min_irq);108update_sum(&sum->sum_irq, &cpu_data->sum_irq);109update_max(&sum->max_irq, &cpu_data->max_irq);110111sum->thread_count += cpu_data->thread_count;112update_min(&sum->min_thread, &cpu_data->min_thread);113update_sum(&sum->sum_thread, &cpu_data->sum_thread);114update_max(&sum->max_thread, &cpu_data->max_thread);115116sum->user_count += cpu_data->user_count;117update_min(&sum->min_user, &cpu_data->min_user);118update_sum(&sum->sum_user, &cpu_data->sum_user);119update_max(&sum->max_user, &cpu_data->max_user);120}121122/*123* timerlat_hist_update - record a new timerlat occurent on cpu, updating data124*/125static void126timerlat_top_update(struct osnoise_tool *tool, int cpu,127unsigned long long thread,128unsigned long long latency)129{130struct timerlat_params *params = tool->params;131struct timerlat_top_data *data = tool->data;132struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];133134if (params->output_divisor)135latency = latency / params->output_divisor;136137if (!thread) {138cpu_data->irq_count++;139cpu_data->cur_irq = latency;140update_min(&cpu_data->min_irq, &latency);141update_sum(&cpu_data->sum_irq, &latency);142update_max(&cpu_data->max_irq, &latency);143} else if (thread == 1) {144cpu_data->thread_count++;145cpu_data->cur_thread = latency;146update_min(&cpu_data->min_thread, &latency);147update_sum(&cpu_data->sum_thread, &latency);148update_max(&cpu_data->max_thread, &latency);149} else {150cpu_data->user_count++;151cpu_data->cur_user = latency;152update_min(&cpu_data->min_user, &latency);153update_sum(&cpu_data->sum_user, &latency);154update_max(&cpu_data->max_user, &latency);155}156}157158/*159* timerlat_top_handler - this is the handler for timerlat tracer events160*/161static int162timerlat_top_handler(struct trace_seq *s, struct tep_record *record,163struct tep_event *event, void *context)164{165struct trace_instance *trace = context;166struct timerlat_params *params;167unsigned long long latency, thread;168struct osnoise_tool *top;169int cpu = record->cpu;170171top = container_of(trace, struct osnoise_tool, trace);172params = top->params;173174if (!params->aa_only) {175tep_get_field_val(s, event, "context", record, &thread, 1);176tep_get_field_val(s, event, "timer_latency", record, &latency, 1);177178timerlat_top_update(top, cpu, thread, latency);179}180181return 0;182}183184/*185* timerlat_top_bpf_pull_data - copy data from BPF maps into userspace186*/187static int timerlat_top_bpf_pull_data(struct osnoise_tool *tool)188{189struct timerlat_top_data *data = tool->data;190int i, err;191long long value_irq[data->nr_cpus],192value_thread[data->nr_cpus],193value_user[data->nr_cpus];194195/* Pull summary */196err = timerlat_bpf_get_summary_value(SUMMARY_CURRENT,197value_irq, value_thread, value_user,198data->nr_cpus);199if (err)200return err;201for (i = 0; i < data->nr_cpus; i++) {202data->cpu_data[i].cur_irq = value_irq[i];203data->cpu_data[i].cur_thread = value_thread[i];204data->cpu_data[i].cur_user = value_user[i];205}206207err = timerlat_bpf_get_summary_value(SUMMARY_COUNT,208value_irq, value_thread, value_user,209data->nr_cpus);210if (err)211return err;212for (i = 0; i < data->nr_cpus; i++) {213data->cpu_data[i].irq_count = value_irq[i];214data->cpu_data[i].thread_count = value_thread[i];215data->cpu_data[i].user_count = value_user[i];216}217218err = timerlat_bpf_get_summary_value(SUMMARY_MIN,219value_irq, value_thread, value_user,220data->nr_cpus);221if (err)222return err;223for (i = 0; i < data->nr_cpus; i++) {224data->cpu_data[i].min_irq = value_irq[i];225data->cpu_data[i].min_thread = value_thread[i];226data->cpu_data[i].min_user = value_user[i];227}228229err = timerlat_bpf_get_summary_value(SUMMARY_MAX,230value_irq, value_thread, value_user,231data->nr_cpus);232if (err)233return err;234for (i = 0; i < data->nr_cpus; i++) {235data->cpu_data[i].max_irq = value_irq[i];236data->cpu_data[i].max_thread = value_thread[i];237data->cpu_data[i].max_user = value_user[i];238}239240err = timerlat_bpf_get_summary_value(SUMMARY_SUM,241value_irq, value_thread, value_user,242data->nr_cpus);243if (err)244return err;245for (i = 0; i < data->nr_cpus; i++) {246data->cpu_data[i].sum_irq = value_irq[i];247data->cpu_data[i].sum_thread = value_thread[i];248data->cpu_data[i].sum_user = value_user[i];249}250251return 0;252}253254/*255* timerlat_top_header - print the header of the tool output256*/257static void timerlat_top_header(struct timerlat_params *params, struct osnoise_tool *top)258{259struct trace_seq *s = top->trace.seq;260char duration[26];261262get_duration(top->start_time, duration, sizeof(duration));263264if (params->pretty_output)265trace_seq_printf(s, "\033[2;37;40m");266267trace_seq_printf(s, " Timer Latency ");268if (params->user_data)269trace_seq_printf(s, " ");270271if (params->pretty_output)272trace_seq_printf(s, "\033[0;0;0m");273trace_seq_printf(s, "\n");274275trace_seq_printf(s, "%-6s | IRQ Timer Latency (%s) | Thread Timer Latency (%s)", duration,276params->output_divisor == 1 ? "ns" : "us",277params->output_divisor == 1 ? "ns" : "us");278279if (params->user_data) {280trace_seq_printf(s, " | Ret user Timer Latency (%s)",281params->output_divisor == 1 ? "ns" : "us");282}283284trace_seq_printf(s, "\n");285if (params->pretty_output)286trace_seq_printf(s, "\033[2;30;47m");287288trace_seq_printf(s, "CPU COUNT | cur min avg max | cur min avg max");289if (params->user_data)290trace_seq_printf(s, " | cur min avg max");291292if (params->pretty_output)293trace_seq_printf(s, "\033[0;0;0m");294trace_seq_printf(s, "\n");295}296297static const char *no_value = " -";298299/*300* timerlat_top_print - prints the output of a given CPU301*/302static void timerlat_top_print(struct osnoise_tool *top, int cpu)303{304305struct timerlat_params *params = top->params;306struct timerlat_top_data *data = top->data;307struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];308struct trace_seq *s = top->trace.seq;309310/*311* Skip if no data is available: is this cpu offline?312*/313if (!cpu_data->irq_count && !cpu_data->thread_count)314return;315316/*317* Unless trace is being lost, IRQ counter is always the max.318*/319trace_seq_printf(s, "%3d #%-9llu |", cpu, cpu_data->irq_count);320321if (!cpu_data->irq_count) {322trace_seq_printf(s, "%s %s %s %s |", no_value, no_value, no_value, no_value);323} else {324trace_seq_printf(s, "%9llu ", cpu_data->cur_irq);325trace_seq_printf(s, "%9llu ", cpu_data->min_irq);326trace_seq_printf(s, "%9llu ", cpu_data->sum_irq / cpu_data->irq_count);327trace_seq_printf(s, "%9llu |", cpu_data->max_irq);328}329330if (!cpu_data->thread_count) {331trace_seq_printf(s, "%s %s %s %s", no_value, no_value, no_value, no_value);332} else {333trace_seq_printf(s, "%9llu ", cpu_data->cur_thread);334trace_seq_printf(s, "%9llu ", cpu_data->min_thread);335trace_seq_printf(s, "%9llu ",336cpu_data->sum_thread / cpu_data->thread_count);337trace_seq_printf(s, "%9llu", cpu_data->max_thread);338}339340if (!params->user_data) {341trace_seq_printf(s, "\n");342return;343}344345trace_seq_printf(s, " |");346347if (!cpu_data->user_count) {348trace_seq_printf(s, "%s %s %s %s\n", no_value, no_value, no_value, no_value);349} else {350trace_seq_printf(s, "%9llu ", cpu_data->cur_user);351trace_seq_printf(s, "%9llu ", cpu_data->min_user);352trace_seq_printf(s, "%9llu ",353cpu_data->sum_user / cpu_data->user_count);354trace_seq_printf(s, "%9llu\n", cpu_data->max_user);355}356}357358/*359* timerlat_top_print_sum - prints the summary output360*/361static void362timerlat_top_print_sum(struct osnoise_tool *top, struct timerlat_top_cpu *summary)363{364const char *split = "----------------------------------------";365struct timerlat_params *params = top->params;366unsigned long long count = summary->irq_count;367struct trace_seq *s = top->trace.seq;368int e = 0;369370/*371* Skip if no data is available: is this cpu offline?372*/373if (!summary->irq_count && !summary->thread_count)374return;375376while (count > 999999) {377e++;378count /= 10;379}380381trace_seq_printf(s, "%.*s|%.*s|%.*s", 15, split, 40, split, 39, split);382if (params->user_data)383trace_seq_printf(s, "-|%.*s", 39, split);384trace_seq_printf(s, "\n");385386trace_seq_printf(s, "ALL #%-6llu e%d |", count, e);387388if (!summary->irq_count) {389trace_seq_printf(s, " %s %s %s |", no_value, no_value, no_value);390} else {391trace_seq_printf(s, " ");392trace_seq_printf(s, "%9llu ", summary->min_irq);393trace_seq_printf(s, "%9llu ", summary->sum_irq / summary->irq_count);394trace_seq_printf(s, "%9llu |", summary->max_irq);395}396397if (!summary->thread_count) {398trace_seq_printf(s, "%s %s %s %s", no_value, no_value, no_value, no_value);399} else {400trace_seq_printf(s, " ");401trace_seq_printf(s, "%9llu ", summary->min_thread);402trace_seq_printf(s, "%9llu ",403summary->sum_thread / summary->thread_count);404trace_seq_printf(s, "%9llu", summary->max_thread);405}406407if (!params->user_data) {408trace_seq_printf(s, "\n");409return;410}411412trace_seq_printf(s, " |");413414if (!summary->user_count) {415trace_seq_printf(s, " %s %s %s |", no_value, no_value, no_value);416} else {417trace_seq_printf(s, " ");418trace_seq_printf(s, "%9llu ", summary->min_user);419trace_seq_printf(s, "%9llu ",420summary->sum_user / summary->user_count);421trace_seq_printf(s, "%9llu\n", summary->max_user);422}423}424425/*426* clear_terminal - clears the output terminal427*/428static void clear_terminal(struct trace_seq *seq)429{430if (!config_debug)431trace_seq_printf(seq, "\033c");432}433434/*435* timerlat_print_stats - print data for all cpus436*/437static void438timerlat_print_stats(struct timerlat_params *params, struct osnoise_tool *top)439{440struct trace_instance *trace = &top->trace;441struct timerlat_top_cpu summary;442static int nr_cpus = -1;443int i;444445if (params->aa_only)446return;447448if (nr_cpus == -1)449nr_cpus = sysconf(_SC_NPROCESSORS_CONF);450451if (!params->quiet)452clear_terminal(trace->seq);453454timerlat_top_reset_sum(&summary);455456timerlat_top_header(params, top);457458for (i = 0; i < nr_cpus; i++) {459if (params->cpus && !CPU_ISSET(i, ¶ms->monitored_cpus))460continue;461timerlat_top_print(top, i);462timerlat_top_update_sum(top, i, &summary);463}464465timerlat_top_print_sum(top, &summary);466467trace_seq_do_printf(trace->seq);468trace_seq_reset(trace->seq);469osnoise_report_missed_events(top);470}471472/*473* timerlat_top_usage - prints timerlat top usage message474*/475static void timerlat_top_usage(char *usage)476{477int i;478479static const char *const msg[] = {480"",481" usage: rtla timerlat [top] [-h] [-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\",482" [[-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",483" [-P priority] [--dma-latency us] [--aa-only us] [-C[=cgroup_name]] [-u|-k] [--warm-up s] [--deepest-idle-state n]",484"",485" -h/--help: print this menu",486" -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",487" --aa-only us: stop if <us> latency is hit, only printing the auto analysis (reduces CPU usage)",488" -p/--period us: timerlat period in us",489" -i/--irq us: stop trace if the irq latency is higher than the argument in us",490" -T/--thread us: stop trace if the thread latency is higher than the argument in us",491" -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",492" -c/--cpus cpus: run the tracer only on the given cpus",493" -H/--house-keeping cpus: run rtla control threads only on the given cpus",494" -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",495" -d/--duration time[s|m|h|d]: duration of the session",496" -D/--debug: print debug info",497" --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",498" -t/--trace[file]: save the stopped trace to [file|timerlat_trace.txt]",499" -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",500" --filter <command>: enable a trace event filter to the previous -e event",501" --trigger <command>: enable a trace event trigger to the previous -e event",502" -n/--nano: display data in nanoseconds",503" --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",504" -q/--quiet print only a summary at the end",505" --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",506" -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",507" o:prio - use SCHED_OTHER with prio",508" r:prio - use SCHED_RR with prio",509" f:prio - use SCHED_FIFO with prio",510" d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",511" in nanoseconds",512" -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads",513" -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads",514" -U/--user-load: enable timerlat for user-defined user-space workload",515" --warm-up s: let the workload run for s seconds before collecting data",516" --trace-buffer-size kB: set the per-cpu trace buffer size in kB",517" --deepest-idle-state n: only go down to idle state n on cpus used by timerlat to reduce exit from idle latency",518" --on-threshold <action>: define action to be executed at latency threshold, multiple are allowed",519" --on-end: define action to be executed at measurement end, multiple are allowed",520NULL,521};522523if (usage)524fprintf(stderr, "%s\n", usage);525526fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",527VERSION);528529for (i = 0; msg[i]; i++)530fprintf(stderr, "%s\n", msg[i]);531532if (usage)533exit(EXIT_FAILURE);534535exit(EXIT_SUCCESS);536}537538/*539* timerlat_top_parse_args - allocs, parse and fill the cmd line parameters540*/541static struct timerlat_params542*timerlat_top_parse_args(int argc, char **argv)543{544struct timerlat_params *params;545struct trace_events *tevent;546long long auto_thresh;547int retval;548int c;549char *trace_output = NULL;550551params = calloc(1, sizeof(*params));552if (!params)553exit(1);554555actions_init(¶ms->threshold_actions);556actions_init(¶ms->end_actions);557558/* disabled by default */559params->dma_latency = -1;560561/* disabled by default */562params->deepest_idle_state = -2;563564/* display data in microseconds */565params->output_divisor = 1000;566567/* default to BPF mode */568params->mode = TRACING_MODE_BPF;569570while (1) {571static struct option long_options[] = {572{"auto", required_argument, 0, 'a'},573{"cpus", required_argument, 0, 'c'},574{"cgroup", optional_argument, 0, 'C'},575{"debug", no_argument, 0, 'D'},576{"duration", required_argument, 0, 'd'},577{"event", required_argument, 0, 'e'},578{"help", no_argument, 0, 'h'},579{"house-keeping", required_argument, 0, 'H'},580{"irq", required_argument, 0, 'i'},581{"nano", no_argument, 0, 'n'},582{"period", required_argument, 0, 'p'},583{"priority", required_argument, 0, 'P'},584{"quiet", no_argument, 0, 'q'},585{"stack", required_argument, 0, 's'},586{"thread", required_argument, 0, 'T'},587{"trace", optional_argument, 0, 't'},588{"user-threads", no_argument, 0, 'u'},589{"kernel-threads", no_argument, 0, 'k'},590{"user-load", no_argument, 0, 'U'},591{"trigger", required_argument, 0, '0'},592{"filter", required_argument, 0, '1'},593{"dma-latency", required_argument, 0, '2'},594{"no-aa", no_argument, 0, '3'},595{"dump-tasks", no_argument, 0, '4'},596{"aa-only", required_argument, 0, '5'},597{"warm-up", required_argument, 0, '6'},598{"trace-buffer-size", required_argument, 0, '7'},599{"deepest-idle-state", required_argument, 0, '8'},600{"on-threshold", required_argument, 0, '9'},601{"on-end", required_argument, 0, '\1'},602{0, 0, 0, 0}603};604605/* getopt_long stores the option index here. */606int option_index = 0;607608c = getopt_long(argc, argv, "a:c:C::d:De:hH:i:knp:P:qs:t::T:uU0:1:2:345:6:7:",609long_options, &option_index);610611/* detect the end of the options. */612if (c == -1)613break;614615switch (c) {616case 'a':617auto_thresh = get_llong_from_str(optarg);618619/* set thread stop to auto_thresh */620params->stop_total_us = auto_thresh;621params->stop_us = auto_thresh;622623/* get stack trace */624params->print_stack = auto_thresh;625626/* set trace */627trace_output = "timerlat_trace.txt";628629break;630case '5':631/* it is here because it is similar to -a */632auto_thresh = get_llong_from_str(optarg);633634/* set thread stop to auto_thresh */635params->stop_total_us = auto_thresh;636params->stop_us = auto_thresh;637638/* get stack trace */639params->print_stack = auto_thresh;640641/* set aa_only to avoid parsing the trace */642params->aa_only = 1;643break;644case 'c':645retval = parse_cpu_set(optarg, ¶ms->monitored_cpus);646if (retval)647timerlat_top_usage("\nInvalid -c cpu list\n");648params->cpus = optarg;649break;650case 'C':651params->cgroup = 1;652if (!optarg) {653/* will inherit this cgroup */654params->cgroup_name = NULL;655} else if (*optarg == '=') {656/* skip the = */657params->cgroup_name = ++optarg;658}659break;660case 'D':661config_debug = 1;662break;663case 'd':664params->duration = parse_seconds_duration(optarg);665if (!params->duration)666timerlat_top_usage("Invalid -d duration\n");667break;668case 'e':669tevent = trace_event_alloc(optarg);670if (!tevent) {671err_msg("Error alloc trace event");672exit(EXIT_FAILURE);673}674675if (params->events)676tevent->next = params->events;677params->events = tevent;678break;679case 'h':680case '?':681timerlat_top_usage(NULL);682break;683case 'H':684params->hk_cpus = 1;685retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set);686if (retval) {687err_msg("Error parsing house keeping CPUs\n");688exit(EXIT_FAILURE);689}690break;691case 'i':692params->stop_us = get_llong_from_str(optarg);693break;694case 'k':695params->kernel_workload = true;696break;697case 'n':698params->output_divisor = 1;699break;700case 'p':701params->timerlat_period_us = get_llong_from_str(optarg);702if (params->timerlat_period_us > 1000000)703timerlat_top_usage("Period longer than 1 s\n");704break;705case 'P':706retval = parse_prio(optarg, ¶ms->sched_param);707if (retval == -1)708timerlat_top_usage("Invalid -P priority");709params->set_sched = 1;710break;711case 'q':712params->quiet = 1;713break;714case 's':715params->print_stack = get_llong_from_str(optarg);716break;717case 'T':718params->stop_total_us = get_llong_from_str(optarg);719break;720case 't':721if (optarg) {722if (optarg[0] == '=')723trace_output = &optarg[1];724else725trace_output = &optarg[0];726} else if (optind < argc && argv[optind][0] != '-')727trace_output = argv[optind];728else729trace_output = "timerlat_trace.txt";730break;731case 'u':732params->user_workload = true;733/* fallback: -u implies -U */734case 'U':735params->user_data = true;736break;737case '0': /* trigger */738if (params->events) {739retval = trace_event_add_trigger(params->events, optarg);740if (retval) {741err_msg("Error adding trigger %s\n", optarg);742exit(EXIT_FAILURE);743}744} else {745timerlat_top_usage("--trigger requires a previous -e\n");746}747break;748case '1': /* filter */749if (params->events) {750retval = trace_event_add_filter(params->events, optarg);751if (retval) {752err_msg("Error adding filter %s\n", optarg);753exit(EXIT_FAILURE);754}755} else {756timerlat_top_usage("--filter requires a previous -e\n");757}758break;759case '2': /* dma-latency */760params->dma_latency = get_llong_from_str(optarg);761if (params->dma_latency < 0 || params->dma_latency > 10000) {762err_msg("--dma-latency needs to be >= 0 and < 10000");763exit(EXIT_FAILURE);764}765break;766case '3': /* no-aa */767params->no_aa = 1;768break;769case '4':770params->dump_tasks = 1;771break;772case '6':773params->warmup = get_llong_from_str(optarg);774break;775case '7':776params->buffer_size = get_llong_from_str(optarg);777break;778case '8':779params->deepest_idle_state = get_llong_from_str(optarg);780break;781case '9':782retval = actions_parse(¶ms->threshold_actions, optarg);783if (retval) {784err_msg("Invalid action %s\n", optarg);785exit(EXIT_FAILURE);786}787break;788case '\1':789retval = actions_parse(¶ms->end_actions, optarg);790if (retval) {791err_msg("Invalid action %s\n", optarg);792exit(EXIT_FAILURE);793}794break;795default:796timerlat_top_usage("Invalid option");797}798}799800if (trace_output)801actions_add_trace_output(¶ms->threshold_actions, trace_output);802803if (geteuid()) {804err_msg("rtla needs root permission\n");805exit(EXIT_FAILURE);806}807808/*809* Auto analysis only happens if stop tracing, thus:810*/811if (!params->stop_us && !params->stop_total_us)812params->no_aa = 1;813814if (params->no_aa && params->aa_only)815timerlat_top_usage("--no-aa and --aa-only are mutually exclusive!");816817if (params->kernel_workload && params->user_workload)818timerlat_top_usage("--kernel-threads and --user-threads are mutually exclusive!");819820/*821* If auto-analysis or trace output is enabled, switch from BPF mode to822* mixed mode823*/824if (params->mode == TRACING_MODE_BPF &&825(params->threshold_actions.present[ACTION_TRACE_OUTPUT] ||826params->end_actions.present[ACTION_TRACE_OUTPUT] || !params->no_aa))827params->mode = TRACING_MODE_MIXED;828829return params;830}831832/*833* timerlat_top_apply_config - apply the top configs to the initialized tool834*/835static int836timerlat_top_apply_config(struct osnoise_tool *top, struct timerlat_params *params)837{838int retval;839840retval = timerlat_apply_config(top, params);841if (retval)842goto out_err;843844if (isatty(STDOUT_FILENO) && !params->quiet)845params->pretty_output = 1;846847return 0;848849out_err:850return -1;851}852853/*854* timerlat_init_top - initialize a timerlat top tool with parameters855*/856static struct osnoise_tool857*timerlat_init_top(struct timerlat_params *params)858{859struct osnoise_tool *top;860int nr_cpus;861862nr_cpus = sysconf(_SC_NPROCESSORS_CONF);863864top = osnoise_init_tool("timerlat_top");865if (!top)866return NULL;867868top->data = timerlat_alloc_top(nr_cpus);869if (!top->data)870goto out_err;871872top->params = params;873874tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",875timerlat_top_handler, top);876877return top;878879out_err:880osnoise_destroy_tool(top);881return NULL;882}883884static int stop_tracing;885static struct trace_instance *top_inst = NULL;886static void stop_top(int sig)887{888if (stop_tracing) {889/*890* Stop requested twice in a row; abort event processing and891* exit immediately892*/893tracefs_iterate_stop(top_inst->inst);894return;895}896stop_tracing = 1;897if (top_inst)898trace_instance_stop(top_inst);899}900901/*902* timerlat_top_set_signals - handles the signal to stop the tool903*/904static void905timerlat_top_set_signals(struct timerlat_params *params)906{907signal(SIGINT, stop_top);908if (params->duration) {909signal(SIGALRM, stop_top);910alarm(params->duration);911}912}913914/*915* timerlat_top_main_loop - main loop to process events916*/917static int918timerlat_top_main_loop(struct osnoise_tool *top,919struct osnoise_tool *record,920struct osnoise_tool *aa,921struct timerlat_params *params,922struct timerlat_u_params *params_u)923{924struct trace_instance *trace = &top->trace;925int retval;926927while (!stop_tracing) {928sleep(params->sleep_time);929930if (params->aa_only && !osnoise_trace_is_off(top, record))931continue;932933retval = tracefs_iterate_raw_events(trace->tep,934trace->inst,935NULL,9360,937collect_registered_events,938trace);939if (retval < 0) {940err_msg("Error iterating on events\n");941return retval;942}943944if (!params->quiet)945timerlat_print_stats(params, top);946947if (osnoise_trace_is_off(top, record)) {948actions_perform(¶ms->threshold_actions);949950if (!params->threshold_actions.continue_flag)951/* continue flag not set, break */952break;953954/* continue action reached, re-enable tracing */955if (record)956trace_instance_start(&record->trace);957if (!params->no_aa)958trace_instance_start(&aa->trace);959trace_instance_start(trace);960}961962/* is there still any user-threads ? */963if (params->user_workload) {964if (params_u->stopped_running) {965debug_msg("timerlat user space threads stopped!\n");966break;967}968}969}970971return 0;972}973974/*975* timerlat_top_bpf_main_loop - main loop to process events (BPF variant)976*/977static int978timerlat_top_bpf_main_loop(struct osnoise_tool *top,979struct osnoise_tool *record,980struct osnoise_tool *aa,981struct timerlat_params *params,982struct timerlat_u_params *params_u)983{984int retval, wait_retval;985986if (params->aa_only) {987/* Auto-analysis only, just wait for stop tracing */988timerlat_bpf_wait(-1);989return 0;990}991992/* Pull and display data in a loop */993while (!stop_tracing) {994wait_retval = timerlat_bpf_wait(params->quiet ? -1 : params->sleep_time);995996retval = timerlat_top_bpf_pull_data(top);997if (retval) {998err_msg("Error pulling BPF data\n");999return retval;1000}10011002if (!params->quiet)1003timerlat_print_stats(params, top);10041005if (wait_retval == 1) {1006/* Stopping requested by tracer */1007actions_perform(¶ms->threshold_actions);10081009if (!params->threshold_actions.continue_flag)1010/* continue flag not set, break */1011break;10121013/* continue action reached, re-enable tracing */1014if (record)1015trace_instance_start(&record->trace);1016if (!params->no_aa)1017trace_instance_start(&aa->trace);1018timerlat_bpf_restart_tracing();1019}10201021/* is there still any user-threads ? */1022if (params->user_workload) {1023if (params_u->stopped_running) {1024debug_msg("timerlat user space threads stopped!\n");1025break;1026}1027}1028}10291030return 0;1031}10321033int timerlat_top_main(int argc, char *argv[])1034{1035struct timerlat_params *params;1036struct osnoise_tool *record = NULL;1037struct timerlat_u_params params_u;1038enum result return_value = ERROR;1039struct osnoise_tool *top = NULL;1040struct osnoise_tool *aa = NULL;1041struct trace_instance *trace;1042int dma_latency_fd = -1;1043pthread_t timerlat_u;1044char *max_lat;1045int retval;1046int nr_cpus, i;10471048params = timerlat_top_parse_args(argc, argv);1049if (!params)1050exit(1);10511052top = timerlat_init_top(params);1053if (!top) {1054err_msg("Could not init osnoise top\n");1055goto out_exit;1056}10571058trace = &top->trace;1059/*1060* Save trace instance into global variable so that SIGINT can stop1061* the timerlat tracer.1062* Otherwise, rtla could loop indefinitely when overloaded.1063*/1064top_inst = trace;10651066/*1067* Try to enable BPF, unless disabled explicitly.1068* If BPF enablement fails, fall back to tracefs mode.1069*/1070if (getenv("RTLA_NO_BPF") && strncmp(getenv("RTLA_NO_BPF"), "1", 2) == 0) {1071debug_msg("RTLA_NO_BPF set, disabling BPF\n");1072params->mode = TRACING_MODE_TRACEFS;1073} else if (!tep_find_event_by_name(trace->tep, "osnoise", "timerlat_sample")) {1074debug_msg("osnoise:timerlat_sample missing, disabling BPF\n");1075params->mode = TRACING_MODE_TRACEFS;1076} else {1077retval = timerlat_bpf_init(params);1078if (retval) {1079debug_msg("Could not enable BPF\n");1080params->mode = TRACING_MODE_TRACEFS;1081}1082}10831084retval = timerlat_top_apply_config(top, params);1085if (retval) {1086err_msg("Could not apply config\n");1087goto out_free;1088}10891090retval = enable_timerlat(trace);1091if (retval) {1092err_msg("Failed to enable timerlat tracer\n");1093goto out_free;1094}10951096if (params->set_sched) {1097retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param);1098if (retval) {1099err_msg("Failed to set sched parameters\n");1100goto out_free;1101}1102}11031104if (params->cgroup && !params->user_data) {1105retval = set_comm_cgroup("timerlat/", params->cgroup_name);1106if (!retval) {1107err_msg("Failed to move threads to cgroup\n");1108goto out_free;1109}1110}11111112if (params->dma_latency >= 0) {1113dma_latency_fd = set_cpu_dma_latency(params->dma_latency);1114if (dma_latency_fd < 0) {1115err_msg("Could not set /dev/cpu_dma_latency.\n");1116goto out_free;1117}1118}11191120if (params->deepest_idle_state >= -1) {1121if (!have_libcpupower_support()) {1122err_msg("rtla built without libcpupower, --deepest-idle-state is not supported\n");1123goto out_free;1124}11251126nr_cpus = sysconf(_SC_NPROCESSORS_CONF);11271128for (i = 0; i < nr_cpus; i++) {1129if (params->cpus && !CPU_ISSET(i, ¶ms->monitored_cpus))1130continue;1131if (save_cpu_idle_disable_state(i) < 0) {1132err_msg("Could not save cpu idle state.\n");1133goto out_free;1134}1135if (set_deepest_cpu_idle_state(i, params->deepest_idle_state) < 0) {1136err_msg("Could not set deepest cpu idle state.\n");1137goto out_free;1138}1139}1140}11411142if (params->threshold_actions.present[ACTION_TRACE_OUTPUT] ||1143params->end_actions.present[ACTION_TRACE_OUTPUT]) {1144record = osnoise_init_trace_tool("timerlat");1145if (!record) {1146err_msg("Failed to enable the trace instance\n");1147goto out_free;1148}1149params->threshold_actions.trace_output_inst = record->trace.inst;1150params->end_actions.trace_output_inst = record->trace.inst;11511152if (params->events) {1153retval = trace_events_enable(&record->trace, params->events);1154if (retval)1155goto out_top;1156}11571158if (params->buffer_size > 0) {1159retval = trace_set_buffer_size(&record->trace, params->buffer_size);1160if (retval)1161goto out_top;1162}1163}11641165if (!params->no_aa) {1166aa = osnoise_init_tool("timerlat_aa");1167if (!aa)1168goto out_top;11691170retval = timerlat_aa_init(aa, params->dump_tasks);1171if (retval) {1172err_msg("Failed to enable the auto analysis instance\n");1173goto out_top;1174}11751176/* if it is re-using the main instance, there is no need to start it */1177if (aa != top) {1178retval = enable_timerlat(&aa->trace);1179if (retval) {1180err_msg("Failed to enable timerlat tracer\n");1181goto out_top;1182}1183}1184}11851186if (params->user_workload) {1187/* rtla asked to stop */1188params_u.should_run = 1;1189/* all threads left */1190params_u.stopped_running = 0;11911192params_u.set = ¶ms->monitored_cpus;1193if (params->set_sched)1194params_u.sched_param = ¶ms->sched_param;1195else1196params_u.sched_param = NULL;11971198params_u.cgroup_name = params->cgroup_name;11991200retval = pthread_create(&timerlat_u, NULL, timerlat_u_dispatcher, ¶ms_u);1201if (retval)1202err_msg("Error creating timerlat user-space threads\n");1203}12041205if (params->warmup > 0) {1206debug_msg("Warming up for %d seconds\n", params->warmup);1207sleep(params->warmup);1208}12091210/*1211* Start the tracers here, after having set all instances.1212*1213* Let the trace instance start first for the case of hitting a stop1214* tracing while enabling other instances. The trace instance is the1215* one with most valuable information.1216*/1217if (record)1218trace_instance_start(&record->trace);1219if (!params->no_aa)1220trace_instance_start(&aa->trace);1221if (params->mode == TRACING_MODE_TRACEFS) {1222trace_instance_start(trace);1223} else {1224retval = timerlat_bpf_attach();1225if (retval) {1226err_msg("Error attaching BPF program\n");1227goto out_top;1228}1229}12301231top->start_time = time(NULL);1232timerlat_top_set_signals(params);12331234if (params->mode == TRACING_MODE_TRACEFS)1235retval = timerlat_top_main_loop(top, record, aa, params, ¶ms_u);1236else1237retval = timerlat_top_bpf_main_loop(top, record, aa, params, ¶ms_u);12381239if (retval)1240goto out_top;12411242if (params->mode != TRACING_MODE_TRACEFS)1243timerlat_bpf_detach();12441245if (params->user_workload && !params_u.stopped_running) {1246params_u.should_run = 0;1247sleep(1);1248}12491250timerlat_print_stats(params, top);12511252actions_perform(¶ms->end_actions);12531254return_value = PASSED;12551256if (osnoise_trace_is_off(top, record) && !stop_tracing) {1257printf("rtla timerlat hit stop tracing\n");12581259if (!params->no_aa)1260timerlat_auto_analysis(params->stop_us, params->stop_total_us);12611262return_value = FAILED;1263} else if (params->aa_only) {1264/*1265* If the trace did not stop with --aa-only, at least print the1266* max known latency.1267*/1268max_lat = tracefs_instance_file_read(trace->inst, "tracing_max_latency", NULL);1269if (max_lat) {1270printf(" Max latency was %s\n", max_lat);1271free(max_lat);1272}1273}12741275out_top:1276timerlat_aa_destroy();1277if (dma_latency_fd >= 0)1278close(dma_latency_fd);1279if (params->deepest_idle_state >= -1) {1280for (i = 0; i < nr_cpus; i++) {1281if (params->cpus && !CPU_ISSET(i, ¶ms->monitored_cpus))1282continue;1283restore_cpu_idle_disable_state(i);1284}1285}1286trace_events_destroy(&record->trace, params->events);1287params->events = NULL;1288out_free:1289timerlat_free_top(top->data);1290if (aa && aa != top)1291osnoise_destroy_tool(aa);1292osnoise_destroy_tool(record);1293osnoise_destroy_tool(top);1294actions_destroy(¶ms->threshold_actions);1295actions_destroy(¶ms->end_actions);1296if (params->mode != TRACING_MODE_TRACEFS)1297timerlat_bpf_destroy();1298free(params);1299free_cpu_idle_disable_states();1300out_exit:1301exit(return_value);1302}130313041305