7 #include <sys/utsname.h>
17 #include <linux/kernel.h>
18 #include <linux/log2.h>
19 #include <linux/time64.h>
24 * XXX We need to find a better place for these things...
27 bool perf_singlethreaded = true;
29 void perf_set_singlethreaded(void)
31 perf_singlethreaded = true;
34 void perf_set_multithreaded(void)
36 perf_singlethreaded = false;
39 unsigned int page_size;
42 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
43 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
45 bool test_attr__enabled;
47 bool perf_host = true;
48 bool perf_guest = false;
50 void event_attr_init(struct perf_event_attr *attr)
53 attr->exclude_host = 1;
55 attr->exclude_guest = 1;
56 /* to capture ABI version */
57 attr->size = sizeof(*attr);
60 int mkdir_p(char *path, mode_t mode)
69 if (stat(path, &st) == 0)
74 while ((d = strchr(d, '/'))) {
76 err = stat(path, &st) && mkdir(path, mode);
83 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
86 int rm_rf(const char *path)
91 char namebuf[PATH_MAX];
97 while ((d = readdir(dir)) != NULL && !ret) {
100 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
103 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
106 /* We have to check symbolic link itself */
107 ret = lstat(namebuf, &statbuf);
109 pr_debug("stat failed: %s\n", namebuf);
113 if (S_ISDIR(statbuf.st_mode))
114 ret = rm_rf(namebuf);
116 ret = unlink(namebuf);
126 /* A filter which removes dot files */
127 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
129 return d->d_name[0] != '.';
132 /* lsdir reads a directory and store it in strlist */
133 struct strlist *lsdir(const char *name,
134 bool (*filter)(const char *, struct dirent *))
136 struct strlist *list = NULL;
144 list = strlist__new(NULL, NULL);
150 while ((d = readdir(dir)) != NULL) {
151 if (!filter || filter(name, d))
152 strlist__add(list, d->d_name);
160 static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
165 FILE *from_fp, *to_fp;
168 nsinfo__mountns_enter(nsi, &nsc);
169 from_fp = fopen(from, "r");
170 nsinfo__mountns_exit(&nsc);
174 to_fp = fopen(to, "w");
176 goto out_fclose_from;
178 while (getline(&line, &n, from_fp) > 0)
179 if (fputs(line, to_fp) == EOF)
191 static int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
196 pgoff = off_in & ~(page_size - 1);
199 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
200 if (ptr == MAP_FAILED)
204 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
205 if (ret < 0 && errno == EINTR)
214 munmap(ptr, off_in + size);
216 return size ? -1 : 0;
219 static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
225 char *tmp = NULL, *ptr = NULL;
228 nsinfo__mountns_enter(nsi, &nsc);
229 err = stat(from, &st);
230 nsinfo__mountns_exit(&nsc);
235 /* extra 'x' at the end is to reserve space for '.' */
236 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
240 ptr = strrchr(tmp, '/');
243 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
250 if (fchmod(tofd, mode))
253 if (st.st_size == 0) { /* /proc? do it slowly... */
254 err = slow_copyfile(from, tmp, nsi);
258 nsinfo__mountns_enter(nsi, &nsc);
259 fromfd = open(from, O_RDONLY);
260 nsinfo__mountns_exit(&nsc);
264 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
277 int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
279 return copyfile_mode_ns(from, to, 0755, nsi);
282 int copyfile_mode(const char *from, const char *to, mode_t mode)
284 return copyfile_mode_ns(from, to, mode, NULL);
287 int copyfile(const char *from, const char *to)
289 return copyfile_mode(from, to, 0755);
292 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
294 void *buf_start = buf;
298 /* buf must be treated as const if !is_read. */
299 ssize_t ret = is_read ? read(fd, buf, left) :
300 write(fd, buf, left);
302 if (ret < 0 && errno == EINTR)
311 BUG_ON((size_t)(buf - buf_start) != n);
316 * Read exactly 'n' bytes or return an error.
318 ssize_t readn(int fd, void *buf, size_t n)
320 return ion(true, fd, buf, n);
324 * Write exactly 'n' bytes or return an error.
326 ssize_t writen(int fd, const void *buf, size_t n)
328 /* ion does not modify buf. */
329 return ion(false, fd, (void *)buf, n);
332 size_t hex_width(u64 v)
342 static int hex(char ch)
344 if ((ch >= '0') && (ch <= '9'))
346 if ((ch >= 'a') && (ch <= 'f'))
347 return ch - 'a' + 10;
348 if ((ch >= 'A') && (ch <= 'F'))
349 return ch - 'A' + 10;
354 * While we find nice hex chars, build a long_val.
355 * Return number of chars processed.
357 int hex2u64(const char *ptr, u64 *long_val)
363 const int hex_val = hex(*p);
368 *long_val = (*long_val << 4) | hex_val;
375 int perf_event_paranoid(void)
379 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
385 fetch_ubuntu_kernel_version(unsigned int *puint)
389 char *ptr, *line = NULL;
390 int version, patchlevel, sublevel, err;
396 vsig = fopen("/proc/version_signature", "r");
398 pr_debug("Open /proc/version_signature failed: %s\n",
403 len = getline(&line, &line_len, vsig);
407 pr_debug("Reading from /proc/version_signature failed: %s\n",
412 ptr = strrchr(line, ' ');
414 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
418 err = sscanf(ptr + 1, "%d.%d.%d",
419 &version, &patchlevel, &sublevel);
421 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
426 *puint = (version << 16) + (patchlevel << 8) + sublevel;
434 fetch_kernel_version(unsigned int *puint, char *str,
437 struct utsname utsname;
438 int version, patchlevel, sublevel, err;
439 bool int_ver_ready = false;
441 if (access("/proc/version_signature", R_OK) == 0)
442 if (!fetch_ubuntu_kernel_version(puint))
443 int_ver_ready = true;
448 if (str && str_size) {
449 strncpy(str, utsname.release, str_size);
450 str[str_size - 1] = '\0';
453 if (!puint || int_ver_ready)
456 err = sscanf(utsname.release, "%d.%d.%d",
457 &version, &patchlevel, &sublevel);
460 pr_debug("Unable to get kernel version from uname '%s'\n",
465 *puint = (version << 16) + (patchlevel << 8) + sublevel;
469 const char *perf_tip(const char *dirpath)
471 struct strlist *tips;
472 struct str_node *node;
474 struct strlist_config conf = {
479 tips = strlist__new("tips.txt", &conf);
481 return errno == ENOENT ? NULL :
482 "Tip: check path of tips.txt or get more memory! ;-p";
484 if (strlist__nr_entries(tips) == 0)
487 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
488 if (asprintf(&tip, "Tip: %s", node->s) < 0)
489 tip = (char *)"Tip: get more memory! ;-)";
492 strlist__delete(tips);