1 // SPDX-License-Identifier: GPL-2.0
5 #include "namespaces.h"
9 #include <sys/utsname.h>
19 #include <linux/kernel.h>
20 #include <linux/log2.h>
21 #include <linux/time64.h>
27 * XXX We need to find a better place for these things...
30 bool perf_singlethreaded = true;
32 void perf_set_singlethreaded(void)
34 perf_singlethreaded = true;
37 void perf_set_multithreaded(void)
39 perf_singlethreaded = false;
42 unsigned int page_size;
44 #ifdef _SC_LEVEL1_DCACHE_LINESIZE
45 #define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
47 static void cache_line_size(int *cacheline_sizep)
49 if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
50 pr_debug("cannot determine cache line size");
54 int cacheline_size(void)
59 cache_line_size(&size);
64 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
65 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
67 int sysctl__max_stack(void)
71 if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
72 sysctl_perf_event_max_stack = value;
74 if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
75 sysctl_perf_event_max_contexts_per_stack = value;
77 return sysctl_perf_event_max_stack;
80 bool test_attr__enabled;
82 bool perf_host = true;
83 bool perf_guest = false;
85 void event_attr_init(struct perf_event_attr *attr)
88 attr->exclude_host = 1;
90 attr->exclude_guest = 1;
91 /* to capture ABI version */
92 attr->size = sizeof(*attr);
95 int mkdir_p(char *path, mode_t mode)
104 if (stat(path, &st) == 0)
109 while ((d = strchr(d, '/'))) {
111 err = stat(path, &st) && mkdir(path, mode);
118 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
121 static bool match_pat(char *file, const char **pat)
129 if (strglobmatch(file, pat[i]))
139 * The depth specify how deep the removal will go.
140 * 0 - will remove only files under the 'path' directory
141 * 1 .. x - will dive in x-level deep under the 'path' directory
143 * If specified the pat is array of string patterns ended with NULL,
144 * which are checked upon every file/directory found. Only matching
147 * The function returns:
149 * -1 on removal failure with errno set
150 * -2 on pattern failure
152 static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
157 char namebuf[PATH_MAX];
160 /* Do not fail if there's no file. */
161 ret = lstat(path, &statbuf);
165 /* Try to remove any file we get. */
166 if (!(statbuf.st_mode & S_IFDIR))
169 /* We have directory in path. */
174 while ((d = readdir(dir)) != NULL && !ret) {
176 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
179 if (!match_pat(d->d_name, pat))
182 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
185 /* We have to check symbolic link itself */
186 ret = lstat(namebuf, &statbuf);
188 pr_debug("stat failed: %s\n", namebuf);
192 if (S_ISDIR(statbuf.st_mode))
193 ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0;
195 ret = unlink(namebuf);
205 int rm_rf_perf_data(const char *path)
207 const char *pat[] = {
213 return rm_rf_depth_pat(path, 0, pat);
216 int rm_rf(const char *path)
218 return rm_rf_depth_pat(path, INT_MAX, NULL);
221 /* A filter which removes dot files */
222 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
224 return d->d_name[0] != '.';
227 /* lsdir reads a directory and store it in strlist */
228 struct strlist *lsdir(const char *name,
229 bool (*filter)(const char *, struct dirent *))
231 struct strlist *list = NULL;
239 list = strlist__new(NULL, NULL);
245 while ((d = readdir(dir)) != NULL) {
246 if (!filter || filter(name, d))
247 strlist__add(list, d->d_name);
255 static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
260 FILE *from_fp, *to_fp;
263 nsinfo__mountns_enter(nsi, &nsc);
264 from_fp = fopen(from, "r");
265 nsinfo__mountns_exit(&nsc);
269 to_fp = fopen(to, "w");
271 goto out_fclose_from;
273 while (getline(&line, &n, from_fp) > 0)
274 if (fputs(line, to_fp) == EOF)
286 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
291 pgoff = off_in & ~(page_size - 1);
294 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
295 if (ptr == MAP_FAILED)
299 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
300 if (ret < 0 && errno == EINTR)
309 munmap(ptr, off_in + size);
311 return size ? -1 : 0;
314 static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
320 char *tmp = NULL, *ptr = NULL;
323 nsinfo__mountns_enter(nsi, &nsc);
324 err = stat(from, &st);
325 nsinfo__mountns_exit(&nsc);
330 /* extra 'x' at the end is to reserve space for '.' */
331 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
335 ptr = strrchr(tmp, '/');
338 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
345 if (fchmod(tofd, mode))
348 if (st.st_size == 0) { /* /proc? do it slowly... */
349 err = slow_copyfile(from, tmp, nsi);
353 nsinfo__mountns_enter(nsi, &nsc);
354 fromfd = open(from, O_RDONLY);
355 nsinfo__mountns_exit(&nsc);
359 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
372 int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
374 return copyfile_mode_ns(from, to, 0755, nsi);
377 int copyfile_mode(const char *from, const char *to, mode_t mode)
379 return copyfile_mode_ns(from, to, mode, NULL);
382 int copyfile(const char *from, const char *to)
384 return copyfile_mode(from, to, 0755);
387 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
389 void *buf_start = buf;
393 /* buf must be treated as const if !is_read. */
394 ssize_t ret = is_read ? read(fd, buf, left) :
395 write(fd, buf, left);
397 if (ret < 0 && errno == EINTR)
406 BUG_ON((size_t)(buf - buf_start) != n);
411 * Read exactly 'n' bytes or return an error.
413 ssize_t readn(int fd, void *buf, size_t n)
415 return ion(true, fd, buf, n);
419 * Write exactly 'n' bytes or return an error.
421 ssize_t writen(int fd, const void *buf, size_t n)
423 /* ion does not modify buf. */
424 return ion(false, fd, (void *)buf, n);
427 size_t hex_width(u64 v)
438 * While we find nice hex chars, build a long_val.
439 * Return number of chars processed.
441 int hex2u64(const char *ptr, u64 *long_val)
445 *long_val = strtoull(ptr, &p, 16);
450 int perf_event_paranoid(void)
454 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
460 fetch_ubuntu_kernel_version(unsigned int *puint)
464 char *ptr, *line = NULL;
465 int version, patchlevel, sublevel, err;
471 vsig = fopen("/proc/version_signature", "r");
473 pr_debug("Open /proc/version_signature failed: %s\n",
478 len = getline(&line, &line_len, vsig);
482 pr_debug("Reading from /proc/version_signature failed: %s\n",
487 ptr = strrchr(line, ' ');
489 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
493 err = sscanf(ptr + 1, "%d.%d.%d",
494 &version, &patchlevel, &sublevel);
496 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
501 *puint = (version << 16) + (patchlevel << 8) + sublevel;
509 fetch_kernel_version(unsigned int *puint, char *str,
512 struct utsname utsname;
513 int version, patchlevel, sublevel, err;
514 bool int_ver_ready = false;
516 if (access("/proc/version_signature", R_OK) == 0)
517 if (!fetch_ubuntu_kernel_version(puint))
518 int_ver_ready = true;
523 if (str && str_size) {
524 strncpy(str, utsname.release, str_size);
525 str[str_size - 1] = '\0';
528 if (!puint || int_ver_ready)
531 err = sscanf(utsname.release, "%d.%d.%d",
532 &version, &patchlevel, &sublevel);
535 pr_debug("Unable to get kernel version from uname '%s'\n",
540 *puint = (version << 16) + (patchlevel << 8) + sublevel;
544 const char *perf_tip(const char *dirpath)
546 struct strlist *tips;
547 struct str_node *node;
549 struct strlist_config conf = {
554 tips = strlist__new("tips.txt", &conf);
556 return errno == ENOENT ? NULL :
557 "Tip: check path of tips.txt or get more memory! ;-p";
559 if (strlist__nr_entries(tips) == 0)
562 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
563 if (asprintf(&tip, "Tip: %s", node->s) < 0)
564 tip = (char *)"Tip: get more memory! ;-)";
567 strlist__delete(tips);
572 char *perf_exe(char *buf, int len)
574 int n = readlink("/proc/self/exe", buf, len);
579 return strcpy(buf, "perf");