cpu_buffer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. struct oprofile_cpu_buffer cpu_buffer[NR_CPUS] __cacheline_aligned;
  28. static void wq_sync_buffer(void *);
  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(cpu_buffer[i].buffer);
  36. }
  37. int alloc_cpu_buffers(void)
  38. {
  39. int i;
  40. unsigned long buffer_size = fs_cpu_buffer_size;
  41. for_each_online_cpu(i) {
  42. struct oprofile_cpu_buffer * b = &cpu_buffer[i];
  43. b->buffer = vmalloc_node(sizeof(struct op_sample) * buffer_size,
  44. cpu_to_node(i));
  45. if (!b->buffer)
  46. goto fail;
  47. b->last_task = NULL;
  48. b->last_is_kernel = -1;
  49. b->tracing = 0;
  50. b->buffer_size = buffer_size;
  51. b->tail_pos = 0;
  52. b->head_pos = 0;
  53. b->sample_received = 0;
  54. b->sample_lost_overflow = 0;
  55. b->cpu = i;
  56. INIT_WORK(&b->work, wq_sync_buffer, b);
  57. }
  58. return 0;
  59. fail:
  60. free_cpu_buffers();
  61. return -ENOMEM;
  62. }
  63. void start_cpu_work(void)
  64. {
  65. int i;
  66. work_enabled = 1;
  67. for_each_online_cpu(i) {
  68. struct oprofile_cpu_buffer * b = &cpu_buffer[i];
  69. /*
  70. * Spread the work by 1 jiffy per cpu so they dont all
  71. * fire at once.
  72. */
  73. schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
  74. }
  75. }
  76. void end_cpu_work(void)
  77. {
  78. int i;
  79. work_enabled = 0;
  80. for_each_online_cpu(i) {
  81. struct oprofile_cpu_buffer * b = &cpu_buffer[i];
  82. cancel_delayed_work(&b->work);
  83. }
  84. flush_scheduled_work();
  85. }
  86. /* Resets the cpu buffer to a sane state. */
  87. void cpu_buffer_reset(struct oprofile_cpu_buffer * cpu_buf)
  88. {
  89. /* reset these to invalid values; the next sample
  90. * collected will populate the buffer with proper
  91. * values to initialize the buffer
  92. */
  93. cpu_buf->last_is_kernel = -1;
  94. cpu_buf->last_task = NULL;
  95. }
  96. /* compute number of available slots in cpu_buffer queue */
  97. static unsigned long nr_available_slots(struct oprofile_cpu_buffer const * b)
  98. {
  99. unsigned long head = b->head_pos;
  100. unsigned long tail = b->tail_pos;
  101. if (tail > head)
  102. return (tail - head) - 1;
  103. return tail + (b->buffer_size - head) - 1;
  104. }
  105. static void increment_head(struct oprofile_cpu_buffer * b)
  106. {
  107. unsigned long new_head = b->head_pos + 1;
  108. /* Ensure anything written to the slot before we
  109. * increment is visible */
  110. wmb();
  111. if (new_head < b->buffer_size)
  112. b->head_pos = new_head;
  113. else
  114. b->head_pos = 0;
  115. }
  116. static inline void
  117. add_sample(struct oprofile_cpu_buffer * cpu_buf,
  118. unsigned long pc, unsigned long event)
  119. {
  120. struct op_sample * entry = &cpu_buf->buffer[cpu_buf->head_pos];
  121. entry->eip = pc;
  122. entry->event = event;
  123. increment_head(cpu_buf);
  124. }
  125. static inline void
  126. add_code(struct oprofile_cpu_buffer * buffer, unsigned long value)
  127. {
  128. add_sample(buffer, ESCAPE_CODE, value);
  129. }
  130. /* This must be safe from any context. It's safe writing here
  131. * because of the head/tail separation of the writer and reader
  132. * of the CPU buffer.
  133. *
  134. * is_kernel is needed because on some architectures you cannot
  135. * tell if you are in kernel or user space simply by looking at
  136. * pc. We tag this in the buffer by generating kernel enter/exit
  137. * events whenever is_kernel changes
  138. */
  139. static int log_sample(struct oprofile_cpu_buffer * cpu_buf, unsigned long pc,
  140. int is_kernel, unsigned long event)
  141. {
  142. struct task_struct * task;
  143. cpu_buf->sample_received++;
  144. if (nr_available_slots(cpu_buf) < 3) {
  145. cpu_buf->sample_lost_overflow++;
  146. return 0;
  147. }
  148. is_kernel = !!is_kernel;
  149. task = current;
  150. /* notice a switch from user->kernel or vice versa */
  151. if (cpu_buf->last_is_kernel != is_kernel) {
  152. cpu_buf->last_is_kernel = is_kernel;
  153. add_code(cpu_buf, is_kernel);
  154. }
  155. /* notice a task switch */
  156. if (cpu_buf->last_task != task) {
  157. cpu_buf->last_task = task;
  158. add_code(cpu_buf, (unsigned long)task);
  159. }
  160. add_sample(cpu_buf, pc, event);
  161. return 1;
  162. }
  163. static int oprofile_begin_trace(struct oprofile_cpu_buffer * cpu_buf)
  164. {
  165. if (nr_available_slots(cpu_buf) < 4) {
  166. cpu_buf->sample_lost_overflow++;
  167. return 0;
  168. }
  169. add_code(cpu_buf, CPU_TRACE_BEGIN);
  170. cpu_buf->tracing = 1;
  171. return 1;
  172. }
  173. static void oprofile_end_trace(struct oprofile_cpu_buffer * cpu_buf)
  174. {
  175. cpu_buf->tracing = 0;
  176. }
  177. void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
  178. unsigned long event, int is_kernel)
  179. {
  180. struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];
  181. if (!backtrace_depth) {
  182. log_sample(cpu_buf, pc, is_kernel, event);
  183. return;
  184. }
  185. if (!oprofile_begin_trace(cpu_buf))
  186. return;
  187. /* if log_sample() fail we can't backtrace since we lost the source
  188. * of this event */
  189. if (log_sample(cpu_buf, pc, is_kernel, event))
  190. oprofile_ops.backtrace(regs, backtrace_depth);
  191. oprofile_end_trace(cpu_buf);
  192. }
  193. void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
  194. {
  195. int is_kernel = !user_mode(regs);
  196. unsigned long pc = profile_pc(regs);
  197. oprofile_add_ext_sample(pc, regs, event, is_kernel);
  198. }
  199. void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
  200. {
  201. struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];
  202. log_sample(cpu_buf, pc, is_kernel, event);
  203. }
  204. void oprofile_add_trace(unsigned long pc)
  205. {
  206. struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];
  207. if (!cpu_buf->tracing)
  208. return;
  209. if (nr_available_slots(cpu_buf) < 1) {
  210. cpu_buf->tracing = 0;
  211. cpu_buf->sample_lost_overflow++;
  212. return;
  213. }
  214. /* broken frame can give an eip with the same value as an escape code,
  215. * abort the trace if we get it */
  216. if (pc == ESCAPE_CODE) {
  217. cpu_buf->tracing = 0;
  218. cpu_buf->backtrace_aborted++;
  219. return;
  220. }
  221. add_sample(cpu_buf, pc, 0);
  222. }
  223. /*
  224. * This serves to avoid cpu buffer overflow, and makes sure
  225. * the task mortuary progresses
  226. *
  227. * By using schedule_delayed_work_on and then schedule_delayed_work
  228. * we guarantee this will stay on the correct cpu
  229. */
  230. static void wq_sync_buffer(void * data)
  231. {
  232. struct oprofile_cpu_buffer * b = data;
  233. if (b->cpu != smp_processor_id()) {
  234. printk("WQ on CPU%d, prefer CPU%d\n",
  235. smp_processor_id(), b->cpu);
  236. }
  237. sync_buffer(b->cpu);
  238. /* don't re-add the work if we're shutting down */
  239. if (work_enabled)
  240. schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
  241. }