cpu_buffer.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @file cpu_buffer.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. *
  9. * Each CPU has a local buffer that stores PC value/event
  10. * pairs. We also log context switches when we notice them.
  11. * Eventually each CPU's buffer is processed into the global
  12. * event buffer by sync_buffer().
  13. *
  14. * We use a local buffer for two reasons: an NMI or similar
  15. * interrupt cannot synchronise, and high sampling rates
  16. * would lead to catastrophic global synchronisation if
  17. * a global buffer was used.
  18. */
  19. #include <linux/sched.h>
  20. #include <linux/oprofile.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/errno.h>
  23. #include "event_buffer.h"
  24. #include "cpu_buffer.h"
  25. #include "buffer_sync.h"
  26. #include "oprof.h"
  27. DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
  28. static void wq_sync_buffer(struct work_struct *work);
  29. #define DEFAULT_TIMER_EXPIRE (HZ / 10)
  30. static int work_enabled;
  31. void free_cpu_buffers(void)
  32. {
  33. int i;
  34. for_each_online_cpu(i) {
  35. vfree(per_cpu(cpu_buffer, i).buffer);
  36. per_cpu(cpu_buffer, i).buffer = NULL;
  37. }
  38. }
  39. int alloc_cpu_buffers(void)
  40. {
  41. int i;
  42. unsigned long buffer_size = fs_cpu_buffer_size;
  43. for_each_online_cpu(i) {
  44. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  45. b->buffer = vmalloc_node(sizeof(struct op_sample) * buffer_size,
  46. cpu_to_node(i));
  47. if (!b->buffer)
  48. goto fail;
  49. b->last_task = NULL;
  50. b->last_is_kernel = -1;
  51. b->tracing = 0;
  52. b->buffer_size = buffer_size;
  53. b->tail_pos = 0;
  54. b->head_pos = 0;
  55. b->sample_received = 0;
  56. b->sample_lost_overflow = 0;
  57. b->backtrace_aborted = 0;
  58. b->sample_invalid_eip = 0;
  59. b->cpu = i;
  60. INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
  61. }
  62. return 0;
  63. fail:
  64. free_cpu_buffers();
  65. return -ENOMEM;
  66. }
  67. void start_cpu_work(void)
  68. {
  69. int i;
  70. work_enabled = 1;
  71. for_each_online_cpu(i) {
  72. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  73. /*
  74. * Spread the work by 1 jiffy per cpu so they dont all
  75. * fire at once.
  76. */
  77. schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
  78. }
  79. }
  80. void end_cpu_work(void)
  81. {
  82. int i;
  83. work_enabled = 0;
  84. for_each_online_cpu(i) {
  85. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  86. cancel_delayed_work(&b->work);
  87. }
  88. flush_scheduled_work();
  89. }
  90. /* Resets the cpu buffer to a sane state. */
  91. void cpu_buffer_reset(struct oprofile_cpu_buffer * cpu_buf)
  92. {
  93. /* reset these to invalid values; the next sample
  94. * collected will populate the buffer with proper
  95. * values to initialize the buffer
  96. */
  97. cpu_buf->last_is_kernel = -1;
  98. cpu_buf->last_task = NULL;
  99. }
  100. /* compute number of available slots in cpu_buffer queue */
  101. static unsigned long nr_available_slots(struct oprofile_cpu_buffer const * b)
  102. {
  103. unsigned long head = b->head_pos;
  104. unsigned long tail = b->tail_pos;
  105. if (tail > head)
  106. return (tail - head) - 1;
  107. return tail + (b->buffer_size - head) - 1;
  108. }
  109. static void increment_head(struct oprofile_cpu_buffer * b)
  110. {
  111. unsigned long new_head = b->head_pos + 1;
  112. /* Ensure anything written to the slot before we
  113. * increment is visible */
  114. wmb();
  115. if (new_head < b->buffer_size)
  116. b->head_pos = new_head;
  117. else
  118. b->head_pos = 0;
  119. }
  120. static inline void
  121. add_sample(struct oprofile_cpu_buffer * cpu_buf,
  122. unsigned long pc, unsigned long event)
  123. {
  124. struct op_sample * entry = &cpu_buf->buffer[cpu_buf->head_pos];
  125. entry->eip = pc;
  126. entry->event = event;
  127. increment_head(cpu_buf);
  128. }
  129. static inline void
  130. add_code(struct oprofile_cpu_buffer * buffer, unsigned long value)
  131. {
  132. add_sample(buffer, ESCAPE_CODE, value);
  133. }
  134. /* This must be safe from any context. It's safe writing here
  135. * because of the head/tail separation of the writer and reader
  136. * of the CPU buffer.
  137. *
  138. * is_kernel is needed because on some architectures you cannot
  139. * tell if you are in kernel or user space simply by looking at
  140. * pc. We tag this in the buffer by generating kernel enter/exit
  141. * events whenever is_kernel changes
  142. */
  143. static int log_sample(struct oprofile_cpu_buffer * cpu_buf, unsigned long pc,
  144. int is_kernel, unsigned long event)
  145. {
  146. struct task_struct * task;
  147. cpu_buf->sample_received++;
  148. if (pc == ESCAPE_CODE) {
  149. cpu_buf->sample_invalid_eip++;
  150. return 0;
  151. }
  152. if (nr_available_slots(cpu_buf) < 3) {
  153. cpu_buf->sample_lost_overflow++;
  154. return 0;
  155. }
  156. is_kernel = !!is_kernel;
  157. task = current;
  158. /* notice a switch from user->kernel or vice versa */
  159. if (cpu_buf->last_is_kernel != is_kernel) {
  160. cpu_buf->last_is_kernel = is_kernel;
  161. add_code(cpu_buf, is_kernel);
  162. }
  163. /* notice a task switch */
  164. if (cpu_buf->last_task != task) {
  165. cpu_buf->last_task = task;
  166. add_code(cpu_buf, (unsigned long)task);
  167. }
  168. add_sample(cpu_buf, pc, event);
  169. return 1;
  170. }
  171. static int oprofile_begin_trace(struct oprofile_cpu_buffer * cpu_buf)
  172. {
  173. if (nr_available_slots(cpu_buf) < 4) {
  174. cpu_buf->sample_lost_overflow++;
  175. return 0;
  176. }
  177. add_code(cpu_buf, CPU_TRACE_BEGIN);
  178. cpu_buf->tracing = 1;
  179. return 1;
  180. }
  181. static void oprofile_end_trace(struct oprofile_cpu_buffer * cpu_buf)
  182. {
  183. cpu_buf->tracing = 0;
  184. }
  185. void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
  186. unsigned long event, int is_kernel)
  187. {
  188. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  189. if (!backtrace_depth) {
  190. log_sample(cpu_buf, pc, is_kernel, event);
  191. return;
  192. }
  193. if (!oprofile_begin_trace(cpu_buf))
  194. return;
  195. /* if log_sample() fail we can't backtrace since we lost the source
  196. * of this event */
  197. if (log_sample(cpu_buf, pc, is_kernel, event))
  198. oprofile_ops.backtrace(regs, backtrace_depth);
  199. oprofile_end_trace(cpu_buf);
  200. }
  201. void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
  202. {
  203. int is_kernel = !user_mode(regs);
  204. unsigned long pc = profile_pc(regs);
  205. oprofile_add_ext_sample(pc, regs, event, is_kernel);
  206. }
  207. void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
  208. {
  209. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  210. log_sample(cpu_buf, pc, is_kernel, event);
  211. }
  212. void oprofile_add_trace(unsigned long pc)
  213. {
  214. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  215. if (!cpu_buf->tracing)
  216. return;
  217. if (nr_available_slots(cpu_buf) < 1) {
  218. cpu_buf->tracing = 0;
  219. cpu_buf->sample_lost_overflow++;
  220. return;
  221. }
  222. /* broken frame can give an eip with the same value as an escape code,
  223. * abort the trace if we get it */
  224. if (pc == ESCAPE_CODE) {
  225. cpu_buf->tracing = 0;
  226. cpu_buf->backtrace_aborted++;
  227. return;
  228. }
  229. add_sample(cpu_buf, pc, 0);
  230. }
  231. /*
  232. * This serves to avoid cpu buffer overflow, and makes sure
  233. * the task mortuary progresses
  234. *
  235. * By using schedule_delayed_work_on and then schedule_delayed_work
  236. * we guarantee this will stay on the correct cpu
  237. */
  238. static void wq_sync_buffer(struct work_struct *work)
  239. {
  240. struct oprofile_cpu_buffer * b =
  241. container_of(work, struct oprofile_cpu_buffer, work.work);
  242. if (b->cpu != smp_processor_id()) {
  243. printk("WQ on CPU%d, prefer CPU%d\n",
  244. smp_processor_id(), b->cpu);
  245. }
  246. sync_buffer(b->cpu);
  247. /* don't re-add the work if we're shutting down */
  248. if (work_enabled)
  249. schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
  250. }