00001 #ifndef KAL_TIME_TS_H_
00002 #define KAL_TIME_TS_H_
00003
00010 #include <linux/time.h>
00011 #ifdef QOS_KS
00012 # include <linux/ktime.h>
00013 #endif
00014
00015
00016
00023 typedef struct timespec kal_time_t;
00024
00025 #define KAL_TIME_NS(sec, nsec) (struct timespec) { .tv_sec = (sec), .tv_nsec = ((long unsigned) nsec) }
00026 #define KAL_TIME_US(sec, usec) (struct timespec) { .tv_sec = (sec), .tv_nsec = ((long unsigned) usec) * 1000 }
00027
00028 #define KAL_TIME_FMT "<%6lu.%06lu>"
00029
00030 #define KAL_TIME_FMT_ARG(t) kal_time_get_sec(t), kal_time_get_usec(t)
00031
00032 static inline kal_time_t kal_time_ns(unsigned long sec, unsigned long nsec) {
00033 struct timespec ts = { .tv_sec = (sec), .tv_nsec = (nsec) };
00034 return ts;
00035 }
00036
00037 static inline signed long kal_time_get_sec(kal_time_t t) {
00038 return t.tv_sec;
00039 }
00040
00041 static inline unsigned long kal_time_get_nsec(kal_time_t t) {
00042 return t.tv_nsec;
00043 }
00044
00045 static inline unsigned long kal_time_get_usec(kal_time_t t) {
00046 return t.tv_nsec / 1000;
00047 }
00048
00049 static inline unsigned long long kal_time2usec(kal_time_t t) {
00050 return t.tv_sec * 1000000ull + (unsigned long long) (t.tv_nsec / 1000);
00051 }
00052
00053 static inline kal_time_t kal_usec2time(unsigned long long usec) {
00054 unsigned long us = (unsigned long) usec;
00055 kal_time_t t = KAL_TIME_US((unsigned long) (us / 1000000ul), (unsigned long) (us % 1000000ul));
00056 return t;
00057 }
00058
00059 static inline kal_time_t kal_time_add(kal_time_t ta, kal_time_t tb) {
00060 struct timespec ts = {
00061 .tv_sec = ta.tv_sec + tb.tv_sec,
00062 .tv_nsec = ta.tv_nsec + tb.tv_nsec
00063 };
00064 if (unlikely(ts.tv_nsec >= 1000000000L)) {
00065 ts.tv_nsec -= 1000000000L;
00066 ts.tv_sec++;
00067 }
00068 return ts;
00069 }
00070
00071 static inline kal_time_t kal_time_sub(kal_time_t ta, kal_time_t tb) {
00072 struct timespec ts = {
00073 .tv_sec = ta.tv_sec - tb.tv_sec,
00074 .tv_nsec = ta.tv_nsec - tb.tv_nsec
00075 };
00076 if (unlikely( ((signed long) ts.tv_nsec) < 0L )) {
00077 ts.tv_nsec += 1000000000L;
00078 ts.tv_sec--;
00079 }
00080 return ts;
00081 }
00082
00083 static inline struct timespec kal_time2timespec(kal_time_t t) {
00084 return (struct timespec) t;
00085 }
00086
00088 static inline int kal_time_lt(kal_time_t t1, kal_time_t t2) {
00089 return (((signed long) t1.tv_sec) < (signed long) t2.tv_sec)
00090 || (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec);
00091 }
00092
00093 static inline int kal_time_le(kal_time_t t1, kal_time_t t2) {
00094 return (((signed long) t1.tv_sec) < (signed long) t2.tv_sec)
00095 || (t1.tv_sec == t2.tv_sec && t1.tv_nsec <= t2.tv_nsec);
00096 }
00097
00098 #ifdef QOS_KS
00099
00100 static inline kal_time_t kal_time_now(void) {
00101 struct timespec ts;
00102 ktime_get_ts(&ts);
00103
00104 return ts;
00105 }
00106
00107 #else
00108
00109 unsigned long get_simulation_time(void);
00110
00111 static inline unsigned long timespec_to_usec(const struct timespec *t) {
00112 return t->tv_sec * 1000000ull + (unsigned long long) (t->tv_nsec / 1000);
00113 }
00114
00115 static inline void usec_to_timespec(const unsigned long usec, struct timespec *t) {
00116 *t = KAL_TIME_US((unsigned long) (usec / 1000000ul), (unsigned long) (usec % 1000000ul));
00117 }
00118
00119 static inline kal_time_t kal_time_now(void) {
00120 return usec_to_timespec(get_simulation_time());
00121 }
00122
00123 #endif
00124
00125 #endif