]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/perf/util/util.h
perf tools: Don't include terminal handling headers in util.h
[karo-tx-linux.git] / tools / perf / util / util.h
1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
3
4 #define _ALL_SOURCE 1
5 #define _BSD_SOURCE 1
6 /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
7 #define _DEFAULT_SOURCE 1
8 #define HAS_BOOL
9
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <sys/stat.h>
13 #include <sys/statfs.h>
14 #include <fcntl.h>
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <string.h>
20 #include <limits.h>
21 #include <sys/param.h>
22 #include <sys/types.h>
23 #include <dirent.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <signal.h>
27 #include <fnmatch.h>
28 #include <assert.h>
29 #include <regex.h>
30 #include <utime.h>
31 #include <sys/wait.h>
32 #include <poll.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <api/fs/tracing_path.h>
38 #include <linux/bitops.h>
39
40 extern char buildid_dir[];
41
42 #ifdef __GNUC__
43 #define NORETURN __attribute__((__noreturn__))
44 #else
45 #define NORETURN
46 #ifndef __attribute__
47 #define __attribute__(x)
48 #endif
49 #endif
50
51 #define PERF_GTK_DSO  "libperf-gtk.so"
52
53 /* General helper functions */
54 void usage(const char *err) NORETURN;
55 void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
56 int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
57 void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
58
59 void set_warning_routine(void (*routine)(const char *err, va_list params));
60
61 int prefixcmp(const char *str, const char *prefix);
62 void set_buildid_dir(const char *dir);
63
64 #ifdef __GLIBC_PREREQ
65 #if __GLIBC_PREREQ(2, 1)
66 #define HAVE_STRCHRNUL
67 #endif
68 #endif
69
70 #ifndef HAVE_STRCHRNUL
71 #define strchrnul gitstrchrnul
72 static inline char *gitstrchrnul(const char *s, int c)
73 {
74         while (*s && *s != c)
75                 s++;
76         return (char *)s;
77 }
78 #endif
79
80 static inline void *zalloc(size_t size)
81 {
82         return calloc(1, size);
83 }
84
85 #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
86
87 struct strlist;
88
89 int mkdir_p(char *path, mode_t mode);
90 int rm_rf(const char *path);
91 struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
92 bool lsdir_no_dot_filter(const char *name, struct dirent *d);
93 int copyfile(const char *from, const char *to);
94 int copyfile_mode(const char *from, const char *to, mode_t mode);
95 int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size);
96
97 unsigned long convert_unit(unsigned long value, char *unit);
98 ssize_t readn(int fd, void *buf, size_t n);
99 ssize_t writen(int fd, void *buf, size_t n);
100
101 struct perf_event_attr;
102
103 void event_attr_init(struct perf_event_attr *attr);
104
105 size_t hex_width(u64 v);
106 int hex2u64(const char *ptr, u64 *val);
107
108 void dump_stack(void);
109 void sighandler_dump_stack(int sig);
110
111 extern unsigned int page_size;
112 extern int cacheline_size;
113 extern int sysctl_perf_event_max_stack;
114 extern int sysctl_perf_event_max_contexts_per_stack;
115
116 struct parse_tag {
117         char tag;
118         int mult;
119 };
120
121 unsigned long parse_tag_value(const char *str, struct parse_tag *tags);
122
123 static inline int path__join(char *bf, size_t size,
124                              const char *path1, const char *path2)
125 {
126         return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
127 }
128
129 static inline int path__join3(char *bf, size_t size,
130                               const char *path1, const char *path2,
131                               const char *path3)
132 {
133         return scnprintf(bf, size, "%s%s%s%s%s",
134                          path1, path1[0] ? "/" : "",
135                          path2, path2[0] ? "/" : "", path3);
136 }
137
138 int perf_event_paranoid(void);
139
140 void mem_bswap_64(void *src, int byte_size);
141 void mem_bswap_32(void *src, int byte_size);
142
143 const char *get_filename_for_perf_kvm(void);
144 bool find_process(const char *name);
145
146 #ifdef HAVE_ZLIB_SUPPORT
147 int gzip_decompress_to_file(const char *input, int output_fd);
148 #endif
149
150 #ifdef HAVE_LZMA_SUPPORT
151 int lzma_decompress_to_file(const char *input, int output_fd);
152 #endif
153
154 int get_stack_size(const char *str, unsigned long *_size);
155
156 int fetch_kernel_version(unsigned int *puint,
157                          char *str, size_t str_sz);
158 #define KVER_VERSION(x)         (((x) >> 16) & 0xff)
159 #define KVER_PATCHLEVEL(x)      (((x) >> 8) & 0xff)
160 #define KVER_SUBLEVEL(x)        ((x) & 0xff)
161 #define KVER_FMT        "%d.%d.%d"
162 #define KVER_PARAM(x)   KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
163
164 const char *perf_tip(const char *dirpath);
165 bool is_regular_file(const char *file);
166 int fetch_current_timestamp(char *buf, size_t sz);
167
168 #ifndef HAVE_SCHED_GETCPU_SUPPORT
169 int sched_getcpu(void);
170 #endif
171
172 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
173
174 int unit_number__scnprintf(char *buf, size_t size, u64 n);
175
176 #endif /* GIT_COMPAT_UTIL_H */