xref: /linux/tools/tracing/rtla/src/utils.h (revision 009a8e681fb003f38dd57a640e11ed826740b5c1)
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 enum stack_format {
66 	STACK_FORMAT_TRUNCATE,
67 	STACK_FORMAT_SKIP,
68 	STACK_FORMAT_FULL
69 };
70 
71 int parse_prio(char *arg, struct sched_attr *sched_param);
72 int parse_cpu_set(char *cpu_list, cpu_set_t *set);
73 int parse_stack_format(char *arg);
74 int __set_sched_attr(int pid, struct sched_attr *attr);
75 int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr);
76 int set_comm_cgroup(const char *comm_prefix, const char *cgroup);
77 int set_pid_cgroup(pid_t pid, const char *cgroup);
78 int set_cpu_dma_latency(int32_t latency);
79 void *calloc_fatal(size_t n, size_t size);
80 void *reallocarray_fatal(void *p, size_t n, size_t size);
81 char *strdup_fatal(const char *s);
82 #ifdef HAVE_LIBCPUPOWER_SUPPORT
83 int save_cpu_idle_disable_state(unsigned int cpu);
84 int restore_cpu_idle_disable_state(unsigned int cpu);
85 void free_cpu_idle_disable_states(void);
86 int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int state);
87 static inline int have_libcpupower_support(void) { return 1; }
88 #else
89 static inline int save_cpu_idle_disable_state(unsigned int cpu) { return -1; }
90 static inline int restore_cpu_idle_disable_state(unsigned int cpu) { return -1; }
91 static inline void free_cpu_idle_disable_states(void) { }
92 static inline int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int state) { return -1; }
93 static inline int have_libcpupower_support(void) { return 0; }
94 #endif /* HAVE_LIBCPUPOWER_SUPPORT */
95 int auto_house_keeping(cpu_set_t *monitored_cpus);
96 __attribute__((__warn_unused_result__)) int strtoi(const char *s, int *res);
97 
98 #define ns_to_usf(x) (((double)x/1000))
99 #define ns_to_per(total, part) ((part * 100) / (double)total)
100 
101 enum result {
102 	PASSED	= EXIT_SUCCESS,
103 	ERROR	= EXIT_FAILURE,
104 	FAILED, /* test hit the stop tracing condition */
105 };
106