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 | 21 | |
2ba7ac58 JO |
22 | char *perf_mem_events__name(int i) |
23 | { | |
24 | return (char *)perf_mem_events[i].name; | |
25 | } | |
26 | ||
ce1e22b0 JO |
27 | int perf_mem_events__parse(const char *str) |
28 | { | |
29 | char *tok, *saveptr = NULL; | |
30 | bool found = false; | |
31 | char *buf; | |
32 | int j; | |
33 | ||
34 | /* We need buffer that we know we can write to. */ | |
35 | buf = malloc(strlen(str) + 1); | |
36 | if (!buf) | |
37 | return -ENOMEM; | |
38 | ||
39 | strcpy(buf, str); | |
40 | ||
41 | tok = strtok_r((char *)buf, ",", &saveptr); | |
42 | ||
43 | while (tok) { | |
44 | for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { | |
45 | struct perf_mem_event *e = &perf_mem_events[j]; | |
46 | ||
47 | if (strstr(e->tag, tok)) | |
48 | e->record = found = true; | |
49 | } | |
50 | ||
51 | tok = strtok_r(NULL, ",", &saveptr); | |
52 | } | |
53 | ||
54 | free(buf); | |
55 | ||
56 | if (found) | |
57 | return 0; | |
58 | ||
59 | pr_err("failed: event '%s' not found, use '-e list' to get list of available events\n", str); | |
60 | return -1; | |
61 | } | |
54fbad54 JO |
62 | |
63 | int perf_mem_events__init(void) | |
64 | { | |
65 | const char *mnt = sysfs__mount(); | |
66 | bool found = false; | |
67 | int j; | |
68 | ||
69 | if (!mnt) | |
70 | return -ENOENT; | |
71 | ||
72 | for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { | |
73 | char path[PATH_MAX]; | |
74 | struct perf_mem_event *e = &perf_mem_events[j]; | |
75 | struct stat st; | |
76 | ||
77 | scnprintf(path, PATH_MAX, "%s/devices/cpu/events/%s", | |
78 | mnt, e->sysfs_name); | |
79 | ||
80 | if (!stat(path, &st)) | |
81 | e->supported = found = true; | |
82 | } | |
83 | ||
84 | return found ? 0 : -ENOENT; | |
85 | } |