1 /* SPDX-License-Identifier: GPL-2.0 */
2 #pragma once
3
4 #include <getopt.h>
5 #include "actions.h"
6 #include "timerlat_u.h"
7 #include "trace.h"
8 #include "utils.h"
9
10 /*
11 * osnoise_context - read, store, write, restore osnoise configs.
12 */
13 struct osnoise_context {
14 int flags;
15 int ref;
16
17 char *curr_cpus;
18 char *orig_cpus;
19
20 /* 0 as init value */
21 unsigned long long orig_runtime_us;
22 unsigned long long runtime_us;
23
24 /* 0 as init value */
25 unsigned long long orig_period_us;
26 unsigned long long period_us;
27
28 /* 0 as init value */
29 long long orig_timerlat_period_us;
30 long long timerlat_period_us;
31
32 /* 0 as init value */
33 long long orig_tracing_thresh;
34 long long tracing_thresh;
35
36 /* -1 as init value because 0 is disabled */
37 long long orig_stop_us;
38 long long stop_us;
39
40 /* -1 as init value because 0 is disabled */
41 long long orig_stop_total_us;
42 long long stop_total_us;
43
44 /* -1 as init value because 0 is disabled */
45 long long orig_print_stack;
46 long long print_stack;
47
48 /* -1 as init value because 0 is off */
49 int orig_opt_irq_disable;
50 int opt_irq_disable;
51
52 /* -1 as init value because 0 is off */
53 int orig_opt_workload;
54 int opt_workload;
55 };
56
57 extern struct trace_instance *trace_inst;
58 extern volatile int stop_tracing;
59
60 struct hist_params {
61 char no_irq;
62 char no_thread;
63 char no_header;
64 char no_summary;
65 char no_index;
66 char with_zeros;
67 int bucket_size;
68 int entries;
69 };
70
71 /*
72 * common_params - Parameters shared between timerlat_params and osnoise_params
73 */
74 struct common_params {
75 /* trace configuration */
76 char *cpus;
77 cpu_set_t monitored_cpus;
78 struct trace_events *events;
79 int buffer_size;
80
81 /* Timing parameters */
82 int warmup;
83 long long stop_us;
84 long long stop_total_us;
85 int sleep_time;
86 int duration;
87
88 /* Scheduling parameters */
89 int set_sched;
90 struct sched_attr sched_param;
91 int cgroup;
92 char *cgroup_name;
93 int hk_cpus;
94 cpu_set_t hk_cpu_set;
95
96 /* Other parameters */
97 struct hist_params hist;
98 int output_divisor;
99 int pretty_output;
100 int quiet;
101 int user_workload;
102 int kernel_workload;
103 int user_data;
104 int aa_only;
105
106 struct actions threshold_actions;
107 struct actions end_actions;
108 struct timerlat_u_params user;
109 };
110
111 extern int nr_cpus;
112
113 #define for_each_monitored_cpu(cpu, common) \
114 for (cpu = 0; cpu < nr_cpus; cpu++) \
115 if (!(common)->cpus || CPU_ISSET(cpu, &(common)->monitored_cpus))
116
117 struct tool_ops;
118
119 /*
120 * osnoise_tool - osnoise based tool definition.
121 *
122 * Only the "trace" and "context" fields are used for
123 * the additional trace instances (record and aa).
124 */
125 struct osnoise_tool {
126 struct tool_ops *ops;
127 struct trace_instance trace;
128 struct osnoise_context *context;
129 void *data;
130 struct common_params *params;
131 time_t start_time;
132 struct osnoise_tool *record;
133 struct osnoise_tool *aa;
134 };
135
136 struct tool_ops {
137 const char *tracer;
138 const char *comm_prefix;
139 struct common_params *(*parse_args)(int argc, char *argv[]);
140 struct osnoise_tool *(*init_tool)(struct common_params *params);
141 int (*apply_config)(struct osnoise_tool *tool);
142 int (*enable)(struct osnoise_tool *tool);
143 int (*main)(struct osnoise_tool *tool);
144 void (*print_stats)(struct osnoise_tool *tool);
145 void (*analyze)(struct osnoise_tool *tool, bool stopped);
146 void (*free)(struct osnoise_tool *tool);
147 };
148
149 /**
150 * should_continue_tracing - check if tracing should continue after threshold
151 * @params: pointer to the common parameters structure
152 *
153 * Returns true if the continue action was configured (--on-threshold continue),
154 * indicating that tracing should be restarted after handling the threshold event.
155 *
156 * Return: 1 if tracing should continue, 0 otherwise.
157 */
158 static inline int
should_continue_tracing(const struct common_params * params)159 should_continue_tracing(const struct common_params *params)
160 {
161 return params->threshold_actions.continue_flag;
162 }
163
164 int
165 common_threshold_handler(const struct osnoise_tool *tool);
166
167 int osnoise_set_cpus(struct osnoise_context *context, char *cpus);
168 void osnoise_restore_cpus(struct osnoise_context *context);
169
170 int osnoise_set_workload(struct osnoise_context *context, bool onoff);
171
172 void osnoise_destroy_tool(struct osnoise_tool *top);
173 struct osnoise_tool *osnoise_init_tool(char *tool_name);
174 struct osnoise_tool *osnoise_init_trace_tool(const char *tracer);
175 bool osnoise_trace_is_off(struct osnoise_tool *tool, struct osnoise_tool *record);
176 int osnoise_set_stop_us(struct osnoise_context *context, long long stop_us);
177 int osnoise_set_stop_total_us(struct osnoise_context *context,
178 long long stop_total_us);
179
180 int getopt_auto(int argc, char **argv, const struct option *long_opts);
181 int common_parse_options(int argc, char **argv, struct common_params *common);
182 int common_apply_config(struct osnoise_tool *tool, struct common_params *params);
183 int top_main_loop(struct osnoise_tool *tool);
184 int hist_main_loop(struct osnoise_tool *tool);
185 int osn_set_stop(struct osnoise_tool *tool);
186
187 void common_usage(const char *tool, const char *mode,
188 const char *desc, const char * const *start_msgs, const char * const *opt_msgs);
189