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...
26 unsigned int page_size;
29 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
30 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
32 bool test_attr__enabled;
34 bool perf_host = true;
35 bool perf_guest = false;
37 void event_attr_init(struct perf_event_attr *attr)
40 attr->exclude_host = 1;
42 attr->exclude_guest = 1;
43 /* to capture ABI version */
44 attr->size = sizeof(*attr);
47 int mkdir_p(char *path, mode_t mode)
56 if (stat(path, &st) == 0)
61 while ((d = strchr(d, '/'))) {
63 err = stat(path, &st) && mkdir(path, mode);
70 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
73 int rm_rf(const char *path)
78 char namebuf[PATH_MAX];
84 while ((d = readdir(dir)) != NULL && !ret) {
87 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
90 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
93 /* We have to check symbolic link itself */
94 ret = lstat(namebuf, &statbuf);
96 pr_debug("stat failed: %s\n", namebuf);
100 if (S_ISDIR(statbuf.st_mode))
101 ret = rm_rf(namebuf);
103 ret = unlink(namebuf);
113 /* A filter which removes dot files */
114 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
116 return d->d_name[0] != '.';
119 /* lsdir reads a directory and store it in strlist */
120 struct strlist *lsdir(const char *name,
121 bool (*filter)(const char *, struct dirent *))
123 struct strlist *list = NULL;
131 list = strlist__new(NULL, NULL);
137 while ((d = readdir(dir)) != NULL) {
138 if (!filter || filter(name, d))
139 strlist__add(list, d->d_name);
147 static int slow_copyfile(const char *from, const char *to)
152 FILE *from_fp = fopen(from, "r"), *to_fp;
157 to_fp = fopen(to, "w");
159 goto out_fclose_from;
161 while (getline(&line, &n, from_fp) > 0)
162 if (fputs(line, to_fp) == EOF)
174 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
179 pgoff = off_in & ~(page_size - 1);
182 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
183 if (ptr == MAP_FAILED)
187 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
188 if (ret < 0 && errno == EINTR)
197 munmap(ptr, off_in + size);
199 return size ? -1 : 0;
202 int copyfile_mode(const char *from, const char *to, mode_t mode)
207 char *tmp = NULL, *ptr = NULL;
212 /* extra 'x' at the end is to reserve space for '.' */
213 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
217 ptr = strrchr(tmp, '/');
220 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
227 if (fchmod(tofd, mode))
230 if (st.st_size == 0) { /* /proc? do it slowly... */
231 err = slow_copyfile(from, tmp);
235 fromfd = open(from, O_RDONLY);
239 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
252 int copyfile(const char *from, const char *to)
254 return copyfile_mode(from, to, 0755);
257 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
259 void *buf_start = buf;
263 ssize_t ret = is_read ? read(fd, buf, left) :
264 write(fd, buf, left);
266 if (ret < 0 && errno == EINTR)
275 BUG_ON((size_t)(buf - buf_start) != n);
280 * Read exactly 'n' bytes or return an error.
282 ssize_t readn(int fd, void *buf, size_t n)
284 return ion(true, fd, buf, n);
288 * Write exactly 'n' bytes or return an error.
290 ssize_t writen(int fd, void *buf, size_t n)
292 return ion(false, fd, buf, n);
295 size_t hex_width(u64 v)
305 static int hex(char ch)
307 if ((ch >= '0') && (ch <= '9'))
309 if ((ch >= 'a') && (ch <= 'f'))
310 return ch - 'a' + 10;
311 if ((ch >= 'A') && (ch <= 'F'))
312 return ch - 'A' + 10;
317 * While we find nice hex chars, build a long_val.
318 * Return number of chars processed.
320 int hex2u64(const char *ptr, u64 *long_val)
326 const int hex_val = hex(*p);
331 *long_val = (*long_val << 4) | hex_val;
338 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
340 struct parse_tag *i = tags;
345 s = strchr(str, i->tag);
347 unsigned long int value;
350 value = strtoul(str, &endptr, 10);
354 if (value > ULONG_MAX / i->mult)
362 return (unsigned long) -1;
365 int perf_event_paranoid(void)
369 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
375 void mem_bswap_32(void *src, int byte_size)
378 while (byte_size > 0) {
380 byte_size -= sizeof(u32);
385 void mem_bswap_64(void *src, int byte_size)
389 while (byte_size > 0) {
391 byte_size -= sizeof(u64);
396 bool find_process(const char *name)
398 size_t len = strlen(name);
403 dir = opendir(procfs__mountpoint());
407 /* Walk through the directory. */
408 while (ret && (d = readdir(dir)) != NULL) {
413 if ((d->d_type != DT_DIR) ||
414 !strcmp(".", d->d_name) ||
415 !strcmp("..", d->d_name))
418 scnprintf(path, sizeof(path), "%s/%s/comm",
419 procfs__mountpoint(), d->d_name);
421 if (filename__read_str(path, &data, &size))
424 ret = strncmp(name, data, len);
429 return ret ? false : true;
433 fetch_ubuntu_kernel_version(unsigned int *puint)
437 char *ptr, *line = NULL;
438 int version, patchlevel, sublevel, err;
439 FILE *vsig = fopen("/proc/version_signature", "r");
442 pr_debug("Open /proc/version_signature failed: %s\n",
447 len = getline(&line, &line_len, vsig);
451 pr_debug("Reading from /proc/version_signature failed: %s\n",
456 ptr = strrchr(line, ' ');
458 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
462 err = sscanf(ptr + 1, "%d.%d.%d",
463 &version, &patchlevel, &sublevel);
465 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
471 *puint = (version << 16) + (patchlevel << 8) + sublevel;
479 fetch_kernel_version(unsigned int *puint, char *str,
482 struct utsname utsname;
483 int version, patchlevel, sublevel, err;
484 bool int_ver_ready = false;
486 if (access("/proc/version_signature", R_OK) == 0)
487 if (!fetch_ubuntu_kernel_version(puint))
488 int_ver_ready = true;
493 if (str && str_size) {
494 strncpy(str, utsname.release, str_size);
495 str[str_size - 1] = '\0';
498 err = sscanf(utsname.release, "%d.%d.%d",
499 &version, &patchlevel, &sublevel);
502 pr_debug("Unable to get kernel version from uname '%s'\n",
507 if (puint && !int_ver_ready)
508 *puint = (version << 16) + (patchlevel << 8) + sublevel;
512 const char *perf_tip(const char *dirpath)
514 struct strlist *tips;
515 struct str_node *node;
517 struct strlist_config conf = {
522 tips = strlist__new("tips.txt", &conf);
524 return errno == ENOENT ? NULL :
525 "Tip: check path of tips.txt or get more memory! ;-p";
527 if (strlist__nr_entries(tips) == 0)
530 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
531 if (asprintf(&tip, "Tip: %s", node->s) < 0)
532 tip = (char *)"Tip: get more memory! ;-)";
535 strlist__delete(tips);