blob: 28a0a89c1f739749a59c8b80aa3d3b37d8f4000f [file] [log] [blame]
Joerg Roedel1aed2672012-01-04 17:54:20 +01001#include "../perf.h"
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02002#include "util.h"
Frederic Weisbecker69e3f522010-01-16 14:21:15 +01003#include <sys/mman.h>
Ingo Molnar89fe8082013-09-30 12:07:11 +02004#ifdef HAVE_BACKTRACE_SUPPORT
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -03005#include <execinfo.h>
Irina Tirdeac9f08be2012-09-08 03:43:23 +03006#endif
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -03007#include <stdio.h>
8#include <stdlib.h>
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02009
Joerg Roedel1aed2672012-01-04 17:54:20 +010010/*
11 * XXX We need to find a better place for these things...
12 */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030013unsigned int page_size;
14
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -030015bool test_attr__enabled;
16
Joerg Roedel1aed2672012-01-04 17:54:20 +010017bool perf_host = true;
Joerg Roedelc4a7dca2012-02-10 18:05:05 +010018bool perf_guest = false;
Joerg Roedel1aed2672012-01-04 17:54:20 +010019
Borislav Petkov13559152013-02-20 16:32:31 +010020char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
21
Joerg Roedel1aed2672012-01-04 17:54:20 +010022void event_attr_init(struct perf_event_attr *attr)
23{
24 if (!perf_host)
25 attr->exclude_host = 1;
26 if (!perf_guest)
27 attr->exclude_guest = 1;
Stephane Eranian7e1ccd32012-02-09 16:12:38 +010028 /* to capture ABI version */
29 attr->size = sizeof(*attr);
Joerg Roedel1aed2672012-01-04 17:54:20 +010030}
31
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020032int mkdir_p(char *path, mode_t mode)
33{
34 struct stat st;
35 int err;
36 char *d = path;
37
38 if (*d != '/')
39 return -1;
40
41 if (stat(path, &st) == 0)
42 return 0;
43
44 while (*++d == '/');
45
46 while ((d = strchr(d, '/'))) {
47 *d = '\0';
48 err = stat(path, &st) && mkdir(path, mode);
49 *d++ = '/';
50 if (err)
51 return -1;
52 while (*d == '/')
53 ++d;
54 }
55 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
56}
57
Adrian Hunter9a17d722013-10-14 13:43:41 +030058static int slow_copyfile(const char *from, const char *to, mode_t mode)
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -020059{
Adrian Hunter9a17d722013-10-14 13:43:41 +030060 int err = -1;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -020061 char *line = NULL;
62 size_t n;
63 FILE *from_fp = fopen(from, "r"), *to_fp;
Adrian Hunter9a17d722013-10-14 13:43:41 +030064 mode_t old_umask;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -020065
66 if (from_fp == NULL)
67 goto out;
68
Adrian Hunter9a17d722013-10-14 13:43:41 +030069 old_umask = umask(mode ^ 0777);
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -020070 to_fp = fopen(to, "w");
Adrian Hunter9a17d722013-10-14 13:43:41 +030071 umask(old_umask);
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -020072 if (to_fp == NULL)
73 goto out_fclose_from;
74
75 while (getline(&line, &n, from_fp) > 0)
76 if (fputs(line, to_fp) == EOF)
77 goto out_fclose_to;
78 err = 0;
79out_fclose_to:
80 fclose(to_fp);
81 free(line);
82out_fclose_from:
83 fclose(from_fp);
84out:
85 return err;
86}
87
Adrian Hunter9a17d722013-10-14 13:43:41 +030088int copyfile_mode(const char *from, const char *to, mode_t mode)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020089{
90 int fromfd, tofd;
91 struct stat st;
92 void *addr;
93 int err = -1;
94
95 if (stat(from, &st))
96 goto out;
97
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -020098 if (st.st_size == 0) /* /proc? do it slowly... */
Adrian Hunter9a17d722013-10-14 13:43:41 +030099 return slow_copyfile(from, to, mode);
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200100
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200101 fromfd = open(from, O_RDONLY);
102 if (fromfd < 0)
103 goto out;
104
Adrian Hunter9a17d722013-10-14 13:43:41 +0300105 tofd = creat(to, mode);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200106 if (tofd < 0)
107 goto out_close_from;
108
109 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
110 if (addr == MAP_FAILED)
111 goto out_close_to;
112
113 if (write(tofd, addr, st.st_size) == st.st_size)
114 err = 0;
115
116 munmap(addr, st.st_size);
117out_close_to:
118 close(tofd);
119 if (err)
120 unlink(to);
121out_close_from:
122 close(fromfd);
123out:
124 return err;
125}
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300126
Adrian Hunter9a17d722013-10-14 13:43:41 +0300127int copyfile(const char *from, const char *to)
128{
129 return copyfile_mode(from, to, 0755);
130}
131
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300132unsigned long convert_unit(unsigned long value, char *unit)
133{
134 *unit = ' ';
135
136 if (value > 1000) {
137 value /= 1000;
138 *unit = 'K';
139 }
140
141 if (value > 1000) {
142 value /= 1000;
143 *unit = 'M';
144 }
145
146 if (value > 1000) {
147 value /= 1000;
148 *unit = 'G';
149 }
150
151 return value;
152}
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200153
154int readn(int fd, void *buf, size_t n)
155{
156 void *buf_start = buf;
157
158 while (n) {
159 int ret = read(fd, buf, n);
160
161 if (ret <= 0)
162 return ret;
163
164 n -= ret;
165 buf += ret;
166 }
167
168 return buf - buf_start;
169}
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300170
171size_t hex_width(u64 v)
172{
173 size_t n = 1;
174
175 while ((v >>= 4))
176 ++n;
177
178 return n;
179}
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300180
Jiri Olsab2aff5f2012-10-27 23:18:30 +0200181static int hex(char ch)
182{
183 if ((ch >= '0') && (ch <= '9'))
184 return ch - '0';
185 if ((ch >= 'a') && (ch <= 'f'))
186 return ch - 'a' + 10;
187 if ((ch >= 'A') && (ch <= 'F'))
188 return ch - 'A' + 10;
189 return -1;
190}
191
192/*
193 * While we find nice hex chars, build a long_val.
194 * Return number of chars processed.
195 */
196int hex2u64(const char *ptr, u64 *long_val)
197{
198 const char *p = ptr;
199 *long_val = 0;
200
201 while (*p) {
202 const int hex_val = hex(*p);
203
204 if (hex_val < 0)
205 break;
206
207 *long_val = (*long_val << 4) | hex_val;
208 p++;
209 }
210
211 return p - ptr;
212}
213
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300214/* Obtain a backtrace and print it to stdout. */
Ingo Molnar89fe8082013-09-30 12:07:11 +0200215#ifdef HAVE_BACKTRACE_SUPPORT
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300216void dump_stack(void)
217{
218 void *array[16];
219 size_t size = backtrace(array, ARRAY_SIZE(array));
220 char **strings = backtrace_symbols(array, size);
221 size_t i;
222
223 printf("Obtained %zd stack frames.\n", size);
224
225 for (i = 0; i < size; i++)
226 printf("%s\n", strings[i]);
227
228 free(strings);
229}
Irina Tirdeac9f08be2012-09-08 03:43:23 +0300230#else
231void dump_stack(void) {}
232#endif
David Ahern2c803e52013-01-14 10:48:01 -0700233
234void get_term_dimensions(struct winsize *ws)
235{
236 char *s = getenv("LINES");
237
238 if (s != NULL) {
239 ws->ws_row = atoi(s);
240 s = getenv("COLUMNS");
241 if (s != NULL) {
242 ws->ws_col = atoi(s);
243 if (ws->ws_row && ws->ws_col)
244 return;
245 }
246 }
247#ifdef TIOCGWINSZ
248 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
249 ws->ws_row && ws->ws_col)
250 return;
251#endif
252 ws->ws_row = 25;
253 ws->ws_col = 80;
254}
Borislav Petkov13559152013-02-20 16:32:31 +0100255
256static void set_tracing_events_path(const char *mountpoint)
257{
258 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
259 mountpoint, "tracing/events");
260}
261
262const char *perf_debugfs_mount(const char *mountpoint)
263{
264 const char *mnt;
265
266 mnt = debugfs_mount(mountpoint);
267 if (!mnt)
268 return NULL;
269
270 set_tracing_events_path(mnt);
271
272 return mnt;
273}
274
275void perf_debugfs_set_path(const char *mntpt)
276{
277 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
278 set_tracing_events_path(mntpt);
279}
Namhyung Kim167aedc2013-06-26 16:14:04 +0900280
281static const char *find_debugfs(void)
282{
283 const char *path = perf_debugfs_mount(NULL);
284
285 if (!path)
286 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
287
288 return path;
289}
290
291/*
292 * Finds the path to the debugfs/tracing
293 * Allocates the string and stores it.
294 */
295const char *find_tracing_dir(void)
296{
297 static char *tracing;
298 static int tracing_found;
299 const char *debugfs;
300
301 if (tracing_found)
302 return tracing;
303
304 debugfs = find_debugfs();
305 if (!debugfs)
306 return NULL;
307
308 tracing = malloc(strlen(debugfs) + 9);
309 if (!tracing)
310 return NULL;
311
312 sprintf(tracing, "%s/tracing", debugfs);
313
314 tracing_found = 1;
315 return tracing;
316}
317
318char *get_tracing_file(const char *name)
319{
320 const char *tracing;
321 char *file;
322
323 tracing = find_tracing_dir();
324 if (!tracing)
325 return NULL;
326
327 file = malloc(strlen(tracing) + strlen(name) + 2);
328 if (!file)
329 return NULL;
330
331 sprintf(file, "%s/%s", tracing, name);
332 return file;
333}
334
335void put_tracing_file(char *file)
336{
337 free(file);
338}
Namhyung Kim3b47abe2013-06-04 10:50:29 +0900339
340int parse_nsec_time(const char *str, u64 *ptime)
341{
342 u64 time_sec, time_nsec;
343 char *end;
344
345 time_sec = strtoul(str, &end, 10);
346 if (*end != '.' && *end != '\0')
347 return -1;
348
349 if (*end == '.') {
350 int i;
351 char nsec_buf[10];
352
353 if (strlen(++end) > 9)
354 return -1;
355
356 strncpy(nsec_buf, end, 9);
357 nsec_buf[9] = '\0';
358
359 /* make it nsec precision */
360 for (i = strlen(nsec_buf); i < 9; i++)
361 nsec_buf[i] = '0';
362
363 time_nsec = strtoul(nsec_buf, &end, 10);
364 if (*end != '\0')
365 return -1;
366 } else
367 time_nsec = 0;
368
369 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
370 return 0;
371}
Jiri Olsa27050f52013-09-01 12:36:13 +0200372
373unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
374{
375 struct parse_tag *i = tags;
376
377 while (i->tag) {
378 char *s;
379
380 s = strchr(str, i->tag);
381 if (s) {
382 unsigned long int value;
383 char *endptr;
384
385 value = strtoul(str, &endptr, 10);
386 if (s != endptr)
387 break;
388
Adrian Hunter56921be2013-10-22 10:34:17 +0300389 if (value > ULONG_MAX / i->mult)
390 break;
Jiri Olsa27050f52013-09-01 12:36:13 +0200391 value *= i->mult;
392 return value;
393 }
394 i++;
395 }
396
397 return (unsigned long) -1;
398}
Arnaldo Carvalho de Melo97a07f12013-10-17 16:33:43 -0300399
400int filename__read_int(const char *filename, int *value)
401{
402 char line[64];
403 int fd = open(filename, O_RDONLY), err = -1;
404
405 if (fd < 0)
406 return -1;
407
408 if (read(fd, line, sizeof(line)) > 0) {
409 *value = atoi(line);
410 err = 0;
411 }
412
413 close(fd);
414 return err;
415}