Commit | Line | Data |
---|---|---|
ce1e22b0 JO |
1 | #include <stddef.h> |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | #include <errno.h> | |
54fbad54 JO |
5 | #include <sys/types.h> |
6 | #include <sys/stat.h> | |
7 | #include <unistd.h> | |
8 | #include <api/fs/fs.h> | |
acbe613e | 9 | #include "mem-events.h" |
ce1e22b0 | 10 | #include "debug.h" |
acbe613e | 11 | |
54fbad54 | 12 | #define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s } |
acbe613e JO |
13 | |
14 | struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = { | |
54fbad54 JO |
15 | E("ldlat-loads", "cpu/mem-loads,ldlat=30/P", "mem-loads"), |
16 | E("ldlat-stores", "cpu/mem-stores/P", "mem-stores"), | |
acbe613e | 17 | }; |
54fbad54 | 18 | #undef E |
acbe613e JO |
19 | |
20 | #undef E | |
ce1e22b0 JO |
21 | |
22 | int perf_mem_events__parse(const char *str) | |
23 | { | |
24 | char *tok, *saveptr = NULL; | |
25 | bool found = false; | |
26 | char *buf; | |
27 | int j; | |
28 | ||
29 | /* We need buffer that we know we can write to. */ | |
30 | buf = malloc(strlen(str) + 1); | |
31 | if (!buf) | |
32 | return -ENOMEM; | |
33 | ||
34 | strcpy(buf, str); | |
35 | ||
36 | tok = strtok_r((char *)buf, ",", &saveptr); | |
37 | ||
38 | while (tok) { | |
39 | for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { | |
40 | struct perf_mem_event *e = &perf_mem_events[j]; | |
41 | ||
42 | if (strstr(e->tag, tok)) | |
43 | e->record = found = true; | |
44 | } | |
45 | ||
46 | tok = strtok_r(NULL, ",", &saveptr); | |
47 | } | |
48 | ||
49 | free(buf); | |
50 | ||
51 | if (found) | |
52 | return 0; | |
53 | ||
54 | pr_err("failed: event '%s' not found, use '-e list' to get list of available events\n", str); | |
55 | return -1; | |
56 | } | |
54fbad54 JO |
57 | |
58 | int perf_mem_events__init(void) | |
59 | { | |
60 | const char *mnt = sysfs__mount(); | |
61 | bool found = false; | |
62 | int j; | |
63 | ||
64 | if (!mnt) | |
65 | return -ENOENT; | |
66 | ||
67 | for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { | |
68 | char path[PATH_MAX]; | |
69 | struct perf_mem_event *e = &perf_mem_events[j]; | |
70 | struct stat st; | |
71 | ||
72 | scnprintf(path, PATH_MAX, "%s/devices/cpu/events/%s", | |
73 | mnt, e->sysfs_name); | |
74 | ||
75 | if (!stat(path, &st)) | |
76 | e->supported = found = true; | |
77 | } | |
78 | ||
79 | return found ? 0 : -ENOENT; | |
80 | } |