cpu_buffer.c 11 KB

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