Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / tools / perf / util / util.c
blob95aefa78bb075a52491b892f7ddccc259d909799
1 #include "../perf.h"
2 #include "util.h"
3 #include <api/fs/fs.h>
4 #include <sys/mman.h>
5 #ifdef HAVE_BACKTRACE_SUPPORT
6 #include <execinfo.h>
7 #endif
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <limits.h>
13 #include <byteswap.h>
14 #include <linux/kernel.h>
17 * XXX We need to find a better place for these things...
19 unsigned int page_size;
20 int cacheline_size;
22 bool test_attr__enabled;
24 bool perf_host = true;
25 bool perf_guest = false;
27 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
29 void event_attr_init(struct perf_event_attr *attr)
31 if (!perf_host)
32 attr->exclude_host = 1;
33 if (!perf_guest)
34 attr->exclude_guest = 1;
35 /* to capture ABI version */
36 attr->size = sizeof(*attr);
39 int mkdir_p(char *path, mode_t mode)
41 struct stat st;
42 int err;
43 char *d = path;
45 if (*d != '/')
46 return -1;
48 if (stat(path, &st) == 0)
49 return 0;
51 while (*++d == '/');
53 while ((d = strchr(d, '/'))) {
54 *d = '\0';
55 err = stat(path, &st) && mkdir(path, mode);
56 *d++ = '/';
57 if (err)
58 return -1;
59 while (*d == '/')
60 ++d;
62 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
65 static int slow_copyfile(const char *from, const char *to, mode_t mode)
67 int err = -1;
68 char *line = NULL;
69 size_t n;
70 FILE *from_fp = fopen(from, "r"), *to_fp;
71 mode_t old_umask;
73 if (from_fp == NULL)
74 goto out;
76 old_umask = umask(mode ^ 0777);
77 to_fp = fopen(to, "w");
78 umask(old_umask);
79 if (to_fp == NULL)
80 goto out_fclose_from;
82 while (getline(&line, &n, from_fp) > 0)
83 if (fputs(line, to_fp) == EOF)
84 goto out_fclose_to;
85 err = 0;
86 out_fclose_to:
87 fclose(to_fp);
88 free(line);
89 out_fclose_from:
90 fclose(from_fp);
91 out:
92 return err;
95 int copyfile_mode(const char *from, const char *to, mode_t mode)
97 int fromfd, tofd;
98 struct stat st;
99 void *addr;
100 int err = -1;
102 if (stat(from, &st))
103 goto out;
105 if (st.st_size == 0) /* /proc? do it slowly... */
106 return slow_copyfile(from, to, mode);
108 fromfd = open(from, O_RDONLY);
109 if (fromfd < 0)
110 goto out;
112 tofd = creat(to, mode);
113 if (tofd < 0)
114 goto out_close_from;
116 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
117 if (addr == MAP_FAILED)
118 goto out_close_to;
120 if (write(tofd, addr, st.st_size) == st.st_size)
121 err = 0;
123 munmap(addr, st.st_size);
124 out_close_to:
125 close(tofd);
126 if (err)
127 unlink(to);
128 out_close_from:
129 close(fromfd);
130 out:
131 return err;
134 int copyfile(const char *from, const char *to)
136 return copyfile_mode(from, to, 0755);
139 unsigned long convert_unit(unsigned long value, char *unit)
141 *unit = ' ';
143 if (value > 1000) {
144 value /= 1000;
145 *unit = 'K';
148 if (value > 1000) {
149 value /= 1000;
150 *unit = 'M';
153 if (value > 1000) {
154 value /= 1000;
155 *unit = 'G';
158 return value;
161 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
163 void *buf_start = buf;
164 size_t left = n;
166 while (left) {
167 ssize_t ret = is_read ? read(fd, buf, left) :
168 write(fd, buf, left);
170 if (ret < 0 && errno == EINTR)
171 continue;
172 if (ret <= 0)
173 return ret;
175 left -= ret;
176 buf += ret;
179 BUG_ON((size_t)(buf - buf_start) != n);
180 return n;
184 * Read exactly 'n' bytes or return an error.
186 ssize_t readn(int fd, void *buf, size_t n)
188 return ion(true, fd, buf, n);
192 * Write exactly 'n' bytes or return an error.
194 ssize_t writen(int fd, void *buf, size_t n)
196 return ion(false, fd, buf, n);
199 size_t hex_width(u64 v)
201 size_t n = 1;
203 while ((v >>= 4))
204 ++n;
206 return n;
209 static int hex(char ch)
211 if ((ch >= '0') && (ch <= '9'))
212 return ch - '0';
213 if ((ch >= 'a') && (ch <= 'f'))
214 return ch - 'a' + 10;
215 if ((ch >= 'A') && (ch <= 'F'))
216 return ch - 'A' + 10;
217 return -1;
221 * While we find nice hex chars, build a long_val.
222 * Return number of chars processed.
224 int hex2u64(const char *ptr, u64 *long_val)
226 const char *p = ptr;
227 *long_val = 0;
229 while (*p) {
230 const int hex_val = hex(*p);
232 if (hex_val < 0)
233 break;
235 *long_val = (*long_val << 4) | hex_val;
236 p++;
239 return p - ptr;
242 /* Obtain a backtrace and print it to stdout. */
243 #ifdef HAVE_BACKTRACE_SUPPORT
244 void dump_stack(void)
246 void *array[16];
247 size_t size = backtrace(array, ARRAY_SIZE(array));
248 char **strings = backtrace_symbols(array, size);
249 size_t i;
251 printf("Obtained %zd stack frames.\n", size);
253 for (i = 0; i < size; i++)
254 printf("%s\n", strings[i]);
256 free(strings);
258 #else
259 void dump_stack(void) {}
260 #endif
262 void get_term_dimensions(struct winsize *ws)
264 char *s = getenv("LINES");
266 if (s != NULL) {
267 ws->ws_row = atoi(s);
268 s = getenv("COLUMNS");
269 if (s != NULL) {
270 ws->ws_col = atoi(s);
271 if (ws->ws_row && ws->ws_col)
272 return;
275 #ifdef TIOCGWINSZ
276 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
277 ws->ws_row && ws->ws_col)
278 return;
279 #endif
280 ws->ws_row = 25;
281 ws->ws_col = 80;
284 static void set_tracing_events_path(const char *mountpoint)
286 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
287 mountpoint, "tracing/events");
290 const char *perf_debugfs_mount(const char *mountpoint)
292 const char *mnt;
294 mnt = debugfs_mount(mountpoint);
295 if (!mnt)
296 return NULL;
298 set_tracing_events_path(mnt);
300 return mnt;
303 void perf_debugfs_set_path(const char *mntpt)
305 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
306 set_tracing_events_path(mntpt);
309 static const char *find_debugfs(void)
311 const char *path = perf_debugfs_mount(NULL);
313 if (!path)
314 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
316 return path;
320 * Finds the path to the debugfs/tracing
321 * Allocates the string and stores it.
323 const char *find_tracing_dir(void)
325 static char *tracing;
326 static int tracing_found;
327 const char *debugfs;
329 if (tracing_found)
330 return tracing;
332 debugfs = find_debugfs();
333 if (!debugfs)
334 return NULL;
336 tracing = malloc(strlen(debugfs) + 9);
337 if (!tracing)
338 return NULL;
340 sprintf(tracing, "%s/tracing", debugfs);
342 tracing_found = 1;
343 return tracing;
346 char *get_tracing_file(const char *name)
348 const char *tracing;
349 char *file;
351 tracing = find_tracing_dir();
352 if (!tracing)
353 return NULL;
355 file = malloc(strlen(tracing) + strlen(name) + 2);
356 if (!file)
357 return NULL;
359 sprintf(file, "%s/%s", tracing, name);
360 return file;
363 void put_tracing_file(char *file)
365 free(file);
368 int parse_nsec_time(const char *str, u64 *ptime)
370 u64 time_sec, time_nsec;
371 char *end;
373 time_sec = strtoul(str, &end, 10);
374 if (*end != '.' && *end != '\0')
375 return -1;
377 if (*end == '.') {
378 int i;
379 char nsec_buf[10];
381 if (strlen(++end) > 9)
382 return -1;
384 strncpy(nsec_buf, end, 9);
385 nsec_buf[9] = '\0';
387 /* make it nsec precision */
388 for (i = strlen(nsec_buf); i < 9; i++)
389 nsec_buf[i] = '0';
391 time_nsec = strtoul(nsec_buf, &end, 10);
392 if (*end != '\0')
393 return -1;
394 } else
395 time_nsec = 0;
397 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
398 return 0;
401 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
403 struct parse_tag *i = tags;
405 while (i->tag) {
406 char *s;
408 s = strchr(str, i->tag);
409 if (s) {
410 unsigned long int value;
411 char *endptr;
413 value = strtoul(str, &endptr, 10);
414 if (s != endptr)
415 break;
417 if (value > ULONG_MAX / i->mult)
418 break;
419 value *= i->mult;
420 return value;
422 i++;
425 return (unsigned long) -1;
428 int filename__read_int(const char *filename, int *value)
430 char line[64];
431 int fd = open(filename, O_RDONLY), err = -1;
433 if (fd < 0)
434 return -1;
436 if (read(fd, line, sizeof(line)) > 0) {
437 *value = atoi(line);
438 err = 0;
441 close(fd);
442 return err;
445 int filename__read_str(const char *filename, char **buf, size_t *sizep)
447 size_t size = 0, alloc_size = 0;
448 void *bf = NULL, *nbf;
449 int fd, n, err = 0;
451 fd = open(filename, O_RDONLY);
452 if (fd < 0)
453 return -errno;
455 do {
456 if (size == alloc_size) {
457 alloc_size += BUFSIZ;
458 nbf = realloc(bf, alloc_size);
459 if (!nbf) {
460 err = -ENOMEM;
461 break;
464 bf = nbf;
467 n = read(fd, bf + size, alloc_size - size);
468 if (n < 0) {
469 if (size) {
470 pr_warning("read failed %d: %s\n",
471 errno, strerror(errno));
472 err = 0;
473 } else
474 err = -errno;
476 break;
479 size += n;
480 } while (n > 0);
482 if (!err) {
483 *sizep = size;
484 *buf = bf;
485 } else
486 free(bf);
488 close(fd);
489 return err;
492 const char *get_filename_for_perf_kvm(void)
494 const char *filename;
496 if (perf_host && !perf_guest)
497 filename = strdup("perf.data.host");
498 else if (!perf_host && perf_guest)
499 filename = strdup("perf.data.guest");
500 else
501 filename = strdup("perf.data.kvm");
503 return filename;
506 int perf_event_paranoid(void)
508 char path[PATH_MAX];
509 const char *procfs = procfs__mountpoint();
510 int value;
512 if (!procfs)
513 return INT_MAX;
515 scnprintf(path, PATH_MAX, "%s/sys/kernel/perf_event_paranoid", procfs);
517 if (filename__read_int(path, &value))
518 return INT_MAX;
520 return value;
523 void mem_bswap_32(void *src, int byte_size)
525 u32 *m = src;
526 while (byte_size > 0) {
527 *m = bswap_32(*m);
528 byte_size -= sizeof(u32);
529 ++m;
533 void mem_bswap_64(void *src, int byte_size)
535 u64 *m = src;
537 while (byte_size > 0) {
538 *m = bswap_64(*m);
539 byte_size -= sizeof(u64);
540 ++m;