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>
26 * XXX We need to find a better place for these things...
29 bool perf_singlethreaded = true;
31 void perf_set_singlethreaded(void)
33 perf_singlethreaded = true;
36 void perf_set_multithreaded(void)
38 perf_singlethreaded = false;
41 unsigned int page_size;
43 #ifdef _SC_LEVEL1_DCACHE_LINESIZE
44 #define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
46 static void cache_line_size(int *cacheline_sizep)
48 if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
49 pr_debug("cannot determine cache line size");
53 int cacheline_size(void)
58 cache_line_size(&size);
63 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
64 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
66 int sysctl__max_stack(void)
70 if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
71 sysctl_perf_event_max_stack = value;
73 if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
74 sysctl_perf_event_max_contexts_per_stack = value;
76 return sysctl_perf_event_max_stack;
79 bool test_attr__enabled;
81 bool perf_host = true;
82 bool perf_guest = false;
84 void event_attr_init(struct perf_event_attr *attr)
87 attr->exclude_host = 1;
89 attr->exclude_guest = 1;
90 /* to capture ABI version */
91 attr->size = sizeof(*attr);
94 int mkdir_p(char *path, mode_t mode)
103 if (stat(path, &st) == 0)
108 while ((d = strchr(d, '/'))) {
110 err = stat(path, &st) && mkdir(path, mode);
117 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
121 * The depth specify how deep the removal will go.
122 * 0 - will remove only files under the 'path' directory
123 * 1 .. x - will dive in x-level deep under the 'path' directory
125 static int rm_rf_depth(const char *path, int depth)
130 char namebuf[PATH_MAX];
133 /* Do not fail if there's no file. */
134 ret = lstat(path, &statbuf);
138 /* Try to remove any file we get. */
139 if (!(statbuf.st_mode & S_IFDIR))
142 /* We have directory in path. */
147 while ((d = readdir(dir)) != NULL && !ret) {
149 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
152 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
155 /* We have to check symbolic link itself */
156 ret = lstat(namebuf, &statbuf);
158 pr_debug("stat failed: %s\n", namebuf);
162 if (S_ISDIR(statbuf.st_mode))
163 ret = depth ? rm_rf_depth(namebuf, depth - 1) : 0;
165 ret = unlink(namebuf);
175 int rm_rf(const char *path)
177 return rm_rf_depth(path, INT_MAX);
180 /* A filter which removes dot files */
181 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
183 return d->d_name[0] != '.';
186 /* lsdir reads a directory and store it in strlist */
187 struct strlist *lsdir(const char *name,
188 bool (*filter)(const char *, struct dirent *))
190 struct strlist *list = NULL;
198 list = strlist__new(NULL, NULL);
204 while ((d = readdir(dir)) != NULL) {
205 if (!filter || filter(name, d))
206 strlist__add(list, d->d_name);
214 static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
219 FILE *from_fp, *to_fp;
222 nsinfo__mountns_enter(nsi, &nsc);
223 from_fp = fopen(from, "r");
224 nsinfo__mountns_exit(&nsc);
228 to_fp = fopen(to, "w");
230 goto out_fclose_from;
232 while (getline(&line, &n, from_fp) > 0)
233 if (fputs(line, to_fp) == EOF)
245 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
250 pgoff = off_in & ~(page_size - 1);
253 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
254 if (ptr == MAP_FAILED)
258 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
259 if (ret < 0 && errno == EINTR)
268 munmap(ptr, off_in + size);
270 return size ? -1 : 0;
273 static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
279 char *tmp = NULL, *ptr = NULL;
282 nsinfo__mountns_enter(nsi, &nsc);
283 err = stat(from, &st);
284 nsinfo__mountns_exit(&nsc);
289 /* extra 'x' at the end is to reserve space for '.' */
290 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
294 ptr = strrchr(tmp, '/');
297 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
304 if (fchmod(tofd, mode))
307 if (st.st_size == 0) { /* /proc? do it slowly... */
308 err = slow_copyfile(from, tmp, nsi);
312 nsinfo__mountns_enter(nsi, &nsc);
313 fromfd = open(from, O_RDONLY);
314 nsinfo__mountns_exit(&nsc);
318 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
331 int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
333 return copyfile_mode_ns(from, to, 0755, nsi);
336 int copyfile_mode(const char *from, const char *to, mode_t mode)
338 return copyfile_mode_ns(from, to, mode, NULL);
341 int copyfile(const char *from, const char *to)
343 return copyfile_mode(from, to, 0755);
346 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
348 void *buf_start = buf;
352 /* buf must be treated as const if !is_read. */
353 ssize_t ret = is_read ? read(fd, buf, left) :
354 write(fd, buf, left);
356 if (ret < 0 && errno == EINTR)
365 BUG_ON((size_t)(buf - buf_start) != n);
370 * Read exactly 'n' bytes or return an error.
372 ssize_t readn(int fd, void *buf, size_t n)
374 return ion(true, fd, buf, n);
378 * Write exactly 'n' bytes or return an error.
380 ssize_t writen(int fd, const void *buf, size_t n)
382 /* ion does not modify buf. */
383 return ion(false, fd, (void *)buf, n);
386 size_t hex_width(u64 v)
397 * While we find nice hex chars, build a long_val.
398 * Return number of chars processed.
400 int hex2u64(const char *ptr, u64 *long_val)
404 *long_val = strtoull(ptr, &p, 16);
409 int perf_event_paranoid(void)
413 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
419 fetch_ubuntu_kernel_version(unsigned int *puint)
423 char *ptr, *line = NULL;
424 int version, patchlevel, sublevel, err;
430 vsig = fopen("/proc/version_signature", "r");
432 pr_debug("Open /proc/version_signature failed: %s\n",
437 len = getline(&line, &line_len, vsig);
441 pr_debug("Reading from /proc/version_signature failed: %s\n",
446 ptr = strrchr(line, ' ');
448 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
452 err = sscanf(ptr + 1, "%d.%d.%d",
453 &version, &patchlevel, &sublevel);
455 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
460 *puint = (version << 16) + (patchlevel << 8) + sublevel;
468 fetch_kernel_version(unsigned int *puint, char *str,
471 struct utsname utsname;
472 int version, patchlevel, sublevel, err;
473 bool int_ver_ready = false;
475 if (access("/proc/version_signature", R_OK) == 0)
476 if (!fetch_ubuntu_kernel_version(puint))
477 int_ver_ready = true;
482 if (str && str_size) {
483 strncpy(str, utsname.release, str_size);
484 str[str_size - 1] = '\0';
487 if (!puint || int_ver_ready)
490 err = sscanf(utsname.release, "%d.%d.%d",
491 &version, &patchlevel, &sublevel);
494 pr_debug("Unable to get kernel version from uname '%s'\n",
499 *puint = (version << 16) + (patchlevel << 8) + sublevel;
503 const char *perf_tip(const char *dirpath)
505 struct strlist *tips;
506 struct str_node *node;
508 struct strlist_config conf = {
513 tips = strlist__new("tips.txt", &conf);
515 return errno == ENOENT ? NULL :
516 "Tip: check path of tips.txt or get more memory! ;-p";
518 if (strlist__nr_entries(tips) == 0)
521 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
522 if (asprintf(&tip, "Tip: %s", node->s) < 0)
523 tip = (char *)"Tip: get more memory! ;-)";
526 strlist__delete(tips);