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