8 #include <sys/utsname.h> 18 #include <linux/kernel.h> 19 #include <linux/log2.h> 20 #include <linux/time64.h> 42 #ifdef _SC_LEVEL1_DCACHE_LINESIZE 43 #define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE) 47 if (sysfs__read_int(
"devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
48 pr_debug(
"cannot determine cache line size");
69 if (sysctl__read_int(
"kernel/perf_event_max_stack", &value) == 0)
72 if (sysctl__read_int(
"kernel/perf_event_max_contexts_per_stack", &value) == 0)
86 attr->exclude_host = 1;
88 attr->exclude_guest = 1;
90 attr->size =
sizeof(*attr);
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;
130 while ((d = readdir(dir)) != NULL && !ret) {
133 if (!strcmp(d->d_name,
".") || !strcmp(d->d_name,
".."))
136 scnprintf(namebuf,
sizeof(namebuf),
"%s/%s",
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);
162 return d->d_name[0] !=
'.';
167 bool (*
filter)(
const char *,
struct dirent *))
183 while ((d = readdir(dir)) != NULL) {
198 FILE *from_fp, *to_fp;
202 from_fp = fopen(from,
"r");
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)
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;
258 char *tmp = NULL, *ptr = NULL;
262 err = stat(from, &st);
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) {
292 fromfd = open(from, O_RDONLY);
325 static ssize_t
ion(
bool is_read,
int fd,
void *buf,
size_t n)
327 void *buf_start = buf;
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);
351 ssize_t
readn(
int fd,
void *buf,
size_t n)
353 return ion(
true, fd, buf, n);
359 ssize_t
writen(
int fd,
const void *buf,
size_t n)
362 return ion(
false, fd, (
void *)buf, n);
383 *long_val = strtoull(ptr, &p, 16);
392 if (sysctl__read_int(
"kernel/perf_event_paranoid", &value))
402 char *ptr, *line = NULL;
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;
450 struct utsname utsname;
452 bool int_ver_ready =
false;
454 if (access(
"/proc/version_signature", R_OK) == 0)
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;
494 return errno == ENOENT ? NULL :
495 "Tip: check path of tips.txt or get more memory! ;-p";
501 if (asprintf(&tip,
"Tip: %s", node->
s) < 0)
502 tip = (
char *)
"Tip: get more memory! ;-)";
int fetch_kernel_version(unsigned int *puint, char *str, size_t str_size)
static int copyfile_mode_ns(const char *from, const char *to, mode_t mode, struct nsinfo *nsi)
void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc)
static void cache_line_size(int *cacheline_sizep)
const char * perf_tip(const char *dirpath)
int hex2u64(const char *ptr, u64 *long_val)
int perf_event_paranoid(void)
static struct version version
ssize_t readn(int fd, void *buf, size_t n)
void perf_set_singlethreaded(void)
int strlist__add(struct strlist *slist, const char *new_entry)
static int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
int copyfile_mode(const char *from, const char *to, mode_t mode)
static int fetch_ubuntu_kernel_version(unsigned int *puint)
static unsigned int strlist__nr_entries(const struct strlist *slist)
void event_attr_init(struct perf_event_attr *attr)
int rm_rf(const char *path)
int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
void nsinfo__mountns_exit(struct nscookie *nc)
static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
#define pr_debug(fmt,...)
void perf_set_multithreaded(void)
struct str_node * strlist__entry(const struct strlist *slist, unsigned int idx)
static int str(yyscan_t scanner, int token)
int sysctl_perf_event_max_stack
int mkdir_p(char *path, mode_t mode)
struct strfilter * filter
int copyfile(const char *from, const char *to)
void strlist__delete(struct strlist *slist)
bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
ssize_t writen(int fd, const void *buf, size_t n)
int sysctl_perf_event_max_contexts_per_stack
struct strlist * lsdir(const char *name, bool(*filter)(const char *, struct dirent *))
struct strlist * strlist__new(const char *list, const struct strlist_config *config)
int sysctl__max_stack(void)