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