cpu_buffer.c 8.6 KB

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