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" |
Frederic Weisbecker | 69e3f52 | 2010-01-16 14:21:15 +0100 | [diff] [blame] | 3 | #include <sys/mman.h> |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 4 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 5 | /* |
| 6 | * XXX We need to find a better place for these things... |
| 7 | */ |
| 8 | bool perf_host = true; |
Joerg Roedel | c4a7dca | 2012-02-10 18:05:05 +0100 | [diff] [blame] | 9 | bool perf_guest = false; |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 10 | |
| 11 | void event_attr_init(struct perf_event_attr *attr) |
| 12 | { |
| 13 | if (!perf_host) |
| 14 | attr->exclude_host = 1; |
| 15 | if (!perf_guest) |
| 16 | attr->exclude_guest = 1; |
Stephane Eranian | 7e1ccd3 | 2012-02-09 16:12:38 +0100 | [diff] [blame^] | 17 | /* to capture ABI version */ |
| 18 | attr->size = sizeof(*attr); |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 19 | } |
| 20 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 21 | int mkdir_p(char *path, mode_t mode) |
| 22 | { |
| 23 | struct stat st; |
| 24 | int err; |
| 25 | char *d = path; |
| 26 | |
| 27 | if (*d != '/') |
| 28 | return -1; |
| 29 | |
| 30 | if (stat(path, &st) == 0) |
| 31 | return 0; |
| 32 | |
| 33 | while (*++d == '/'); |
| 34 | |
| 35 | while ((d = strchr(d, '/'))) { |
| 36 | *d = '\0'; |
| 37 | err = stat(path, &st) && mkdir(path, mode); |
| 38 | *d++ = '/'; |
| 39 | if (err) |
| 40 | return -1; |
| 41 | while (*d == '/') |
| 42 | ++d; |
| 43 | } |
| 44 | return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; |
| 45 | } |
| 46 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 47 | static int slow_copyfile(const char *from, const char *to) |
| 48 | { |
| 49 | int err = 0; |
| 50 | char *line = NULL; |
| 51 | size_t n; |
| 52 | FILE *from_fp = fopen(from, "r"), *to_fp; |
| 53 | |
| 54 | if (from_fp == NULL) |
| 55 | goto out; |
| 56 | |
| 57 | to_fp = fopen(to, "w"); |
| 58 | if (to_fp == NULL) |
| 59 | goto out_fclose_from; |
| 60 | |
| 61 | while (getline(&line, &n, from_fp) > 0) |
| 62 | if (fputs(line, to_fp) == EOF) |
| 63 | goto out_fclose_to; |
| 64 | err = 0; |
| 65 | out_fclose_to: |
| 66 | fclose(to_fp); |
| 67 | free(line); |
| 68 | out_fclose_from: |
| 69 | fclose(from_fp); |
| 70 | out: |
| 71 | return err; |
| 72 | } |
| 73 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 74 | int copyfile(const char *from, const char *to) |
| 75 | { |
| 76 | int fromfd, tofd; |
| 77 | struct stat st; |
| 78 | void *addr; |
| 79 | int err = -1; |
| 80 | |
| 81 | if (stat(from, &st)) |
| 82 | goto out; |
| 83 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 84 | if (st.st_size == 0) /* /proc? do it slowly... */ |
| 85 | return slow_copyfile(from, to); |
| 86 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 87 | fromfd = open(from, O_RDONLY); |
| 88 | if (fromfd < 0) |
| 89 | goto out; |
| 90 | |
| 91 | tofd = creat(to, 0755); |
| 92 | if (tofd < 0) |
| 93 | goto out_close_from; |
| 94 | |
| 95 | addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0); |
| 96 | if (addr == MAP_FAILED) |
| 97 | goto out_close_to; |
| 98 | |
| 99 | if (write(tofd, addr, st.st_size) == st.st_size) |
| 100 | err = 0; |
| 101 | |
| 102 | munmap(addr, st.st_size); |
| 103 | out_close_to: |
| 104 | close(tofd); |
| 105 | if (err) |
| 106 | unlink(to); |
| 107 | out_close_from: |
| 108 | close(fromfd); |
| 109 | out: |
| 110 | return err; |
| 111 | } |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 112 | |
| 113 | unsigned long convert_unit(unsigned long value, char *unit) |
| 114 | { |
| 115 | *unit = ' '; |
| 116 | |
| 117 | if (value > 1000) { |
| 118 | value /= 1000; |
| 119 | *unit = 'K'; |
| 120 | } |
| 121 | |
| 122 | if (value > 1000) { |
| 123 | value /= 1000; |
| 124 | *unit = 'M'; |
| 125 | } |
| 126 | |
| 127 | if (value > 1000) { |
| 128 | value /= 1000; |
| 129 | *unit = 'G'; |
| 130 | } |
| 131 | |
| 132 | return value; |
| 133 | } |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 134 | |
| 135 | int readn(int fd, void *buf, size_t n) |
| 136 | { |
| 137 | void *buf_start = buf; |
| 138 | |
| 139 | while (n) { |
| 140 | int ret = read(fd, buf, n); |
| 141 | |
| 142 | if (ret <= 0) |
| 143 | return ret; |
| 144 | |
| 145 | n -= ret; |
| 146 | buf += ret; |
| 147 | } |
| 148 | |
| 149 | return buf - buf_start; |
| 150 | } |