xref: /linux/tools/tracing/rtla/src/common.h (revision a92bd1a28b5dab2899b9f51dd2780b2d9e52e324)
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 volatile int stop_tracing;
58 
59 struct hist_params {
60 	char			no_irq;
61 	char			no_thread;
62 	char			no_header;
63 	char			no_summary;
64 	char			no_index;
65 	char			with_zeros;
66 	int			bucket_size;
67 	int			entries;
68 };
69 
70 /*
71  * common_params - Parameters shared between timerlat_params and osnoise_params
72  */
73 struct common_params {
74 	/* trace configuration */
75 	char			*cpus;
76 	cpu_set_t		monitored_cpus;
77 	struct trace_events	*events;
78 	int			buffer_size;
79 
80 	/* Timing parameters */
81 	int			warmup;
82 	long long		stop_us;
83 	long long		stop_total_us;
84 	int			sleep_time;
85 	int			duration;
86 
87 	/* Scheduling parameters */
88 	int			set_sched;
89 	struct sched_attr	sched_param;
90 	int			cgroup;
91 	char			*cgroup_name;
92 	int			hk_cpus;
93 	cpu_set_t		hk_cpu_set;
94 
95 	/* Other parameters */
96 	struct hist_params	hist;
97 	int			output_divisor;
98 	int			pretty_output;
99 	int			quiet;
100 	int			user_workload;
101 	int			kernel_workload;
102 	int			user_data;
103 	int			aa_only;
104 
105 	struct actions		threshold_actions;
106 	struct actions		end_actions;
107 	struct timerlat_u_params user;
108 };
109 
110 extern int nr_cpus;
111 
112 #define for_each_monitored_cpu(cpu, common) \
113 	for (cpu = 0; cpu < nr_cpus; cpu++) \
114 		if (!(common)->cpus || CPU_ISSET(cpu, &(common)->monitored_cpus))
115 
116 struct tool_ops;
117 
118 /*
119  * osnoise_tool -  osnoise based tool definition.
120  *
121  * Only the "trace" and "context" fields are used for
122  * the additional trace instances (record and aa).
123  */
124 struct osnoise_tool {
125 	struct tool_ops			*ops;
126 	struct trace_instance		trace;
127 	struct osnoise_context		*context;
128 	void				*data;
129 	struct common_params		*params;
130 	time_t				start_time;
131 	struct osnoise_tool		*record;
132 	struct osnoise_tool		*aa;
133 };
134 
135 struct tool_ops {
136 	const char *tracer;
137 	const char *comm_prefix;
138 	struct common_params *(*parse_args)(int argc, char *argv[]);
139 	struct osnoise_tool *(*init_tool)(struct common_params *params);
140 	int (*apply_config)(struct osnoise_tool *tool);
141 	int (*enable)(struct osnoise_tool *tool);
142 	int (*main)(struct osnoise_tool *tool);
143 	void (*print_stats)(struct osnoise_tool *tool);
144 	void (*analyze)(struct osnoise_tool *tool, bool stopped);
145 	void (*free)(struct osnoise_tool *tool);
146 };
147 
148 /**
149  * should_continue_tracing - check if tracing should continue after threshold
150  * @params: pointer to the common parameters structure
151  *
152  * Returns true if the continue action was configured (--on-threshold continue),
153  * indicating that tracing should be restarted after handling the threshold event.
154  *
155  * Return: 1 if tracing should continue, 0 otherwise.
156  */
157 static inline int
158 should_continue_tracing(const struct common_params *params)
159 {
160 	return params->threshold_actions.continue_flag;
161 }
162 
163 int
164 common_threshold_handler(const struct osnoise_tool *tool);
165 
166 int osnoise_set_cpus(struct osnoise_context *context, char *cpus);
167 void osnoise_restore_cpus(struct osnoise_context *context);
168 
169 int osnoise_set_workload(struct osnoise_context *context, bool onoff);
170 
171 void osnoise_destroy_tool(struct osnoise_tool *top);
172 struct osnoise_tool *osnoise_init_tool(char *tool_name);
173 struct osnoise_tool *osnoise_init_trace_tool(const char *tracer);
174 bool osnoise_trace_is_off(struct osnoise_tool *tool, struct osnoise_tool *record);
175 int osnoise_set_stop_us(struct osnoise_context *context, long long stop_us);
176 int osnoise_set_stop_total_us(struct osnoise_context *context,
177 			      long long stop_total_us);
178 
179 int getopt_auto(int argc, char **argv, const struct option *long_opts);
180 int common_parse_options(int argc, char **argv, struct common_params *common);
181 int common_apply_config(struct osnoise_tool *tool, struct common_params *params);
182 int top_main_loop(struct osnoise_tool *tool);
183 int hist_main_loop(struct osnoise_tool *tool);
184 int osn_set_stop(struct osnoise_tool *tool);
185 
186 void common_usage(const char *tool, const char *mode,
187 		  const char *desc, const char * const *start_msgs, const char * const *opt_msgs);
188