xref: /linux/tools/tracing/rtla/src/utils.h (revision 9bf942f3c370c9b3af639df04cb5f34daf512dab)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <stdint.h>
4 #include <time.h>
5 #include <sched.h>
6 #include <stdbool.h>
7 #include <stdlib.h>
8 
9 /*
10  * '18446744073709551615\0'
11  */
12 #define BUFF_U64_STR_SIZE	24
13 #define MAX_PATH		1024
14 #define MAX_NICE		20
15 #define MIN_NICE		-19
16 
17 #define container_of(ptr, type, member)({			\
18 	const typeof(((type *)0)->member) *__mptr = (ptr);	\
19 	(type *)((char *)__mptr - offsetof(type, member)) ; })
20 
21 extern int config_debug;
22 void debug_msg(const char *fmt, ...);
23 void err_msg(const char *fmt, ...);
24 void fatal(const char *fmt, ...);
25 
26 long parse_seconds_duration(char *val);
27 void get_duration(time_t start_time, char *output, int output_size);
28 
29 char *parse_optional_arg(int argc, char **argv);
30 long long get_llong_from_str(char *start);
31 
32 static inline void
33 update_min(unsigned long long *a, unsigned long long *b)
34 {
35 	if (*a > *b)
36 		*a = *b;
37 }
38 
39 static inline void
40 update_max(unsigned long long *a, unsigned long long *b)
41 {
42 	if (*a < *b)
43 		*a = *b;
44 }
45 
46 static inline void
47 update_sum(unsigned long long *a, unsigned long long *b)
48 {
49 	*a += *b;
50 }
51 
52 #ifndef SCHED_ATTR_SIZE_VER0
53 struct sched_attr {
54 	uint32_t size;
55 	uint32_t sched_policy;
56 	uint64_t sched_flags;
57 	int32_t sched_nice;
58 	uint32_t sched_priority;
59 	uint64_t sched_runtime;
60 	uint64_t sched_deadline;
61 	uint64_t sched_period;
62 };
63 #endif /* SCHED_ATTR_SIZE_VER0 */
64 
65 int parse_prio(char *arg, struct sched_attr *sched_param);
66 int parse_cpu_set(char *cpu_list, cpu_set_t *set);
67 int __set_sched_attr(int pid, struct sched_attr *attr);
68 int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr);
69 int set_comm_cgroup(const char *comm_prefix, const char *cgroup);
70 int set_pid_cgroup(pid_t pid, const char *cgroup);
71 int set_cpu_dma_latency(int32_t latency);
72 #ifdef HAVE_LIBCPUPOWER_SUPPORT
73 int save_cpu_idle_disable_state(unsigned int cpu);
74 int restore_cpu_idle_disable_state(unsigned int cpu);
75 void free_cpu_idle_disable_states(void);
76 int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int state);
77 static inline int have_libcpupower_support(void) { return 1; }
78 #else
79 static inline int save_cpu_idle_disable_state(unsigned int cpu) { return -1; }
80 static inline int restore_cpu_idle_disable_state(unsigned int cpu) { return -1; }
81 static inline void free_cpu_idle_disable_states(void) { }
82 static inline int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int state) { return -1; }
83 static inline int have_libcpupower_support(void) { return 0; }
84 #endif /* HAVE_LIBCPUPOWER_SUPPORT */
85 int auto_house_keeping(cpu_set_t *monitored_cpus);
86 __attribute__((__warn_unused_result__)) int strtoi(const char *s, int *res);
87 
88 #define ns_to_usf(x) (((double)x/1000))
89 #define ns_to_per(total, part) ((part * 100) / (double)total)
90 
91 enum result {
92 	PASSED	= EXIT_SUCCESS,
93 	ERROR	= EXIT_FAILURE,
94 	FAILED, /* test hit the stop tracing condition */
95 };
96