diff options
-rw-r--r-- | include/linux/jiffies.h | 218 | ||||
-rw-r--r-- | kernel/time.c | 213 |
2 files changed, 230 insertions, 201 deletions
diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h index 0ec6e28bccd2..9243cefa4512 100644 --- a/include/linux/jiffies.h +++ b/include/linux/jiffies.h | |||
@@ -259,207 +259,23 @@ static inline u64 get_jiffies_64(void) | |||
259 | #endif | 259 | #endif |
260 | 260 | ||
261 | /* | 261 | /* |
262 | * Convert jiffies to milliseconds and back. | 262 | * Convert various time units to each other: |
263 | * | ||
264 | * Avoid unnecessary multiplications/divisions in the | ||
265 | * two most common HZ cases: | ||
266 | */ | ||
267 | static inline unsigned int jiffies_to_msecs(const unsigned long j) | ||
268 | { | ||
269 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) | ||
270 | return (MSEC_PER_SEC / HZ) * j; | ||
271 | #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) | ||
272 | return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); | ||
273 | #else | ||
274 | return (j * MSEC_PER_SEC) / HZ; | ||
275 | #endif | ||
276 | } | ||
277 | |||
278 | static inline unsigned int jiffies_to_usecs(const unsigned long j) | ||
279 | { | ||
280 | #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) | ||
281 | return (USEC_PER_SEC / HZ) * j; | ||
282 | #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) | ||
283 | return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC); | ||
284 | #else | ||
285 | return (j * USEC_PER_SEC) / HZ; | ||
286 | #endif | ||
287 | } | ||
288 | |||
289 | static inline unsigned long msecs_to_jiffies(const unsigned int m) | ||
290 | { | ||
291 | if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) | ||
292 | return MAX_JIFFY_OFFSET; | ||
293 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) | ||
294 | return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ); | ||
295 | #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) | ||
296 | return m * (HZ / MSEC_PER_SEC); | ||
297 | #else | ||
298 | return (m * HZ + MSEC_PER_SEC - 1) / MSEC_PER_SEC; | ||
299 | #endif | ||
300 | } | ||
301 | |||
302 | static inline unsigned long usecs_to_jiffies(const unsigned int u) | ||
303 | { | ||
304 | if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) | ||
305 | return MAX_JIFFY_OFFSET; | ||
306 | #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) | ||
307 | return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ); | ||
308 | #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) | ||
309 | return u * (HZ / USEC_PER_SEC); | ||
310 | #else | ||
311 | return (u * HZ + USEC_PER_SEC - 1) / USEC_PER_SEC; | ||
312 | #endif | ||
313 | } | ||
314 | |||
315 | /* | ||
316 | * The TICK_NSEC - 1 rounds up the value to the next resolution. Note | ||
317 | * that a remainder subtract here would not do the right thing as the | ||
318 | * resolution values don't fall on second boundries. I.e. the line: | ||
319 | * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding. | ||
320 | * | ||
321 | * Rather, we just shift the bits off the right. | ||
322 | * | ||
323 | * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec | ||
324 | * value to a scaled second value. | ||
325 | */ | ||
326 | static __inline__ unsigned long | ||
327 | timespec_to_jiffies(const struct timespec *value) | ||
328 | { | ||
329 | unsigned long sec = value->tv_sec; | ||
330 | long nsec = value->tv_nsec + TICK_NSEC - 1; | ||
331 | |||
332 | if (sec >= MAX_SEC_IN_JIFFIES){ | ||
333 | sec = MAX_SEC_IN_JIFFIES; | ||
334 | nsec = 0; | ||
335 | } | ||
336 | return (((u64)sec * SEC_CONVERSION) + | ||
337 | (((u64)nsec * NSEC_CONVERSION) >> | ||
338 | (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; | ||
339 | |||
340 | } | ||
341 | |||
342 | static __inline__ void | ||
343 | jiffies_to_timespec(const unsigned long jiffies, struct timespec *value) | ||
344 | { | ||
345 | /* | ||
346 | * Convert jiffies to nanoseconds and separate with | ||
347 | * one divide. | ||
348 | */ | ||
349 | u64 nsec = (u64)jiffies * TICK_NSEC; | ||
350 | value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_nsec); | ||
351 | } | ||
352 | |||
353 | /* Same for "timeval" | ||
354 | * | ||
355 | * Well, almost. The problem here is that the real system resolution is | ||
356 | * in nanoseconds and the value being converted is in micro seconds. | ||
357 | * Also for some machines (those that use HZ = 1024, in-particular), | ||
358 | * there is a LARGE error in the tick size in microseconds. | ||
359 | |||
360 | * The solution we use is to do the rounding AFTER we convert the | ||
361 | * microsecond part. Thus the USEC_ROUND, the bits to be shifted off. | ||
362 | * Instruction wise, this should cost only an additional add with carry | ||
363 | * instruction above the way it was done above. | ||
364 | */ | ||
365 | static __inline__ unsigned long | ||
366 | timeval_to_jiffies(const struct timeval *value) | ||
367 | { | ||
368 | unsigned long sec = value->tv_sec; | ||
369 | long usec = value->tv_usec; | ||
370 | |||
371 | if (sec >= MAX_SEC_IN_JIFFIES){ | ||
372 | sec = MAX_SEC_IN_JIFFIES; | ||
373 | usec = 0; | ||
374 | } | ||
375 | return (((u64)sec * SEC_CONVERSION) + | ||
376 | (((u64)usec * USEC_CONVERSION + USEC_ROUND) >> | ||
377 | (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; | ||
378 | } | ||
379 | |||
380 | static __inline__ void | ||
381 | jiffies_to_timeval(const unsigned long jiffies, struct timeval *value) | ||
382 | { | ||
383 | /* | ||
384 | * Convert jiffies to nanoseconds and separate with | ||
385 | * one divide. | ||
386 | */ | ||
387 | u64 nsec = (u64)jiffies * TICK_NSEC; | ||
388 | long tv_usec; | ||
389 | |||
390 | value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &tv_usec); | ||
391 | tv_usec /= NSEC_PER_USEC; | ||
392 | value->tv_usec = tv_usec; | ||
393 | } | ||
394 | |||
395 | /* | ||
396 | * Convert jiffies/jiffies_64 to clock_t and back. | ||
397 | */ | 263 | */ |
398 | static inline clock_t jiffies_to_clock_t(long x) | 264 | extern unsigned int jiffies_to_msecs(const unsigned long j); |
399 | { | 265 | extern unsigned int jiffies_to_usecs(const unsigned long j); |
400 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 | 266 | extern unsigned long msecs_to_jiffies(const unsigned int m); |
401 | return x / (HZ / USER_HZ); | 267 | extern unsigned long usecs_to_jiffies(const unsigned int u); |
402 | #else | 268 | extern unsigned long timespec_to_jiffies(const struct timespec *value); |
403 | u64 tmp = (u64)x * TICK_NSEC; | 269 | extern void jiffies_to_timespec(const unsigned long jiffies, |
404 | do_div(tmp, (NSEC_PER_SEC / USER_HZ)); | 270 | struct timespec *value); |
405 | return (long)tmp; | 271 | extern unsigned long timeval_to_jiffies(const struct timeval *value); |
406 | #endif | 272 | extern void jiffies_to_timeval(const unsigned long jiffies, |
407 | } | 273 | struct timeval *value); |
408 | 274 | extern clock_t jiffies_to_clock_t(long x); | |
409 | static inline unsigned long clock_t_to_jiffies(unsigned long x) | 275 | extern unsigned long clock_t_to_jiffies(unsigned long x); |
410 | { | 276 | extern u64 jiffies_64_to_clock_t(u64 x); |
411 | #if (HZ % USER_HZ)==0 | 277 | extern u64 nsec_to_clock_t(u64 x); |
412 | if (x >= ~0UL / (HZ / USER_HZ)) | 278 | |
413 | return ~0UL; | 279 | #define TIMESTAMP_SIZE 30 |
414 | return x * (HZ / USER_HZ); | ||
415 | #else | ||
416 | u64 jif; | ||
417 | |||
418 | /* Don't worry about loss of precision here .. */ | ||
419 | if (x >= ~0UL / HZ * USER_HZ) | ||
420 | return ~0UL; | ||
421 | |||
422 | /* .. but do try to contain it here */ | ||
423 | jif = x * (u64) HZ; | ||
424 | do_div(jif, USER_HZ); | ||
425 | return jif; | ||
426 | #endif | ||
427 | } | ||
428 | |||
429 | static inline u64 jiffies_64_to_clock_t(u64 x) | ||
430 | { | ||
431 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 | ||
432 | do_div(x, HZ / USER_HZ); | ||
433 | #else | ||
434 | /* | ||
435 | * There are better ways that don't overflow early, | ||
436 | * but even this doesn't overflow in hundreds of years | ||
437 | * in 64 bits, so.. | ||
438 | */ | ||
439 | x *= TICK_NSEC; | ||
440 | do_div(x, (NSEC_PER_SEC / USER_HZ)); | ||
441 | #endif | ||
442 | return x; | ||
443 | } | ||
444 | |||
445 | static inline u64 nsec_to_clock_t(u64 x) | ||
446 | { | ||
447 | #if (NSEC_PER_SEC % USER_HZ) == 0 | ||
448 | do_div(x, (NSEC_PER_SEC / USER_HZ)); | ||
449 | #elif (USER_HZ % 512) == 0 | ||
450 | x *= USER_HZ/512; | ||
451 | do_div(x, (NSEC_PER_SEC / 512)); | ||
452 | #else | ||
453 | /* | ||
454 | * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024, | ||
455 | * overflow after 64.99 years. | ||
456 | * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ... | ||
457 | */ | ||
458 | x *= 9; | ||
459 | do_div(x, (unsigned long)((9ull * NSEC_PER_SEC + (USER_HZ/2)) | ||
460 | / USER_HZ)); | ||
461 | #endif | ||
462 | return x; | ||
463 | } | ||
464 | 280 | ||
465 | #endif | 281 | #endif |
diff --git a/kernel/time.c b/kernel/time.c index 0e017bff4c19..4a8657171584 100644 --- a/kernel/time.c +++ b/kernel/time.c | |||
@@ -470,6 +470,219 @@ struct timeval ns_to_timeval(const s64 nsec) | |||
470 | return tv; | 470 | return tv; |
471 | } | 471 | } |
472 | 472 | ||
473 | /* | ||
474 | * Convert jiffies to milliseconds and back. | ||
475 | * | ||
476 | * Avoid unnecessary multiplications/divisions in the | ||
477 | * two most common HZ cases: | ||
478 | */ | ||
479 | unsigned int jiffies_to_msecs(const unsigned long j) | ||
480 | { | ||
481 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) | ||
482 | return (MSEC_PER_SEC / HZ) * j; | ||
483 | #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) | ||
484 | return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); | ||
485 | #else | ||
486 | return (j * MSEC_PER_SEC) / HZ; | ||
487 | #endif | ||
488 | } | ||
489 | EXPORT_SYMBOL(jiffies_to_msecs); | ||
490 | |||
491 | unsigned int jiffies_to_usecs(const unsigned long j) | ||
492 | { | ||
493 | #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) | ||
494 | return (USEC_PER_SEC / HZ) * j; | ||
495 | #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) | ||
496 | return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC); | ||
497 | #else | ||
498 | return (j * USEC_PER_SEC) / HZ; | ||
499 | #endif | ||
500 | } | ||
501 | EXPORT_SYMBOL(jiffies_to_usecs); | ||
502 | |||
503 | unsigned long msecs_to_jiffies(const unsigned int m) | ||
504 | { | ||
505 | if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) | ||
506 | return MAX_JIFFY_OFFSET; | ||
507 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) | ||
508 | return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ); | ||
509 | #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) | ||
510 | return m * (HZ / MSEC_PER_SEC); | ||
511 | #else | ||
512 | return (m * HZ + MSEC_PER_SEC - 1) / MSEC_PER_SEC; | ||
513 | #endif | ||
514 | } | ||
515 | EXPORT_SYMBOL(msecs_to_jiffies); | ||
516 | |||
517 | unsigned long usecs_to_jiffies(const unsigned int u) | ||
518 | { | ||
519 | if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) | ||
520 | return MAX_JIFFY_OFFSET; | ||
521 | #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) | ||
522 | return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ); | ||
523 | #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) | ||
524 | return u * (HZ / USEC_PER_SEC); | ||
525 | #else | ||
526 | return (u * HZ + USEC_PER_SEC - 1) / USEC_PER_SEC; | ||
527 | #endif | ||
528 | } | ||
529 | EXPORT_SYMBOL(usecs_to_jiffies); | ||
530 | |||
531 | /* | ||
532 | * The TICK_NSEC - 1 rounds up the value to the next resolution. Note | ||
533 | * that a remainder subtract here would not do the right thing as the | ||
534 | * resolution values don't fall on second boundries. I.e. the line: | ||
535 | * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding. | ||
536 | * | ||
537 | * Rather, we just shift the bits off the right. | ||
538 | * | ||
539 | * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec | ||
540 | * value to a scaled second value. | ||
541 | */ | ||
542 | unsigned long | ||
543 | timespec_to_jiffies(const struct timespec *value) | ||
544 | { | ||
545 | unsigned long sec = value->tv_sec; | ||
546 | long nsec = value->tv_nsec + TICK_NSEC - 1; | ||
547 | |||
548 | if (sec >= MAX_SEC_IN_JIFFIES){ | ||
549 | sec = MAX_SEC_IN_JIFFIES; | ||
550 | nsec = 0; | ||
551 | } | ||
552 | return (((u64)sec * SEC_CONVERSION) + | ||
553 | (((u64)nsec * NSEC_CONVERSION) >> | ||
554 | (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; | ||
555 | |||
556 | } | ||
557 | EXPORT_SYMBOL(timespec_to_jiffies); | ||
558 | |||
559 | void | ||
560 | jiffies_to_timespec(const unsigned long jiffies, struct timespec *value) | ||
561 | { | ||
562 | /* | ||
563 | * Convert jiffies to nanoseconds and separate with | ||
564 | * one divide. | ||
565 | */ | ||
566 | u64 nsec = (u64)jiffies * TICK_NSEC; | ||
567 | value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_nsec); | ||
568 | } | ||
569 | EXPORT_SYMBOL(jiffies_to_timespec); | ||
570 | |||
571 | /* Same for "timeval" | ||
572 | * | ||
573 | * Well, almost. The problem here is that the real system resolution is | ||
574 | * in nanoseconds and the value being converted is in micro seconds. | ||
575 | * Also for some machines (those that use HZ = 1024, in-particular), | ||
576 | * there is a LARGE error in the tick size in microseconds. | ||
577 | |||
578 | * The solution we use is to do the rounding AFTER we convert the | ||
579 | * microsecond part. Thus the USEC_ROUND, the bits to be shifted off. | ||
580 | * Instruction wise, this should cost only an additional add with carry | ||
581 | * instruction above the way it was done above. | ||
582 | */ | ||
583 | unsigned long | ||
584 | timeval_to_jiffies(const struct timeval *value) | ||
585 | { | ||
586 | unsigned long sec = value->tv_sec; | ||
587 | long usec = value->tv_usec; | ||
588 | |||
589 | if (sec >= MAX_SEC_IN_JIFFIES){ | ||
590 | sec = MAX_SEC_IN_JIFFIES; | ||
591 | usec = 0; | ||
592 | } | ||
593 | return (((u64)sec * SEC_CONVERSION) + | ||
594 | (((u64)usec * USEC_CONVERSION + USEC_ROUND) >> | ||
595 | (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; | ||
596 | } | ||
597 | |||
598 | void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value) | ||
599 | { | ||
600 | /* | ||
601 | * Convert jiffies to nanoseconds and separate with | ||
602 | * one divide. | ||
603 | */ | ||
604 | u64 nsec = (u64)jiffies * TICK_NSEC; | ||
605 | long tv_usec; | ||
606 | |||
607 | value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &tv_usec); | ||
608 | tv_usec /= NSEC_PER_USEC; | ||
609 | value->tv_usec = tv_usec; | ||
610 | } | ||
611 | |||
612 | /* | ||
613 | * Convert jiffies/jiffies_64 to clock_t and back. | ||
614 | */ | ||
615 | clock_t jiffies_to_clock_t(long x) | ||
616 | { | ||
617 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 | ||
618 | return x / (HZ / USER_HZ); | ||
619 | #else | ||
620 | u64 tmp = (u64)x * TICK_NSEC; | ||
621 | do_div(tmp, (NSEC_PER_SEC / USER_HZ)); | ||
622 | return (long)tmp; | ||
623 | #endif | ||
624 | } | ||
625 | EXPORT_SYMBOL(jiffies_to_clock_t); | ||
626 | |||
627 | unsigned long clock_t_to_jiffies(unsigned long x) | ||
628 | { | ||
629 | #if (HZ % USER_HZ)==0 | ||
630 | if (x >= ~0UL / (HZ / USER_HZ)) | ||
631 | return ~0UL; | ||
632 | return x * (HZ / USER_HZ); | ||
633 | #else | ||
634 | u64 jif; | ||
635 | |||
636 | /* Don't worry about loss of precision here .. */ | ||
637 | if (x >= ~0UL / HZ * USER_HZ) | ||
638 | return ~0UL; | ||
639 | |||
640 | /* .. but do try to contain it here */ | ||
641 | jif = x * (u64) HZ; | ||
642 | do_div(jif, USER_HZ); | ||
643 | return jif; | ||
644 | #endif | ||
645 | } | ||
646 | EXPORT_SYMBOL(clock_t_to_jiffies); | ||
647 | |||
648 | u64 jiffies_64_to_clock_t(u64 x) | ||
649 | { | ||
650 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 | ||
651 | do_div(x, HZ / USER_HZ); | ||
652 | #else | ||
653 | /* | ||
654 | * There are better ways that don't overflow early, | ||
655 | * but even this doesn't overflow in hundreds of years | ||
656 | * in 64 bits, so.. | ||
657 | */ | ||
658 | x *= TICK_NSEC; | ||
659 | do_div(x, (NSEC_PER_SEC / USER_HZ)); | ||
660 | #endif | ||
661 | return x; | ||
662 | } | ||
663 | |||
664 | EXPORT_SYMBOL(jiffies_64_to_clock_t); | ||
665 | |||
666 | u64 nsec_to_clock_t(u64 x) | ||
667 | { | ||
668 | #if (NSEC_PER_SEC % USER_HZ) == 0 | ||
669 | do_div(x, (NSEC_PER_SEC / USER_HZ)); | ||
670 | #elif (USER_HZ % 512) == 0 | ||
671 | x *= USER_HZ/512; | ||
672 | do_div(x, (NSEC_PER_SEC / 512)); | ||
673 | #else | ||
674 | /* | ||
675 | * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024, | ||
676 | * overflow after 64.99 years. | ||
677 | * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ... | ||
678 | */ | ||
679 | x *= 9; | ||
680 | do_div(x, (unsigned long)((9ull * NSEC_PER_SEC + (USER_HZ/2)) / | ||
681 | USER_HZ)); | ||
682 | #endif | ||
683 | return x; | ||
684 | } | ||
685 | |||
473 | #if (BITS_PER_LONG < 64) | 686 | #if (BITS_PER_LONG < 64) |
474 | u64 get_jiffies_64(void) | 687 | u64 get_jiffies_64(void) |
475 | { | 688 | { |