timers, sched/clock: Remove redundant notrace from update function
[deliverable/linux.git] / kernel / time / sched_clock.c
1 /*
2 * sched_clock.c: support for extending counters to full 64-bit ns counter
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8 #include <linux/clocksource.h>
9 #include <linux/init.h>
10 #include <linux/jiffies.h>
11 #include <linux/ktime.h>
12 #include <linux/kernel.h>
13 #include <linux/moduleparam.h>
14 #include <linux/sched.h>
15 #include <linux/syscore_ops.h>
16 #include <linux/hrtimer.h>
17 #include <linux/sched_clock.h>
18 #include <linux/seqlock.h>
19 #include <linux/bitops.h>
20
21 /**
22 * struct clock_read_data - data required to read from sched_clock
23 *
24 * @epoch_ns: sched_clock value at last update
25 * @epoch_cyc: Clock cycle value at last update
26 * @sched_clock_mask: Bitmask for two's complement subtraction of non 64bit
27 * clocks
28 * @read_sched_clock: Current clock source (or dummy source when suspended)
29 * @mult: Multipler for scaled math conversion
30 * @shift: Shift value for scaled math conversion
31 *
32 * Care must be taken when updating this structure; it is read by
33 * some very hot code paths. It occupies <=40 bytes and, when combined
34 * with the seqcount used to synchronize access, comfortably fits into
35 * a 64 byte cache line.
36 */
37 struct clock_read_data {
38 u64 epoch_ns;
39 u64 epoch_cyc;
40 u64 sched_clock_mask;
41 u64 (*read_sched_clock)(void);
42 u32 mult;
43 u32 shift;
44 };
45
46 /**
47 * struct clock_data - all data needed for sched_clock (including
48 * registration of a new clock source)
49 *
50 * @seq: Sequence counter for protecting updates.
51 * @read_data: Data required to read from sched_clock.
52 * @wrap_kt: Duration for which clock can run before wrapping
53 * @rate: Tick rate of the registered clock
54 * @actual_read_sched_clock: Registered clock read function
55 *
56 * The ordering of this structure has been chosen to optimize cache
57 * performance. In particular seq and read_data (combined) should fit
58 * into a single 64 byte cache line.
59 */
60 struct clock_data {
61 seqcount_t seq;
62 struct clock_read_data read_data;
63 ktime_t wrap_kt;
64 unsigned long rate;
65 u64 (*actual_read_sched_clock)(void);
66 };
67
68 static struct hrtimer sched_clock_timer;
69 static int irqtime = -1;
70
71 core_param(irqtime, irqtime, int, 0400);
72
73 static u64 notrace jiffy_sched_clock_read(void)
74 {
75 /*
76 * We don't need to use get_jiffies_64 on 32-bit arches here
77 * because we register with BITS_PER_LONG
78 */
79 return (u64)(jiffies - INITIAL_JIFFIES);
80 }
81
82 static struct clock_data cd ____cacheline_aligned = {
83 .read_data = { .mult = NSEC_PER_SEC / HZ,
84 .read_sched_clock = jiffy_sched_clock_read, },
85 .actual_read_sched_clock = jiffy_sched_clock_read,
86
87 };
88
89 static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
90 {
91 return (cyc * mult) >> shift;
92 }
93
94 unsigned long long notrace sched_clock(void)
95 {
96 u64 cyc, res;
97 unsigned long seq;
98 struct clock_read_data *rd = &cd.read_data;
99
100 do {
101 seq = raw_read_seqcount_begin(&cd.seq);
102
103 cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
104 rd->sched_clock_mask;
105 res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
106 } while (read_seqcount_retry(&cd.seq, seq));
107
108 return res;
109 }
110
111 /*
112 * Atomically update the sched_clock epoch.
113 */
114 static void update_sched_clock(void)
115 {
116 unsigned long flags;
117 u64 cyc;
118 u64 ns;
119 struct clock_read_data *rd = &cd.read_data;
120
121 cyc = cd.actual_read_sched_clock();
122 ns = rd->epoch_ns +
123 cyc_to_ns((cyc - rd->epoch_cyc) & rd->sched_clock_mask,
124 rd->mult, rd->shift);
125
126 raw_local_irq_save(flags);
127 raw_write_seqcount_begin(&cd.seq);
128 rd->epoch_ns = ns;
129 rd->epoch_cyc = cyc;
130 raw_write_seqcount_end(&cd.seq);
131 raw_local_irq_restore(flags);
132 }
133
134 static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
135 {
136 update_sched_clock();
137 hrtimer_forward_now(hrt, cd.wrap_kt);
138 return HRTIMER_RESTART;
139 }
140
141 void __init sched_clock_register(u64 (*read)(void), int bits,
142 unsigned long rate)
143 {
144 u64 res, wrap, new_mask, new_epoch, cyc, ns;
145 u32 new_mult, new_shift;
146 unsigned long r;
147 char r_unit;
148 struct clock_read_data *rd = &cd.read_data;
149
150 if (cd.rate > rate)
151 return;
152
153 WARN_ON(!irqs_disabled());
154
155 /* calculate the mult/shift to convert counter ticks to ns. */
156 clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
157
158 new_mask = CLOCKSOURCE_MASK(bits);
159 cd.rate = rate;
160
161 /* calculate how many nanosecs until we risk wrapping */
162 wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
163 cd.wrap_kt = ns_to_ktime(wrap);
164
165 /* update epoch for new counter and update epoch_ns from old counter*/
166 new_epoch = read();
167 cyc = cd.actual_read_sched_clock();
168 ns = rd->epoch_ns +
169 cyc_to_ns((cyc - rd->epoch_cyc) & rd->sched_clock_mask,
170 rd->mult, rd->shift);
171 cd.actual_read_sched_clock = read;
172
173 raw_write_seqcount_begin(&cd.seq);
174 rd->read_sched_clock = read;
175 rd->sched_clock_mask = new_mask;
176 rd->mult = new_mult;
177 rd->shift = new_shift;
178 rd->epoch_cyc = new_epoch;
179 rd->epoch_ns = ns;
180 raw_write_seqcount_end(&cd.seq);
181
182 r = rate;
183 if (r >= 4000000) {
184 r /= 1000000;
185 r_unit = 'M';
186 } else if (r >= 1000) {
187 r /= 1000;
188 r_unit = 'k';
189 } else
190 r_unit = ' ';
191
192 /* calculate the ns resolution of this counter */
193 res = cyc_to_ns(1ULL, new_mult, new_shift);
194
195 pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
196 bits, r, r_unit, res, wrap);
197
198 /* Enable IRQ time accounting if we have a fast enough sched_clock */
199 if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
200 enable_sched_clock_irqtime();
201
202 pr_debug("Registered %pF as sched_clock source\n", read);
203 }
204
205 void __init sched_clock_postinit(void)
206 {
207 /*
208 * If no sched_clock function has been provided at that point,
209 * make it the final one one.
210 */
211 if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
212 sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
213
214 update_sched_clock();
215
216 /*
217 * Start the timer to keep sched_clock() properly updated and
218 * sets the initial epoch.
219 */
220 hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
221 sched_clock_timer.function = sched_clock_poll;
222 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
223 }
224
225 /*
226 * Clock read function for use when the clock is suspended.
227 *
228 * This function makes it appear to sched_clock() as if the clock
229 * stopped counting at its last update.
230 */
231 static u64 notrace suspended_sched_clock_read(void)
232 {
233 return cd.read_data.epoch_cyc;
234 }
235
236 static int sched_clock_suspend(void)
237 {
238 struct clock_read_data *rd = &cd.read_data;
239
240 update_sched_clock();
241 hrtimer_cancel(&sched_clock_timer);
242 rd->read_sched_clock = suspended_sched_clock_read;
243 return 0;
244 }
245
246 static void sched_clock_resume(void)
247 {
248 struct clock_read_data *rd = &cd.read_data;
249
250 rd->epoch_cyc = cd.actual_read_sched_clock();
251 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
252 rd->read_sched_clock = cd.actual_read_sched_clock;
253 }
254
255 static struct syscore_ops sched_clock_ops = {
256 .suspend = sched_clock_suspend,
257 .resume = sched_clock_resume,
258 };
259
260 static int __init sched_clock_syscore_init(void)
261 {
262 register_syscore_ops(&sched_clock_ops);
263 return 0;
264 }
265 device_initcall(sched_clock_syscore_init);
This page took 0.035779 seconds and 5 git commands to generate.