cpu_buffer.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. * @file cpu_buffer.c
  3. *
  4. * @remark Copyright 2002 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. *
  10. * Each CPU has a local buffer that stores PC value/event
  11. * pairs. We also log context switches when we notice them.
  12. * Eventually each CPU's buffer is processed into the global
  13. * event buffer by sync_buffer().
  14. *
  15. * We use a local buffer for two reasons: an NMI or similar
  16. * interrupt cannot synchronise, and high sampling rates
  17. * would lead to catastrophic global synchronisation if
  18. * a global buffer was used.
  19. */
  20. #include <linux/sched.h>
  21. #include <linux/oprofile.h>
  22. #include <linux/vmalloc.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_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
  29. static void wq_sync_buffer(struct work_struct *work);
  30. #define DEFAULT_TIMER_EXPIRE (HZ / 10)
  31. static int work_enabled;
  32. void free_cpu_buffers(void)
  33. {
  34. int i;
  35. for_each_possible_cpu(i) {
  36. vfree(per_cpu(cpu_buffer, i).buffer);
  37. per_cpu(cpu_buffer, i).buffer = NULL;
  38. }
  39. }
  40. unsigned long oprofile_get_cpu_buffer_size(void)
  41. {
  42. return fs_cpu_buffer_size;
  43. }
  44. void oprofile_cpu_buffer_inc_smpl_lost(void)
  45. {
  46. struct oprofile_cpu_buffer *cpu_buf
  47. = &__get_cpu_var(cpu_buffer);
  48. cpu_buf->sample_lost_overflow++;
  49. }
  50. int alloc_cpu_buffers(void)
  51. {
  52. int i;
  53. unsigned long buffer_size = fs_cpu_buffer_size;
  54. for_each_possible_cpu(i) {
  55. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  56. b->buffer = vmalloc_node(sizeof(struct op_sample) * buffer_size,
  57. cpu_to_node(i));
  58. if (!b->buffer)
  59. goto fail;
  60. b->last_task = NULL;
  61. b->last_is_kernel = -1;
  62. b->tracing = 0;
  63. b->buffer_size = buffer_size;
  64. b->tail_pos = 0;
  65. b->head_pos = 0;
  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(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. int i;
  94. work_enabled = 0;
  95. for_each_online_cpu(i) {
  96. struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
  97. cancel_delayed_work(&b->work);
  98. }
  99. flush_scheduled_work();
  100. }
  101. /* compute number of available slots in cpu_buffer queue */
  102. static unsigned long nr_available_slots(struct oprofile_cpu_buffer const *b)
  103. {
  104. unsigned long head = b->head_pos;
  105. unsigned long tail = b->tail_pos;
  106. if (tail > head)
  107. return (tail - head) - 1;
  108. return tail + (b->buffer_size - head) - 1;
  109. }
  110. static inline void
  111. add_sample(struct oprofile_cpu_buffer *cpu_buf,
  112. unsigned long pc, unsigned long event)
  113. {
  114. struct op_sample *entry = cpu_buffer_write_entry(cpu_buf);
  115. entry->eip = pc;
  116. entry->event = event;
  117. cpu_buffer_write_commit(cpu_buf);
  118. }
  119. static inline void
  120. add_code(struct oprofile_cpu_buffer *buffer, unsigned long value)
  121. {
  122. add_sample(buffer, ESCAPE_CODE, value);
  123. }
  124. /* This must be safe from any context. It's safe writing here
  125. * because of the head/tail separation of the writer and reader
  126. * of the CPU buffer.
  127. *
  128. * is_kernel is needed because on some architectures you cannot
  129. * tell if you are in kernel or user space simply by looking at
  130. * pc. We tag this in the buffer by generating kernel enter/exit
  131. * events whenever is_kernel changes
  132. */
  133. static int log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
  134. int is_kernel, unsigned long event)
  135. {
  136. struct task_struct *task;
  137. cpu_buf->sample_received++;
  138. if (pc == ESCAPE_CODE) {
  139. cpu_buf->sample_invalid_eip++;
  140. return 0;
  141. }
  142. if (nr_available_slots(cpu_buf) < 3) {
  143. cpu_buf->sample_lost_overflow++;
  144. return 0;
  145. }
  146. is_kernel = !!is_kernel;
  147. task = current;
  148. /* notice a switch from user->kernel or vice versa */
  149. if (cpu_buf->last_is_kernel != is_kernel) {
  150. cpu_buf->last_is_kernel = is_kernel;
  151. add_code(cpu_buf, is_kernel);
  152. }
  153. /* notice a task switch */
  154. if (cpu_buf->last_task != task) {
  155. cpu_buf->last_task = task;
  156. add_code(cpu_buf, (unsigned long)task);
  157. }
  158. add_sample(cpu_buf, pc, event);
  159. return 1;
  160. }
  161. static int oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
  162. {
  163. if (nr_available_slots(cpu_buf) < 4) {
  164. cpu_buf->sample_lost_overflow++;
  165. return 0;
  166. }
  167. add_code(cpu_buf, CPU_TRACE_BEGIN);
  168. cpu_buf->tracing = 1;
  169. return 1;
  170. }
  171. static void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
  172. {
  173. cpu_buf->tracing = 0;
  174. }
  175. void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
  176. unsigned long event, int is_kernel)
  177. {
  178. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  179. if (!backtrace_depth) {
  180. log_sample(cpu_buf, pc, is_kernel, event);
  181. return;
  182. }
  183. if (!oprofile_begin_trace(cpu_buf))
  184. return;
  185. /*
  186. * if log_sample() fail we can't backtrace since we lost the
  187. * source of this event
  188. */
  189. if (log_sample(cpu_buf, pc, is_kernel, event))
  190. oprofile_ops.backtrace(regs, backtrace_depth);
  191. oprofile_end_trace(cpu_buf);
  192. }
  193. void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
  194. {
  195. int is_kernel = !user_mode(regs);
  196. unsigned long pc = profile_pc(regs);
  197. oprofile_add_ext_sample(pc, regs, event, is_kernel);
  198. }
  199. #ifdef CONFIG_OPROFILE_IBS
  200. #define MAX_IBS_SAMPLE_SIZE 14
  201. void oprofile_add_ibs_sample(struct pt_regs * const regs,
  202. unsigned int * const ibs_sample, int ibs_code)
  203. {
  204. int is_kernel = !user_mode(regs);
  205. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  206. struct task_struct *task;
  207. cpu_buf->sample_received++;
  208. if (nr_available_slots(cpu_buf) < MAX_IBS_SAMPLE_SIZE) {
  209. /* we can't backtrace since we lost the source of this event */
  210. cpu_buf->sample_lost_overflow++;
  211. return;
  212. }
  213. /* notice a switch from user->kernel or vice versa */
  214. if (cpu_buf->last_is_kernel != is_kernel) {
  215. cpu_buf->last_is_kernel = is_kernel;
  216. add_code(cpu_buf, is_kernel);
  217. }
  218. /* notice a task switch */
  219. if (!is_kernel) {
  220. task = current;
  221. if (cpu_buf->last_task != task) {
  222. cpu_buf->last_task = task;
  223. add_code(cpu_buf, (unsigned long)task);
  224. }
  225. }
  226. add_code(cpu_buf, ibs_code);
  227. add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]);
  228. add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]);
  229. add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]);
  230. if (ibs_code == IBS_OP_BEGIN) {
  231. add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]);
  232. add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]);
  233. add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]);
  234. }
  235. if (backtrace_depth)
  236. oprofile_ops.backtrace(regs, backtrace_depth);
  237. }
  238. #endif
  239. void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
  240. {
  241. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  242. log_sample(cpu_buf, pc, is_kernel, event);
  243. }
  244. void oprofile_add_trace(unsigned long pc)
  245. {
  246. struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
  247. if (!cpu_buf->tracing)
  248. return;
  249. if (nr_available_slots(cpu_buf) < 1) {
  250. cpu_buf->tracing = 0;
  251. cpu_buf->sample_lost_overflow++;
  252. return;
  253. }
  254. /*
  255. * broken frame can give an eip with the same value as an
  256. * escape code, abort the trace if we get it
  257. */
  258. if (pc == ESCAPE_CODE) {
  259. cpu_buf->tracing = 0;
  260. cpu_buf->backtrace_aborted++;
  261. return;
  262. }
  263. add_sample(cpu_buf, pc, 0);
  264. }
  265. /*
  266. * This serves to avoid cpu buffer overflow, and makes sure
  267. * the task mortuary progresses
  268. *
  269. * By using schedule_delayed_work_on and then schedule_delayed_work
  270. * we guarantee this will stay on the correct cpu
  271. */
  272. static void wq_sync_buffer(struct work_struct *work)
  273. {
  274. struct oprofile_cpu_buffer *b =
  275. container_of(work, struct oprofile_cpu_buffer, work.work);
  276. if (b->cpu != smp_processor_id()) {
  277. printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
  278. smp_processor_id(), b->cpu);
  279. if (!cpu_online(b->cpu)) {
  280. cancel_delayed_work(&b->work);
  281. return;
  282. }
  283. }
  284. sync_buffer(b->cpu);
  285. /* don't re-add the work if we're shutting down */
  286. if (work_enabled)
  287. schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
  288. }