blob: e21853fe1312ce1bc5dcfe1529cb44708ee73913 [file] [log] [blame]
Jiri Olsace1e22b2016-02-15 09:34:35 +01001#include <stddef.h>
2#include <stdlib.h>
3#include <string.h>
4#include <errno.h>
Jiri Olsa54fbad52016-02-24 09:46:42 +01005#include <sys/types.h>
6#include <sys/stat.h>
7#include <unistd.h>
8#include <api/fs/fs.h>
Jiri Olsaacbe6132016-02-15 09:34:34 +01009#include "mem-events.h"
Jiri Olsace1e22b2016-02-15 09:34:35 +010010#include "debug.h"
Jiri Olsaacbe6132016-02-15 09:34:34 +010011
Jiri Olsa54fbad52016-02-24 09:46:42 +010012#define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s }
Jiri Olsaacbe6132016-02-15 09:34:34 +010013
14struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = {
Jiri Olsa54fbad52016-02-24 09:46:42 +010015 E("ldlat-loads", "cpu/mem-loads,ldlat=30/P", "mem-loads"),
16 E("ldlat-stores", "cpu/mem-stores/P", "mem-stores"),
Jiri Olsaacbe6132016-02-15 09:34:34 +010017};
Jiri Olsa54fbad52016-02-24 09:46:42 +010018#undef E
Jiri Olsaacbe6132016-02-15 09:34:34 +010019
20#undef E
Jiri Olsace1e22b2016-02-15 09:34:35 +010021
22int 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 Olsa54fbad52016-02-24 09:46:42 +010057
58int 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}