timer_int.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file timer_int.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/notifier.h>
  11. #include <linux/smp.h>
  12. #include <linux/oprofile.h>
  13. #include <linux/profile.h>
  14. #include <linux/init.h>
  15. #include <linux/cpu.h>
  16. #include <linux/hrtimer.h>
  17. #include <asm/irq_regs.h>
  18. #include <asm/ptrace.h>
  19. #include "oprof.h"
  20. static DEFINE_PER_CPU(struct hrtimer, oprofile_hrtimer);
  21. static int ctr_running;
  22. static enum hrtimer_restart oprofile_hrtimer_notify(struct hrtimer *hrtimer)
  23. {
  24. oprofile_add_sample(get_irq_regs(), 0);
  25. hrtimer_forward_now(hrtimer, ns_to_ktime(TICK_NSEC));
  26. return HRTIMER_RESTART;
  27. }
  28. static void __oprofile_hrtimer_start(void *unused)
  29. {
  30. struct hrtimer *hrtimer = this_cpu_ptr(&oprofile_hrtimer);
  31. if (!ctr_running)
  32. return;
  33. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  34. hrtimer->function = oprofile_hrtimer_notify;
  35. hrtimer_start(hrtimer, ns_to_ktime(TICK_NSEC),
  36. HRTIMER_MODE_REL_PINNED);
  37. }
  38. static int oprofile_hrtimer_start(void)
  39. {
  40. get_online_cpus();
  41. ctr_running = 1;
  42. on_each_cpu(__oprofile_hrtimer_start, NULL, 1);
  43. put_online_cpus();
  44. return 0;
  45. }
  46. static void __oprofile_hrtimer_stop(int cpu)
  47. {
  48. struct hrtimer *hrtimer = &per_cpu(oprofile_hrtimer, cpu);
  49. if (!ctr_running)
  50. return;
  51. hrtimer_cancel(hrtimer);
  52. }
  53. static void oprofile_hrtimer_stop(void)
  54. {
  55. int cpu;
  56. get_online_cpus();
  57. for_each_online_cpu(cpu)
  58. __oprofile_hrtimer_stop(cpu);
  59. ctr_running = 0;
  60. put_online_cpus();
  61. }
  62. static int oprofile_cpu_notify(struct notifier_block *self,
  63. unsigned long action, void *hcpu)
  64. {
  65. long cpu = (long) hcpu;
  66. switch (action) {
  67. case CPU_ONLINE:
  68. case CPU_ONLINE_FROZEN:
  69. smp_call_function_single(cpu, __oprofile_hrtimer_start,
  70. NULL, 1);
  71. break;
  72. case CPU_DEAD:
  73. case CPU_DEAD_FROZEN:
  74. __oprofile_hrtimer_stop(cpu);
  75. break;
  76. }
  77. return NOTIFY_OK;
  78. }
  79. static struct notifier_block __refdata oprofile_cpu_notifier = {
  80. .notifier_call = oprofile_cpu_notify,
  81. };
  82. static int oprofile_hrtimer_setup(void)
  83. {
  84. return register_hotcpu_notifier(&oprofile_cpu_notifier);
  85. }
  86. static void oprofile_hrtimer_shutdown(void)
  87. {
  88. unregister_hotcpu_notifier(&oprofile_cpu_notifier);
  89. }
  90. int oprofile_timer_init(struct oprofile_operations *ops)
  91. {
  92. ops->create_files = NULL;
  93. ops->setup = oprofile_hrtimer_setup;
  94. ops->shutdown = oprofile_hrtimer_shutdown;
  95. ops->start = oprofile_hrtimer_start;
  96. ops->stop = oprofile_hrtimer_stop;
  97. ops->cpu_type = "timer";
  98. printk(KERN_INFO "oprofile: using timer interrupt.\n");
  99. return 0;
  100. }