cpu_buffer.c 11 KB

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