cpu_buffer.c 11 KB

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