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;
120 int rm_rf(const char *path)
125 char namebuf[PATH_MAX];
128 /* Do not fail if there's no file. */
129 ret = lstat(path, &statbuf);
133 /* Try to remove any file we get. */
134 if (!(statbuf.st_mode & S_IFDIR))
137 /* We have directory in path. */
142 while ((d = readdir(dir)) != NULL && !ret) {
144 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
147 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
150 /* We have to check symbolic link itself */
151 ret = lstat(namebuf, &statbuf);
153 pr_debug("stat failed: %s\n", namebuf);
157 if (S_ISDIR(statbuf.st_mode))
158 ret = rm_rf(namebuf);
160 ret = unlink(namebuf);
170 /* A filter which removes dot files */
171 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
173 return d->d_name[0] != '.';
176 /* lsdir reads a directory and store it in strlist */
177 struct strlist *lsdir(const char *name,
178 bool (*filter)(const char *, struct dirent *))
180 struct strlist *list = NULL;
188 list = strlist__new(NULL, NULL);
194 while ((d = readdir(dir)) != NULL) {
195 if (!filter || filter(name, d))
196 strlist__add(list, d->d_name);
204 static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
209 FILE *from_fp, *to_fp;
212 nsinfo__mountns_enter(nsi, &nsc);
213 from_fp = fopen(from, "r");
214 nsinfo__mountns_exit(&nsc);
218 to_fp = fopen(to, "w");
220 goto out_fclose_from;
222 while (getline(&line, &n, from_fp) > 0)
223 if (fputs(line, to_fp) == EOF)
235 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
240 pgoff = off_in & ~(page_size - 1);
243 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
244 if (ptr == MAP_FAILED)
248 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
249 if (ret < 0 && errno == EINTR)
258 munmap(ptr, off_in + size);
260 return size ? -1 : 0;
263 static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
269 char *tmp = NULL, *ptr = NULL;
272 nsinfo__mountns_enter(nsi, &nsc);
273 err = stat(from, &st);
274 nsinfo__mountns_exit(&nsc);
279 /* extra 'x' at the end is to reserve space for '.' */
280 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
284 ptr = strrchr(tmp, '/');
287 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
294 if (fchmod(tofd, mode))
297 if (st.st_size == 0) { /* /proc? do it slowly... */
298 err = slow_copyfile(from, tmp, nsi);
302 nsinfo__mountns_enter(nsi, &nsc);
303 fromfd = open(from, O_RDONLY);
304 nsinfo__mountns_exit(&nsc);
308 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
321 int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
323 return copyfile_mode_ns(from, to, 0755, nsi);
326 int copyfile_mode(const char *from, const char *to, mode_t mode)
328 return copyfile_mode_ns(from, to, mode, NULL);
331 int copyfile(const char *from, const char *to)
333 return copyfile_mode(from, to, 0755);
336 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
338 void *buf_start = buf;
342 /* buf must be treated as const if !is_read. */
343 ssize_t ret = is_read ? read(fd, buf, left) :
344 write(fd, buf, left);
346 if (ret < 0 && errno == EINTR)
355 BUG_ON((size_t)(buf - buf_start) != n);
360 * Read exactly 'n' bytes or return an error.
362 ssize_t readn(int fd, void *buf, size_t n)
364 return ion(true, fd, buf, n);
368 * Write exactly 'n' bytes or return an error.
370 ssize_t writen(int fd, const void *buf, size_t n)
372 /* ion does not modify buf. */
373 return ion(false, fd, (void *)buf, n);
376 size_t hex_width(u64 v)
387 * While we find nice hex chars, build a long_val.
388 * Return number of chars processed.
390 int hex2u64(const char *ptr, u64 *long_val)
394 *long_val = strtoull(ptr, &p, 16);
399 int perf_event_paranoid(void)
403 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
409 fetch_ubuntu_kernel_version(unsigned int *puint)
413 char *ptr, *line = NULL;
414 int version, patchlevel, sublevel, err;
420 vsig = fopen("/proc/version_signature", "r");
422 pr_debug("Open /proc/version_signature failed: %s\n",
427 len = getline(&line, &line_len, vsig);
431 pr_debug("Reading from /proc/version_signature failed: %s\n",
436 ptr = strrchr(line, ' ');
438 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
442 err = sscanf(ptr + 1, "%d.%d.%d",
443 &version, &patchlevel, &sublevel);
445 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
450 *puint = (version << 16) + (patchlevel << 8) + sublevel;
458 fetch_kernel_version(unsigned int *puint, char *str,
461 struct utsname utsname;
462 int version, patchlevel, sublevel, err;
463 bool int_ver_ready = false;
465 if (access("/proc/version_signature", R_OK) == 0)
466 if (!fetch_ubuntu_kernel_version(puint))
467 int_ver_ready = true;
472 if (str && str_size) {
473 strncpy(str, utsname.release, str_size);
474 str[str_size - 1] = '\0';
477 if (!puint || int_ver_ready)
480 err = sscanf(utsname.release, "%d.%d.%d",
481 &version, &patchlevel, &sublevel);
484 pr_debug("Unable to get kernel version from uname '%s'\n",
489 *puint = (version << 16) + (patchlevel << 8) + sublevel;
493 const char *perf_tip(const char *dirpath)
495 struct strlist *tips;
496 struct str_node *node;
498 struct strlist_config conf = {
503 tips = strlist__new("tips.txt", &conf);
505 return errno == ENOENT ? NULL :
506 "Tip: check path of tips.txt or get more memory! ;-p";
508 if (strlist__nr_entries(tips) == 0)
511 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
512 if (asprintf(&tip, "Tip: %s", node->s) < 0)
513 tip = (char *)"Tip: get more memory! ;-)";
516 strlist__delete(tips);