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