aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/time.c')
-rw-r--r--kernel/time.c213
1 files changed, 213 insertions, 0 deletions
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 */
479unsigned 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}
489EXPORT_SYMBOL(jiffies_to_msecs);
490
491unsigned 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}
501EXPORT_SYMBOL(jiffies_to_usecs);
502
503unsigned 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}
515EXPORT_SYMBOL(msecs_to_jiffies);
516
517unsigned 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}
529EXPORT_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 */
542unsigned long
543timespec_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}
557EXPORT_SYMBOL(timespec_to_jiffies);
558
559void
560jiffies_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}
569EXPORT_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 */
583unsigned long
584timeval_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
598void 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 */
615clock_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}
625EXPORT_SYMBOL(jiffies_to_clock_t);
626
627unsigned 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}
646EXPORT_SYMBOL(clock_t_to_jiffies);
647
648u64 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
664EXPORT_SYMBOL(jiffies_64_to_clock_t);
665
666u64 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)
474u64 get_jiffies_64(void) 687u64 get_jiffies_64(void)
475{ 688{