Jiri Olsa | ce1e22b | 2016-02-15 09:34:35 +0100 | [diff] [blame] | 1 | #include <stddef.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame^] | 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <unistd.h> |
| 8 | #include <api/fs/fs.h> |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 9 | #include "mem-events.h" |
Jiri Olsa | ce1e22b | 2016-02-15 09:34:35 +0100 | [diff] [blame] | 10 | #include "debug.h" |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 11 | |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame^] | 12 | #define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s } |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 13 | |
| 14 | struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = { |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame^] | 15 | E("ldlat-loads", "cpu/mem-loads,ldlat=30/P", "mem-loads"), |
| 16 | E("ldlat-stores", "cpu/mem-stores/P", "mem-stores"), |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 17 | }; |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame^] | 18 | #undef E |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 19 | |
| 20 | #undef E |
Jiri Olsa | ce1e22b | 2016-02-15 09:34:35 +0100 | [diff] [blame] | 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 | } |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame^] | 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 | } |