event.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2013, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  4. */
  5. #define _GNU_SOURCE
  6. #include <unistd.h>
  7. #include <sys/syscall.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <sys/ioctl.h>
  11. #include "event.h"
  12. int perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu,
  13. int group_fd, unsigned long flags)
  14. {
  15. return syscall(__NR_perf_event_open, attr, pid, cpu,
  16. group_fd, flags);
  17. }
  18. void event_init_opts(struct event *e, u64 config, int type, char *name)
  19. {
  20. memset(e, 0, sizeof(*e));
  21. e->name = name;
  22. e->attr.type = type;
  23. e->attr.config = config;
  24. e->attr.size = sizeof(e->attr);
  25. /* This has to match the structure layout in the header */
  26. e->attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | \
  27. PERF_FORMAT_TOTAL_TIME_RUNNING;
  28. }
  29. void event_init_named(struct event *e, u64 config, char *name)
  30. {
  31. event_init_opts(e, config, PERF_TYPE_RAW, name);
  32. }
  33. void event_init(struct event *e, u64 config)
  34. {
  35. event_init_opts(e, config, PERF_TYPE_RAW, "event");
  36. }
  37. #define PERF_CURRENT_PID 0
  38. #define PERF_NO_PID -1
  39. #define PERF_NO_CPU -1
  40. #define PERF_NO_GROUP -1
  41. int event_open_with_options(struct event *e, pid_t pid, int cpu, int group_fd)
  42. {
  43. e->fd = perf_event_open(&e->attr, pid, cpu, group_fd, 0);
  44. if (e->fd == -1) {
  45. perror("perf_event_open");
  46. return -1;
  47. }
  48. return 0;
  49. }
  50. int event_open_with_group(struct event *e, int group_fd)
  51. {
  52. return event_open_with_options(e, PERF_CURRENT_PID, PERF_NO_CPU, group_fd);
  53. }
  54. int event_open_with_pid(struct event *e, pid_t pid)
  55. {
  56. return event_open_with_options(e, pid, PERF_NO_CPU, PERF_NO_GROUP);
  57. }
  58. int event_open_with_cpu(struct event *e, int cpu)
  59. {
  60. return event_open_with_options(e, PERF_NO_PID, cpu, PERF_NO_GROUP);
  61. }
  62. int event_open(struct event *e)
  63. {
  64. return event_open_with_options(e, PERF_CURRENT_PID, PERF_NO_CPU, PERF_NO_GROUP);
  65. }
  66. void event_close(struct event *e)
  67. {
  68. close(e->fd);
  69. }
  70. int event_enable(struct event *e)
  71. {
  72. return ioctl(e->fd, PERF_EVENT_IOC_ENABLE);
  73. }
  74. int event_disable(struct event *e)
  75. {
  76. return ioctl(e->fd, PERF_EVENT_IOC_DISABLE);
  77. }
  78. int event_reset(struct event *e)
  79. {
  80. return ioctl(e->fd, PERF_EVENT_IOC_RESET);
  81. }
  82. int event_read(struct event *e)
  83. {
  84. int rc;
  85. rc = read(e->fd, &e->result, sizeof(e->result));
  86. if (rc != sizeof(e->result)) {
  87. fprintf(stderr, "read error on event %p!\n", e);
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. void event_report_justified(struct event *e, int name_width, int result_width)
  93. {
  94. printf("%*s: result %*llu ", name_width, e->name, result_width,
  95. e->result.value);
  96. if (e->result.running == e->result.enabled)
  97. printf("running/enabled %llu\n", e->result.running);
  98. else
  99. printf("running %llu enabled %llu\n", e->result.running,
  100. e->result.enabled);
  101. }
  102. void event_report(struct event *e)
  103. {
  104. event_report_justified(e, 0, 0);
  105. }