Browse Source

- ser equivalents to time(2) and gettimeofday(2), using internal ser time
(faster then making a syscall, but at least in the gettimeofday case more
imprecise, can be about 0.1-0.2 s off)

Andrei Pelinescu-Onciul 17 năm trước cách đây
mục cha
commit
9b089e21ee
2 tập tin đã thay đổi với 104 bổ sung0 xóa
  1. 38 0
      ser_time.h
  2. 66 0
      timer.c

+ 38 - 0
ser_time.h

@@ -0,0 +1,38 @@
+/* 
+ * $Id$
+ * 
+ * time related functions
+ *
+ * Copyright (C) 2006 iptelorg GmbH
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+/* History:
+ * --------
+ *  2008-07-16  initial version (andrei)
+ */
+#ifndef _ser_time_h
+#define _ser_time_h
+
+#include <sys/time.h>
+#include <time.h>
+
+/* time(2) equivalent, using ser internal timers (faster then a syscall) */
+time_t ser_time(time_t* t);
+
+/* gettimeofday(2) equivalent, faster but much more imprecise
+ * (in normal conditions should be within 0.1 s of the real time)
+ * WARNING: ignores tz (it's obsolete anyway) */
+int ser_gettimeofday(struct timeval* tv, const struct timezone *tz);
+
+#endif /* _ser_time_h */

+ 66 - 0
timer.c

@@ -394,6 +394,13 @@ int arm_timer()
 
 
 
+#ifdef DBG_ser_time
+/* debugging  only */
+void check_ser_drift();
+#endif /* DBG_set_time */
+
+
+
 /* adjust the timer using the "real" time, each TIMER_RESYNC_TICKS, but only
  * if timer drift > TIMER_MAX_DRIFT
  * NOTES: - it will adjust time within  TIMER_MAX_DRIFT from the "real"
@@ -412,6 +419,9 @@ inline static void adjust_ticks()
 	
 	/* fix ticks if necessary */
 	if ((*ticks-last_adj_check)>=(ticks_t)TIMER_RESYNC_TICKS){
+#ifdef DBG_ser_time
+		check_ser_drift();
+#endif /* DBG_ser_time */
 		last_adj_check=*ticks;
 		if (gettimeofday(&crt_time, 0)<0){
 			LOG(L_ERR, "ERROR: adjust_ticks: gettimeofday failed: %s [%d]\n",
@@ -461,6 +471,62 @@ inline static void adjust_ticks()
 
 
 
+/* time(2) equivalent, using ser internal timers (faster then a syscall) */
+time_t ser_time(time_t *t)
+{
+	if (likely(t==0))
+		return last_time.tv_sec+TICKS_TO_S(*ticks-last_ticks);
+	*t=last_time.tv_sec+TICKS_TO_S(*ticks-last_ticks);
+	return *t;
+}
+
+
+
+/* gettimeofday(2) equivalent, using ser internal timers (faster 
+ * but more imprecise)
+ * WARNING: ignores tz (it's obsolete anyway)*/
+int ser_gettimeofday(struct timeval* tv, struct timezone* tz)
+{
+	if (likely(tv!=0)){
+		tv->tv_sec=last_time.tv_sec+TICKS_TO_S(*ticks-last_ticks);
+		tv->tv_usec=last_time.tv_usec+
+					(TICKS_TO_MS(*ticks-last_ticks)%1000)*1000;
+	}
+	return 0;
+}
+
+
+
+#ifdef DBG_ser_time
+/* debugging  only, remove */
+void check_ser_drift()
+{
+	time_t t1, t2;
+	struct timeval tv1, tv2;
+	int r;
+	
+	t1=time(0);
+	t2=ser_time(0);
+	if (t1!=t2)
+		BUG("time(0)!=ser_time(0) : %d != %d \n", (unsigned)t1, (unsigned)t2);
+	
+	r=gettimeofday(&tv1, 0);
+	ser_gettimeofday(&tv2, 0);
+	if (tv1.tv_sec!=tv2.tv_sec)
+		BUG("gettimeofday seconds!=ser_gettimeofday seconds : %d != %d \n",
+				(unsigned)tv1.tv_sec, (unsigned)tv2.tv_sec);
+	else if ((tv1.tv_usec > tv2.tv_usec) && 
+				(unsigned)(tv1.tv_usec-tv2.tv_usec)>100000)
+		BUG("gettimeofday usecs > ser_gettimeofday with > 0.1s : %d ms\n",
+			(unsigned)(tv1.tv_usec-tv2.tv_usec)/1000);
+	else if ((tv1.tv_usec < tv2.tv_usec) && 
+				(unsigned)(tv2.tv_usec-tv1.tv_usec)>100000)
+		BUG("gettimeofday usecs < ser_gettimeofday with > 0.1s : %d ms\n",
+			(unsigned)(tv2.tv_usec-tv1.tv_usec)/1000);
+}
+#endif /* DBG_ser_time */
+
+
 
 struct timer_ln* timer_alloc()
 {