timer_int.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 enum hrtimer_restart oprofile_hrtimer_notify(struct hrtimer *hrtimer)
  22. {
  23. oprofile_add_sample(get_irq_regs(), 0);
  24. hrtimer_forward_now(hrtimer, ns_to_ktime(TICK_NSEC));
  25. return HRTIMER_RESTART;
  26. }
  27. static void __oprofile_hrtimer_start(void *unused)
  28. {
  29. struct hrtimer *hrtimer = &__get_cpu_var(oprofile_hrtimer);
  30. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  31. hrtimer->function = oprofile_hrtimer_notify;
  32. hrtimer_start(hrtimer, ns_to_ktime(TICK_NSEC),
  33. HRTIMER_MODE_REL_PINNED);
  34. }
  35. static int oprofile_hrtimer_start(void)
  36. {
  37. on_each_cpu(__oprofile_hrtimer_start, NULL, 1);
  38. return 0;
  39. }
  40. static void __oprofile_hrtimer_stop(int cpu)
  41. {
  42. struct hrtimer *hrtimer = &per_cpu(oprofile_hrtimer, cpu);
  43. hrtimer_cancel(hrtimer);
  44. }
  45. static void oprofile_hrtimer_stop(void)
  46. {
  47. int cpu;
  48. for_each_online_cpu(cpu)
  49. __oprofile_hrtimer_stop(cpu);
  50. }
  51. static int __cpuinit oprofile_cpu_notify(struct notifier_block *self,
  52. unsigned long action, void *hcpu)
  53. {
  54. long cpu = (long) hcpu;
  55. switch (action) {
  56. case CPU_ONLINE:
  57. case CPU_ONLINE_FROZEN:
  58. smp_call_function_single(cpu, __oprofile_hrtimer_start,
  59. NULL, 1);
  60. break;
  61. case CPU_DEAD:
  62. case CPU_DEAD_FROZEN:
  63. __oprofile_hrtimer_stop(cpu);
  64. break;
  65. }
  66. return NOTIFY_OK;
  67. }
  68. static struct notifier_block __refdata oprofile_cpu_notifier = {
  69. .notifier_call = oprofile_cpu_notify,
  70. };
  71. int __init oprofile_timer_init(struct oprofile_operations *ops)
  72. {
  73. int rc;
  74. rc = register_hotcpu_notifier(&oprofile_cpu_notifier);
  75. if (rc)
  76. return rc;
  77. ops->create_files = NULL;
  78. ops->setup = NULL;
  79. ops->shutdown = NULL;
  80. ops->start = oprofile_hrtimer_start;
  81. ops->stop = oprofile_hrtimer_stop;
  82. ops->cpu_type = "timer";
  83. return 0;
  84. }
  85. void __exit oprofile_timer_exit(void)
  86. {
  87. unregister_hotcpu_notifier(&oprofile_cpu_notifier);
  88. }