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