1 // SPDX-License-Identifier: GPL-2.0
8 #include <sys/utsname.h>
18 #include <linux/kernel.h>
19 #include <linux/log2.h>
20 #include <linux/time64.h>
25 * XXX We need to find a better place for these things...
28 bool perf_singlethreaded
= true;
30 void perf_set_singlethreaded(void)
32 perf_singlethreaded
= true;
35 void perf_set_multithreaded(void)
37 perf_singlethreaded
= false;
40 unsigned int page_size
;
42 #ifdef _SC_LEVEL1_DCACHE_LINESIZE
43 #define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
45 static void cache_line_size(int *cacheline_sizep
)
47 if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep
))
48 pr_debug("cannot determine cache line size");
52 int cacheline_size(void)
57 cache_line_size(&size
);
62 int sysctl_perf_event_max_stack
= PERF_MAX_STACK_DEPTH
;
63 int sysctl_perf_event_max_contexts_per_stack
= PERF_MAX_CONTEXTS_PER_STACK
;
65 int sysctl__max_stack(void)
69 if (sysctl__read_int("kernel/perf_event_max_stack", &value
) == 0)
70 sysctl_perf_event_max_stack
= value
;
72 if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value
) == 0)
73 sysctl_perf_event_max_contexts_per_stack
= value
;
75 return sysctl_perf_event_max_stack
;
78 bool test_attr__enabled
;
80 bool perf_host
= true;
81 bool perf_guest
= false;
83 void event_attr_init(struct perf_event_attr
*attr
)
86 attr
->exclude_host
= 1;
88 attr
->exclude_guest
= 1;
89 /* to capture ABI version */
90 attr
->size
= sizeof(*attr
);
93 int mkdir_p(char *path
, mode_t mode
)
102 if (stat(path
, &st
) == 0)
107 while ((d
= strchr(d
, '/'))) {
109 err
= stat(path
, &st
) && mkdir(path
, mode
);
116 return (stat(path
, &st
) && mkdir(path
, mode
)) ? -1 : 0;
119 int rm_rf(const char *path
)
124 char namebuf
[PATH_MAX
];
130 while ((d
= readdir(dir
)) != NULL
&& !ret
) {
133 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
136 scnprintf(namebuf
, sizeof(namebuf
), "%s/%s",
139 /* We have to check symbolic link itself */
140 ret
= lstat(namebuf
, &statbuf
);
142 pr_debug("stat failed: %s\n", namebuf
);
146 if (S_ISDIR(statbuf
.st_mode
))
147 ret
= rm_rf(namebuf
);
149 ret
= unlink(namebuf
);
159 /* A filter which removes dot files */
160 bool lsdir_no_dot_filter(const char *name __maybe_unused
, struct dirent
*d
)
162 return d
->d_name
[0] != '.';
165 /* lsdir reads a directory and store it in strlist */
166 struct strlist
*lsdir(const char *name
,
167 bool (*filter
)(const char *, struct dirent
*))
169 struct strlist
*list
= NULL
;
177 list
= strlist__new(NULL
, NULL
);
183 while ((d
= readdir(dir
)) != NULL
) {
184 if (!filter
|| filter(name
, d
))
185 strlist__add(list
, d
->d_name
);
193 static int slow_copyfile(const char *from
, const char *to
, struct nsinfo
*nsi
)
198 FILE *from_fp
, *to_fp
;
201 nsinfo__mountns_enter(nsi
, &nsc
);
202 from_fp
= fopen(from
, "r");
203 nsinfo__mountns_exit(&nsc
);
207 to_fp
= fopen(to
, "w");
209 goto out_fclose_from
;
211 while (getline(&line
, &n
, from_fp
) > 0)
212 if (fputs(line
, to_fp
) == EOF
)
224 static int copyfile_offset(int ifd
, loff_t off_in
, int ofd
, loff_t off_out
, u64 size
)
229 pgoff
= off_in
& ~(page_size
- 1);
232 ptr
= mmap(NULL
, off_in
+ size
, PROT_READ
, MAP_PRIVATE
, ifd
, pgoff
);
233 if (ptr
== MAP_FAILED
)
237 ssize_t ret
= pwrite(ofd
, ptr
+ off_in
, size
, off_out
);
238 if (ret
< 0 && errno
== EINTR
)
247 munmap(ptr
, off_in
+ size
);
249 return size
? -1 : 0;
252 static int copyfile_mode_ns(const char *from
, const char *to
, mode_t mode
,
258 char *tmp
= NULL
, *ptr
= NULL
;
261 nsinfo__mountns_enter(nsi
, &nsc
);
262 err
= stat(from
, &st
);
263 nsinfo__mountns_exit(&nsc
);
268 /* extra 'x' at the end is to reserve space for '.' */
269 if (asprintf(&tmp
, "%s.XXXXXXx", to
) < 0) {
273 ptr
= strrchr(tmp
, '/');
276 ptr
= memmove(ptr
+ 1, ptr
, strlen(ptr
) - 1);
283 if (fchmod(tofd
, mode
))
286 if (st
.st_size
== 0) { /* /proc? do it slowly... */
287 err
= slow_copyfile(from
, tmp
, nsi
);
291 nsinfo__mountns_enter(nsi
, &nsc
);
292 fromfd
= open(from
, O_RDONLY
);
293 nsinfo__mountns_exit(&nsc
);
297 err
= copyfile_offset(fromfd
, 0, tofd
, 0, st
.st_size
);
310 int copyfile_ns(const char *from
, const char *to
, struct nsinfo
*nsi
)
312 return copyfile_mode_ns(from
, to
, 0755, nsi
);
315 int copyfile_mode(const char *from
, const char *to
, mode_t mode
)
317 return copyfile_mode_ns(from
, to
, mode
, NULL
);
320 int copyfile(const char *from
, const char *to
)
322 return copyfile_mode(from
, to
, 0755);
325 static ssize_t
ion(bool is_read
, int fd
, void *buf
, size_t n
)
327 void *buf_start
= buf
;
331 /* buf must be treated as const if !is_read. */
332 ssize_t ret
= is_read
? read(fd
, buf
, left
) :
333 write(fd
, buf
, left
);
335 if (ret
< 0 && errno
== EINTR
)
344 BUG_ON((size_t)(buf
- buf_start
) != n
);
349 * Read exactly 'n' bytes or return an error.
351 ssize_t
readn(int fd
, void *buf
, size_t n
)
353 return ion(true, fd
, buf
, n
);
357 * Write exactly 'n' bytes or return an error.
359 ssize_t
writen(int fd
, const void *buf
, size_t n
)
361 /* ion does not modify buf. */
362 return ion(false, fd
, (void *)buf
, n
);
365 size_t hex_width(u64 v
)
376 * While we find nice hex chars, build a long_val.
377 * Return number of chars processed.
379 int hex2u64(const char *ptr
, u64
*long_val
)
383 *long_val
= strtoull(ptr
, &p
, 16);
388 int perf_event_paranoid(void)
392 if (sysctl__read_int("kernel/perf_event_paranoid", &value
))
398 fetch_ubuntu_kernel_version(unsigned int *puint
)
402 char *ptr
, *line
= NULL
;
403 int version
, patchlevel
, sublevel
, err
;
409 vsig
= fopen("/proc/version_signature", "r");
411 pr_debug("Open /proc/version_signature failed: %s\n",
416 len
= getline(&line
, &line_len
, vsig
);
420 pr_debug("Reading from /proc/version_signature failed: %s\n",
425 ptr
= strrchr(line
, ' ');
427 pr_debug("Parsing /proc/version_signature failed: %s\n", line
);
431 err
= sscanf(ptr
+ 1, "%d.%d.%d",
432 &version
, &patchlevel
, &sublevel
);
434 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
439 *puint
= (version
<< 16) + (patchlevel
<< 8) + sublevel
;
447 fetch_kernel_version(unsigned int *puint
, char *str
,
450 struct utsname utsname
;
451 int version
, patchlevel
, sublevel
, err
;
452 bool int_ver_ready
= false;
454 if (access("/proc/version_signature", R_OK
) == 0)
455 if (!fetch_ubuntu_kernel_version(puint
))
456 int_ver_ready
= true;
461 if (str
&& str_size
) {
462 strncpy(str
, utsname
.release
, str_size
);
463 str
[str_size
- 1] = '\0';
466 if (!puint
|| int_ver_ready
)
469 err
= sscanf(utsname
.release
, "%d.%d.%d",
470 &version
, &patchlevel
, &sublevel
);
473 pr_debug("Unable to get kernel version from uname '%s'\n",
478 *puint
= (version
<< 16) + (patchlevel
<< 8) + sublevel
;
482 const char *perf_tip(const char *dirpath
)
484 struct strlist
*tips
;
485 struct str_node
*node
;
487 struct strlist_config conf
= {
492 tips
= strlist__new("tips.txt", &conf
);
494 return errno
== ENOENT
? NULL
:
495 "Tip: check path of tips.txt or get more memory! ;-p";
497 if (strlist__nr_entries(tips
) == 0)
500 node
= strlist__entry(tips
, random() % strlist__nr_entries(tips
));
501 if (asprintf(&tip
, "Tip: %s", node
->s
) < 0)
502 tip
= (char *)"Tip: get more memory! ;-)";
505 strlist__delete(tips
);