Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 1 | #include "../perf.h" |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 2 | #include "util.h" |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 3 | #include "debug.h" |
Borislav Petkov | cd0cfad | 2013-12-09 17:14:24 +0100 | [diff] [blame] | 4 | #include <api/fs/fs.h> |
Frederic Weisbecker | 69e3f52 | 2010-01-16 14:21:15 +0100 | [diff] [blame] | 5 | #include <sys/mman.h> |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 6 | #include <sys/utsname.h> |
Arnaldo Carvalho de Melo | 76b31a2 | 2017-04-18 12:26:44 -0300 | [diff] [blame] | 7 | #include <dirent.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 8 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 9607ad3 | 2017-04-19 15:49:18 -0300 | [diff] [blame] | 9 | #include <signal.h> |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
Jiri Olsa | cef82c9 | 2013-12-03 14:09:22 +0100 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <errno.h> |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 14 | #include <limits.h> |
Adrian Hunter | 71db07b | 2013-12-11 14:36:32 +0200 | [diff] [blame] | 15 | #include <byteswap.h> |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 17 | #include <linux/log2.h> |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 18 | #include <linux/time64.h> |
Jiri Olsa | 9398c48 | 2014-08-11 10:50:02 +0200 | [diff] [blame] | 19 | #include <unistd.h> |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 20 | #include "strlist.h" |
Jiri Olsa | 23aadb1 | 2014-10-01 18:00:26 +0200 | [diff] [blame] | 21 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 22 | /* |
| 23 | * XXX We need to find a better place for these things... |
| 24 | */ |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 25 | unsigned int page_size; |
Don Zickus | 2b1b710 | 2014-05-30 16:10:05 -0400 | [diff] [blame] | 26 | int cacheline_size; |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 27 | |
Arnaldo Carvalho de Melo | a29d5c9 | 2016-05-16 21:16:54 -0300 | [diff] [blame] | 28 | int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH; |
| 29 | int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK; |
Arnaldo Carvalho de Melo | 4cb9344 | 2016-04-27 10:16:24 -0300 | [diff] [blame] | 30 | |
Arnaldo Carvalho de Melo | 0c6332e | 2012-12-13 16:43:04 -0300 | [diff] [blame] | 31 | bool test_attr__enabled; |
| 32 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 33 | bool perf_host = true; |
Joerg Roedel | c4a7dca | 2012-02-10 18:05:05 +0100 | [diff] [blame] | 34 | bool perf_guest = false; |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 35 | |
| 36 | void event_attr_init(struct perf_event_attr *attr) |
| 37 | { |
| 38 | if (!perf_host) |
| 39 | attr->exclude_host = 1; |
| 40 | if (!perf_guest) |
| 41 | attr->exclude_guest = 1; |
Stephane Eranian | 7e1ccd3 | 2012-02-09 16:12:38 +0100 | [diff] [blame] | 42 | /* to capture ABI version */ |
| 43 | attr->size = sizeof(*attr); |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 46 | int mkdir_p(char *path, mode_t mode) |
| 47 | { |
| 48 | struct stat st; |
| 49 | int err; |
| 50 | char *d = path; |
| 51 | |
| 52 | if (*d != '/') |
| 53 | return -1; |
| 54 | |
| 55 | if (stat(path, &st) == 0) |
| 56 | return 0; |
| 57 | |
| 58 | while (*++d == '/'); |
| 59 | |
| 60 | while ((d = strchr(d, '/'))) { |
| 61 | *d = '\0'; |
| 62 | err = stat(path, &st) && mkdir(path, mode); |
| 63 | *d++ = '/'; |
| 64 | if (err) |
| 65 | return -1; |
| 66 | while (*d == '/') |
| 67 | ++d; |
| 68 | } |
| 69 | return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; |
| 70 | } |
| 71 | |
Joe Stringer | 9a9c733 | 2017-01-26 13:19:59 -0800 | [diff] [blame] | 72 | int rm_rf(const char *path) |
Namhyung Kim | 0b1de0be | 2015-05-18 09:30:17 +0900 | [diff] [blame] | 73 | { |
| 74 | DIR *dir; |
| 75 | int ret = 0; |
| 76 | struct dirent *d; |
| 77 | char namebuf[PATH_MAX]; |
| 78 | |
| 79 | dir = opendir(path); |
| 80 | if (dir == NULL) |
| 81 | return 0; |
| 82 | |
| 83 | while ((d = readdir(dir)) != NULL && !ret) { |
| 84 | struct stat statbuf; |
| 85 | |
| 86 | if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) |
| 87 | continue; |
| 88 | |
| 89 | scnprintf(namebuf, sizeof(namebuf), "%s/%s", |
| 90 | path, d->d_name); |
| 91 | |
Masami Hiramatsu | 2a1ef03 | 2016-06-08 18:29:11 +0900 | [diff] [blame] | 92 | /* We have to check symbolic link itself */ |
| 93 | ret = lstat(namebuf, &statbuf); |
Namhyung Kim | 0b1de0be | 2015-05-18 09:30:17 +0900 | [diff] [blame] | 94 | if (ret < 0) { |
| 95 | pr_debug("stat failed: %s\n", namebuf); |
| 96 | break; |
| 97 | } |
| 98 | |
Masami Hiramatsu | 2a1ef03 | 2016-06-08 18:29:11 +0900 | [diff] [blame] | 99 | if (S_ISDIR(statbuf.st_mode)) |
Namhyung Kim | 0b1de0be | 2015-05-18 09:30:17 +0900 | [diff] [blame] | 100 | ret = rm_rf(namebuf); |
Masami Hiramatsu | 2a1ef03 | 2016-06-08 18:29:11 +0900 | [diff] [blame] | 101 | else |
| 102 | ret = unlink(namebuf); |
Namhyung Kim | 0b1de0be | 2015-05-18 09:30:17 +0900 | [diff] [blame] | 103 | } |
| 104 | closedir(dir); |
| 105 | |
| 106 | if (ret < 0) |
| 107 | return ret; |
| 108 | |
| 109 | return rmdir(path); |
| 110 | } |
| 111 | |
Masami Hiramatsu | e1ce726 | 2016-04-26 18:02:42 +0900 | [diff] [blame] | 112 | /* A filter which removes dot files */ |
| 113 | bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d) |
| 114 | { |
| 115 | return d->d_name[0] != '.'; |
| 116 | } |
| 117 | |
| 118 | /* lsdir reads a directory and store it in strlist */ |
| 119 | struct strlist *lsdir(const char *name, |
| 120 | bool (*filter)(const char *, struct dirent *)) |
| 121 | { |
| 122 | struct strlist *list = NULL; |
| 123 | DIR *dir; |
| 124 | struct dirent *d; |
| 125 | |
| 126 | dir = opendir(name); |
| 127 | if (!dir) |
| 128 | return NULL; |
| 129 | |
| 130 | list = strlist__new(NULL, NULL); |
| 131 | if (!list) { |
Masami Hiramatsu | 357a54f | 2016-05-11 22:51:27 +0900 | [diff] [blame] | 132 | errno = ENOMEM; |
Masami Hiramatsu | e1ce726 | 2016-04-26 18:02:42 +0900 | [diff] [blame] | 133 | goto out; |
| 134 | } |
| 135 | |
| 136 | while ((d = readdir(dir)) != NULL) { |
| 137 | if (!filter || filter(name, d)) |
| 138 | strlist__add(list, d->d_name); |
| 139 | } |
| 140 | |
| 141 | out: |
| 142 | closedir(dir); |
| 143 | return list; |
| 144 | } |
| 145 | |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 146 | static int slow_copyfile(const char *from, const char *to) |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 147 | { |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 148 | int err = -1; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 149 | char *line = NULL; |
| 150 | size_t n; |
| 151 | FILE *from_fp = fopen(from, "r"), *to_fp; |
| 152 | |
| 153 | if (from_fp == NULL) |
| 154 | goto out; |
| 155 | |
| 156 | to_fp = fopen(to, "w"); |
| 157 | if (to_fp == NULL) |
| 158 | goto out_fclose_from; |
| 159 | |
| 160 | while (getline(&line, &n, from_fp) > 0) |
| 161 | if (fputs(line, to_fp) == EOF) |
| 162 | goto out_fclose_to; |
| 163 | err = 0; |
| 164 | out_fclose_to: |
| 165 | fclose(to_fp); |
| 166 | free(line); |
| 167 | out_fclose_from: |
| 168 | fclose(from_fp); |
| 169 | out: |
| 170 | return err; |
| 171 | } |
| 172 | |
Namhyung Kim | 9c9f5a2 | 2015-05-18 09:30:18 +0900 | [diff] [blame] | 173 | int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size) |
| 174 | { |
| 175 | void *ptr; |
| 176 | loff_t pgoff; |
| 177 | |
| 178 | pgoff = off_in & ~(page_size - 1); |
| 179 | off_in -= pgoff; |
| 180 | |
| 181 | ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff); |
| 182 | if (ptr == MAP_FAILED) |
| 183 | return -1; |
| 184 | |
| 185 | while (size) { |
| 186 | ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out); |
| 187 | if (ret < 0 && errno == EINTR) |
| 188 | continue; |
| 189 | if (ret <= 0) |
| 190 | break; |
| 191 | |
| 192 | size -= ret; |
| 193 | off_in += ret; |
| 194 | off_out -= ret; |
| 195 | } |
| 196 | munmap(ptr, off_in + size); |
| 197 | |
| 198 | return size ? -1 : 0; |
| 199 | } |
| 200 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 201 | int copyfile_mode(const char *from, const char *to, mode_t mode) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 202 | { |
| 203 | int fromfd, tofd; |
| 204 | struct stat st; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 205 | int err = -1; |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 206 | char *tmp = NULL, *ptr = NULL; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 207 | |
| 208 | if (stat(from, &st)) |
| 209 | goto out; |
| 210 | |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 211 | /* extra 'x' at the end is to reserve space for '.' */ |
| 212 | if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) { |
| 213 | tmp = NULL; |
| 214 | goto out; |
| 215 | } |
| 216 | ptr = strrchr(tmp, '/'); |
| 217 | if (!ptr) |
| 218 | goto out; |
| 219 | ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1); |
| 220 | *ptr = '.'; |
| 221 | |
| 222 | tofd = mkstemp(tmp); |
| 223 | if (tofd < 0) |
| 224 | goto out; |
| 225 | |
| 226 | if (fchmod(tofd, mode)) |
| 227 | goto out_close_to; |
| 228 | |
| 229 | if (st.st_size == 0) { /* /proc? do it slowly... */ |
| 230 | err = slow_copyfile(from, tmp); |
| 231 | goto out_close_to; |
| 232 | } |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 233 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 234 | fromfd = open(from, O_RDONLY); |
| 235 | if (fromfd < 0) |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 236 | goto out_close_to; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 237 | |
Namhyung Kim | 9c9f5a2 | 2015-05-18 09:30:18 +0900 | [diff] [blame] | 238 | err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 239 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 240 | close(fromfd); |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 241 | out_close_to: |
| 242 | close(tofd); |
| 243 | if (!err) |
| 244 | err = link(tmp, to); |
| 245 | unlink(tmp); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 246 | out: |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 247 | free(tmp); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 248 | return err; |
| 249 | } |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 250 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 251 | int copyfile(const char *from, const char *to) |
| 252 | { |
| 253 | return copyfile_mode(from, to, 0755); |
| 254 | } |
| 255 | |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 256 | static ssize_t ion(bool is_read, int fd, void *buf, size_t n) |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 257 | { |
| 258 | void *buf_start = buf; |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 259 | size_t left = n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 260 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 261 | while (left) { |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 262 | ssize_t ret = is_read ? read(fd, buf, left) : |
| 263 | write(fd, buf, left); |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 264 | |
Namhyung Kim | e148c76 | 2014-04-24 22:27:32 +0900 | [diff] [blame] | 265 | if (ret < 0 && errno == EINTR) |
| 266 | continue; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 267 | if (ret <= 0) |
| 268 | return ret; |
| 269 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 270 | left -= ret; |
| 271 | buf += ret; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 272 | } |
| 273 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 274 | BUG_ON((size_t)(buf - buf_start) != n); |
| 275 | return n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 276 | } |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 277 | |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 278 | /* |
| 279 | * Read exactly 'n' bytes or return an error. |
| 280 | */ |
| 281 | ssize_t readn(int fd, void *buf, size_t n) |
| 282 | { |
| 283 | return ion(true, fd, buf, n); |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Write exactly 'n' bytes or return an error. |
| 288 | */ |
| 289 | ssize_t writen(int fd, void *buf, size_t n) |
| 290 | { |
| 291 | return ion(false, fd, buf, n); |
| 292 | } |
| 293 | |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 294 | size_t hex_width(u64 v) |
| 295 | { |
| 296 | size_t n = 1; |
| 297 | |
| 298 | while ((v >>= 4)) |
| 299 | ++n; |
| 300 | |
| 301 | return n; |
| 302 | } |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 303 | |
Jiri Olsa | b2aff5f | 2012-10-27 23:18:30 +0200 | [diff] [blame] | 304 | static int hex(char ch) |
| 305 | { |
| 306 | if ((ch >= '0') && (ch <= '9')) |
| 307 | return ch - '0'; |
| 308 | if ((ch >= 'a') && (ch <= 'f')) |
| 309 | return ch - 'a' + 10; |
| 310 | if ((ch >= 'A') && (ch <= 'F')) |
| 311 | return ch - 'A' + 10; |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * While we find nice hex chars, build a long_val. |
| 317 | * Return number of chars processed. |
| 318 | */ |
| 319 | int hex2u64(const char *ptr, u64 *long_val) |
| 320 | { |
| 321 | const char *p = ptr; |
| 322 | *long_val = 0; |
| 323 | |
| 324 | while (*p) { |
| 325 | const int hex_val = hex(*p); |
| 326 | |
| 327 | if (hex_val < 0) |
| 328 | break; |
| 329 | |
| 330 | *long_val = (*long_val << 4) | hex_val; |
| 331 | p++; |
| 332 | } |
| 333 | |
| 334 | return p - ptr; |
| 335 | } |
| 336 | |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 337 | unsigned long parse_tag_value(const char *str, struct parse_tag *tags) |
| 338 | { |
| 339 | struct parse_tag *i = tags; |
| 340 | |
| 341 | while (i->tag) { |
| 342 | char *s; |
| 343 | |
| 344 | s = strchr(str, i->tag); |
| 345 | if (s) { |
| 346 | unsigned long int value; |
| 347 | char *endptr; |
| 348 | |
| 349 | value = strtoul(str, &endptr, 10); |
| 350 | if (s != endptr) |
| 351 | break; |
| 352 | |
Adrian Hunter | 56921be | 2013-10-22 10:34:17 +0300 | [diff] [blame] | 353 | if (value > ULONG_MAX / i->mult) |
| 354 | break; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 355 | value *= i->mult; |
| 356 | return value; |
| 357 | } |
| 358 | i++; |
| 359 | } |
| 360 | |
| 361 | return (unsigned long) -1; |
| 362 | } |
Arnaldo Carvalho de Melo | 97a07f1 | 2013-10-17 16:33:43 -0300 | [diff] [blame] | 363 | |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 364 | int perf_event_paranoid(void) |
| 365 | { |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 366 | int value; |
| 367 | |
Arnaldo Carvalho de Melo | ce27309 | 2014-12-11 13:37:59 -0300 | [diff] [blame] | 368 | if (sysctl__read_int("kernel/perf_event_paranoid", &value)) |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 369 | return INT_MAX; |
| 370 | |
| 371 | return value; |
| 372 | } |
Adrian Hunter | 71db07b | 2013-12-11 14:36:32 +0200 | [diff] [blame] | 373 | |
| 374 | void mem_bswap_32(void *src, int byte_size) |
| 375 | { |
| 376 | u32 *m = src; |
| 377 | while (byte_size > 0) { |
| 378 | *m = bswap_32(*m); |
| 379 | byte_size -= sizeof(u32); |
| 380 | ++m; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void mem_bswap_64(void *src, int byte_size) |
| 385 | { |
| 386 | u64 *m = src; |
| 387 | |
| 388 | while (byte_size > 0) { |
| 389 | *m = bswap_64(*m); |
| 390 | byte_size -= sizeof(u64); |
| 391 | ++m; |
| 392 | } |
| 393 | } |
Jiri Olsa | 63914ac | 2014-08-01 17:46:54 +0200 | [diff] [blame] | 394 | |
| 395 | bool find_process(const char *name) |
| 396 | { |
| 397 | size_t len = strlen(name); |
| 398 | DIR *dir; |
| 399 | struct dirent *d; |
| 400 | int ret = -1; |
| 401 | |
| 402 | dir = opendir(procfs__mountpoint()); |
| 403 | if (!dir) |
Peter Senna Tschudin | bf64456 | 2015-09-17 12:08:53 +0200 | [diff] [blame] | 404 | return false; |
Jiri Olsa | 63914ac | 2014-08-01 17:46:54 +0200 | [diff] [blame] | 405 | |
| 406 | /* Walk through the directory. */ |
| 407 | while (ret && (d = readdir(dir)) != NULL) { |
| 408 | char path[PATH_MAX]; |
| 409 | char *data; |
| 410 | size_t size; |
| 411 | |
| 412 | if ((d->d_type != DT_DIR) || |
| 413 | !strcmp(".", d->d_name) || |
| 414 | !strcmp("..", d->d_name)) |
| 415 | continue; |
| 416 | |
| 417 | scnprintf(path, sizeof(path), "%s/%s/comm", |
| 418 | procfs__mountpoint(), d->d_name); |
| 419 | |
| 420 | if (filename__read_str(path, &data, &size)) |
| 421 | continue; |
| 422 | |
| 423 | ret = strncmp(name, data, len); |
| 424 | free(data); |
| 425 | } |
| 426 | |
| 427 | closedir(dir); |
| 428 | return ret ? false : true; |
| 429 | } |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 430 | |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 431 | static int |
| 432 | fetch_ubuntu_kernel_version(unsigned int *puint) |
| 433 | { |
| 434 | ssize_t len; |
| 435 | size_t line_len = 0; |
| 436 | char *ptr, *line = NULL; |
| 437 | int version, patchlevel, sublevel, err; |
| 438 | FILE *vsig = fopen("/proc/version_signature", "r"); |
| 439 | |
| 440 | if (!vsig) { |
| 441 | pr_debug("Open /proc/version_signature failed: %s\n", |
| 442 | strerror(errno)); |
| 443 | return -1; |
| 444 | } |
| 445 | |
| 446 | len = getline(&line, &line_len, vsig); |
| 447 | fclose(vsig); |
| 448 | err = -1; |
| 449 | if (len <= 0) { |
| 450 | pr_debug("Reading from /proc/version_signature failed: %s\n", |
| 451 | strerror(errno)); |
| 452 | goto errout; |
| 453 | } |
| 454 | |
| 455 | ptr = strrchr(line, ' '); |
| 456 | if (!ptr) { |
| 457 | pr_debug("Parsing /proc/version_signature failed: %s\n", line); |
| 458 | goto errout; |
| 459 | } |
| 460 | |
| 461 | err = sscanf(ptr + 1, "%d.%d.%d", |
| 462 | &version, &patchlevel, &sublevel); |
| 463 | if (err != 3) { |
| 464 | pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n", |
| 465 | line); |
| 466 | goto errout; |
| 467 | } |
| 468 | |
| 469 | if (puint) |
| 470 | *puint = (version << 16) + (patchlevel << 8) + sublevel; |
| 471 | err = 0; |
| 472 | errout: |
| 473 | free(line); |
| 474 | return err; |
| 475 | } |
| 476 | |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 477 | int |
| 478 | fetch_kernel_version(unsigned int *puint, char *str, |
| 479 | size_t str_size) |
| 480 | { |
| 481 | struct utsname utsname; |
| 482 | int version, patchlevel, sublevel, err; |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 483 | bool int_ver_ready = false; |
| 484 | |
| 485 | if (access("/proc/version_signature", R_OK) == 0) |
| 486 | if (!fetch_ubuntu_kernel_version(puint)) |
| 487 | int_ver_ready = true; |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 488 | |
| 489 | if (uname(&utsname)) |
| 490 | return -1; |
| 491 | |
| 492 | if (str && str_size) { |
| 493 | strncpy(str, utsname.release, str_size); |
| 494 | str[str_size - 1] = '\0'; |
| 495 | } |
| 496 | |
| 497 | err = sscanf(utsname.release, "%d.%d.%d", |
| 498 | &version, &patchlevel, &sublevel); |
| 499 | |
| 500 | if (err != 3) { |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 501 | pr_debug("Unable to get kernel version from uname '%s'\n", |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 502 | utsname.release); |
| 503 | return -1; |
| 504 | } |
| 505 | |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 506 | if (puint && !int_ver_ready) |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 507 | *puint = (version << 16) + (patchlevel << 8) + sublevel; |
| 508 | return 0; |
| 509 | } |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 510 | |
| 511 | const char *perf_tip(const char *dirpath) |
| 512 | { |
| 513 | struct strlist *tips; |
| 514 | struct str_node *node; |
| 515 | char *tip = NULL; |
| 516 | struct strlist_config conf = { |
Namhyung Kim | 34b7b0f | 2016-01-09 19:16:29 +0900 | [diff] [blame] | 517 | .dirname = dirpath, |
| 518 | .file_only = true, |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 519 | }; |
| 520 | |
| 521 | tips = strlist__new("tips.txt", &conf); |
Namhyung Kim | 34b7b0f | 2016-01-09 19:16:29 +0900 | [diff] [blame] | 522 | if (tips == NULL) |
David Carrillo-Cisneros | 570eda0 | 2017-04-11 23:49:16 -0700 | [diff] [blame] | 523 | return errno == ENOENT ? NULL : |
| 524 | "Tip: check path of tips.txt or get more memory! ;-p"; |
Namhyung Kim | 34b7b0f | 2016-01-09 19:16:29 +0900 | [diff] [blame] | 525 | |
| 526 | if (strlist__nr_entries(tips) == 0) |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 527 | goto out; |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 528 | |
| 529 | node = strlist__entry(tips, random() % strlist__nr_entries(tips)); |
| 530 | if (asprintf(&tip, "Tip: %s", node->s) < 0) |
| 531 | tip = (char *)"Tip: get more memory! ;-)"; |
| 532 | |
| 533 | out: |
| 534 | strlist__delete(tips); |
| 535 | |
| 536 | return tip; |
| 537 | } |