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> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 6 | #include <sys/stat.h> |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 7 | #include <sys/utsname.h> |
Arnaldo Carvalho de Melo | 76b31a2 | 2017-04-18 12:26:44 -0300 | [diff] [blame] | 8 | #include <dirent.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 9 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 9607ad3 | 2017-04-19 15:49:18 -0300 | [diff] [blame] | 10 | #include <signal.h> |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Jiri Olsa | cef82c9 | 2013-12-03 14:09:22 +0100 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <errno.h> |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 15 | #include <limits.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 | |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 146 | static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi) |
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; |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 151 | FILE *from_fp, *to_fp; |
| 152 | struct nscookie nsc; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 153 | |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 154 | nsinfo__mountns_enter(nsi, &nsc); |
| 155 | from_fp = fopen(from, "r"); |
| 156 | nsinfo__mountns_exit(&nsc); |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 157 | if (from_fp == NULL) |
| 158 | goto out; |
| 159 | |
| 160 | to_fp = fopen(to, "w"); |
| 161 | if (to_fp == NULL) |
| 162 | goto out_fclose_from; |
| 163 | |
| 164 | while (getline(&line, &n, from_fp) > 0) |
| 165 | if (fputs(line, to_fp) == EOF) |
| 166 | goto out_fclose_to; |
| 167 | err = 0; |
| 168 | out_fclose_to: |
| 169 | fclose(to_fp); |
| 170 | free(line); |
| 171 | out_fclose_from: |
| 172 | fclose(from_fp); |
| 173 | out: |
| 174 | return err; |
| 175 | } |
| 176 | |
Namhyung Kim | 9c9f5a2 | 2015-05-18 09:30:18 +0900 | [diff] [blame] | 177 | int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size) |
| 178 | { |
| 179 | void *ptr; |
| 180 | loff_t pgoff; |
| 181 | |
| 182 | pgoff = off_in & ~(page_size - 1); |
| 183 | off_in -= pgoff; |
| 184 | |
| 185 | ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff); |
| 186 | if (ptr == MAP_FAILED) |
| 187 | return -1; |
| 188 | |
| 189 | while (size) { |
| 190 | ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out); |
| 191 | if (ret < 0 && errno == EINTR) |
| 192 | continue; |
| 193 | if (ret <= 0) |
| 194 | break; |
| 195 | |
| 196 | size -= ret; |
| 197 | off_in += ret; |
| 198 | off_out -= ret; |
| 199 | } |
| 200 | munmap(ptr, off_in + size); |
| 201 | |
| 202 | return size ? -1 : 0; |
| 203 | } |
| 204 | |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 205 | static int copyfile_mode_ns(const char *from, const char *to, mode_t mode, |
| 206 | struct nsinfo *nsi) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 207 | { |
| 208 | int fromfd, tofd; |
| 209 | struct stat st; |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 210 | int err; |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 211 | char *tmp = NULL, *ptr = NULL; |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 212 | struct nscookie nsc; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 213 | |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 214 | nsinfo__mountns_enter(nsi, &nsc); |
| 215 | err = stat(from, &st); |
| 216 | nsinfo__mountns_exit(&nsc); |
| 217 | if (err) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 218 | goto out; |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 219 | err = -1; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 220 | |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 221 | /* extra 'x' at the end is to reserve space for '.' */ |
| 222 | if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) { |
| 223 | tmp = NULL; |
| 224 | goto out; |
| 225 | } |
| 226 | ptr = strrchr(tmp, '/'); |
| 227 | if (!ptr) |
| 228 | goto out; |
| 229 | ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1); |
| 230 | *ptr = '.'; |
| 231 | |
| 232 | tofd = mkstemp(tmp); |
| 233 | if (tofd < 0) |
| 234 | goto out; |
| 235 | |
| 236 | if (fchmod(tofd, mode)) |
| 237 | goto out_close_to; |
| 238 | |
| 239 | if (st.st_size == 0) { /* /proc? do it slowly... */ |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 240 | err = slow_copyfile(from, tmp, nsi); |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 241 | goto out_close_to; |
| 242 | } |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 243 | |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 244 | nsinfo__mountns_enter(nsi, &nsc); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 245 | fromfd = open(from, O_RDONLY); |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 246 | nsinfo__mountns_exit(&nsc); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 247 | if (fromfd < 0) |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 248 | goto out_close_to; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 249 | |
Namhyung Kim | 9c9f5a2 | 2015-05-18 09:30:18 +0900 | [diff] [blame] | 250 | err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 251 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 252 | close(fromfd); |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 253 | out_close_to: |
| 254 | close(tofd); |
| 255 | if (!err) |
| 256 | err = link(tmp, to); |
| 257 | unlink(tmp); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 258 | out: |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 259 | free(tmp); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 260 | return err; |
| 261 | } |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 262 | |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 263 | int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi) |
| 264 | { |
| 265 | return copyfile_mode_ns(from, to, 0755, nsi); |
| 266 | } |
| 267 | |
| 268 | int copyfile_mode(const char *from, const char *to, mode_t mode) |
| 269 | { |
| 270 | return copyfile_mode_ns(from, to, mode, NULL); |
| 271 | } |
| 272 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 273 | int copyfile(const char *from, const char *to) |
| 274 | { |
| 275 | return copyfile_mode(from, to, 0755); |
| 276 | } |
| 277 | |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 278 | 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] | 279 | { |
| 280 | void *buf_start = buf; |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 281 | size_t left = n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 282 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 283 | while (left) { |
David Carrillo-Cisneros | 7c72440 | 2017-07-17 21:25:37 -0700 | [diff] [blame] | 284 | /* buf must be treated as const if !is_read. */ |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 285 | ssize_t ret = is_read ? read(fd, buf, left) : |
| 286 | write(fd, buf, left); |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 287 | |
Namhyung Kim | e148c76 | 2014-04-24 22:27:32 +0900 | [diff] [blame] | 288 | if (ret < 0 && errno == EINTR) |
| 289 | continue; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 290 | if (ret <= 0) |
| 291 | return ret; |
| 292 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 293 | left -= ret; |
| 294 | buf += ret; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 295 | } |
| 296 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 297 | BUG_ON((size_t)(buf - buf_start) != n); |
| 298 | return n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 299 | } |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 300 | |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 301 | /* |
| 302 | * Read exactly 'n' bytes or return an error. |
| 303 | */ |
| 304 | ssize_t readn(int fd, void *buf, size_t n) |
| 305 | { |
| 306 | return ion(true, fd, buf, n); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Write exactly 'n' bytes or return an error. |
| 311 | */ |
David Carrillo-Cisneros | 7c72440 | 2017-07-17 21:25:37 -0700 | [diff] [blame] | 312 | ssize_t writen(int fd, const void *buf, size_t n) |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 313 | { |
David Carrillo-Cisneros | 7c72440 | 2017-07-17 21:25:37 -0700 | [diff] [blame] | 314 | /* ion does not modify buf. */ |
| 315 | return ion(false, fd, (void *)buf, n); |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 318 | size_t hex_width(u64 v) |
| 319 | { |
| 320 | size_t n = 1; |
| 321 | |
| 322 | while ((v >>= 4)) |
| 323 | ++n; |
| 324 | |
| 325 | return n; |
| 326 | } |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 327 | |
Jiri Olsa | b2aff5f | 2012-10-27 23:18:30 +0200 | [diff] [blame] | 328 | static int hex(char ch) |
| 329 | { |
| 330 | if ((ch >= '0') && (ch <= '9')) |
| 331 | return ch - '0'; |
| 332 | if ((ch >= 'a') && (ch <= 'f')) |
| 333 | return ch - 'a' + 10; |
| 334 | if ((ch >= 'A') && (ch <= 'F')) |
| 335 | return ch - 'A' + 10; |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * While we find nice hex chars, build a long_val. |
| 341 | * Return number of chars processed. |
| 342 | */ |
| 343 | int hex2u64(const char *ptr, u64 *long_val) |
| 344 | { |
| 345 | const char *p = ptr; |
| 346 | *long_val = 0; |
| 347 | |
| 348 | while (*p) { |
| 349 | const int hex_val = hex(*p); |
| 350 | |
| 351 | if (hex_val < 0) |
| 352 | break; |
| 353 | |
| 354 | *long_val = (*long_val << 4) | hex_val; |
| 355 | p++; |
| 356 | } |
| 357 | |
| 358 | return p - ptr; |
| 359 | } |
| 360 | |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 361 | int perf_event_paranoid(void) |
| 362 | { |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 363 | int value; |
| 364 | |
Arnaldo Carvalho de Melo | ce27309 | 2014-12-11 13:37:59 -0300 | [diff] [blame] | 365 | if (sysctl__read_int("kernel/perf_event_paranoid", &value)) |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 366 | return INT_MAX; |
| 367 | |
| 368 | return value; |
| 369 | } |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 370 | static int |
| 371 | fetch_ubuntu_kernel_version(unsigned int *puint) |
| 372 | { |
| 373 | ssize_t len; |
| 374 | size_t line_len = 0; |
| 375 | char *ptr, *line = NULL; |
| 376 | int version, patchlevel, sublevel, err; |
Arnaldo Carvalho de Melo | 44b58e0 | 2017-06-20 12:19:16 -0300 | [diff] [blame] | 377 | FILE *vsig; |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 378 | |
Arnaldo Carvalho de Melo | 44b58e0 | 2017-06-20 12:19:16 -0300 | [diff] [blame] | 379 | if (!puint) |
| 380 | return 0; |
| 381 | |
| 382 | vsig = fopen("/proc/version_signature", "r"); |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 383 | if (!vsig) { |
| 384 | pr_debug("Open /proc/version_signature failed: %s\n", |
| 385 | strerror(errno)); |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | len = getline(&line, &line_len, vsig); |
| 390 | fclose(vsig); |
| 391 | err = -1; |
| 392 | if (len <= 0) { |
| 393 | pr_debug("Reading from /proc/version_signature failed: %s\n", |
| 394 | strerror(errno)); |
| 395 | goto errout; |
| 396 | } |
| 397 | |
| 398 | ptr = strrchr(line, ' '); |
| 399 | if (!ptr) { |
| 400 | pr_debug("Parsing /proc/version_signature failed: %s\n", line); |
| 401 | goto errout; |
| 402 | } |
| 403 | |
| 404 | err = sscanf(ptr + 1, "%d.%d.%d", |
| 405 | &version, &patchlevel, &sublevel); |
| 406 | if (err != 3) { |
| 407 | pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n", |
| 408 | line); |
| 409 | goto errout; |
| 410 | } |
| 411 | |
Arnaldo Carvalho de Melo | 44b58e0 | 2017-06-20 12:19:16 -0300 | [diff] [blame] | 412 | *puint = (version << 16) + (patchlevel << 8) + sublevel; |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 413 | err = 0; |
| 414 | errout: |
| 415 | free(line); |
| 416 | return err; |
| 417 | } |
| 418 | |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 419 | int |
| 420 | fetch_kernel_version(unsigned int *puint, char *str, |
| 421 | size_t str_size) |
| 422 | { |
| 423 | struct utsname utsname; |
| 424 | int version, patchlevel, sublevel, err; |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 425 | bool int_ver_ready = false; |
| 426 | |
| 427 | if (access("/proc/version_signature", R_OK) == 0) |
| 428 | if (!fetch_ubuntu_kernel_version(puint)) |
| 429 | int_ver_ready = true; |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 430 | |
| 431 | if (uname(&utsname)) |
| 432 | return -1; |
| 433 | |
| 434 | if (str && str_size) { |
| 435 | strncpy(str, utsname.release, str_size); |
| 436 | str[str_size - 1] = '\0'; |
| 437 | } |
| 438 | |
Arnaldo Carvalho de Melo | 44b58e0 | 2017-06-20 12:19:16 -0300 | [diff] [blame] | 439 | if (!puint || int_ver_ready) |
| 440 | return 0; |
| 441 | |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 442 | err = sscanf(utsname.release, "%d.%d.%d", |
| 443 | &version, &patchlevel, &sublevel); |
| 444 | |
| 445 | if (err != 3) { |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 446 | pr_debug("Unable to get kernel version from uname '%s'\n", |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 447 | utsname.release); |
| 448 | return -1; |
| 449 | } |
| 450 | |
Arnaldo Carvalho de Melo | 44b58e0 | 2017-06-20 12:19:16 -0300 | [diff] [blame] | 451 | *puint = (version << 16) + (patchlevel << 8) + sublevel; |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 452 | return 0; |
| 453 | } |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 454 | |
| 455 | const char *perf_tip(const char *dirpath) |
| 456 | { |
| 457 | struct strlist *tips; |
| 458 | struct str_node *node; |
| 459 | char *tip = NULL; |
| 460 | struct strlist_config conf = { |
Namhyung Kim | 34b7b0f | 2016-01-09 19:16:29 +0900 | [diff] [blame] | 461 | .dirname = dirpath, |
| 462 | .file_only = true, |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | tips = strlist__new("tips.txt", &conf); |
Namhyung Kim | 34b7b0f | 2016-01-09 19:16:29 +0900 | [diff] [blame] | 466 | if (tips == NULL) |
David Carrillo-Cisneros | 570eda0 | 2017-04-11 23:49:16 -0700 | [diff] [blame] | 467 | return errno == ENOENT ? NULL : |
| 468 | "Tip: check path of tips.txt or get more memory! ;-p"; |
Namhyung Kim | 34b7b0f | 2016-01-09 19:16:29 +0900 | [diff] [blame] | 469 | |
| 470 | if (strlist__nr_entries(tips) == 0) |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 471 | goto out; |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 472 | |
| 473 | node = strlist__entry(tips, random() % strlist__nr_entries(tips)); |
| 474 | if (asprintf(&tip, "Tip: %s", node->s) < 0) |
| 475 | tip = (char *)"Tip: get more memory! ;-)"; |
| 476 | |
| 477 | out: |
| 478 | strlist__delete(tips); |
| 479 | |
| 480 | return tip; |
| 481 | } |