Rev 5270 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5270 | Rev 6082 | ||
---|---|---|---|
Line -... | Line 1... | ||
- | 1 | /* |
|
- | 2 | * linux/kernel/time.c |
|
- | 3 | * |
|
- | 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
|
- | 5 | * |
|
- | 6 | * This file contains the interface functions for the various |
|
- | 7 | * time related system calls: time, stime, gettimeofday, settimeofday, |
|
- | 8 | * adjtime |
|
- | 9 | */ |
|
- | 10 | /* |
|
- | 11 | * Modification history kernel/time.c |
|
- | 12 | * |
|
- | 13 | * 1993-09-02 Philip Gladstone |
|
- | 14 | * Created file with time related functions from sched/core.c and adjtimex() |
|
- | 15 | * 1993-10-08 Torsten Duwe |
|
- | 16 | * adjtime interface update and CMOS clock write code |
|
- | 17 | * 1995-08-13 Torsten Duwe |
|
- | 18 | * kernel PLL updated to 1994-12-13 specs (rfc-1589) |
|
- | 19 | * 1999-01-16 Ulrich Windl |
|
- | 20 | * Introduced error checking for many cases in adjtimex(). |
|
- | 21 | * Updated NTP code according to technical memorandum Jan '96 |
|
- | 22 | * "A Kernel Model for Precision Timekeeping" by Dave Mills |
|
- | 23 | * Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10) |
|
- | 24 | * (Even though the technical memorandum forbids it) |
|
- | 25 | * 2004-07-14 Christoph Lameter |
|
- | 26 | * Added getnstimeofday to allow the posix timer functions to return |
|
- | 27 | * with nanosecond accuracy |
|
- | 28 | */ |
|
- | 29 | ||
1 | #include |
30 | #include |
- | 31 | #include |
|
- | 32 | #include |
|
Line 2... | Line 33... | ||
2 | 33 | ||
3 | 34 | ||
Line 43... | Line 74... | ||
43 | #define NSEC_PER_MSEC 1000000L |
74 | #define NSEC_PER_MSEC 1000000L |
44 | #define USEC_PER_SEC 1000000L |
75 | #define USEC_PER_SEC 1000000L |
45 | #define NSEC_PER_SEC 1000000000L |
76 | #define NSEC_PER_SEC 1000000000L |
46 | #define FSEC_PER_SEC 1000000000000000LL |
77 | #define FSEC_PER_SEC 1000000000000000LL |
Line -... | Line 78... | ||
- | 78 | ||
- | 79 | # define USER_HZ 100 |
|
- | 80 | /* |
|
47 | 81 | * Convert jiffies to milliseconds and back. |
|
- | 82 | * |
|
- | 83 | * Avoid unnecessary multiplications/divisions in the |
|
- | 84 | * two most common HZ cases: |
|
48 | 85 | */ |
|
49 | unsigned int jiffies_to_msecs(const unsigned long j) |
86 | unsigned int jiffies_to_msecs(const unsigned long j) |
50 | { |
87 | { |
51 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) |
88 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) |
52 | return (MSEC_PER_SEC / HZ) * j; |
89 | return (MSEC_PER_SEC / HZ) * j; |
Line 58... | Line 95... | ||
58 | # else |
95 | # else |
59 | return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN; |
96 | return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN; |
60 | # endif |
97 | # endif |
61 | #endif |
98 | #endif |
62 | } |
99 | } |
- | 100 | EXPORT_SYMBOL(jiffies_to_msecs); |
|
Line 63... | Line 101... | ||
63 | 101 | ||
64 | unsigned int jiffies_to_usecs(const unsigned long j) |
102 | unsigned int jiffies_to_usecs(const unsigned long j) |
65 | { |
103 | { |
66 | #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) |
104 | #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) |
Line 73... | Line 111... | ||
73 | # else |
111 | # else |
74 | return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; |
112 | return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; |
75 | # endif |
113 | # endif |
76 | #endif |
114 | #endif |
77 | } |
115 | } |
- | 116 | EXPORT_SYMBOL(jiffies_to_usecs); |
|
Line -... | Line 117... | ||
- | 117 | ||
- | 118 | /** |
|
- | 119 | * timespec_trunc - Truncate timespec to a granularity |
|
- | 120 | * @t: Timespec |
|
- | 121 | * @gran: Granularity in ns. |
|
- | 122 | * |
|
- | 123 | * Truncate a timespec to a granularity. Always rounds down. gran must |
|
- | 124 | * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns). |
|
- | 125 | */ |
|
- | 126 | struct timespec timespec_trunc(struct timespec t, unsigned gran) |
|
- | 127 | { |
|
- | 128 | /* Avoid division in the common cases 1 ns and 1 s. */ |
|
- | 129 | if (gran == 1) { |
|
- | 130 | /* nothing */ |
|
- | 131 | } else if (gran == NSEC_PER_SEC) { |
|
- | 132 | t.tv_nsec = 0; |
|
- | 133 | } else if (gran > 1 && gran < NSEC_PER_SEC) { |
|
- | 134 | t.tv_nsec -= t.tv_nsec % gran; |
|
- | 135 | } else { |
|
- | 136 | WARN(1, "illegal file time granularity: %u", gran); |
|
- | 137 | } |
|
- | 138 | return t; |
|
- | 139 | } |
|
Line 78... | Line 140... | ||
78 | 140 | EXPORT_SYMBOL(timespec_trunc); |
|
- | 141 | ||
- | 142 | /* |
|
79 | 143 | * mktime64 - Converts date to seconds. |
|
80 | /* |
144 | * Converts Gregorian date to seconds since 1970-01-01 00:00:00. |
81 | * When we convert to jiffies then we interpret incoming values |
145 | * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 |
- | 146 | * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. |
|
- | 147 | * |
|
82 | * the following way: |
148 | * [For the Julian calendar (which was used in Russia before 1917, |
- | 149 | * Britain & colonies before 1752, anywhere else before 1582, |
|
83 | * |
150 | * and is still in use by some communities) leave out the |
84 | * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET) |
151 | * -year/100+year/400 terms, and add 10.] |
- | 152 | * |
|
- | 153 | * This algorithm was first published by Gauss (I think). |
|
- | 154 | */ |
|
- | 155 | time64_t mktime64(const unsigned int year0, const unsigned int mon0, |
|
- | 156 | const unsigned int day, const unsigned int hour, |
|
- | 157 | const unsigned int min, const unsigned int sec) |
|
- | 158 | { |
|
- | 159 | unsigned int mon = mon0, year = year0; |
|
- | 160 | ||
85 | * |
161 | /* 1..12 -> 11,12,1..10 */ |
- | 162 | if (0 >= (int) (mon -= 2)) { |
|
- | 163 | mon += 12; /* Puts Feb last since it has leap day */ |
|
- | 164 | year -= 1; |
|
- | 165 | } |
|
- | 166 | ||
- | 167 | return ((((time64_t) |
|
- | 168 | (year/4 - year/100 + year/400 + 367*mon/12 + day) + |
|
- | 169 | year*365 - 719499 |
|
- | 170 | )*24 + hour /* now have hours */ |
|
- | 171 | )*60 + min /* now have minutes */ |
|
- | 172 | )*60 + sec; /* finally seconds */ |
|
- | 173 | } |
|
- | 174 | EXPORT_SYMBOL(mktime64); |
|
- | 175 | ||
86 | * - 'too large' values [that would result in larger than |
176 | /** |
- | 177 | * set_normalized_timespec - set timespec sec and nsec parts and normalize |
|
- | 178 | * |
|
- | 179 | * @ts: pointer to timespec variable to be set |
|
- | 180 | * @sec: seconds to set |
|
87 | * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. |
181 | * @nsec: nanoseconds to set |
88 | * |
182 | * |
89 | * - all other values are converted to jiffies by either multiplying |
183 | * Set seconds and nanoseconds field of a timespec variable and |
90 | * the input value by a factor or dividing it with a factor |
184 | * normalize to the timespec storage format |
- | 185 | * |
|
- | 186 | * Note: The tv_nsec part is always in the range of |
|
91 | * |
187 | * 0 <= tv_nsec < NSEC_PER_SEC |
92 | * We must also be careful about 32-bit overflows. |
188 | * For negative values only the tv_sec field is negative ! |
93 | */ |
189 | */ |
- | 190 | void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec) |
|
94 | unsigned long msecs_to_jiffies(const unsigned int m) |
191 | { |
- | 192 | while (nsec >= NSEC_PER_SEC) { |
|
- | 193 | /* |
|
95 | { |
194 | * The following asm() prevents the compiler from |
96 | /* |
195 | * optimising this loop into a modulo operation. See |
- | 196 | * also __iter_div_u64_rem() in include/linux/time.h |
|
- | 197 | */ |
|
- | 198 | asm("" : "+rm"(nsec)); |
|
- | 199 | nsec -= NSEC_PER_SEC; |
|
97 | * Negative value, means infinite timeout: |
200 | ++sec; |
- | 201 | } |
|
98 | */ |
202 | while (nsec < 0) { |
- | 203 | asm("" : "+rm"(nsec)); |
|
- | 204 | nsec += NSEC_PER_SEC; |
|
- | 205 | --sec; |
|
- | 206 | } |
|
- | 207 | ts->tv_sec = sec; |
|
- | 208 | ts->tv_nsec = nsec; |
|
- | 209 | } |
|
- | 210 | EXPORT_SYMBOL(set_normalized_timespec); |
|
- | 211 | ||
- | 212 | /** |
|
- | 213 | * ns_to_timespec - Convert nanoseconds to timespec |
|
- | 214 | * @nsec: the nanoseconds value to be converted |
|
- | 215 | * |
|
- | 216 | * Returns the timespec representation of the nsec parameter. |
|
- | 217 | */ |
|
- | 218 | struct timespec ns_to_timespec(const s64 nsec) |
|
- | 219 | { |
|
Line -... | Line 220... | ||
- | 220 | struct timespec ts; |
|
- | 221 | s32 rem; |
|
- | 222 | ||
99 | if ((int)m < 0) |
223 | if (!nsec) |
- | 224 | return (struct timespec) {0, 0}; |
|
- | 225 | ||
- | 226 | ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); |
|
- | 227 | if (unlikely(rem < 0)) { |
|
- | 228 | ts.tv_sec--; |
|
- | 229 | rem += NSEC_PER_SEC; |
|
- | 230 | } |
|
- | 231 | ts.tv_nsec = rem; |
|
- | 232 | ||
- | 233 | return ts; |
|
100 | return MAX_JIFFY_OFFSET; |
234 | } |
101 | 235 | EXPORT_SYMBOL(ns_to_timespec); |
|
102 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) |
236 | |
- | 237 | /** |
|
103 | /* |
238 | * ns_to_timeval - Convert nanoseconds to timeval |
104 | * HZ is equal to or smaller than 1000, and 1000 is a nice |
239 | * @nsec: the nanoseconds value to be converted |
- | 240 | * |
|
- | 241 | * Returns the timeval representation of the nsec parameter. |
|
- | 242 | */ |
|
- | 243 | struct timeval ns_to_timeval(const s64 nsec) |
|
- | 244 | { |
|
- | 245 | struct timespec ts = ns_to_timespec(nsec); |
|
- | 246 | struct timeval tv; |
|
- | 247 | ||
- | 248 | tv.tv_sec = ts.tv_sec; |
|
- | 249 | tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000; |
|
- | 250 | ||
- | 251 | return tv; |
|
- | 252 | } |
|
- | 253 | EXPORT_SYMBOL(ns_to_timeval); |
|
- | 254 | ||
- | 255 | #if BITS_PER_LONG == 32 |
|
- | 256 | /** |
|
- | 257 | * set_normalized_timespec - set timespec sec and nsec parts and normalize |
|
- | 258 | * |
|
- | 259 | * @ts: pointer to timespec variable to be set |
|
105 | * round multiple of HZ, divide with the factor between them, |
260 | * @sec: seconds to set |
- | 261 | * @nsec: nanoseconds to set |
|
- | 262 | * |
|
- | 263 | * Set seconds and nanoseconds field of a timespec variable and |
|
106 | * but round upwards: |
264 | * normalize to the timespec storage format |
- | 265 | * |
|
- | 266 | * Note: The tv_nsec part is always in the range of |
|
- | 267 | * 0 <= tv_nsec < NSEC_PER_SEC |
|
- | 268 | * For negative values only the tv_sec field is negative ! |
|
- | 269 | */ |
|
107 | */ |
270 | void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec) |
- | 271 | { |
|
108 | return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ); |
272 | while (nsec >= NSEC_PER_SEC) { |
- | 273 | /* |
|
- | 274 | * The following asm() prevents the compiler from |
|
- | 275 | * optimising this loop into a modulo operation. See |
|
- | 276 | * also __iter_div_u64_rem() in include/linux/time.h |
|
- | 277 | */ |
|
- | 278 | asm("" : "+rm"(nsec)); |
|
- | 279 | nsec -= NSEC_PER_SEC; |
|
- | 280 | ++sec; |
|
- | 281 | } |
|
- | 282 | while (nsec < 0) { |
|
- | 283 | asm("" : "+rm"(nsec)); |
|
- | 284 | nsec += NSEC_PER_SEC; |
|
- | 285 | --sec; |
|
- | 286 | } |
|
- | 287 | ts->tv_sec = sec; |
|
- | 288 | ts->tv_nsec = nsec; |
|
- | 289 | } |
|
- | 290 | EXPORT_SYMBOL(set_normalized_timespec64); |
|
109 | #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) |
291 | |
110 | /* |
292 | /** |
111 | * HZ is larger than 1000, and HZ is a nice round multiple of |
293 | * ns_to_timespec64 - Convert nanoseconds to timespec64 |
112 | * 1000 - simply multiply with the factor between them. |
- | |
113 | * |
294 | * @nsec: the nanoseconds value to be converted |
114 | * But first make sure the multiplication result cannot |
295 | * |
- | 296 | * Returns the timespec64 representation of the nsec parameter. |
|
115 | * overflow: |
297 | */ |
- | 298 | struct timespec64 ns_to_timespec64(const s64 nsec) |
|
Line -... | Line 299... | ||
- | 299 | { |
|
- | 300 | struct timespec64 ts; |
|
- | 301 | s32 rem; |
|
- | 302 | ||
- | 303 | if (!nsec) |
|
- | 304 | return (struct timespec64) {0, 0}; |
|
116 | */ |
305 | |
- | 306 | ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); |
|
- | 307 | if (unlikely(rem < 0)) { |
|
- | 308 | ts.tv_sec--; |
|
- | 309 | rem += NSEC_PER_SEC; |
|
- | 310 | } |
|
- | 311 | ts.tv_nsec = rem; |
|
117 | if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) |
312 | |
- | 313 | return ts; |
|
- | 314 | } |
|
- | 315 | EXPORT_SYMBOL(ns_to_timespec64); |
|
- | 316 | #endif |
|
- | 317 | /** |
|
- | 318 | * msecs_to_jiffies: - convert milliseconds to jiffies |
|
- | 319 | * @m: time in milliseconds |
|
- | 320 | * |
|
- | 321 | * conversion is done as follows: |
|
- | 322 | * |
|
- | 323 | * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET) |
|
- | 324 | * |
|
- | 325 | * - 'too large' values [that would result in larger than |
|
- | 326 | * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. |
|
- | 327 | * |
|
- | 328 | * - all other values are converted to jiffies by either multiplying |
|
- | 329 | * the input value by a factor or dividing it with a factor and |
|
- | 330 | * handling any 32-bit overflows. |
|
- | 331 | * for the details see __msecs_to_jiffies() |
|
- | 332 | * |
|
- | 333 | * msecs_to_jiffies() checks for the passed in value being a constant |
|
- | 334 | * via __builtin_constant_p() allowing gcc to eliminate most of the |
|
- | 335 | * code, __msecs_to_jiffies() is called if the value passed does not |
|
- | 336 | * allow constant folding and the actual conversion must be done at |
|
- | 337 | * runtime. |
|
- | 338 | * the _msecs_to_jiffies helpers are the HZ dependent conversion |
|
118 | return MAX_JIFFY_OFFSET; |
339 | * routines found in include/linux/jiffies.h |
119 | 340 | */ |
|
120 | return m * (HZ / MSEC_PER_SEC); |
- | |
121 | #else |
- | |
122 | /* |
341 | unsigned long __msecs_to_jiffies(const unsigned int m) |
123 | * Generic case - multiply, round and divide. But first |
342 | { |
124 | * check that if we are doing a net multiplication, that |
343 | /* |
125 | * we wouldn't overflow: |
- | |
126 | */ |
- | |
127 | if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) |
344 | * Negative value, means infinite timeout: |
128 | return MAX_JIFFY_OFFSET; |
- | |
129 | 345 | */ |
|
130 | return (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32) |
346 | if ((int)m < 0) |
Line 131... | Line 347... | ||
131 | >> MSEC_TO_HZ_SHR32; |
347 | return MAX_JIFFY_OFFSET; |
132 | #endif |
348 | return _msecs_to_jiffies(m); |
133 | } |
349 | } |
134 | EXPORT_SYMBOL(msecs_to_jiffies); |
350 | EXPORT_SYMBOL(__msecs_to_jiffies); |
135 | - | ||
136 | unsigned long usecs_to_jiffies(const unsigned int u) |
- | |
137 | { |
- | |
138 | if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) |
351 | |
139 | return MAX_JIFFY_OFFSET; |
- | |
140 | #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) |
- | |
141 | return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ); |
- | |
142 | #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) |
- | |
143 | return u * (HZ / USEC_PER_SEC); |
352 | unsigned long __usecs_to_jiffies(const unsigned int u) |
144 | #else |
353 | { |
Line 145... | Line 354... | ||
145 | return (USEC_TO_HZ_MUL32 * u + USEC_TO_HZ_ADJ32) |
354 | if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) |
146 | >> USEC_TO_HZ_SHR32; |
355 | return MAX_JIFFY_OFFSET; |
147 | #endif |
356 | return _usecs_to_jiffies(u); |
148 | } |
357 | } |
Line 162... | Line 371... | ||
162 | * |
371 | * |
163 | * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec |
372 | * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec |
164 | * value to a scaled second value. |
373 | * value to a scaled second value. |
165 | */ |
374 | */ |
166 | static unsigned long |
375 | static unsigned long |
167 | __timespec_to_jiffies(unsigned long sec, long nsec) |
376 | __timespec64_to_jiffies(u64 sec, long nsec) |
168 | { |
377 | { |
169 | nsec = nsec + TICK_NSEC - 1; |
378 | nsec = nsec + TICK_NSEC - 1; |
Line 170... | Line 379... | ||
170 | 379 | ||
171 | if (sec >= MAX_SEC_IN_JIFFIES){ |
380 | if (sec >= MAX_SEC_IN_JIFFIES){ |
172 | sec = MAX_SEC_IN_JIFFIES; |
381 | sec = MAX_SEC_IN_JIFFIES; |
173 | nsec = 0; |
382 | nsec = 0; |
174 | } |
383 | } |
175 | return (((u64)sec * SEC_CONVERSION) + |
384 | return ((sec * SEC_CONVERSION) + |
176 | (((u64)nsec * NSEC_CONVERSION) >> |
385 | (((u64)nsec * NSEC_CONVERSION) >> |
Line 177... | Line 386... | ||
177 | (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; |
386 | (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; |
Line 178... | Line 387... | ||
178 | 387 | ||
179 | } |
388 | } |
180 | 389 | ||
181 | unsigned long |
390 | static unsigned long |
182 | timespec_to_jiffies(const struct timespec *value) |
391 | __timespec_to_jiffies(unsigned long sec, long nsec) |
Line -... | Line 392... | ||
- | 392 | { |
|
- | 393 | return __timespec64_to_jiffies((u64)sec, nsec); |
|
- | 394 | } |
|
- | 395 | ||
- | 396 | unsigned long |
|
183 | { |
397 | timespec64_to_jiffies(const struct timespec64 *value) |
Line 184... | Line 398... | ||
184 | return __timespec_to_jiffies(value->tv_sec, value->tv_nsec); |
398 | { |
185 | } |
399 | return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec); |
186 | 400 | } |
|
187 | EXPORT_SYMBOL(timespec_to_jiffies); |
401 | EXPORT_SYMBOL(timespec64_to_jiffies); |
188 | 402 | ||
189 | void |
403 | void |
190 | jiffies_to_timespec(const unsigned long jiffies, struct timespec *value) |
404 | jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value) |
191 | { |
405 | { |
192 | /* |
406 | /* |
193 | * Convert jiffies to nanoseconds and separate with |
407 | * Convert jiffies to nanoseconds and separate with |
194 | * one divide. |
408 | * one divide. |
195 | */ |
409 | */ |
196 | u32 rem; |
410 | u32 rem; |
- | 411 | value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, |
|
- | 412 | NSEC_PER_SEC, &rem); |
|
- | 413 | value->tv_nsec = rem; |
|
- | 414 | } |
|
- | 415 | EXPORT_SYMBOL(jiffies_to_timespec64); |
|
- | 416 | ||
- | 417 | /* |
|
- | 418 | * We could use a similar algorithm to timespec_to_jiffies (with a |
|
- | 419 | * different multiplier for usec instead of nsec). But this has a |
|
- | 420 | * problem with rounding: we can't exactly add TICK_NSEC - 1 to the |
|
- | 421 | * usec value, since it's not necessarily integral. |
|
- | 422 | * |
|
- | 423 | * We could instead round in the intermediate scaled representation |
|
- | 424 | * (i.e. in units of 1/2^(large scale) jiffies) but that's also |
|
- | 425 | * perilous: the scaling introduces a small positive error, which |
|
- | 426 | * combined with a division-rounding-upward (i.e. adding 2^(scale) - 1 |
|
- | 427 | * units to the intermediate before shifting) leads to accidental |
|
- | 428 | * overflow and overestimates. |
|
- | 429 | * |
|
- | 430 | * At the cost of one additional multiplication by a constant, just |
|
- | 431 | * use the timespec implementation. |
|
- | 432 | */ |
|
- | 433 | unsigned long |
|
- | 434 | timeval_to_jiffies(const struct timeval *value) |
|
- | 435 | { |
|
- | 436 | return __timespec_to_jiffies(value->tv_sec, |
|
- | 437 | value->tv_usec * NSEC_PER_USEC); |
|
- | 438 | } |
|
- | 439 | EXPORT_SYMBOL(timeval_to_jiffies); |
|
- | 440 | ||
- | 441 | void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value) |
|
- | 442 | { |
|
- | 443 | /* |
|
- | 444 | * Convert jiffies to nanoseconds and separate with |
|
- | 445 | * one divide. |
|
- | 446 | */ |
|
- | 447 | u32 rem; |
|
- | 448 | ||
- | 449 | value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, |
|
- | 450 | NSEC_PER_SEC, &rem); |
|
- | 451 | value->tv_usec = rem / NSEC_PER_USEC; |
|
- | 452 | } |
|
- | 453 | EXPORT_SYMBOL(jiffies_to_timeval); |
|
- | 454 | ||
- | 455 | /* |
|
- | 456 | * Convert jiffies/jiffies_64 to clock_t and back. |
|
- | 457 | */ |
|
- | 458 | clock_t jiffies_to_clock_t(unsigned long x) |
|
- | 459 | { |
|
- | 460 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 |
|
- | 461 | # if HZ < USER_HZ |
|
- | 462 | return x * (USER_HZ / HZ); |
|
- | 463 | # else |
|
- | 464 | return x / (HZ / USER_HZ); |
|
- | 465 | # endif |
|
- | 466 | #else |
|
- | 467 | return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ); |
|
- | 468 | #endif |
|
- | 469 | } |
|
- | 470 | EXPORT_SYMBOL(jiffies_to_clock_t); |
|
- | 471 | ||
- | 472 | unsigned long clock_t_to_jiffies(unsigned long x) |
|
- | 473 | { |
|
- | 474 | #if (HZ % USER_HZ)==0 |
|
- | 475 | if (x >= ~0UL / (HZ / USER_HZ)) |
|
- | 476 | return ~0UL; |
|
- | 477 | return x * (HZ / USER_HZ); |
|
- | 478 | #else |
|
- | 479 | /* Don't worry about loss of precision here .. */ |
|
- | 480 | if (x >= ~0UL / HZ * USER_HZ) |
|
- | 481 | return ~0UL; |
|
- | 482 | ||
- | 483 | /* .. but do try to contain it here */ |
|
- | 484 | return div_u64((u64)x * HZ, USER_HZ); |
|
- | 485 | #endif |
|
- | 486 | } |
|
- | 487 | EXPORT_SYMBOL(clock_t_to_jiffies); |
|
- | 488 | ||
- | 489 | u64 jiffies_64_to_clock_t(u64 x) |
|
- | 490 | { |
|
- | 491 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 |
|
- | 492 | # if HZ < USER_HZ |
|
- | 493 | x = div_u64(x * USER_HZ, HZ); |
|
- | 494 | # elif HZ > USER_HZ |
|
- | 495 | x = div_u64(x, HZ / USER_HZ); |
|
- | 496 | # else |
|
- | 497 | /* Nothing to do */ |
|
- | 498 | # endif |
|
- | 499 | #else |
|
- | 500 | /* |
|
- | 501 | * There are better ways that don't overflow early, |
|
- | 502 | * but even this doesn't overflow in hundreds of years |
|
- | 503 | * in 64 bits, so.. |
|
- | 504 | */ |
|
- | 505 | x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ)); |
|
- | 506 | #endif |
|
- | 507 | return x; |
|
- | 508 | } |
|
- | 509 | EXPORT_SYMBOL(jiffies_64_to_clock_t); |
|
- | 510 | ||
- | 511 | u64 nsec_to_clock_t(u64 x) |
|
- | 512 | { |
|
- | 513 | #if (NSEC_PER_SEC % USER_HZ) == 0 |
|
- | 514 | return div_u64(x, NSEC_PER_SEC / USER_HZ); |
|
- | 515 | #elif (USER_HZ % 512) == 0 |
|
- | 516 | return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512); |
|
- | 517 | #else |
|
- | 518 | /* |
|
- | 519 | * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024, |
|
- | 520 | * overflow after 64.99 years. |
|
- | 521 | * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ... |
|
- | 522 | */ |
|
- | 523 | return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ); |
|
- | 524 | #endif |
|
- | 525 | } |
|
- | 526 | ||
- | 527 | /** |
|
- | 528 | * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64 |
|
- | 529 | * |
|
- | 530 | * @n: nsecs in u64 |
|
- | 531 | * |
|
- | 532 | * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. |
|
- | 533 | * And this doesn't return MAX_JIFFY_OFFSET since this function is designed |
|
- | 534 | * for scheduler, not for use in device drivers to calculate timeout value. |
|
- | 535 | * |
|
- | 536 | * note: |
|
- | 537 | * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) |
|
- | 538 | * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years |
|
- | 539 | */ |
|
- | 540 | u64 nsecs_to_jiffies64(u64 n) |
|
- | 541 | { |
|
- | 542 | #if (NSEC_PER_SEC % HZ) == 0 |
|
- | 543 | /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */ |
|
- | 544 | return div_u64(n, NSEC_PER_SEC / HZ); |
|
- | 545 | #elif (HZ % 512) == 0 |
|
- | 546 | /* overflow after 292 years if HZ = 1024 */ |
|
- | 547 | return div_u64(n * HZ / 512, NSEC_PER_SEC / 512); |
|
- | 548 | #else |
|
- | 549 | /* |
|
- | 550 | * Generic case - optimized for cases where HZ is a multiple of 3. |
|
- | 551 | * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc. |
|
- | 552 | */ |
|
- | 553 | return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ); |
|
- | 554 | #endif |
|
- | 555 | } |
|
- | 556 | EXPORT_SYMBOL(nsecs_to_jiffies64); |
|
- | 557 | ||
- | 558 | /** |
|
- | 559 | * nsecs_to_jiffies - Convert nsecs in u64 to jiffies |
|
- | 560 | * |
|
- | 561 | * @n: nsecs in u64 |
|
- | 562 | * |
|
- | 563 | * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. |
|
- | 564 | * And this doesn't return MAX_JIFFY_OFFSET since this function is designed |
|
- | 565 | * for scheduler, not for use in device drivers to calculate timeout value. |
|
- | 566 | * |
|
- | 567 | * note: |
|
- | 568 | * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) |
|
- | 569 | * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years |
|
- | 570 | */ |
|
- | 571 | unsigned long nsecs_to_jiffies(u64 n) |
|
- | 572 | { |
|
- | 573 | return (unsigned long)nsecs_to_jiffies64(n); |
|
- | 574 | } |
|
- | 575 | EXPORT_SYMBOL_GPL(nsecs_to_jiffies); |
|
- | 576 | ||
- | 577 | /* |
|
- | 578 | * Add two timespec values and do a safety check for overflow. |
|
- | 579 | * It's assumed that both values are valid (>= 0) |
|
- | 580 | */ |
|
- | 581 | struct timespec timespec_add_safe(const struct timespec lhs, |
|
- | 582 | const struct timespec rhs) |
|
- | 583 | { |
|
- | 584 | struct timespec res; |
|
- | 585 | ||
- | 586 | set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec, |
|
- | 587 | lhs.tv_nsec + rhs.tv_nsec); |
|
- | 588 | ||
Line 197... | Line 589... | ||
197 | value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, |
589 | if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec) |
198 | NSEC_PER_SEC, &rem); |
590 | res.tv_sec = TIME_T_MAX; |
199 | value->tv_nsec = rem; |
591 | |
Line 215... | Line 607... | ||
215 | quotient = -quotient; |
607 | quotient = -quotient; |
216 | } |
608 | } |
217 | return quotient; |
609 | return quotient; |
218 | } |
610 | }>>>>=>>>>>>>>=>>>>=>>=>=> |
Line 219... | Line -... | ||
219 | - | ||
220 | struct timespec ns_to_timespec(const s64 nsec) |
- | |
221 | { |
- | |
222 | struct timespec ts; |
- | |
Line 223... | Line -... | ||
223 | s32 rem; |
- | |
224 | - | ||
Line 225... | Line -... | ||
225 | if (!nsec) |
- | |
226 | return (struct timespec) {0, 0}; |
- | |
227 | - | ||
228 | ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); |
- | |
229 | if (unlikely(rem < 0)) { |
- | |
230 | ts.tv_sec--; |
- | |
Line 231... | Line -... | ||
231 | rem += NSEC_PER_SEC; |
- | |
232 | } |
611 |