timer_int.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_timer_online(unsigned int cpu)
  63. {
  64. local_irq_disable();
  65. __oprofile_hrtimer_start(NULL);
  66. local_irq_enable();
  67. return 0;
  68. }
  69. static int oprofile_timer_prep_down(unsigned int cpu)
  70. {
  71. __oprofile_hrtimer_stop(cpu);
  72. return 0;
  73. }
  74. static enum cpuhp_state hp_online;
  75. static int oprofile_hrtimer_setup(void)
  76. {
  77. int ret;
  78. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  79. "oprofile/timer:online",
  80. oprofile_timer_online,
  81. oprofile_timer_prep_down);
  82. if (ret < 0)
  83. return ret;
  84. hp_online = ret;
  85. return 0;
  86. }
  87. static void oprofile_hrtimer_shutdown(void)
  88. {
  89. cpuhp_remove_state_nocalls(hp_online);
  90. }
  91. int oprofile_timer_init(struct oprofile_operations *ops)
  92. {
  93. ops->create_files = NULL;
  94. ops->setup = oprofile_hrtimer_setup;
  95. ops->shutdown = oprofile_hrtimer_shutdown;
  96. ops->start = oprofile_hrtimer_start;
  97. ops->stop = oprofile_hrtimer_stop;
  98. ops->cpu_type = "timer";
  99. printk(KERN_INFO "oprofile: using timer interrupt.\n");
  100. return 0;
  101. }