xref: /linux/tools/perf/util/util.h (revision c6b40a642563c7b9ddaea19880c0c36659cc7743)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_UTIL_H
3 #define __PERF_UTIL_H
4 
5 #define _BSD_SOURCE 1
6 /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
7 #define _DEFAULT_SOURCE 1
8 
9 #include <dirent.h>
10 #include <fcntl.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <linux/compiler.h>
14 #include <linux/bitmap.h>
15 #include <sys/types.h>
16 #ifndef __cplusplus
17 #include <internal/cpumap.h>
18 #endif
19 
20 extern const char *input_name;
21 
22 /* This will control if perf_{host,guest} will set attr.exclude_{host,guest}. */
23 extern bool exclude_GH_default;
24 
25 extern bool perf_host;
26 extern bool perf_guest;
27 
28 /* General helper functions */
29 
30 struct dirent;
31 struct strlist;
32 
33 int mkdir_p(char *path, mode_t mode);
34 int rm_rf(const char *path);
35 int rm_rf_perf_data(const char *path);
36 struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
37 bool lsdir_no_dot_filter(const char *name, struct dirent *d);
38 
39 size_t hex_width(u64 v);
40 
41 int sysctl__max_stack(void);
42 
43 bool sysctl__nmi_watchdog_enabled(void);
44 
45 int perf_tip(char **strp, const char *dirpath);
46 
47 void cpumask_to_cpulist(char *cpumask, char *cpulist);
48 
49 void print_separator2(int pre_dash_cnt, const char *s, int post_dash_cnt);
50 
51 #ifndef HAVE_SCHED_GETCPU_SUPPORT
52 int sched_getcpu(void);
53 #endif
54 
55 #ifndef HAVE_SCANDIRAT_SUPPORT
56 int scandirat(int dirfd, const char *dirp,
57 	      struct dirent ***namelist,
58 	      int (*filter)(const struct dirent *),
59 	      int (*compar)(const struct dirent **, const struct dirent **));
60 #endif
61 
62 extern bool perf_singlethreaded;
63 
64 void perf_set_singlethreaded(void);
65 void perf_set_multithreaded(void);
66 
67 char *perf_exe(char *buf, int len);
68 
69 #ifndef O_CLOEXEC
70 #ifdef __sparc__
71 #define O_CLOEXEC      0x400000
72 #elif defined(__alpha__) || defined(__hppa__)
73 #define O_CLOEXEC      010000000
74 #else
75 #define O_CLOEXEC      02000000
76 #endif
77 #endif
78 
79 struct perf_debuginfod {
80 	const char	*urls;
81 	bool		 set;
82 };
83 void perf_debuginfod_setup(struct perf_debuginfod *di);
84 
85 const char *perf_basename(const char *path);
86 
87 char *filename_with_chroot(int pid, const char *filename);
88 
89 int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x,
90 			       size_t msz, const void *init_val);
91 
92 #define realloc_array_as_needed(a, n, x, v) ({			\
93 	typeof(x) __x = (x);					\
94 	__x >= (n) ?						\
95 		do_realloc_array_as_needed((void **)&(a),	\
96 					   &(n),		\
97 					   __x,			\
98 					   sizeof(*(a)),	\
99 					   (const void *)(v)) :	\
100 		0;						\
101 	})
102 
103 static inline bool host_is_bigendian(void)
104 {
105 #ifdef __BYTE_ORDER__
106 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
107 	return false;
108 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
109 	return true;
110 #else
111 #error "Unrecognized __BYTE_ORDER__"
112 #endif
113 #else /* !__BYTE_ORDER__ */
114 	unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
115 	unsigned int *ptr;
116 
117 	ptr = (unsigned int *)(void *)str;
118 	return *ptr == 0x01020304;
119 #endif
120 }
121 
122 #endif /* __PERF_UTIL_H */
123