7 #include <sys/utsname.h>
16 #include <linux/kernel.h>
17 #include <linux/log2.h>
18 #include <linux/time64.h>
23 * XXX We need to find a better place for these things...
25 unsigned int page_size;
28 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
29 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
31 bool test_attr__enabled;
33 bool perf_host = true;
34 bool perf_guest = false;
36 void event_attr_init(struct perf_event_attr *attr)
39 attr->exclude_host = 1;
41 attr->exclude_guest = 1;
42 /* to capture ABI version */
43 attr->size = sizeof(*attr);
46 int mkdir_p(char *path, mode_t mode)
55 if (stat(path, &st) == 0)
60 while ((d = strchr(d, '/'))) {
62 err = stat(path, &st) && mkdir(path, mode);
69 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
72 int rm_rf(const char *path)
77 char namebuf[PATH_MAX];
83 while ((d = readdir(dir)) != NULL && !ret) {
86 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
89 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
92 /* We have to check symbolic link itself */
93 ret = lstat(namebuf, &statbuf);
95 pr_debug("stat failed: %s\n", namebuf);
99 if (S_ISDIR(statbuf.st_mode))
100 ret = rm_rf(namebuf);
102 ret = unlink(namebuf);
112 /* A filter which removes dot files */
113 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
115 return d->d_name[0] != '.';
118 /* lsdir reads a directory and store it in strlist */
119 struct strlist *lsdir(const char *name,
120 bool (*filter)(const char *, struct dirent *))
122 struct strlist *list = NULL;
130 list = strlist__new(NULL, NULL);
136 while ((d = readdir(dir)) != NULL) {
137 if (!filter || filter(name, d))
138 strlist__add(list, d->d_name);
146 static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
151 FILE *from_fp, *to_fp;
154 nsinfo__mountns_enter(nsi, &nsc);
155 from_fp = fopen(from, "r");
156 nsinfo__mountns_exit(&nsc);
160 to_fp = fopen(to, "w");
162 goto out_fclose_from;
164 while (getline(&line, &n, from_fp) > 0)
165 if (fputs(line, to_fp) == EOF)
177 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
182 pgoff = off_in & ~(page_size - 1);
185 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
186 if (ptr == MAP_FAILED)
190 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
191 if (ret < 0 && errno == EINTR)
200 munmap(ptr, off_in + size);
202 return size ? -1 : 0;
205 static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
211 char *tmp = NULL, *ptr = NULL;
214 nsinfo__mountns_enter(nsi, &nsc);
215 err = stat(from, &st);
216 nsinfo__mountns_exit(&nsc);
221 /* extra 'x' at the end is to reserve space for '.' */
222 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
226 ptr = strrchr(tmp, '/');
229 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
236 if (fchmod(tofd, mode))
239 if (st.st_size == 0) { /* /proc? do it slowly... */
240 err = slow_copyfile(from, tmp, nsi);
244 nsinfo__mountns_enter(nsi, &nsc);
245 fromfd = open(from, O_RDONLY);
246 nsinfo__mountns_exit(&nsc);
250 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
263 int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
265 return copyfile_mode_ns(from, to, 0755, nsi);
268 int copyfile_mode(const char *from, const char *to, mode_t mode)
270 return copyfile_mode_ns(from, to, mode, NULL);
273 int copyfile(const char *from, const char *to)
275 return copyfile_mode(from, to, 0755);
278 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
280 void *buf_start = buf;
284 ssize_t ret = is_read ? read(fd, buf, left) :
285 write(fd, buf, left);
287 if (ret < 0 && errno == EINTR)
296 BUG_ON((size_t)(buf - buf_start) != n);
301 * Read exactly 'n' bytes or return an error.
303 ssize_t readn(int fd, void *buf, size_t n)
305 return ion(true, fd, buf, n);
309 * Write exactly 'n' bytes or return an error.
311 ssize_t writen(int fd, void *buf, size_t n)
313 return ion(false, fd, buf, n);
316 size_t hex_width(u64 v)
326 static int hex(char ch)
328 if ((ch >= '0') && (ch <= '9'))
330 if ((ch >= 'a') && (ch <= 'f'))
331 return ch - 'a' + 10;
332 if ((ch >= 'A') && (ch <= 'F'))
333 return ch - 'A' + 10;
338 * While we find nice hex chars, build a long_val.
339 * Return number of chars processed.
341 int hex2u64(const char *ptr, u64 *long_val)
347 const int hex_val = hex(*p);
352 *long_val = (*long_val << 4) | hex_val;
359 int perf_event_paranoid(void)
363 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
369 fetch_ubuntu_kernel_version(unsigned int *puint)
373 char *ptr, *line = NULL;
374 int version, patchlevel, sublevel, err;
380 vsig = fopen("/proc/version_signature", "r");
382 pr_debug("Open /proc/version_signature failed: %s\n",
387 len = getline(&line, &line_len, vsig);
391 pr_debug("Reading from /proc/version_signature failed: %s\n",
396 ptr = strrchr(line, ' ');
398 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
402 err = sscanf(ptr + 1, "%d.%d.%d",
403 &version, &patchlevel, &sublevel);
405 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
410 *puint = (version << 16) + (patchlevel << 8) + sublevel;
418 fetch_kernel_version(unsigned int *puint, char *str,
421 struct utsname utsname;
422 int version, patchlevel, sublevel, err;
423 bool int_ver_ready = false;
425 if (access("/proc/version_signature", R_OK) == 0)
426 if (!fetch_ubuntu_kernel_version(puint))
427 int_ver_ready = true;
432 if (str && str_size) {
433 strncpy(str, utsname.release, str_size);
434 str[str_size - 1] = '\0';
437 if (!puint || int_ver_ready)
440 err = sscanf(utsname.release, "%d.%d.%d",
441 &version, &patchlevel, &sublevel);
444 pr_debug("Unable to get kernel version from uname '%s'\n",
449 *puint = (version << 16) + (patchlevel << 8) + sublevel;
453 const char *perf_tip(const char *dirpath)
455 struct strlist *tips;
456 struct str_node *node;
458 struct strlist_config conf = {
463 tips = strlist__new("tips.txt", &conf);
465 return errno == ENOENT ? NULL :
466 "Tip: check path of tips.txt or get more memory! ;-p";
468 if (strlist__nr_entries(tips) == 0)
471 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
472 if (asprintf(&tip, "Tip: %s", node->s) < 0)
473 tip = (char *)"Tip: get more memory! ;-)";
476 strlist__delete(tips);