Path: blob/master/tools/tracing/rtla/src/timerlat_top.c
48927 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_each_monitored_cpu(i, nr_cpus, ¶ms->common) {462timerlat_top_print(top, i);463timerlat_top_update_sum(top, i, &summary);464}465466timerlat_top_print_sum(top, &summary);467468trace_seq_do_printf(trace->seq);469trace_seq_reset(trace->seq);470osnoise_report_missed_events(top);471}472473/*474* timerlat_top_usage - prints timerlat top usage message475*/476static void timerlat_top_usage(void)477{478int i;479480static const char *const msg[] = {481"",482" usage: rtla timerlat [top] [-h] [-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\",483" [[-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",484" [-P priority] [--dma-latency us] [--aa-only us] [-C [cgroup_name]] [-u|-k] [--warm-up s] [--deepest-idle-state n]",485"",486" -h/--help: print this menu",487" -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",488" --aa-only us: stop if <us> latency is hit, only printing the auto analysis (reduces CPU usage)",489" -p/--period us: timerlat period in us",490" -i/--irq us: stop trace if the irq latency is higher than the argument in us",491" -T/--thread us: stop trace if the thread latency is higher than the argument in us",492" -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",493" -c/--cpus cpus: run the tracer only on the given cpus",494" -H/--house-keeping cpus: run rtla control threads only on the given cpus",495" -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",496" -d/--duration time[s|m|h|d]: duration of the session",497" -D/--debug: print debug info",498" --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",499" -t/--trace [file]: save the stopped trace to [file|timerlat_trace.txt]",500" -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",501" --filter <command>: enable a trace event filter to the previous -e event",502" --trigger <command>: enable a trace event trigger to the previous -e event",503" -n/--nano: display data in nanoseconds",504" --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",505" -q/--quiet print only a summary at the end",506" --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",507" -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",508" o:prio - use SCHED_OTHER with prio",509" r:prio - use SCHED_RR with prio",510" f:prio - use SCHED_FIFO with prio",511" d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",512" in nanoseconds",513" -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads",514" -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads",515" -U/--user-load: enable timerlat for user-defined user-space workload",516" --warm-up s: let the workload run for s seconds before collecting data",517" --trace-buffer-size kB: set the per-cpu trace buffer size in kB",518" --deepest-idle-state n: only go down to idle state n on cpus used by timerlat to reduce exit from idle latency",519" --on-threshold <action>: define action to be executed at latency threshold, multiple are allowed",520" --on-end: define action to be executed at measurement end, multiple are allowed",521NULL,522};523524fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",525VERSION);526527for (i = 0; msg[i]; i++)528fprintf(stderr, "%s\n", msg[i]);529530exit(EXIT_SUCCESS);531}532533/*534* timerlat_top_parse_args - allocs, parse and fill the cmd line parameters535*/536static struct common_params537*timerlat_top_parse_args(int argc, char **argv)538{539struct timerlat_params *params;540struct trace_events *tevent;541long long auto_thresh;542int retval;543int c;544char *trace_output = NULL;545546params = calloc(1, sizeof(*params));547if (!params)548exit(1);549550actions_init(¶ms->common.threshold_actions);551actions_init(¶ms->common.end_actions);552553/* disabled by default */554params->dma_latency = -1;555556/* disabled by default */557params->deepest_idle_state = -2;558559/* display data in microseconds */560params->common.output_divisor = 1000;561562/* default to BPF mode */563params->mode = TRACING_MODE_BPF;564565while (1) {566static struct option long_options[] = {567{"auto", required_argument, 0, 'a'},568{"cpus", required_argument, 0, 'c'},569{"cgroup", optional_argument, 0, 'C'},570{"debug", no_argument, 0, 'D'},571{"duration", required_argument, 0, 'd'},572{"event", required_argument, 0, 'e'},573{"help", no_argument, 0, 'h'},574{"house-keeping", required_argument, 0, 'H'},575{"irq", required_argument, 0, 'i'},576{"nano", no_argument, 0, 'n'},577{"period", required_argument, 0, 'p'},578{"priority", required_argument, 0, 'P'},579{"quiet", no_argument, 0, 'q'},580{"stack", required_argument, 0, 's'},581{"thread", required_argument, 0, 'T'},582{"trace", optional_argument, 0, 't'},583{"user-threads", no_argument, 0, 'u'},584{"kernel-threads", no_argument, 0, 'k'},585{"user-load", no_argument, 0, 'U'},586{"trigger", required_argument, 0, '0'},587{"filter", required_argument, 0, '1'},588{"dma-latency", required_argument, 0, '2'},589{"no-aa", no_argument, 0, '3'},590{"dump-tasks", no_argument, 0, '4'},591{"aa-only", required_argument, 0, '5'},592{"warm-up", required_argument, 0, '6'},593{"trace-buffer-size", required_argument, 0, '7'},594{"deepest-idle-state", required_argument, 0, '8'},595{"on-threshold", required_argument, 0, '9'},596{"on-end", required_argument, 0, '\1'},597{0, 0, 0, 0}598};599600c = getopt_long(argc, argv, "a:c:C::d:De:hH:i:knp:P:qs:t::T:uU0:1:2:345:6:7:",601long_options, NULL);602603/* detect the end of the options. */604if (c == -1)605break;606607switch (c) {608case 'a':609auto_thresh = get_llong_from_str(optarg);610611/* set thread stop to auto_thresh */612params->common.stop_total_us = auto_thresh;613params->common.stop_us = auto_thresh;614615/* get stack trace */616params->print_stack = auto_thresh;617618/* set trace */619if (!trace_output)620trace_output = "timerlat_trace.txt";621622break;623case '5':624/* it is here because it is similar to -a */625auto_thresh = get_llong_from_str(optarg);626627/* set thread stop to auto_thresh */628params->common.stop_total_us = auto_thresh;629params->common.stop_us = auto_thresh;630631/* get stack trace */632params->print_stack = auto_thresh;633634/* set aa_only to avoid parsing the trace */635params->common.aa_only = 1;636break;637case 'c':638retval = parse_cpu_set(optarg, ¶ms->common.monitored_cpus);639if (retval)640fatal("Invalid -c cpu list");641params->common.cpus = optarg;642break;643case 'C':644params->common.cgroup = 1;645params->common.cgroup_name = optarg;646break;647case 'D':648config_debug = 1;649break;650case 'd':651params->common.duration = parse_seconds_duration(optarg);652if (!params->common.duration)653fatal("Invalid -d duration");654break;655case 'e':656tevent = trace_event_alloc(optarg);657if (!tevent)658fatal("Error alloc trace event");659660if (params->common.events)661tevent->next = params->common.events;662params->common.events = tevent;663break;664case 'h':665case '?':666timerlat_top_usage();667break;668case 'H':669params->common.hk_cpus = 1;670retval = parse_cpu_set(optarg, ¶ms->common.hk_cpu_set);671if (retval)672fatal("Error parsing house keeping CPUs");673break;674case 'i':675params->common.stop_us = get_llong_from_str(optarg);676break;677case 'k':678params->common.kernel_workload = true;679break;680case 'n':681params->common.output_divisor = 1;682break;683case 'p':684params->timerlat_period_us = get_llong_from_str(optarg);685if (params->timerlat_period_us > 1000000)686fatal("Period longer than 1 s");687break;688case 'P':689retval = parse_prio(optarg, ¶ms->common.sched_param);690if (retval == -1)691fatal("Invalid -P priority");692params->common.set_sched = 1;693break;694case 'q':695params->common.quiet = 1;696break;697case 's':698params->print_stack = get_llong_from_str(optarg);699break;700case 'T':701params->common.stop_total_us = get_llong_from_str(optarg);702break;703case 't':704trace_output = parse_optional_arg(argc, argv);705if (!trace_output)706trace_output = "timerlat_trace.txt";707break;708case 'u':709params->common.user_workload = true;710/* fallback: -u implies -U */711case 'U':712params->common.user_data = true;713break;714case '0': /* trigger */715if (params->common.events) {716retval = trace_event_add_trigger(params->common.events, optarg);717if (retval)718fatal("Error adding trigger %s", optarg);719} else {720fatal("--trigger requires a previous -e");721}722break;723case '1': /* filter */724if (params->common.events) {725retval = trace_event_add_filter(params->common.events, optarg);726if (retval)727fatal("Error adding filter %s", optarg);728} else {729fatal("--filter requires a previous -e");730}731break;732case '2': /* dma-latency */733params->dma_latency = get_llong_from_str(optarg);734if (params->dma_latency < 0 || params->dma_latency > 10000)735fatal("--dma-latency needs to be >= 0 and < 10000");736break;737case '3': /* no-aa */738params->no_aa = 1;739break;740case '4':741params->dump_tasks = 1;742break;743case '6':744params->common.warmup = get_llong_from_str(optarg);745break;746case '7':747params->common.buffer_size = get_llong_from_str(optarg);748break;749case '8':750params->deepest_idle_state = get_llong_from_str(optarg);751break;752case '9':753retval = actions_parse(¶ms->common.threshold_actions, optarg,754"timerlat_trace.txt");755if (retval)756fatal("Invalid action %s", optarg);757break;758case '\1':759retval = actions_parse(¶ms->common.end_actions, optarg,760"timerlat_trace.txt");761if (retval)762fatal("Invalid action %s", optarg);763break;764default:765fatal("Invalid option");766}767}768769if (trace_output)770actions_add_trace_output(¶ms->common.threshold_actions, trace_output);771772if (geteuid())773fatal("rtla needs root permission");774775/*776* Auto analysis only happens if stop tracing, thus:777*/778if (!params->common.stop_us && !params->common.stop_total_us)779params->no_aa = 1;780781if (params->no_aa && params->common.aa_only)782fatal("--no-aa and --aa-only are mutually exclusive!");783784if (params->common.kernel_workload && params->common.user_workload)785fatal("--kernel-threads and --user-threads are mutually exclusive!");786787/*788* If auto-analysis or trace output is enabled, switch from BPF mode to789* mixed mode790*/791if (params->mode == TRACING_MODE_BPF &&792(params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] ||793params->common.end_actions.present[ACTION_TRACE_OUTPUT] ||794!params->no_aa))795params->mode = TRACING_MODE_MIXED;796797return ¶ms->common;798}799800/*801* timerlat_top_apply_config - apply the top configs to the initialized tool802*/803static int804timerlat_top_apply_config(struct osnoise_tool *top)805{806struct timerlat_params *params = to_timerlat_params(top->params);807int retval;808809retval = timerlat_apply_config(top, params);810if (retval)811goto out_err;812813if (isatty(STDOUT_FILENO) && !params->common.quiet)814params->common.pretty_output = 1;815816return 0;817818out_err:819return -1;820}821822/*823* timerlat_init_top - initialize a timerlat top tool with parameters824*/825static struct osnoise_tool826*timerlat_init_top(struct common_params *params)827{828struct osnoise_tool *top;829int nr_cpus;830831nr_cpus = sysconf(_SC_NPROCESSORS_CONF);832833top = osnoise_init_tool("timerlat_top");834if (!top)835return NULL;836837top->data = timerlat_alloc_top(nr_cpus);838if (!top->data)839goto out_err;840841tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",842timerlat_top_handler, top);843844return top;845846out_err:847osnoise_destroy_tool(top);848return NULL;849}850851/*852* timerlat_top_bpf_main_loop - main loop to process events (BPF variant)853*/854static int855timerlat_top_bpf_main_loop(struct osnoise_tool *tool)856{857struct timerlat_params *params = to_timerlat_params(tool->params);858int retval, wait_retval;859860if (params->common.aa_only) {861/* Auto-analysis only, just wait for stop tracing */862timerlat_bpf_wait(-1);863return 0;864}865866/* Pull and display data in a loop */867while (!stop_tracing) {868wait_retval = timerlat_bpf_wait(params->common.quiet ? -1 :869params->common.sleep_time);870871retval = timerlat_top_bpf_pull_data(tool);872if (retval) {873err_msg("Error pulling BPF data\n");874return retval;875}876877if (!params->common.quiet)878timerlat_print_stats(tool);879880if (wait_retval != 0) {881/* Stopping requested by tracer */882actions_perform(¶ms->common.threshold_actions);883884if (!params->common.threshold_actions.continue_flag)885/* continue flag not set, break */886break;887888/* continue action reached, re-enable tracing */889if (tool->record)890trace_instance_start(&tool->record->trace);891if (tool->aa)892trace_instance_start(&tool->aa->trace);893timerlat_bpf_restart_tracing();894}895896/* is there still any user-threads ? */897if (params->common.user_workload) {898if (params->common.user.stopped_running) {899debug_msg("timerlat user space threads stopped!\n");900break;901}902}903}904905return 0;906}907908static int timerlat_top_main_loop(struct osnoise_tool *tool)909{910struct timerlat_params *params = to_timerlat_params(tool->params);911int retval;912913if (params->mode == TRACING_MODE_TRACEFS) {914retval = top_main_loop(tool);915} else {916retval = timerlat_top_bpf_main_loop(tool);917timerlat_bpf_detach();918}919920return retval;921}922923struct tool_ops timerlat_top_ops = {924.tracer = "timerlat",925.comm_prefix = "timerlat/",926.parse_args = timerlat_top_parse_args,927.init_tool = timerlat_init_top,928.apply_config = timerlat_top_apply_config,929.enable = timerlat_enable,930.main = timerlat_top_main_loop,931.print_stats = timerlat_print_stats,932.analyze = timerlat_analyze,933.free = timerlat_free_top_tool,934};935936937