projects/linux·blob master · refs

125 lines · 3.5 KiB

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 */
37static inline bool str_has_prefix(const char *str, const char *prefix)
38{
39 return strncmp(str, prefix, strlen(prefix)) == 0;
40}
41
42extern bool config_debug;
43void debug_msg(const char *fmt, ...);
44void err_msg(const char *fmt, ...);
45void fatal(const char *fmt, ...);
46
47long parse_seconds_duration(char *val);
48void get_duration(time_t start_time, char *output, int output_size);
49
50long long get_llong_from_str(char *start);
51
52static inline void
53update_min(unsigned long long *a, unsigned long long *b)
54{
55 if (*a > *b)
56 *a = *b;
57}
58
59static inline void
60update_max(unsigned long long *a, unsigned long long *b)
61{
62 if (*a < *b)
63 *a = *b;
64}
65
66static inline void
67update_sum(unsigned long long *a, unsigned long long *b)
68{
69 *a += *b;
70}
71
72#ifndef SCHED_ATTR_SIZE_VER0
73struct 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
85enum stack_format {
86 STACK_FORMAT_TRUNCATE,
87 STACK_FORMAT_SKIP,
88 STACK_FORMAT_FULL
89};
90
91int parse_prio(char *arg, struct sched_attr *sched_param);
92int parse_cpu_set(char *cpu_list, cpu_set_t *set);
93int parse_stack_format(char *arg);
94int __set_sched_attr(int pid, struct sched_attr *attr);
95int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr);
96int set_comm_cgroup(const char *comm_prefix, const char *cgroup);
97int set_pid_cgroup(pid_t pid, const char *cgroup);
98int set_cpu_dma_latency(int32_t latency);
99void *calloc_fatal(size_t n, size_t size);
100void *reallocarray_fatal(void *p, size_t n, size_t size);
101char *strdup_fatal(const char *s);
102#ifdef HAVE_LIBCPUPOWER_SUPPORT
103int save_cpu_idle_disable_state(unsigned int cpu);
104int restore_cpu_idle_disable_state(unsigned int cpu);
105void free_cpu_idle_disable_states(void);
106int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int state);
107static inline int have_libcpupower_support(void) { return 1; }
108#else
109static inline int save_cpu_idle_disable_state(unsigned int cpu) { return -1; }
110static inline int restore_cpu_idle_disable_state(unsigned int cpu) { return -1; }
111static inline void free_cpu_idle_disable_states(void) { }
112static inline int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int state) { return -1; }
113static inline int have_libcpupower_support(void) { return 0; }
114#endif /* HAVE_LIBCPUPOWER_SUPPORT */
115int 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
121enum result {
122 PASSED = EXIT_SUCCESS,
123 ERROR = EXIT_FAILURE,
124 FAILED, /* test hit the stop tracing condition */
125};