mem-events.c 1.5 KB

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