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