blob: 3852b6d3270a06067560d3f854aa29c8289854b8 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001#ifndef GIT_COMPAT_UTIL_H
2#define GIT_COMPAT_UTIL_H
3
Ingo Molnar07800602009-04-20 15:00:56 +02004#define _ALL_SOURCE 1
Ingo Molnar07800602009-04-20 15:00:56 +02005#define _BSD_SOURCE 1
Chanho Park512fe362014-09-12 11:10:17 +09006/* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
7#define _DEFAULT_SOURCE 1
Arnaldo Carvalho de Meloe206d552010-04-03 10:19:26 -03008#define HAS_BOOL
Ingo Molnar07800602009-04-20 15:00:56 +02009
Ingo Molnar07800602009-04-20 15:00:56 +020010#include <fcntl.h>
Arnaldo Carvalho de Meloe206d552010-04-03 10:19:26 -030011#include <stdbool.h>
Ingo Molnar07800602009-04-20 15:00:56 +020012#include <stddef.h>
13#include <stdlib.h>
14#include <stdarg.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +020015#include <linux/types.h>
Frederic Weisbecker1fe2c102009-08-12 10:19:53 +020016
Stephane Eranian45de34b2010-06-01 21:25:01 +020017extern char buildid_dir[];
Arnaldo Carvalho de Melo28902842009-11-23 17:51:08 -020018
Ingo Molnar07800602009-04-20 15:00:56 +020019#ifdef __GNUC__
20#define NORETURN __attribute__((__noreturn__))
21#else
22#define NORETURN
23#ifndef __attribute__
24#define __attribute__(x)
25#endif
26#endif
27
Namhyung Kimfc672972013-09-13 15:27:43 +090028#define PERF_GTK_DSO "libperf-gtk.so"
29
Ingo Molnar07800602009-04-20 15:00:56 +020030/* General helper functions */
Arnaldo Carvalho de Melo3938bad2016-03-23 15:06:35 -030031void usage(const char *err) NORETURN;
32void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
33int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
34void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
Ingo Molnar07800602009-04-20 15:00:56 +020035
Arnaldo Carvalho de Melo3938bad2016-03-23 15:06:35 -030036void set_warning_routine(void (*routine)(const char *err, va_list params));
Ingo Molnar07800602009-04-20 15:00:56 +020037
Arnaldo Carvalho de Melo3938bad2016-03-23 15:06:35 -030038int prefixcmp(const char *str, const char *prefix);
39void set_buildid_dir(const char *dir);
Ingo Molnar07800602009-04-20 15:00:56 +020040
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020041static inline void *zalloc(size_t size)
42{
43 return calloc(1, size);
44}
45
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -030046#define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
47
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030048struct dirent;
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030049struct strlist;
50
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020051int mkdir_p(char *path, mode_t mode);
Joe Stringer9a9c7332017-01-26 13:19:59 -080052int rm_rf(const char *path);
Masami Hiramatsue1ce7262016-04-26 18:02:42 +090053struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
54bool lsdir_no_dot_filter(const char *name, struct dirent *d);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020055int copyfile(const char *from, const char *to);
Adrian Hunter9a17d722013-10-14 13:43:41 +030056int copyfile_mode(const char *from, const char *to, mode_t mode);
Namhyung Kim9c9f5a22015-05-18 09:30:18 +090057int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020058
Jiri Olsa727ebd52013-11-28 11:30:14 +010059ssize_t readn(int fd, void *buf, size_t n);
Jiri Olsabc3a5022013-11-28 11:30:16 +010060ssize_t writen(int fd, void *buf, size_t n);
Arnaldo Carvalho de Meloe206d552010-04-03 10:19:26 -030061
Joerg Roedel1aed2672012-01-04 17:54:20 +010062struct perf_event_attr;
63
64void event_attr_init(struct perf_event_attr *attr);
65
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -030066size_t hex_width(u64 v);
Jiri Olsab2aff5f2012-10-27 23:18:30 +020067int hex2u64(const char *ptr, u64 *val);
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -030068
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030069extern unsigned int page_size;
Don Zickus2b1b7102014-05-30 16:10:05 -040070extern int cacheline_size;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -030071extern int sysctl_perf_event_max_stack;
72extern int sysctl_perf_event_max_contexts_per_stack;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030073
Jiri Olsa27050f52013-09-01 12:36:13 +020074struct parse_tag {
75 char tag;
76 int mult;
77};
78
79unsigned long parse_tag_value(const char *str, struct parse_tag *tags);
Namhyung Kimf048d542013-09-11 14:09:28 +090080
Adrian Hunter1a472452013-12-11 14:36:23 +020081int perf_event_paranoid(void);
Dongsheng Yange1a2b172013-12-06 17:25:51 -050082
Adrian Hunter71db07b2013-12-11 14:36:32 +020083void mem_bswap_64(void *src, int byte_size);
84void mem_bswap_32(void *src, int byte_size);
85
Jiri Olsa63914ac2014-08-01 17:46:54 +020086bool find_process(const char *name);
Namhyung Kime92ce122014-10-31 16:51:38 +090087
Wang Nan07bc5c62015-11-06 13:55:35 +000088int fetch_kernel_version(unsigned int *puint,
89 char *str, size_t str_sz);
Wang Nand3e0ce32015-11-06 13:58:09 +000090#define KVER_VERSION(x) (((x) >> 16) & 0xff)
91#define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff)
92#define KVER_SUBLEVEL(x) ((x) & 0xff)
93#define KVER_FMT "%d.%d.%d"
94#define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
Wang Nan07bc5c62015-11-06 13:55:35 +000095
Namhyung Kim14cbfbe2016-01-07 20:41:53 +090096const char *perf_tip(const char *dirpath);
97
Arnaldo Carvalho de Melo120010c2017-03-02 12:55:49 -030098#ifndef HAVE_SCHED_GETCPU_SUPPORT
99int sched_getcpu(void);
Arnaldo Carvalho de Meloc7007e92016-07-12 10:29:31 -0300100#endif
101
Borislav Petkov13559152013-02-20 16:32:31 +0100102#endif /* GIT_COMPAT_UTIL_H */