cpu_buffer.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 OP_BUFFER_FLAGS 0
  29. /*
  30. * Read and write access is using spin locking. Thus, writing to the
  31. * buffer by NMI handler (x86) could occur also during critical
  32. * sections when reading the buffer. To avoid this, there are 2
  33. * buffers for independent read and write access. Read access is in
  34. * process context only, write access only in the NMI handler. If the
  35. * read buffer runs empty, both buffers are swapped atomically. There
  36. * is potentially a small window during swapping where the buffers are
  37. * disabled and samples could be lost.
  38. *
  39. * Using 2 buffers is a little bit overhead, but the solution is clear
  40. * and does not require changes in the ring buffer implementation. It
  41. * can be changed to a single buffer solution when the ring buffer
  42. * access is implemented as non-locking atomic code.
  43. */
  44. static struct ring_buffer *op_ring_buffer_read;
  45. static struct ring_buffer *op_ring_buffer_write;
  46. DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
  47. static void wq_sync_buffer(struct work_struct *work);
  48. #define DEFAULT_TIMER_EXPIRE (HZ / 10)
  49. static int work_enabled;
  50. unsigned long oprofile_get_cpu_buffer_size(void)
  51. {
  52. return oprofile_cpu_buffer_size;
  53. }
  54. void oprofile_cpu_buffer_inc_smpl_lost(void)
  55. {
  56. struct oprofile_cpu_buffer *cpu_buf
  57. = &__get_cpu_var(cpu_buffer);
  58. cpu_buf->sample_lost_overflow++;
  59. }
  60. void free_cpu_buffers(void)
  61. {
  62. if (op_ring_buffer_read)
  63. ring_buffer_free(op_ring_buffer_read);
  64. op_ring_buffer_read = NULL;
  65. if (op_ring_buffer_write)
  66. ring_buffer_free(op_ring_buffer_write);
  67. op_ring_buffer_write = NULL;
  68. }
  69. int alloc_cpu_buffers(void)
  70. {
  71. int i;
  72. unsigned long buffer_size = oprofile_cpu_buffer_size;
  73. op_ring_buffer_read = ring_buffer_alloc(buffer_size, OP_BUFFER_FLAGS);
  74. if (!op_ring_buffer_read)
  75. goto fail;
  76. op_ring_buffer_write = ring_buffer_alloc(buffer_size, OP_BUFFER_FLAGS);
  77. if (!op_ring_buffer_write)
  78. goto fail;
  79. for_each_possible_cpu(i) {
  80. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  81. b->last_task = NULL;
  82. b->last_is_kernel = -1;
  83. b->tracing = 0;
  84. b->buffer_size = buffer_size;
  85. b->sample_received = 0;
  86. b->sample_lost_overflow = 0;
  87. b->backtrace_aborted = 0;
  88. b->sample_invalid_eip = 0;
  89. b->cpu = i;
  90. INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
  91. }
  92. return 0;
  93. fail:
  94. free_cpu_buffers();
  95. return -ENOMEM;
  96. }
  97. void start_cpu_work(void)
  98. {
  99. int i;
  100. work_enabled = 1;
  101. for_each_online_cpu(i) {
  102. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  103. /*
  104. * Spread the work by 1 jiffy per cpu so they dont all
  105. * fire at once.
  106. */
  107. schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
  108. }
  109. }
  110. void end_cpu_work(void)
  111. {
  112. int i;
  113. work_enabled = 0;
  114. for_each_online_cpu(i) {
  115. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  116. cancel_delayed_work(&b->work);
  117. }
  118. flush_scheduled_work();
  119. }
  120. int op_cpu_buffer_write_entry(struct op_entry *entry)
  121. {
  122. entry->event = ring_buffer_lock_reserve(op_ring_buffer_write,
  123. sizeof(struct op_sample),
  124. &entry->irq_flags);
  125. if (entry->event)
  126. entry->sample = ring_buffer_event_data(entry->event);
  127. else
  128. entry->sample = NULL;
  129. if (!entry->sample)
  130. return -ENOMEM;
  131. return 0;
  132. }
  133. int op_cpu_buffer_write_commit(struct op_entry *entry)
  134. {
  135. return ring_buffer_unlock_commit(op_ring_buffer_write, entry->event,
  136. entry->irq_flags);
  137. }
  138. struct op_sample *op_cpu_buffer_read_entry(int cpu)
  139. {
  140. struct ring_buffer_event *e;
  141. e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
  142. if (e)
  143. return ring_buffer_event_data(e);
  144. if (ring_buffer_swap_cpu(op_ring_buffer_read,
  145. op_ring_buffer_write,
  146. cpu))
  147. return NULL;
  148. e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
  149. if (e)
  150. return ring_buffer_event_data(e);
  151. return NULL;
  152. }
  153. unsigned long op_cpu_buffer_entries(int cpu)
  154. {
  155. return ring_buffer_entries_cpu(op_ring_buffer_read, cpu)
  156. + ring_buffer_entries_cpu(op_ring_buffer_write, cpu);
  157. }
  158. static inline int
  159. add_sample(struct oprofile_cpu_buffer *cpu_buf,
  160. unsigned long pc, unsigned long event)
  161. {
  162. struct op_entry entry;
  163. int ret;
  164. ret = op_cpu_buffer_write_entry(&entry);
  165. if (ret)
  166. return ret;
  167. entry.sample->eip = pc;
  168. entry.sample->event = event;
  169. return op_cpu_buffer_write_commit(&entry);
  170. }
  171. static inline int
  172. add_code(struct oprofile_cpu_buffer *buffer, unsigned long value)
  173. {
  174. return add_sample(buffer, ESCAPE_CODE, value);
  175. }
  176. /* This must be safe from any context. It's safe writing here
  177. * because of the head/tail separation of the writer and reader
  178. * of the CPU buffer.
  179. *
  180. * is_kernel is needed because on some architectures you cannot
  181. * tell if you are in kernel or user space simply by looking at
  182. * pc. We tag this in the buffer by generating kernel enter/exit
  183. * events whenever is_kernel changes
  184. */
  185. static int log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
  186. int is_kernel, unsigned long event)
  187. {
  188. struct task_struct *task;
  189. cpu_buf->sample_received++;
  190. if (pc == ESCAPE_CODE) {
  191. cpu_buf->sample_invalid_eip++;
  192. return 0;
  193. }
  194. is_kernel = !!is_kernel;
  195. task = current;
  196. /* notice a switch from user->kernel or vice versa */
  197. if (cpu_buf->last_is_kernel != is_kernel) {
  198. cpu_buf->last_is_kernel = is_kernel;
  199. if (add_code(cpu_buf, is_kernel))
  200. goto fail;
  201. }
  202. /* notice a task switch */
  203. if (cpu_buf->last_task != task) {
  204. cpu_buf->last_task = task;
  205. if (add_code(cpu_buf, (unsigned long)task))
  206. goto fail;
  207. }
  208. if (add_sample(cpu_buf, pc, event))
  209. goto fail;
  210. return 1;
  211. fail:
  212. cpu_buf->sample_lost_overflow++;
  213. return 0;
  214. }
  215. static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
  216. {
  217. add_code(cpu_buf, CPU_TRACE_BEGIN);
  218. cpu_buf->tracing = 1;
  219. }
  220. static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
  221. {
  222. cpu_buf->tracing = 0;
  223. }
  224. static inline void
  225. __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
  226. unsigned long event, int is_kernel)
  227. {
  228. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  229. if (!oprofile_backtrace_depth) {
  230. log_sample(cpu_buf, pc, is_kernel, event);
  231. return;
  232. }
  233. oprofile_begin_trace(cpu_buf);
  234. /*
  235. * if log_sample() fail we can't backtrace since we lost the
  236. * source of this event
  237. */
  238. if (log_sample(cpu_buf, pc, is_kernel, event))
  239. oprofile_ops.backtrace(regs, oprofile_backtrace_depth);
  240. oprofile_end_trace(cpu_buf);
  241. }
  242. void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
  243. unsigned long event, int is_kernel)
  244. {
  245. __oprofile_add_ext_sample(pc, regs, event, is_kernel);
  246. }
  247. void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
  248. {
  249. int is_kernel = !user_mode(regs);
  250. unsigned long pc = profile_pc(regs);
  251. __oprofile_add_ext_sample(pc, regs, event, is_kernel);
  252. }
  253. #ifdef CONFIG_OPROFILE_IBS
  254. void oprofile_add_ibs_sample(struct pt_regs * const regs,
  255. unsigned int * const ibs_sample, int ibs_code)
  256. {
  257. int is_kernel = !user_mode(regs);
  258. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  259. struct task_struct *task;
  260. int fail = 0;
  261. cpu_buf->sample_received++;
  262. /* notice a switch from user->kernel or vice versa */
  263. if (cpu_buf->last_is_kernel != is_kernel) {
  264. if (add_code(cpu_buf, is_kernel))
  265. goto fail;
  266. cpu_buf->last_is_kernel = is_kernel;
  267. }
  268. /* notice a task switch */
  269. if (!is_kernel) {
  270. task = current;
  271. if (cpu_buf->last_task != task) {
  272. if (add_code(cpu_buf, (unsigned long)task))
  273. goto fail;
  274. cpu_buf->last_task = task;
  275. }
  276. }
  277. fail = fail || add_code(cpu_buf, ibs_code);
  278. fail = fail || add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]);
  279. fail = fail || add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]);
  280. fail = fail || add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]);
  281. if (ibs_code == IBS_OP_BEGIN) {
  282. fail = fail || add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]);
  283. fail = fail || add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]);
  284. fail = fail || add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]);
  285. }
  286. if (!fail)
  287. return;
  288. fail:
  289. cpu_buf->sample_lost_overflow++;
  290. }
  291. #endif
  292. void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
  293. {
  294. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  295. log_sample(cpu_buf, pc, is_kernel, event);
  296. }
  297. void oprofile_add_trace(unsigned long pc)
  298. {
  299. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  300. if (!cpu_buf->tracing)
  301. return;
  302. /*
  303. * broken frame can give an eip with the same value as an
  304. * escape code, abort the trace if we get it
  305. */
  306. if (pc == ESCAPE_CODE)
  307. goto fail;
  308. if (add_sample(cpu_buf, pc, 0))
  309. goto fail;
  310. return;
  311. fail:
  312. cpu_buf->tracing = 0;
  313. cpu_buf->backtrace_aborted++;
  314. return;
  315. }
  316. /*
  317. * This serves to avoid cpu buffer overflow, and makes sure
  318. * the task mortuary progresses
  319. *
  320. * By using schedule_delayed_work_on and then schedule_delayed_work
  321. * we guarantee this will stay on the correct cpu
  322. */
  323. static void wq_sync_buffer(struct work_struct *work)
  324. {
  325. struct oprofile_cpu_buffer *b =
  326. container_of(work, struct oprofile_cpu_buffer, work.work);
  327. if (b->cpu != smp_processor_id()) {
  328. printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
  329. smp_processor_id(), b->cpu);
  330. if (!cpu_online(b->cpu)) {
  331. cancel_delayed_work(&b->work);
  332. return;
  333. }
  334. }
  335. sync_buffer(b->cpu);
  336. /* don't re-add the work if we're shutting down */
  337. if (work_enabled)
  338. schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
  339. }