cpu_buffer.c 11 KB

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