Browse code

- 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 authored on 17/07/2008 07:51:34
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,38 @@
1
+/* 
2
+ * $Id$
3
+ * 
4
+ * time related functions
5
+ *
6
+ * Copyright (C) 2006 iptelorg GmbH
7
+ *
8
+ * Permission to use, copy, modify, and distribute this software for any
9
+ * purpose with or without fee is hereby granted, provided that the above
10
+ * copyright notice and this permission notice appear in all copies.
11
+ *
12
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
+ */
20
+/* History:
21
+ * --------
22
+ *  2008-07-16  initial version (andrei)
23
+ */
24
+#ifndef _ser_time_h
25
+#define _ser_time_h
26
+
27
+#include <sys/time.h>
28
+#include <time.h>
29
+
30
+/* time(2) equivalent, using ser internal timers (faster then a syscall) */
31
+time_t ser_time(time_t* t);
32
+
33
+/* gettimeofday(2) equivalent, faster but much more imprecise
34
+ * (in normal conditions should be within 0.1 s of the real time)
35
+ * WARNING: ignores tz (it's obsolete anyway) */
36
+int ser_gettimeofday(struct timeval* tv, const struct timezone *tz);
37
+
38
+#endif /* _ser_time_h */
... ...
@@ -394,6 +394,13 @@ int arm_timer()
394 394
 
395 395
 
396 396
 
397
+#ifdef DBG_ser_time
398
+/* debugging  only */
399
+void check_ser_drift();
400
+#endif /* DBG_set_time */
401
+
402
+
403
+
397 404
 /* adjust the timer using the "real" time, each TIMER_RESYNC_TICKS, but only
398 405
  * if timer drift > TIMER_MAX_DRIFT
399 406
  * NOTES: - it will adjust time within  TIMER_MAX_DRIFT from the "real"
... ...
@@ -412,6 +419,9 @@ inline static void adjust_ticks()
412 419
 	
413 420
 	/* fix ticks if necessary */
414 421
 	if ((*ticks-last_adj_check)>=(ticks_t)TIMER_RESYNC_TICKS){
422
+#ifdef DBG_ser_time
423
+		check_ser_drift();
424
+#endif /* DBG_ser_time */
415 425
 		last_adj_check=*ticks;
416 426
 		if (gettimeofday(&crt_time, 0)<0){
417 427
 			LOG(L_ERR, "ERROR: adjust_ticks: gettimeofday failed: %s [%d]\n",
... ...
@@ -461,6 +471,62 @@ inline static void adjust_ticks()
461 471
 
462 472
 
463 473
 
474
+/* time(2) equivalent, using ser internal timers (faster then a syscall) */
475
+time_t ser_time(time_t *t)
476
+{
477
+	if (likely(t==0))
478
+		return last_time.tv_sec+TICKS_TO_S(*ticks-last_ticks);
479
+	*t=last_time.tv_sec+TICKS_TO_S(*ticks-last_ticks);
480
+	return *t;
481
+}
482
+
483
+
484
+
485
+/* gettimeofday(2) equivalent, using ser internal timers (faster 
486
+ * but more imprecise)
487
+ * WARNING: ignores tz (it's obsolete anyway)*/
488
+int ser_gettimeofday(struct timeval* tv, struct timezone* tz)
489
+{
490
+	if (likely(tv!=0)){
491
+		tv->tv_sec=last_time.tv_sec+TICKS_TO_S(*ticks-last_ticks);
492
+		tv->tv_usec=last_time.tv_usec+
493
+					(TICKS_TO_MS(*ticks-last_ticks)%1000)*1000;
494
+	}
495
+	return 0;
496
+}
497
+
498
+
499
+
500
+#ifdef DBG_ser_time
501
+/* debugging  only, remove */
502
+void check_ser_drift()
503
+{
504
+	time_t t1, t2;
505
+	struct timeval tv1, tv2;
506
+	int r;
507
+	
508
+	t1=time(0);
509
+	t2=ser_time(0);
510
+	if (t1!=t2)
511
+		BUG("time(0)!=ser_time(0) : %d != %d \n", (unsigned)t1, (unsigned)t2);
512
+	
513
+	r=gettimeofday(&tv1, 0);
514
+	ser_gettimeofday(&tv2, 0);
515
+	if (tv1.tv_sec!=tv2.tv_sec)
516
+		BUG("gettimeofday seconds!=ser_gettimeofday seconds : %d != %d \n",
517
+				(unsigned)tv1.tv_sec, (unsigned)tv2.tv_sec);
518
+	else if ((tv1.tv_usec > tv2.tv_usec) && 
519
+				(unsigned)(tv1.tv_usec-tv2.tv_usec)>100000)
520
+		BUG("gettimeofday usecs > ser_gettimeofday with > 0.1s : %d ms\n",
521
+			(unsigned)(tv1.tv_usec-tv2.tv_usec)/1000);
522
+	else if ((tv1.tv_usec < tv2.tv_usec) && 
523
+				(unsigned)(tv2.tv_usec-tv1.tv_usec)>100000)
524
+		BUG("gettimeofday usecs < ser_gettimeofday with > 0.1s : %d ms\n",
525
+			(unsigned)(tv2.tv_usec-tv1.tv_usec)/1000);
526
+}
527
+#endif /* DBG_ser_time */
528
+
529
+
464 530
 
465 531
 struct timer_ln* timer_alloc()
466 532
 {