Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  include/linux/ktime.h
0003  *
0004  *  ktime_t - nanosecond-resolution time format.
0005  *
0006  *   Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
0007  *   Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
0008  *
0009  *  data type definitions, declarations, prototypes and macros.
0010  *
0011  *  Started by: Thomas Gleixner and Ingo Molnar
0012  *
0013  *  Credits:
0014  *
0015  *      Roman Zippel provided the ideas and primary code snippets of
0016  *      the ktime_t union and further simplifications of the original
0017  *      code.
0018  *
0019  *  For licencing details see kernel-base/COPYING
0020  */
0021 #ifndef _LINUX_KTIME_H
0022 #define _LINUX_KTIME_H
0023 
0024 #include <linux/time.h>
0025 #include <linux/jiffies.h>
0026 #include <asm/bug.h>
0027 
0028 /* Nanosecond scalar representation for kernel time values */
0029 typedef s64 ktime_t;
0030 
0031 /**
0032  * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
0033  * @secs:   seconds to set
0034  * @nsecs:  nanoseconds to set
0035  *
0036  * Return: The ktime_t representation of the value.
0037  */
0038 static inline ktime_t ktime_set(const s64 secs, const unsigned long nsecs)
0039 {
0040     if (unlikely(secs >= KTIME_SEC_MAX))
0041         return KTIME_MAX;
0042 
0043     return secs * NSEC_PER_SEC + (s64)nsecs;
0044 }
0045 
0046 /* Subtract two ktime_t variables. rem = lhs -rhs: */
0047 #define ktime_sub(lhs, rhs) ((lhs) - (rhs))
0048 
0049 /* Add two ktime_t variables. res = lhs + rhs: */
0050 #define ktime_add(lhs, rhs) ((lhs) + (rhs))
0051 
0052 /*
0053  * Same as ktime_add(), but avoids undefined behaviour on overflow; however,
0054  * this means that you must check the result for overflow yourself.
0055  */
0056 #define ktime_add_unsafe(lhs, rhs)  ((u64) (lhs) + (rhs))
0057 
0058 /*
0059  * Add a ktime_t variable and a scalar nanosecond value.
0060  * res = kt + nsval:
0061  */
0062 #define ktime_add_ns(kt, nsval)     ((kt) + (nsval))
0063 
0064 /*
0065  * Subtract a scalar nanosecod from a ktime_t variable
0066  * res = kt - nsval:
0067  */
0068 #define ktime_sub_ns(kt, nsval)     ((kt) - (nsval))
0069 
0070 /* convert a timespec64 to ktime_t format: */
0071 static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
0072 {
0073     return ktime_set(ts.tv_sec, ts.tv_nsec);
0074 }
0075 
0076 /* Map the ktime_t to timespec conversion to ns_to_timespec function */
0077 #define ktime_to_timespec64(kt)     ns_to_timespec64((kt))
0078 
0079 /* Convert ktime_t to nanoseconds */
0080 static inline s64 ktime_to_ns(const ktime_t kt)
0081 {
0082     return kt;
0083 }
0084 
0085 /**
0086  * ktime_compare - Compares two ktime_t variables for less, greater or equal
0087  * @cmp1:   comparable1
0088  * @cmp2:   comparable2
0089  *
0090  * Return: ...
0091  *   cmp1  < cmp2: return <0
0092  *   cmp1 == cmp2: return 0
0093  *   cmp1  > cmp2: return >0
0094  */
0095 static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
0096 {
0097     if (cmp1 < cmp2)
0098         return -1;
0099     if (cmp1 > cmp2)
0100         return 1;
0101     return 0;
0102 }
0103 
0104 /**
0105  * ktime_after - Compare if a ktime_t value is bigger than another one.
0106  * @cmp1:   comparable1
0107  * @cmp2:   comparable2
0108  *
0109  * Return: true if cmp1 happened after cmp2.
0110  */
0111 static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
0112 {
0113     return ktime_compare(cmp1, cmp2) > 0;
0114 }
0115 
0116 /**
0117  * ktime_before - Compare if a ktime_t value is smaller than another one.
0118  * @cmp1:   comparable1
0119  * @cmp2:   comparable2
0120  *
0121  * Return: true if cmp1 happened before cmp2.
0122  */
0123 static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
0124 {
0125     return ktime_compare(cmp1, cmp2) < 0;
0126 }
0127 
0128 #if BITS_PER_LONG < 64
0129 extern s64 __ktime_divns(const ktime_t kt, s64 div);
0130 static inline s64 ktime_divns(const ktime_t kt, s64 div)
0131 {
0132     /*
0133      * Negative divisors could cause an inf loop,
0134      * so bug out here.
0135      */
0136     BUG_ON(div < 0);
0137     if (__builtin_constant_p(div) && !(div >> 32)) {
0138         s64 ns = kt;
0139         u64 tmp = ns < 0 ? -ns : ns;
0140 
0141         do_div(tmp, div);
0142         return ns < 0 ? -tmp : tmp;
0143     } else {
0144         return __ktime_divns(kt, div);
0145     }
0146 }
0147 #else /* BITS_PER_LONG < 64 */
0148 static inline s64 ktime_divns(const ktime_t kt, s64 div)
0149 {
0150     /*
0151      * 32-bit implementation cannot handle negative divisors,
0152      * so catch them on 64bit as well.
0153      */
0154     WARN_ON(div < 0);
0155     return kt / div;
0156 }
0157 #endif
0158 
0159 static inline s64 ktime_to_us(const ktime_t kt)
0160 {
0161     return ktime_divns(kt, NSEC_PER_USEC);
0162 }
0163 
0164 static inline s64 ktime_to_ms(const ktime_t kt)
0165 {
0166     return ktime_divns(kt, NSEC_PER_MSEC);
0167 }
0168 
0169 static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
0170 {
0171        return ktime_to_us(ktime_sub(later, earlier));
0172 }
0173 
0174 static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
0175 {
0176     return ktime_to_ms(ktime_sub(later, earlier));
0177 }
0178 
0179 static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec)
0180 {
0181     return ktime_add_ns(kt, usec * NSEC_PER_USEC);
0182 }
0183 
0184 static inline ktime_t ktime_add_ms(const ktime_t kt, const u64 msec)
0185 {
0186     return ktime_add_ns(kt, msec * NSEC_PER_MSEC);
0187 }
0188 
0189 static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec)
0190 {
0191     return ktime_sub_ns(kt, usec * NSEC_PER_USEC);
0192 }
0193 
0194 static inline ktime_t ktime_sub_ms(const ktime_t kt, const u64 msec)
0195 {
0196     return ktime_sub_ns(kt, msec * NSEC_PER_MSEC);
0197 }
0198 
0199 extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs);
0200 
0201 /**
0202  * ktime_to_timespec64_cond - convert a ktime_t variable to timespec64
0203  *              format only if the variable contains data
0204  * @kt:     the ktime_t variable to convert
0205  * @ts:     the timespec variable to store the result in
0206  *
0207  * Return: %true if there was a successful conversion, %false if kt was 0.
0208  */
0209 static inline __must_check bool ktime_to_timespec64_cond(const ktime_t kt,
0210                                struct timespec64 *ts)
0211 {
0212     if (kt) {
0213         *ts = ktime_to_timespec64(kt);
0214         return true;
0215     } else {
0216         return false;
0217     }
0218 }
0219 
0220 #include <vdso/ktime.h>
0221 
0222 static inline ktime_t ns_to_ktime(u64 ns)
0223 {
0224     return ns;
0225 }
0226 
0227 static inline ktime_t ms_to_ktime(u64 ms)
0228 {
0229     return ms * NSEC_PER_MSEC;
0230 }
0231 
0232 # include <linux/timekeeping.h>
0233 
0234 #endif