cpu_buffer.c 11 KB

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