xref: /openbmc/linux/tools/perf/util/util.c (revision 123a039d0d54de6d5bafd551e7aa17569e13e315)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "../perf.h"
3 #include "util.h"
4 #include "debug.h"
5 #include "namespaces.h"
6 #include <api/fs/fs.h>
7 #include <sys/mman.h>
8 #include <sys/stat.h>
9 #include <sys/utsname.h>
10 #include <dirent.h>
11 #include <fcntl.h>
12 #include <inttypes.h>
13 #include <signal.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <linux/kernel.h>
20 #include <linux/log2.h>
21 #include <linux/time64.h>
22 #include <unistd.h>
23 #include "strlist.h"
24 #include "string2.h"
25 
26 /*
27  * XXX We need to find a better place for these things...
28  */
29 
30 bool perf_singlethreaded = true;
31 
32 void perf_set_singlethreaded(void)
33 {
34 	perf_singlethreaded = true;
35 }
36 
37 void perf_set_multithreaded(void)
38 {
39 	perf_singlethreaded = false;
40 }
41 
42 unsigned int page_size;
43 
44 #ifdef _SC_LEVEL1_DCACHE_LINESIZE
45 #define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
46 #else
47 static void cache_line_size(int *cacheline_sizep)
48 {
49 	if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
50 		pr_debug("cannot determine cache line size");
51 }
52 #endif
53 
54 int cacheline_size(void)
55 {
56 	static int size;
57 
58 	if (!size)
59 		cache_line_size(&size);
60 
61 	return size;
62 }
63 
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;
66 
67 int sysctl__max_stack(void)
68 {
69 	int value;
70 
71 	if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
72 		sysctl_perf_event_max_stack = value;
73 
74 	if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
75 		sysctl_perf_event_max_contexts_per_stack = value;
76 
77 	return sysctl_perf_event_max_stack;
78 }
79 
80 bool test_attr__enabled;
81 
82 bool perf_host  = true;
83 bool perf_guest = false;
84 
85 void event_attr_init(struct perf_event_attr *attr)
86 {
87 	if (!perf_host)
88 		attr->exclude_host  = 1;
89 	if (!perf_guest)
90 		attr->exclude_guest = 1;
91 	/* to capture ABI version */
92 	attr->size = sizeof(*attr);
93 }
94 
95 int mkdir_p(char *path, mode_t mode)
96 {
97 	struct stat st;
98 	int err;
99 	char *d = path;
100 
101 	if (*d != '/')
102 		return -1;
103 
104 	if (stat(path, &st) == 0)
105 		return 0;
106 
107 	while (*++d == '/');
108 
109 	while ((d = strchr(d, '/'))) {
110 		*d = '\0';
111 		err = stat(path, &st) && mkdir(path, mode);
112 		*d++ = '/';
113 		if (err)
114 			return -1;
115 		while (*d == '/')
116 			++d;
117 	}
118 	return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
119 }
120 
121 static bool match_pat(char *file, const char **pat)
122 {
123 	int i = 0;
124 
125 	if (!pat)
126 		return true;
127 
128 	while (pat[i]) {
129 		if (strglobmatch(file, pat[i]))
130 			return true;
131 
132 		i++;
133 	}
134 
135 	return false;
136 }
137 
138 /*
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
142  *
143  * If specified the pat is array of string patterns ended with NULL,
144  * which are checked upon every file/directory found. Only matching
145  * ones are removed.
146  *
147  * The function returns:
148  *    0 on success
149  *   -1 on removal failure with errno set
150  *   -2 on pattern failure
151  */
152 static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
153 {
154 	DIR *dir;
155 	int ret;
156 	struct dirent *d;
157 	char namebuf[PATH_MAX];
158 	struct stat statbuf;
159 
160 	/* Do not fail if there's no file. */
161 	ret = lstat(path, &statbuf);
162 	if (ret)
163 		return 0;
164 
165 	/* Try to remove any file we get. */
166 	if (!(statbuf.st_mode & S_IFDIR))
167 		return unlink(path);
168 
169 	/* We have directory in path. */
170 	dir = opendir(path);
171 	if (dir == NULL)
172 		return -1;
173 
174 	while ((d = readdir(dir)) != NULL && !ret) {
175 
176 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
177 			continue;
178 
179 		if (!match_pat(d->d_name, pat))
180 			return -2;
181 
182 		scnprintf(namebuf, sizeof(namebuf), "%s/%s",
183 			  path, d->d_name);
184 
185 		/* We have to check symbolic link itself */
186 		ret = lstat(namebuf, &statbuf);
187 		if (ret < 0) {
188 			pr_debug("stat failed: %s\n", namebuf);
189 			break;
190 		}
191 
192 		if (S_ISDIR(statbuf.st_mode))
193 			ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0;
194 		else
195 			ret = unlink(namebuf);
196 	}
197 	closedir(dir);
198 
199 	if (ret < 0)
200 		return ret;
201 
202 	return rmdir(path);
203 }
204 
205 int rm_rf_perf_data(const char *path)
206 {
207 	const char *pat[] = {
208 		"header",
209 		"data.*",
210 		NULL,
211 	};
212 
213 	return rm_rf_depth_pat(path, 0, pat);
214 }
215 
216 int rm_rf(const char *path)
217 {
218 	return rm_rf_depth_pat(path, INT_MAX, NULL);
219 }
220 
221 /* A filter which removes dot files */
222 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
223 {
224 	return d->d_name[0] != '.';
225 }
226 
227 /* lsdir reads a directory and store it in strlist */
228 struct strlist *lsdir(const char *name,
229 		      bool (*filter)(const char *, struct dirent *))
230 {
231 	struct strlist *list = NULL;
232 	DIR *dir;
233 	struct dirent *d;
234 
235 	dir = opendir(name);
236 	if (!dir)
237 		return NULL;
238 
239 	list = strlist__new(NULL, NULL);
240 	if (!list) {
241 		errno = ENOMEM;
242 		goto out;
243 	}
244 
245 	while ((d = readdir(dir)) != NULL) {
246 		if (!filter || filter(name, d))
247 			strlist__add(list, d->d_name);
248 	}
249 
250 out:
251 	closedir(dir);
252 	return list;
253 }
254 
255 static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
256 {
257 	int err = -1;
258 	char *line = NULL;
259 	size_t n;
260 	FILE *from_fp, *to_fp;
261 	struct nscookie nsc;
262 
263 	nsinfo__mountns_enter(nsi, &nsc);
264 	from_fp = fopen(from, "r");
265 	nsinfo__mountns_exit(&nsc);
266 	if (from_fp == NULL)
267 		goto out;
268 
269 	to_fp = fopen(to, "w");
270 	if (to_fp == NULL)
271 		goto out_fclose_from;
272 
273 	while (getline(&line, &n, from_fp) > 0)
274 		if (fputs(line, to_fp) == EOF)
275 			goto out_fclose_to;
276 	err = 0;
277 out_fclose_to:
278 	fclose(to_fp);
279 	free(line);
280 out_fclose_from:
281 	fclose(from_fp);
282 out:
283 	return err;
284 }
285 
286 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
287 {
288 	void *ptr;
289 	loff_t pgoff;
290 
291 	pgoff = off_in & ~(page_size - 1);
292 	off_in -= pgoff;
293 
294 	ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
295 	if (ptr == MAP_FAILED)
296 		return -1;
297 
298 	while (size) {
299 		ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
300 		if (ret < 0 && errno == EINTR)
301 			continue;
302 		if (ret <= 0)
303 			break;
304 
305 		size -= ret;
306 		off_in += ret;
307 		off_out += ret;
308 	}
309 	munmap(ptr, off_in + size);
310 
311 	return size ? -1 : 0;
312 }
313 
314 static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
315 			    struct nsinfo *nsi)
316 {
317 	int fromfd, tofd;
318 	struct stat st;
319 	int err;
320 	char *tmp = NULL, *ptr = NULL;
321 	struct nscookie nsc;
322 
323 	nsinfo__mountns_enter(nsi, &nsc);
324 	err = stat(from, &st);
325 	nsinfo__mountns_exit(&nsc);
326 	if (err)
327 		goto out;
328 	err = -1;
329 
330 	/* extra 'x' at the end is to reserve space for '.' */
331 	if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
332 		tmp = NULL;
333 		goto out;
334 	}
335 	ptr = strrchr(tmp, '/');
336 	if (!ptr)
337 		goto out;
338 	ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
339 	*ptr = '.';
340 
341 	tofd = mkstemp(tmp);
342 	if (tofd < 0)
343 		goto out;
344 
345 	if (fchmod(tofd, mode))
346 		goto out_close_to;
347 
348 	if (st.st_size == 0) { /* /proc? do it slowly... */
349 		err = slow_copyfile(from, tmp, nsi);
350 		goto out_close_to;
351 	}
352 
353 	nsinfo__mountns_enter(nsi, &nsc);
354 	fromfd = open(from, O_RDONLY);
355 	nsinfo__mountns_exit(&nsc);
356 	if (fromfd < 0)
357 		goto out_close_to;
358 
359 	err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
360 
361 	close(fromfd);
362 out_close_to:
363 	close(tofd);
364 	if (!err)
365 		err = link(tmp, to);
366 	unlink(tmp);
367 out:
368 	free(tmp);
369 	return err;
370 }
371 
372 int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
373 {
374 	return copyfile_mode_ns(from, to, 0755, nsi);
375 }
376 
377 int copyfile_mode(const char *from, const char *to, mode_t mode)
378 {
379 	return copyfile_mode_ns(from, to, mode, NULL);
380 }
381 
382 int copyfile(const char *from, const char *to)
383 {
384 	return copyfile_mode(from, to, 0755);
385 }
386 
387 size_t hex_width(u64 v)
388 {
389 	size_t n = 1;
390 
391 	while ((v >>= 4))
392 		++n;
393 
394 	return n;
395 }
396 
397 int perf_event_paranoid(void)
398 {
399 	int value;
400 
401 	if (sysctl__read_int("kernel/perf_event_paranoid", &value))
402 		return INT_MAX;
403 
404 	return value;
405 }
406 static int
407 fetch_ubuntu_kernel_version(unsigned int *puint)
408 {
409 	ssize_t len;
410 	size_t line_len = 0;
411 	char *ptr, *line = NULL;
412 	int version, patchlevel, sublevel, err;
413 	FILE *vsig;
414 
415 	if (!puint)
416 		return 0;
417 
418 	vsig = fopen("/proc/version_signature", "r");
419 	if (!vsig) {
420 		pr_debug("Open /proc/version_signature failed: %s\n",
421 			 strerror(errno));
422 		return -1;
423 	}
424 
425 	len = getline(&line, &line_len, vsig);
426 	fclose(vsig);
427 	err = -1;
428 	if (len <= 0) {
429 		pr_debug("Reading from /proc/version_signature failed: %s\n",
430 			 strerror(errno));
431 		goto errout;
432 	}
433 
434 	ptr = strrchr(line, ' ');
435 	if (!ptr) {
436 		pr_debug("Parsing /proc/version_signature failed: %s\n", line);
437 		goto errout;
438 	}
439 
440 	err = sscanf(ptr + 1, "%d.%d.%d",
441 		     &version, &patchlevel, &sublevel);
442 	if (err != 3) {
443 		pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
444 			 line);
445 		goto errout;
446 	}
447 
448 	*puint = (version << 16) + (patchlevel << 8) + sublevel;
449 	err = 0;
450 errout:
451 	free(line);
452 	return err;
453 }
454 
455 int
456 fetch_kernel_version(unsigned int *puint, char *str,
457 		     size_t str_size)
458 {
459 	struct utsname utsname;
460 	int version, patchlevel, sublevel, err;
461 	bool int_ver_ready = false;
462 
463 	if (access("/proc/version_signature", R_OK) == 0)
464 		if (!fetch_ubuntu_kernel_version(puint))
465 			int_ver_ready = true;
466 
467 	if (uname(&utsname))
468 		return -1;
469 
470 	if (str && str_size) {
471 		strncpy(str, utsname.release, str_size);
472 		str[str_size - 1] = '\0';
473 	}
474 
475 	if (!puint || int_ver_ready)
476 		return 0;
477 
478 	err = sscanf(utsname.release, "%d.%d.%d",
479 		     &version, &patchlevel, &sublevel);
480 
481 	if (err != 3) {
482 		pr_debug("Unable to get kernel version from uname '%s'\n",
483 			 utsname.release);
484 		return -1;
485 	}
486 
487 	*puint = (version << 16) + (patchlevel << 8) + sublevel;
488 	return 0;
489 }
490 
491 const char *perf_tip(const char *dirpath)
492 {
493 	struct strlist *tips;
494 	struct str_node *node;
495 	char *tip = NULL;
496 	struct strlist_config conf = {
497 		.dirname = dirpath,
498 		.file_only = true,
499 	};
500 
501 	tips = strlist__new("tips.txt", &conf);
502 	if (tips == NULL)
503 		return errno == ENOENT ? NULL :
504 			"Tip: check path of tips.txt or get more memory! ;-p";
505 
506 	if (strlist__nr_entries(tips) == 0)
507 		goto out;
508 
509 	node = strlist__entry(tips, random() % strlist__nr_entries(tips));
510 	if (asprintf(&tip, "Tip: %s", node->s) < 0)
511 		tip = (char *)"Tip: get more memory! ;-)";
512 
513 out:
514 	strlist__delete(tips);
515 
516 	return tip;
517 }
518 
519 char *perf_exe(char *buf, int len)
520 {
521 	int n = readlink("/proc/self/exe", buf, len);
522 	if (n > 0) {
523 		buf[n] = 0;
524 		return buf;
525 	}
526 	return strcpy(buf, "perf");
527 }
528