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