Merge branch 'sched/clock' into tracing/ftrace
[safe/jmp/linux-2.6] / kernel / sched_clock.c
1 /*
2  * sched_clock for unstable cpu clocks
3  *
4  *  Copyright (C) 2008 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
5  *
6  *  Updates and enhancements:
7  *    Copyright (C) 2008 Red Hat, Inc. Steven Rostedt <srostedt@redhat.com>
8  *
9  * Based on code by:
10  *   Ingo Molnar <mingo@redhat.com>
11  *   Guillaume Chazarain <guichaz@gmail.com>
12  *
13  * Create a semi stable clock from a mixture of other events, including:
14  *  - gtod
15  *  - sched_clock()
16  *  - explicit idle events
17  *
18  * We use gtod as base and the unstable clock deltas. The deltas are filtered,
19  * making it monotonic and keeping it within an expected window.
20  *
21  * Furthermore, explicit sleep and wakeup hooks allow us to account for time
22  * that is otherwise invisible (TSC gets stopped).
23  *
24  * The clock: sched_clock_cpu() is monotonic per cpu, and should be somewhat
25  * consistent between cpus (never more than 2 jiffies difference).
26  */
27 #include <linux/spinlock.h>
28 #include <linux/hardirq.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/ktime.h>
32 #include <linux/sched.h>
33
34 /*
35  * Scheduler clock - returns current time in nanosec units.
36  * This is default implementation.
37  * Architectures and sub-architectures can override this.
38  */
39 unsigned long long __attribute__((weak)) sched_clock(void)
40 {
41         return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
42 }
43
44 static __read_mostly int sched_clock_running;
45
46 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
47 __read_mostly int sched_clock_stable;
48 #else
49 static const int sched_clock_stable = 1;
50 #endif
51
52 struct sched_clock_data {
53         /*
54          * Raw spinlock - this is a special case: this might be called
55          * from within instrumentation code so we dont want to do any
56          * instrumentation ourselves.
57          */
58         raw_spinlock_t          lock;
59
60         u64                     tick_raw;
61         u64                     tick_gtod;
62         u64                     clock;
63 };
64
65 static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
66
67 static inline struct sched_clock_data *this_scd(void)
68 {
69         return &__get_cpu_var(sched_clock_data);
70 }
71
72 static inline struct sched_clock_data *cpu_sdc(int cpu)
73 {
74         return &per_cpu(sched_clock_data, cpu);
75 }
76
77 void sched_clock_init(void)
78 {
79         u64 ktime_now = ktime_to_ns(ktime_get());
80         int cpu;
81
82         for_each_possible_cpu(cpu) {
83                 struct sched_clock_data *scd = cpu_sdc(cpu);
84
85                 scd->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
86                 scd->tick_raw = 0;
87                 scd->tick_gtod = ktime_now;
88                 scd->clock = ktime_now;
89         }
90
91         sched_clock_running = 1;
92 }
93
94 /*
95  * min, max except they take wrapping into account
96  */
97
98 static inline u64 wrap_min(u64 x, u64 y)
99 {
100         return (s64)(x - y) < 0 ? x : y;
101 }
102
103 static inline u64 wrap_max(u64 x, u64 y)
104 {
105         return (s64)(x - y) > 0 ? x : y;
106 }
107
108 /*
109  * update the percpu scd from the raw @now value
110  *
111  *  - filter out backward motion
112  *  - use the GTOD tick value to create a window to filter crazy TSC values
113  */
114 static u64 __update_sched_clock(struct sched_clock_data *scd, u64 now)
115 {
116         s64 delta = now - scd->tick_raw;
117         u64 clock, min_clock, max_clock;
118
119         WARN_ON_ONCE(!irqs_disabled());
120
121         if (unlikely(delta < 0))
122                 delta = 0;
123
124         if (unlikely(!sched_clock_running))
125                 return 0ull;
126
127         /*
128          * scd->clock = clamp(scd->tick_gtod + delta,
129          *                    max(scd->tick_gtod, scd->clock),
130          *                    scd->tick_gtod + TICK_NSEC);
131          */
132
133         clock = scd->tick_gtod + delta;
134         min_clock = wrap_max(scd->tick_gtod, scd->clock);
135         max_clock = wrap_max(scd->clock, scd->tick_gtod + TICK_NSEC);
136
137         clock = wrap_max(clock, min_clock);
138         clock = wrap_min(clock, max_clock);
139
140         scd->clock = clock;
141
142         return scd->clock;
143 }
144
145 static void lock_double_clock(struct sched_clock_data *data1,
146                                 struct sched_clock_data *data2)
147 {
148         if (data1 < data2) {
149                 __raw_spin_lock(&data1->lock);
150                 __raw_spin_lock(&data2->lock);
151         } else {
152                 __raw_spin_lock(&data2->lock);
153                 __raw_spin_lock(&data1->lock);
154         }
155 }
156
157 u64 sched_clock_cpu(int cpu)
158 {
159         u64 now, clock, this_clock, remote_clock;
160         struct sched_clock_data *scd;
161
162         if (sched_clock_stable)
163                 return sched_clock();
164
165         /*
166          * Normally this is not called in NMI context - but if it is,
167          * trying to do any locking here is totally lethal.
168          */
169         if (unlikely(in_nmi()))
170                 return scd->clock;
171
172         if (unlikely(!sched_clock_running))
173                 return 0ull;
174
175         scd = cpu_sdc(cpu);
176         WARN_ON_ONCE(!irqs_disabled());
177         now = sched_clock();
178
179         if (cpu != raw_smp_processor_id()) {
180                 struct sched_clock_data *my_scd = this_scd();
181
182                 lock_double_clock(scd, my_scd);
183
184                 this_clock = __update_sched_clock(my_scd, now);
185                 remote_clock = scd->clock;
186
187                 /*
188                  * Use the opportunity that we have both locks
189                  * taken to couple the two clocks: we take the
190                  * larger time as the latest time for both
191                  * runqueues. (this creates monotonic movement)
192                  */
193                 if (likely((s64)(remote_clock - this_clock) < 0)) {
194                         clock = this_clock;
195                         scd->clock = clock;
196                 } else {
197                         /*
198                          * Should be rare, but possible:
199                          */
200                         clock = remote_clock;
201                         my_scd->clock = remote_clock;
202                 }
203
204                 __raw_spin_unlock(&my_scd->lock);
205         } else {
206                 __raw_spin_lock(&scd->lock);
207                 clock = __update_sched_clock(scd, now);
208         }
209
210         __raw_spin_unlock(&scd->lock);
211
212         return clock;
213 }
214
215 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
216
217 void sched_clock_tick(void)
218 {
219         struct sched_clock_data *scd = this_scd();
220         u64 now, now_gtod;
221
222         if (unlikely(!sched_clock_running))
223                 return;
224
225         WARN_ON_ONCE(!irqs_disabled());
226
227         now_gtod = ktime_to_ns(ktime_get());
228         now = sched_clock();
229
230         __raw_spin_lock(&scd->lock);
231         scd->tick_raw = now;
232         scd->tick_gtod = now_gtod;
233         __update_sched_clock(scd, now);
234         __raw_spin_unlock(&scd->lock);
235 }
236
237 /*
238  * We are going deep-idle (irqs are disabled):
239  */
240 void sched_clock_idle_sleep_event(void)
241 {
242         sched_clock_cpu(smp_processor_id());
243 }
244 EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
245
246 /*
247  * We just idled delta nanoseconds (called with irqs disabled):
248  */
249 void sched_clock_idle_wakeup_event(u64 delta_ns)
250 {
251         if (timekeeping_suspended)
252                 return;
253
254         sched_clock_tick();
255         touch_softlockup_watchdog();
256 }
257 EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
258
259 #endif /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
260
261 unsigned long long cpu_clock(int cpu)
262 {
263         unsigned long long clock;
264         unsigned long flags;
265
266         local_irq_save(flags);
267         clock = sched_clock_cpu(cpu);
268         local_irq_restore(flags);
269
270         return clock;
271 }
272 EXPORT_SYMBOL_GPL(cpu_clock);