extern "C" {
#endif
-/**
- * A typedef for "server local" time. This is the time in
- * nanoseconds since the application started.
+/** "server local" time. This is the time in nanoseconds since the application started.
+ *
*/
typedef int64_t fr_time_t;
-/**
- * A structure to track the time spent processing a request.
+/** A time delta, a difference in time measured in nanoseconds.
+ *
+ * This is easier to distinguish where server epoch time is being
+ * used, and where relative time is being used.
+ */
+typedef int64_t fr_time_delta_t;
+
+/** A structure to track the time spent processing a request.
*
- * The same structure is used by threads to track when they are
- * running / waiting. The functions modifying fr_time_tracking_t all
- * take an explicit "when" parameter. This parameter allows the
- * thread to update a requests tracking structure, and then use that
- * same fr_time_t to update the threads tracking structure.
+ * The same structure is used by threads to track when they are
+ * running / waiting. The functions modifying fr_time_tracking_t all
+ * take an explicit "when" parameter. This parameter allows the
+ * thread to update a requests tracking structure, and then use that
+ * same fr_time_t to update the threads tracking structure.
*
- * While fr_time() is fast, it is also called very often. We should
- * therefore be careful to call it only when necessary.
+ * While fr_time() is fast, it is also called very often. We should
+ * therefore be careful to call it only when necessary.
*/
typedef struct {
fr_time_t when; //!< last time we changed a field
int fr_time_start(void);
fr_time_t fr_time(void);
-static inline fr_time_t fr_time_delta_from_usec(uint64_t usec)
+static inline fr_time_delta_t fr_time_delta_from_usec(uint64_t usec)
{
return (usec * 1000);
}
-static inline fr_time_t fr_time_delta_from_msec(uint64_t msec)
+static inline fr_time_delta_t fr_time_delta_from_msec(uint64_t msec)
{
return (msec * 1000000);
}
-static inline fr_time_t fr_time_delta_from_sec(uint64_t sec)
+static inline fr_time_delta_t fr_time_delta_from_sec(uint64_t sec)
{
return (sec * NSEC);
}
-static inline fr_time_t fr_time_delta_from_timeval(struct timeval const *tv)
+static inline fr_time_delta_t fr_time_delta_from_timeval(struct timeval const *tv)
{
return (tv->tv_sec * NSEC) + (tv->tv_usec * 1000);
}
-static inline fr_time_t fr_time_delta_from_timespec(struct timespec const *ts)
+static inline fr_time_delta_t fr_time_delta_from_timespec(struct timespec const *ts)
{
return (ts->tv_sec * NSEC) + ts->tv_nsec;
}
-static inline fr_time_t fr_time_delta_to_usec(uint64_t usec)
+static inline int64_t fr_time_delta_to_usec(fr_time_delta_t delta)
{
- return (usec / 1000);
+ return (delta / 1000);
}
-static inline fr_time_t fr_time_delta_to_msec(uint64_t msec)
+static inline int64_t fr_time_delta_to_msec(fr_time_delta_t delta)
{
- return (msec / 1000000);
+ return (delta / 1000000);
}
-static inline fr_time_t fr_time_delta_to_sec(uint64_t sec)
+static inline int64_t fr_time_delta_to_sec(fr_time_delta_t delta)
{
- return (sec / NSEC);
+ return (delta / NSEC);
}
-static inline void fr_time_delta_to_timeval(struct timeval *tv, fr_time_t delta)
+static inline void fr_time_delta_to_timeval(struct timeval *tv, fr_time_delta_t delta)
{
tv->tv_sec = delta / NSEC;
tv->tv_usec = (delta % NSEC) / 1000;
}
-static inline void fr_time_delta_to_timespec(struct timespec *ts, fr_time_t delta)
+static inline void fr_time_delta_to_timespec(struct timespec *ts, fr_time_delta_t delta)
{
ts->tv_sec = delta / NSEC;
ts->tv_nsec = delta % NSEC;
}
-void fr_time_to_timeval(struct timeval *tv, fr_time_t when) CC_HINT(nonnull);
-void fr_time_to_timespec(struct timespec *ts, fr_time_t when) CC_HINT(nonnull);
-int64_t fr_time_to_usec(fr_time_t when);
-int64_t fr_time_to_msec(fr_time_t when);
-int64_t fr_time_to_sec(fr_time_t when);
+/** Compare two fr_time_t values
+ *
+ * @param[in] a The first value to compare.
+ * @param[in] b The second value to compare.
+ * @return
+ * - +1 if a > b
+ * - 0 if a == b
+ * - -1 if a < b
+ */
+static inline int fr_time_cmp(fr_time_t a, fr_time_t b)
+{
+ return (a > b) - (a < b);
+}
+
+void fr_time_to_timeval(struct timeval *tv, fr_time_t when) CC_HINT(nonnull);
+void fr_time_to_timespec(struct timespec *ts, fr_time_t when) CC_HINT(nonnull);
+int64_t fr_time_to_usec(fr_time_t when);
+int64_t fr_time_to_msec(fr_time_t when);
+int64_t fr_time_to_sec(fr_time_t when);
-fr_time_t fr_time_from_timeval(struct timeval const *when_tv) CC_HINT(nonnull);
-fr_time_t fr_time_from_timespec(struct timespec const *when_tv) CC_HINT(nonnull);
+fr_time_t fr_time_from_timeval(struct timeval const *when_tv) CC_HINT(nonnull);
+fr_time_t fr_time_from_timespec(struct timespec const *when_tv) CC_HINT(nonnull);
void fr_time_tracking_start(fr_time_tracking_t *tt, fr_time_t when, fr_time_tracking_t *worker) CC_HINT(nonnull);
void fr_time_tracking_end(fr_time_tracking_t *tt, fr_time_t when, fr_time_tracking_t *worker) CC_HINT(nonnull);