blob: bdef916e5dda3c02eb9f29504b0e69faa6a7bd8b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
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
10#include <linux/kernel.h>
11#include <linux/notifier.h>
12#include <linux/smp.h>
13#include <linux/oprofile.h>
14#include <linux/profile.h>
15#include <linux/init.h>
Martin Schwidefskybc078e42010-03-02 16:01:10 +010016#include <linux/cpu.h>
17#include <linux/hrtimer.h>
18#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/ptrace.h>
20
21#include "oprof.h"
22
Martin Schwidefskybc078e42010-03-02 16:01:10 +010023static DEFINE_PER_CPU(struct hrtimer, oprofile_hrtimer);
Santosh Shilimkar4ac3dbe2010-10-27 11:17:15 -040024static int ctr_running;
Martin Schwidefskybc078e42010-03-02 16:01:10 +010025
26static enum hrtimer_restart oprofile_hrtimer_notify(struct hrtimer *hrtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Martin Schwidefskybc078e42010-03-02 16:01:10 +010028 oprofile_add_sample(get_irq_regs(), 0);
29 hrtimer_forward_now(hrtimer, ns_to_ktime(TICK_NSEC));
30 return HRTIMER_RESTART;
31}
32
33static void __oprofile_hrtimer_start(void *unused)
34{
Christoph Lameter879d9272014-08-17 12:30:31 -050035 struct hrtimer *hrtimer = this_cpu_ptr(&oprofile_hrtimer);
Martin Schwidefskybc078e42010-03-02 16:01:10 +010036
Santosh Shilimkar4ac3dbe2010-10-27 11:17:15 -040037 if (!ctr_running)
38 return;
39
Martin Schwidefskybc078e42010-03-02 16:01:10 +010040 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
41 hrtimer->function = oprofile_hrtimer_notify;
42
43 hrtimer_start(hrtimer, ns_to_ktime(TICK_NSEC),
44 HRTIMER_MODE_REL_PINNED);
45}
46
47static int oprofile_hrtimer_start(void)
48{
Santosh Shilimkar4ac3dbe2010-10-27 11:17:15 -040049 get_online_cpus();
50 ctr_running = 1;
Martin Schwidefskybc078e42010-03-02 16:01:10 +010051 on_each_cpu(__oprofile_hrtimer_start, NULL, 1);
Santosh Shilimkar4ac3dbe2010-10-27 11:17:15 -040052 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return 0;
54}
55
Martin Schwidefskybc078e42010-03-02 16:01:10 +010056static void __oprofile_hrtimer_stop(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Martin Schwidefskybc078e42010-03-02 16:01:10 +010058 struct hrtimer *hrtimer = &per_cpu(oprofile_hrtimer, cpu);
59
Santosh Shilimkar4ac3dbe2010-10-27 11:17:15 -040060 if (!ctr_running)
61 return;
62
Martin Schwidefskybc078e42010-03-02 16:01:10 +010063 hrtimer_cancel(hrtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Martin Schwidefskybc078e42010-03-02 16:01:10 +010066static void oprofile_hrtimer_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Martin Schwidefskybc078e42010-03-02 16:01:10 +010068 int cpu;
69
Santosh Shilimkar4ac3dbe2010-10-27 11:17:15 -040070 get_online_cpus();
Martin Schwidefskybc078e42010-03-02 16:01:10 +010071 for_each_online_cpu(cpu)
72 __oprofile_hrtimer_stop(cpu);
Santosh Shilimkar4ac3dbe2010-10-27 11:17:15 -040073 ctr_running = 0;
74 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Paul Gortmakera83048e2013-06-19 15:22:41 -040077static int oprofile_cpu_notify(struct notifier_block *self,
78 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Martin Schwidefskybc078e42010-03-02 16:01:10 +010080 long cpu = (long) hcpu;
81
82 switch (action) {
83 case CPU_ONLINE:
84 case CPU_ONLINE_FROZEN:
85 smp_call_function_single(cpu, __oprofile_hrtimer_start,
86 NULL, 1);
87 break;
88 case CPU_DEAD:
89 case CPU_DEAD_FROZEN:
90 __oprofile_hrtimer_stop(cpu);
91 break;
92 }
93 return NOTIFY_OK;
94}
95
96static struct notifier_block __refdata oprofile_cpu_notifier = {
97 .notifier_call = oprofile_cpu_notify,
98};
99
Robert Richter75c43a22011-10-14 15:46:10 +0200100static int oprofile_hrtimer_setup(void)
Martin Schwidefskybc078e42010-03-02 16:01:10 +0100101{
Robert Richter75c43a22011-10-14 15:46:10 +0200102 return register_hotcpu_notifier(&oprofile_cpu_notifier);
Martin Schwidefskybc078e42010-03-02 16:01:10 +0100103}
104
Robert Richter75c43a22011-10-14 15:46:10 +0200105static void oprofile_hrtimer_shutdown(void)
Martin Schwidefskybc078e42010-03-02 16:01:10 +0100106{
107 unregister_hotcpu_notifier(&oprofile_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
Robert Richter75c43a22011-10-14 15:46:10 +0200109
110int oprofile_timer_init(struct oprofile_operations *ops)
111{
112 ops->create_files = NULL;
113 ops->setup = oprofile_hrtimer_setup;
114 ops->shutdown = oprofile_hrtimer_shutdown;
115 ops->start = oprofile_hrtimer_start;
116 ops->stop = oprofile_hrtimer_stop;
117 ops->cpu_type = "timer";
118 printk(KERN_INFO "oprofile: using timer interrupt.\n");
119 return 0;
120}