xref: /linux/tools/tracing/rtla/src/utils.h (revision 244d0cbff2efa13931115784e5dc4d1270a04ec7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <stdint.h>
4 #include <string.h>
5 #include <time.h>
6 #include <sched.h>
7 #include <stdbool.h>
8 #include <stdlib.h>
9 
10 #include <linux/container_of.h>
11 
12 /*
13  * '18446744073709551615\0'
14  */
15 #define BUFF_U64_STR_SIZE	24
16 #define MAX_PATH		1024
17 #define MAX_NICE		20
18 #define MIN_NICE		-19
19 
20 #ifndef ARRAY_SIZE
21 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
22 #endif
23 
24 /* Calculate string length at compile time (excluding null terminator) */
25 #define STRING_LENGTH(s) (ARRAY_SIZE(s) - sizeof(*(s)))
26 
27 /* Compare string with static string, length determined at compile time */
28 #define strncmp_static(s1, s2) strncmp(s1, s2, ARRAY_SIZE(s2))
29 
30 /**
31  * str_has_prefix - Test if a string has a given prefix
32  * @str: The string to test
33  * @prefix: The string to see if @str starts with
34  *
35  * Returns: true if @str starts with @prefix, false otherwise
36  */
37 static inline bool str_has_prefix(const char *str, const char *prefix)
38 {
39 	return strncmp(str, prefix, strlen(prefix)) == 0;
40 }
41 
42 extern bool config_debug;
43 void debug_msg(const char *fmt, ...);
44 void err_msg(const char *fmt, ...);
45 void fatal(const char *fmt, ...);
46 
47 long parse_seconds_duration(char *val);
48 void get_duration(time_t start_time, char *output, int output_size);
49 
50 long long get_llong_from_str(char *start);
51 
52 static inline void
53 update_min(unsigned long long *a, unsigned long long *b)
54 {
55 	if (*a > *b)
56 		*a = *b;
57 }
58 
59 static inline void
60 update_max(unsigned long long *a, unsigned long long *b)
61 {
62 	if (*a < *b)
63 		*a = *b;
64 }
65 
66 static inline void
67 update_sum(unsigned long long *a, unsigned long long *b)
68 {
69 	*a += *b;
70 }
71 
72 #ifndef SCHED_ATTR_SIZE_VER0
73 struct sched_attr {
74 	uint32_t size;
75 	uint32_t sched_policy;
76 	uint64_t sched_flags;
77 	int32_t sched_nice;
78 	uint32_t sched_priority;
79 	uint64_t sched_runtime;
80 	uint64_t sched_deadline;
81 	uint64_t sched_period;
82 };
83 #endif /* SCHED_ATTR_SIZE_VER0 */
84 
85 enum stack_format {
86 	STACK_FORMAT_TRUNCATE,
87 	STACK_FORMAT_SKIP,
88 	STACK_FORMAT_FULL
89 };
90 
91 int parse_prio(char *arg, struct sched_attr *sched_param);
92 int parse_cpu_set(char *cpu_list, cpu_set_t *set);
93 int parse_stack_format(char *arg);
94 int __set_sched_attr(int pid, struct sched_attr *attr);
95 int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr);
96 int set_comm_cgroup(const char *comm_prefix, const char *cgroup);
97 int set_pid_cgroup(pid_t pid, const char *cgroup);
98 int set_cpu_dma_latency(int32_t latency);
99 void *calloc_fatal(size_t n, size_t size);
100 void *reallocarray_fatal(void *p, size_t n, size_t size);
101 char *strdup_fatal(const char *s);
102 #ifdef HAVE_LIBCPUPOWER_SUPPORT
103 int save_cpu_idle_disable_state(unsigned int cpu);
104 int restore_cpu_idle_disable_state(unsigned int cpu);
105 void free_cpu_idle_disable_states(void);
106 int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int state);
107 static inline int have_libcpupower_support(void) { return 1; }
108 #else
109 static inline int save_cpu_idle_disable_state(unsigned int cpu) { return -1; }
110 static inline int restore_cpu_idle_disable_state(unsigned int cpu) { return -1; }
111 static inline void free_cpu_idle_disable_states(void) { }
112 static inline int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int state) { return -1; }
113 static inline int have_libcpupower_support(void) { return 0; }
114 #endif /* HAVE_LIBCPUPOWER_SUPPORT */
115 int auto_house_keeping(cpu_set_t *monitored_cpus);
116 __attribute__((__warn_unused_result__)) int strtoi(const char *s, int *res);
117 
118 #define ns_to_usf(x) (((double)x/1000))
119 #define ns_to_per(total, part) ((part * 100) / (double)total)
120 
121 enum result {
122 	PASSED	= EXIT_SUCCESS,
123 	ERROR	= EXIT_FAILURE,
124 	FAILED, /* test hit the stop tracing condition */
125 };
126