00001 #ifndef KAL_TIME_JIFFY_H_
00002 #define KAL_TIME_JIFFY_H_
00003
00004 #include <linux/time.h>
00005 #ifdef QOS_KS
00006 # include <linux/timer.h>
00007 # include <linux/jiffies.h>
00008 #endif
00009
00010
00011
00013 typedef unsigned long kal_time_t;
00014
00015 #define KAL_TIME_NS(sec, nsec) timespec_to_jiffies(&(struct timespec) { .tv_sec = (sec), .tv_nsec = ((long unsigned) nsec) })
00016 #define KAL_TIME_US(sec, usec) timespec_to_jiffies(&(struct timespec) { .tv_sec = (sec), .tv_nsec = ((long unsigned) usec) * 1000 })
00017
00018 #define KAL_TIME_FMT "<%6lu.%5lu>"
00019
00020 #define KAL_TIME_FMT_ARG(t) kal_time_get_sec(t), kal_time_get_usec(t)
00021
00022 static inline kal_time_t kal_time_ns(unsigned long sec, unsigned long nsec) {
00023 struct timespec ts = { .tv_sec = (sec), .tv_nsec = (nsec) };
00024 return timespec_to_jiffies(&ts);
00025 }
00026
00027 static inline unsigned long kal_time_get_sec(kal_time_t t) {
00028 return jiffies_to_msecs(t) / 1000;
00029 }
00030
00031 static inline unsigned long kal_time_get_usec(kal_time_t t) {
00032 return jiffies_to_usecs(t) % 1000000;
00033 }
00034
00035 static inline unsigned long kal_time_get_nsec(kal_time_t t) {
00036 struct timespec ts;
00037 jiffies_to_timespec(t, &ts);
00038 return ts.tv_nsec;
00039 }
00040
00041 static inline unsigned long long kal_time2usec(kal_time_t t) {
00042 return jiffies_to_usecs(t);
00043 }
00044
00045 static inline kal_time_t kal_usec2time(unsigned long long usec) {
00046 return usecs_to_jiffies(usec);
00047 }
00048
00049 static inline kal_time_t kal_time_add(kal_time_t ta, kal_time_t tb) {
00050 return ta + tb;
00051 }
00052
00053 static inline kal_time_t kal_time_sub(kal_time_t ta, kal_time_t tb) {
00054 return ta - tb;
00055 }
00056
00058 static inline int kal_time_lt(kal_time_t t1, kal_time_t t2) {
00059 return time_before(t1, t2);
00060 }
00061
00062 static inline int kal_time_le(kal_time_t t1, kal_time_t t2) {
00063 return time_before_eq(t1, t2);
00064 }
00065
00066 static inline struct timespec kal_time2timespec(kal_time_t t) {
00067 struct timespec ts;
00068 jiffies_to_timespec(t, &ts);
00069 return ts;
00070 }
00071
00072 #ifdef QOS_KS
00073 static inline unsigned long get_jiffies(void) { return jiffies; }
00074
00075 #else
00076
00077 unsigned long get_simulation_time(void);
00078 static inline unsigned long get_jiffies(void) { return get_simulation_time();}
00079
00080 static inline unsigned long timespec_to_jiffies(const struct timespec *t) {
00081 return t->tv_sec * 1000000ull + (unsigned long long) (t->tv_nsec / 1000);
00082 }
00083
00084 static inline void jiffies_to_timespec(const unsigned long jiffies, struct timespec *t) {
00085 unsigned long us = jiffies;
00086 *t = KAL_TIME_US((unsigned long) (us / 1000000ul), (unsigned long) (us % 1000000ul));
00087 }
00088 #endif
00089
00090 static inline kal_time_t kal_time_now(void) {
00091 return jiffies;
00092 }
00093
00094 static inline unsigned long kal_time2jiffies(kal_time_t t) {
00095 return t;
00096 }
00097
00098 static inline kal_time_t kal_jiffies2time(unsigned long jiffies) {
00099 return jiffies;
00100 }
00101
00102 #endif