cpu_buffer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. #define RB_EVENT_HDR_SIZE 4
  71. int alloc_cpu_buffers(void)
  72. {
  73. int i;
  74. unsigned long buffer_size = oprofile_cpu_buffer_size;
  75. unsigned long byte_size = buffer_size * (sizeof(struct op_sample) +
  76. RB_EVENT_HDR_SIZE);
  77. op_ring_buffer_read = ring_buffer_alloc(byte_size, OP_BUFFER_FLAGS);
  78. if (!op_ring_buffer_read)
  79. goto fail;
  80. op_ring_buffer_write = ring_buffer_alloc(byte_size, OP_BUFFER_FLAGS);
  81. if (!op_ring_buffer_write)
  82. goto fail;
  83. for_each_possible_cpu(i) {
  84. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  85. b->last_task = NULL;
  86. b->last_is_kernel = -1;
  87. b->tracing = 0;
  88. b->buffer_size = buffer_size;
  89. b->sample_received = 0;
  90. b->sample_lost_overflow = 0;
  91. b->backtrace_aborted = 0;
  92. b->sample_invalid_eip = 0;
  93. b->cpu = i;
  94. INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
  95. }
  96. return 0;
  97. fail:
  98. free_cpu_buffers();
  99. return -ENOMEM;
  100. }
  101. void start_cpu_work(void)
  102. {
  103. int i;
  104. work_enabled = 1;
  105. for_each_online_cpu(i) {
  106. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  107. /*
  108. * Spread the work by 1 jiffy per cpu so they dont all
  109. * fire at once.
  110. */
  111. schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
  112. }
  113. }
  114. void end_cpu_work(void)
  115. {
  116. int i;
  117. work_enabled = 0;
  118. for_each_online_cpu(i) {
  119. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  120. cancel_delayed_work(&b->work);
  121. }
  122. flush_scheduled_work();
  123. }
  124. /*
  125. * This function prepares the cpu buffer to write a sample.
  126. *
  127. * Struct op_entry is used during operations on the ring buffer while
  128. * struct op_sample contains the data that is stored in the ring
  129. * buffer. Struct entry can be uninitialized. The function reserves a
  130. * data array that is specified by size. Use
  131. * op_cpu_buffer_write_commit() after preparing the sample. In case of
  132. * errors a null pointer is returned, otherwise the pointer to the
  133. * sample.
  134. *
  135. */
  136. struct op_sample
  137. *op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size)
  138. {
  139. entry->event = ring_buffer_lock_reserve
  140. (op_ring_buffer_write, sizeof(struct op_sample) +
  141. size * sizeof(entry->sample->data[0]));
  142. if (entry->event)
  143. entry->sample = ring_buffer_event_data(entry->event);
  144. else
  145. entry->sample = NULL;
  146. if (!entry->sample)
  147. return NULL;
  148. entry->size = size;
  149. entry->data = entry->sample->data;
  150. return entry->sample;
  151. }
  152. int op_cpu_buffer_write_commit(struct op_entry *entry)
  153. {
  154. return ring_buffer_unlock_commit(op_ring_buffer_write, entry->event);
  155. }
  156. struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu)
  157. {
  158. struct ring_buffer_event *e;
  159. e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
  160. if (e)
  161. goto event;
  162. if (ring_buffer_swap_cpu(op_ring_buffer_read,
  163. op_ring_buffer_write,
  164. cpu))
  165. return NULL;
  166. e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
  167. if (e)
  168. goto event;
  169. return NULL;
  170. event:
  171. entry->event = e;
  172. entry->sample = ring_buffer_event_data(e);
  173. entry->size = (ring_buffer_event_length(e) - sizeof(struct op_sample))
  174. / sizeof(entry->sample->data[0]);
  175. entry->data = entry->sample->data;
  176. return entry->sample;
  177. }
  178. unsigned long op_cpu_buffer_entries(int cpu)
  179. {
  180. return ring_buffer_entries_cpu(op_ring_buffer_read, cpu)
  181. + ring_buffer_entries_cpu(op_ring_buffer_write, cpu);
  182. }
  183. static int
  184. op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
  185. int is_kernel, struct task_struct *task)
  186. {
  187. struct op_entry entry;
  188. struct op_sample *sample;
  189. unsigned long flags;
  190. int size;
  191. flags = 0;
  192. if (backtrace)
  193. flags |= TRACE_BEGIN;
  194. /* notice a switch from user->kernel or vice versa */
  195. is_kernel = !!is_kernel;
  196. if (cpu_buf->last_is_kernel != is_kernel) {
  197. cpu_buf->last_is_kernel = is_kernel;
  198. flags |= KERNEL_CTX_SWITCH;
  199. if (is_kernel)
  200. flags |= IS_KERNEL;
  201. }
  202. /* notice a task switch */
  203. if (cpu_buf->last_task != task) {
  204. cpu_buf->last_task = task;
  205. flags |= USER_CTX_SWITCH;
  206. }
  207. if (!flags)
  208. /* nothing to do */
  209. return 0;
  210. if (flags & USER_CTX_SWITCH)
  211. size = 1;
  212. else
  213. size = 0;
  214. sample = op_cpu_buffer_write_reserve(&entry, size);
  215. if (!sample)
  216. return -ENOMEM;
  217. sample->eip = ESCAPE_CODE;
  218. sample->event = flags;
  219. if (size)
  220. op_cpu_buffer_add_data(&entry, (unsigned long)task);
  221. op_cpu_buffer_write_commit(&entry);
  222. return 0;
  223. }
  224. static inline int
  225. op_add_sample(struct oprofile_cpu_buffer *cpu_buf,
  226. unsigned long pc, unsigned long event)
  227. {
  228. struct op_entry entry;
  229. struct op_sample *sample;
  230. sample = op_cpu_buffer_write_reserve(&entry, 0);
  231. if (!sample)
  232. return -ENOMEM;
  233. sample->eip = pc;
  234. sample->event = event;
  235. return op_cpu_buffer_write_commit(&entry);
  236. }
  237. /*
  238. * This must be safe from any context.
  239. *
  240. * is_kernel is needed because on some architectures you cannot
  241. * tell if you are in kernel or user space simply by looking at
  242. * pc. We tag this in the buffer by generating kernel enter/exit
  243. * events whenever is_kernel changes
  244. */
  245. static int
  246. log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
  247. unsigned long backtrace, int is_kernel, unsigned long event)
  248. {
  249. cpu_buf->sample_received++;
  250. if (pc == ESCAPE_CODE) {
  251. cpu_buf->sample_invalid_eip++;
  252. return 0;
  253. }
  254. if (op_add_code(cpu_buf, backtrace, is_kernel, current))
  255. goto fail;
  256. if (op_add_sample(cpu_buf, pc, event))
  257. goto fail;
  258. return 1;
  259. fail:
  260. cpu_buf->sample_lost_overflow++;
  261. return 0;
  262. }
  263. static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
  264. {
  265. cpu_buf->tracing = 1;
  266. }
  267. static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
  268. {
  269. cpu_buf->tracing = 0;
  270. }
  271. static inline void
  272. __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
  273. unsigned long event, int is_kernel)
  274. {
  275. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  276. unsigned long backtrace = oprofile_backtrace_depth;
  277. /*
  278. * if log_sample() fail we can't backtrace since we lost the
  279. * source of this event
  280. */
  281. if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event))
  282. /* failed */
  283. return;
  284. if (!backtrace)
  285. return;
  286. oprofile_begin_trace(cpu_buf);
  287. oprofile_ops.backtrace(regs, backtrace);
  288. oprofile_end_trace(cpu_buf);
  289. }
  290. void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
  291. unsigned long event, int is_kernel)
  292. {
  293. __oprofile_add_ext_sample(pc, regs, event, is_kernel);
  294. }
  295. void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
  296. {
  297. int is_kernel = !user_mode(regs);
  298. unsigned long pc = profile_pc(regs);
  299. __oprofile_add_ext_sample(pc, regs, event, is_kernel);
  300. }
  301. /*
  302. * Add samples with data to the ring buffer.
  303. *
  304. * Use oprofile_add_data(&entry, val) to add data and
  305. * oprofile_write_commit(&entry) to commit the sample.
  306. */
  307. void
  308. oprofile_write_reserve(struct op_entry *entry, struct pt_regs * const regs,
  309. unsigned long pc, int code, int size)
  310. {
  311. struct op_sample *sample;
  312. int is_kernel = !user_mode(regs);
  313. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  314. cpu_buf->sample_received++;
  315. /* no backtraces for samples with data */
  316. if (op_add_code(cpu_buf, 0, is_kernel, current))
  317. goto fail;
  318. sample = op_cpu_buffer_write_reserve(entry, size + 2);
  319. if (!sample)
  320. goto fail;
  321. sample->eip = ESCAPE_CODE;
  322. sample->event = 0; /* no flags */
  323. op_cpu_buffer_add_data(entry, code);
  324. op_cpu_buffer_add_data(entry, pc);
  325. return;
  326. fail:
  327. entry->event = NULL;
  328. cpu_buf->sample_lost_overflow++;
  329. }
  330. int oprofile_add_data(struct op_entry *entry, unsigned long val)
  331. {
  332. if (!entry->event)
  333. return 0;
  334. return op_cpu_buffer_add_data(entry, val);
  335. }
  336. int oprofile_write_commit(struct op_entry *entry)
  337. {
  338. if (!entry->event)
  339. return -EINVAL;
  340. return op_cpu_buffer_write_commit(entry);
  341. }
  342. void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
  343. {
  344. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  345. log_sample(cpu_buf, pc, 0, is_kernel, event);
  346. }
  347. void oprofile_add_trace(unsigned long pc)
  348. {
  349. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  350. if (!cpu_buf->tracing)
  351. return;
  352. /*
  353. * broken frame can give an eip with the same value as an
  354. * escape code, abort the trace if we get it
  355. */
  356. if (pc == ESCAPE_CODE)
  357. goto fail;
  358. if (op_add_sample(cpu_buf, pc, 0))
  359. goto fail;
  360. return;
  361. fail:
  362. cpu_buf->tracing = 0;
  363. cpu_buf->backtrace_aborted++;
  364. return;
  365. }
  366. /*
  367. * This serves to avoid cpu buffer overflow, and makes sure
  368. * the task mortuary progresses
  369. *
  370. * By using schedule_delayed_work_on and then schedule_delayed_work
  371. * we guarantee this will stay on the correct cpu
  372. */
  373. static void wq_sync_buffer(struct work_struct *work)
  374. {
  375. struct oprofile_cpu_buffer *b =
  376. container_of(work, struct oprofile_cpu_buffer, work.work);
  377. if (b->cpu != smp_processor_id()) {
  378. printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
  379. smp_processor_id(), b->cpu);
  380. if (!cpu_online(b->cpu)) {
  381. cancel_delayed_work(&b->work);
  382. return;
  383. }
  384. }
  385. sync_buffer(b->cpu);
  386. /* don't re-add the work if we're shutting down */
  387. if (work_enabled)
  388. schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
  389. }