xref: /openbmc/linux/tools/tracing/rtla/src/utils.h (revision 83146efc)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <stdint.h>
4 #include <time.h>
5 #include <sched.h>
6 
7 /*
8  * '18446744073709551615\0'
9  */
10 #define BUFF_U64_STR_SIZE	24
11 #define MAX_PATH		1024
12 
13 #define container_of(ptr, type, member)({			\
14 	const typeof(((type *)0)->member) *__mptr = (ptr);	\
15 	(type *)((char *)__mptr - offsetof(type, member)) ; })
16 
17 extern int config_debug;
18 void debug_msg(const char *fmt, ...);
19 void err_msg(const char *fmt, ...);
20 
21 long parse_seconds_duration(char *val);
22 void get_duration(time_t start_time, char *output, int output_size);
23 
24 int parse_cpu_list(char *cpu_list, char **monitored_cpus);
25 long long get_llong_from_str(char *start);
26 
27 static inline void
28 update_min(unsigned long long *a, unsigned long long *b)
29 {
30 	if (*a > *b)
31 		*a = *b;
32 }
33 
34 static inline void
35 update_max(unsigned long long *a, unsigned long long *b)
36 {
37 	if (*a < *b)
38 		*a = *b;
39 }
40 
41 static inline void
42 update_sum(unsigned long long *a, unsigned long long *b)
43 {
44 	*a += *b;
45 }
46 
47 struct sched_attr {
48 	uint32_t size;
49 	uint32_t sched_policy;
50 	uint64_t sched_flags;
51 	int32_t sched_nice;
52 	uint32_t sched_priority;
53 	uint64_t sched_runtime;
54 	uint64_t sched_deadline;
55 	uint64_t sched_period;
56 };
57 
58 int parse_prio(char *arg, struct sched_attr *sched_param);
59 int parse_cpu_set(char *cpu_list, cpu_set_t *set);
60 int __set_sched_attr(int pid, struct sched_attr *attr);
61 int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr);
62 int set_comm_cgroup(const char *comm_prefix, const char *cgroup);
63 int set_pid_cgroup(pid_t pid, const char *cgroup);
64 int set_cpu_dma_latency(int32_t latency);
65 int auto_house_keeping(cpu_set_t *monitored_cpus);
66 
67 #define ns_to_usf(x) (((double)x/1000))
68 #define ns_to_per(total, part) ((part * 100) / (double)total)
69