perf.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef _PERF_PERF_H
  2. #define _PERF_PERF_H
  3. struct winsize;
  4. void get_term_dimensions(struct winsize *ws);
  5. #if defined(__i386__)
  6. #include "../../arch/x86/include/asm/unistd.h"
  7. #define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
  8. #define cpu_relax() asm volatile("rep; nop" ::: "memory");
  9. #endif
  10. #if defined(__x86_64__)
  11. #include "../../arch/x86/include/asm/unistd.h"
  12. #define rmb() asm volatile("lfence" ::: "memory")
  13. #define cpu_relax() asm volatile("rep; nop" ::: "memory");
  14. #endif
  15. #ifdef __powerpc__
  16. #include "../../arch/powerpc/include/asm/unistd.h"
  17. #define rmb() asm volatile ("sync" ::: "memory")
  18. #define cpu_relax() asm volatile ("" ::: "memory");
  19. #endif
  20. #ifdef __s390__
  21. #include "../../arch/s390/include/asm/unistd.h"
  22. #define rmb() asm volatile("bcr 15,0" ::: "memory")
  23. #define cpu_relax() asm volatile("" ::: "memory");
  24. #endif
  25. #ifdef __sh__
  26. #include "../../arch/sh/include/asm/unistd.h"
  27. #if defined(__SH4A__) || defined(__SH5__)
  28. # define rmb() asm volatile("synco" ::: "memory")
  29. #else
  30. # define rmb() asm volatile("" ::: "memory")
  31. #endif
  32. #define cpu_relax() asm volatile("" ::: "memory")
  33. #endif
  34. #ifdef __hppa__
  35. #include "../../arch/parisc/include/asm/unistd.h"
  36. #define rmb() asm volatile("" ::: "memory")
  37. #define cpu_relax() asm volatile("" ::: "memory");
  38. #endif
  39. #ifdef __sparc__
  40. #include "../../arch/sparc/include/asm/unistd.h"
  41. #define rmb() asm volatile("":::"memory")
  42. #define cpu_relax() asm volatile("":::"memory")
  43. #endif
  44. #ifdef __alpha__
  45. #include "../../arch/alpha/include/asm/unistd.h"
  46. #define rmb() asm volatile("mb" ::: "memory")
  47. #define cpu_relax() asm volatile("" ::: "memory")
  48. #endif
  49. #ifdef __ia64__
  50. #include "../../arch/ia64/include/asm/unistd.h"
  51. #define rmb() asm volatile ("mf" ::: "memory")
  52. #define cpu_relax() asm volatile ("hint @pause" ::: "memory")
  53. #endif
  54. #ifdef __arm__
  55. #include "../../arch/arm/include/asm/unistd.h"
  56. /*
  57. * Use the __kuser_memory_barrier helper in the CPU helper page. See
  58. * arch/arm/kernel/entry-armv.S in the kernel source for details.
  59. */
  60. #define rmb() ((void(*)(void))0xffff0fa0)()
  61. #define cpu_relax() asm volatile("":::"memory")
  62. #endif
  63. #ifdef __mips__
  64. #include "../../arch/mips/include/asm/unistd.h"
  65. #define rmb() asm volatile( \
  66. ".set mips2\n\t" \
  67. "sync\n\t" \
  68. ".set mips0" \
  69. : /* no output */ \
  70. : /* no input */ \
  71. : "memory")
  72. #define cpu_relax() asm volatile("" ::: "memory")
  73. #endif
  74. #include <time.h>
  75. #include <unistd.h>
  76. #include <sys/types.h>
  77. #include <sys/syscall.h>
  78. #include "../../include/linux/perf_event.h"
  79. #include "util/types.h"
  80. #include <stdbool.h>
  81. struct perf_mmap {
  82. void *base;
  83. int mask;
  84. unsigned int prev;
  85. };
  86. static inline unsigned int perf_mmap__read_head(struct perf_mmap *mm)
  87. {
  88. struct perf_event_mmap_page *pc = mm->base;
  89. int head = pc->data_head;
  90. rmb();
  91. return head;
  92. }
  93. static inline void perf_mmap__write_tail(struct perf_mmap *md,
  94. unsigned long tail)
  95. {
  96. struct perf_event_mmap_page *pc = md->base;
  97. /*
  98. * ensure all reads are done before we write the tail out.
  99. */
  100. /* mb(); */
  101. pc->data_tail = tail;
  102. }
  103. /*
  104. * prctl(PR_TASK_PERF_EVENTS_DISABLE) will (cheaply) disable all
  105. * counters in the current task.
  106. */
  107. #define PR_TASK_PERF_EVENTS_DISABLE 31
  108. #define PR_TASK_PERF_EVENTS_ENABLE 32
  109. #ifndef NSEC_PER_SEC
  110. # define NSEC_PER_SEC 1000000000ULL
  111. #endif
  112. static inline unsigned long long rdclock(void)
  113. {
  114. struct timespec ts;
  115. clock_gettime(CLOCK_MONOTONIC, &ts);
  116. return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  117. }
  118. /*
  119. * Pick up some kernel type conventions:
  120. */
  121. #define __user
  122. #define asmlinkage
  123. #define unlikely(x) __builtin_expect(!!(x), 0)
  124. #define min(x, y) ({ \
  125. typeof(x) _min1 = (x); \
  126. typeof(y) _min2 = (y); \
  127. (void) (&_min1 == &_min2); \
  128. _min1 < _min2 ? _min1 : _min2; })
  129. static inline int
  130. sys_perf_event_open(struct perf_event_attr *attr,
  131. pid_t pid, int cpu, int group_fd,
  132. unsigned long flags)
  133. {
  134. attr->size = sizeof(*attr);
  135. return syscall(__NR_perf_event_open, attr, pid, cpu,
  136. group_fd, flags);
  137. }
  138. #define MAX_COUNTERS 256
  139. #define MAX_NR_CPUS 256
  140. struct ip_callchain {
  141. u64 nr;
  142. u64 ips[0];
  143. };
  144. extern bool perf_host, perf_guest;
  145. #endif