oprof.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * @file oprof.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/oprofile.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/time.h>
  16. #include <asm/mutex.h>
  17. #include "oprof.h"
  18. #include "event_buffer.h"
  19. #include "cpu_buffer.h"
  20. #include "buffer_sync.h"
  21. #include "oprofile_stats.h"
  22. struct oprofile_operations oprofile_ops;
  23. unsigned long oprofile_started;
  24. unsigned long oprofile_backtrace_depth;
  25. static unsigned long is_setup;
  26. static DEFINE_MUTEX(start_mutex);
  27. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  28. static void switch_worker(struct work_struct *work);
  29. static DECLARE_DELAYED_WORK(switch_work, switch_worker);
  30. #define TIME_SLICE_DEFAULT 1
  31. #endif
  32. /* timer
  33. 0 - use performance monitoring hardware if available
  34. 1 - use the timer int mechanism regardless
  35. */
  36. static int timer = 0;
  37. int oprofile_setup(void)
  38. {
  39. int err;
  40. mutex_lock(&start_mutex);
  41. if ((err = alloc_cpu_buffers()))
  42. goto out;
  43. if ((err = alloc_event_buffer()))
  44. goto out1;
  45. if (oprofile_ops.setup && (err = oprofile_ops.setup()))
  46. goto out2;
  47. /* Note even though this starts part of the
  48. * profiling overhead, it's necessary to prevent
  49. * us missing task deaths and eventually oopsing
  50. * when trying to process the event buffer.
  51. */
  52. if (oprofile_ops.sync_start) {
  53. int sync_ret = oprofile_ops.sync_start();
  54. switch (sync_ret) {
  55. case 0:
  56. goto post_sync;
  57. case 1:
  58. goto do_generic;
  59. case -1:
  60. goto out3;
  61. default:
  62. goto out3;
  63. }
  64. }
  65. do_generic:
  66. if ((err = sync_start()))
  67. goto out3;
  68. post_sync:
  69. is_setup = 1;
  70. mutex_unlock(&start_mutex);
  71. return 0;
  72. out3:
  73. if (oprofile_ops.shutdown)
  74. oprofile_ops.shutdown();
  75. out2:
  76. free_event_buffer();
  77. out1:
  78. free_cpu_buffers();
  79. out:
  80. mutex_unlock(&start_mutex);
  81. return err;
  82. }
  83. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  84. static void start_switch_worker(void)
  85. {
  86. schedule_delayed_work(&switch_work, oprofile_time_slice);
  87. }
  88. static void switch_worker(struct work_struct *work)
  89. {
  90. if (!oprofile_ops.switch_events())
  91. start_switch_worker();
  92. }
  93. #endif
  94. /* Actually start profiling (echo 1>/dev/oprofile/enable) */
  95. int oprofile_start(void)
  96. {
  97. int err = -EINVAL;
  98. mutex_lock(&start_mutex);
  99. if (!is_setup)
  100. goto out;
  101. err = 0;
  102. if (oprofile_started)
  103. goto out;
  104. oprofile_reset_stats();
  105. if ((err = oprofile_ops.start()))
  106. goto out;
  107. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  108. if (oprofile_ops.switch_events)
  109. start_switch_worker();
  110. #endif
  111. oprofile_started = 1;
  112. out:
  113. mutex_unlock(&start_mutex);
  114. return err;
  115. }
  116. /* echo 0>/dev/oprofile/enable */
  117. void oprofile_stop(void)
  118. {
  119. mutex_lock(&start_mutex);
  120. if (!oprofile_started)
  121. goto out;
  122. oprofile_ops.stop();
  123. oprofile_started = 0;
  124. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  125. cancel_delayed_work_sync(&switch_work);
  126. #endif
  127. /* wake up the daemon to read what remains */
  128. wake_up_buffer_waiter();
  129. out:
  130. mutex_unlock(&start_mutex);
  131. }
  132. void oprofile_shutdown(void)
  133. {
  134. mutex_lock(&start_mutex);
  135. if (oprofile_ops.sync_stop) {
  136. int sync_ret = oprofile_ops.sync_stop();
  137. switch (sync_ret) {
  138. case 0:
  139. goto post_sync;
  140. case 1:
  141. goto do_generic;
  142. default:
  143. goto post_sync;
  144. }
  145. }
  146. do_generic:
  147. sync_stop();
  148. post_sync:
  149. if (oprofile_ops.shutdown)
  150. oprofile_ops.shutdown();
  151. is_setup = 0;
  152. free_event_buffer();
  153. free_cpu_buffers();
  154. mutex_unlock(&start_mutex);
  155. }
  156. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  157. /* User inputs in ms, converts to jiffies */
  158. int oprofile_set_timeout(unsigned long val_msec)
  159. {
  160. int err = 0;
  161. unsigned long time_slice;
  162. mutex_lock(&start_mutex);
  163. if (oprofile_started) {
  164. err = -EBUSY;
  165. goto out;
  166. }
  167. if (!oprofile_ops.switch_events) {
  168. err = -EINVAL;
  169. goto out;
  170. }
  171. time_slice = msecs_to_jiffies(val_msec);
  172. if (time_slice == MAX_JIFFY_OFFSET) {
  173. err = -EINVAL;
  174. goto out;
  175. }
  176. oprofile_time_slice = time_slice;
  177. out:
  178. mutex_unlock(&start_mutex);
  179. return err;
  180. }
  181. #endif
  182. int oprofile_set_backtrace(unsigned long val)
  183. {
  184. int err = 0;
  185. mutex_lock(&start_mutex);
  186. if (oprofile_started) {
  187. err = -EBUSY;
  188. goto out;
  189. }
  190. if (!oprofile_ops.backtrace) {
  191. err = -EINVAL;
  192. goto out;
  193. }
  194. oprofile_backtrace_depth = val;
  195. out:
  196. mutex_unlock(&start_mutex);
  197. return err;
  198. }
  199. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  200. static void __init oprofile_multiplexing_init(void)
  201. {
  202. oprofile_time_slice = msecs_to_jiffies(TIME_SLICE_DEFAULT);
  203. }
  204. #endif
  205. static int __init oprofile_init(void)
  206. {
  207. int err;
  208. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  209. oprofile_multiplexing_init();
  210. #endif
  211. err = oprofile_arch_init(&oprofile_ops);
  212. if (err < 0 || timer) {
  213. printk(KERN_INFO "oprofile: using timer interrupt.\n");
  214. oprofile_timer_init(&oprofile_ops);
  215. }
  216. err = oprofilefs_register();
  217. if (err)
  218. oprofile_arch_exit();
  219. return err;
  220. }
  221. static void __exit oprofile_exit(void)
  222. {
  223. oprofilefs_unregister();
  224. oprofile_arch_exit();
  225. }
  226. module_init(oprofile_init);
  227. module_exit(oprofile_exit);
  228. module_param_named(timer, timer, int, 0644);
  229. MODULE_PARM_DESC(timer, "force use of timer interrupt");
  230. MODULE_LICENSE("GPL");
  231. MODULE_AUTHOR("John Levon <levon@movementarian.org>");
  232. MODULE_DESCRIPTION("OProfile system profiler");