cpu_buffer.c 6.9 KB

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