Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c74b4dd66fe4c26e1be1b7952b79bb6215d965b9
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* $Id$ */
2
3 /* sysdep.c -- all system dependency                                        */
4 /*  no system header should be loaded out of this file so that we have only */
5 /*  one file to check when porting to another OS                            */
6
7 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #include "xbt/sysdep.h"
13 #include "xbt/xbt_os_time.h" /* this module */
14 #include "xbt/log.h"
15 #include "portable.h"
16
17
18 /** @brief like free 
19     @hideinitializer */
20 XBT_PUBLIC(void) xbt_free_f(void* p)
21 {
22         free(p);
23 }
24
25
26 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus 
27    disabled in SDL source code */ 
28
29
30 /* \defgroup XBT_sysdep All system dependency
31  * \brief This section describes many macros/functions that can serve as
32  *  an OS abstraction.
33  */
34
35 /*
36 double xbt_os_time(void) {
37 #ifdef HAVE_GETTIMEOFDAY
38   struct timeval tv;
39
40   gettimeofday(&tv, NULL);
41
42   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
43 #else*/
44   /* Poor resolution */
45 /*  return (double)(time(NULL));*/
46 //#endif /* HAVE_GETTIMEOFDAY? */       
47 //}
48
49 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");*/
50
51
52 struct s_xbt_os_timer {
53 #ifdef HAVE_GETTIMEOFDAY
54    struct timeval start,stop;
55 #else
56    unsigned long int start,stop;
57 #endif
58 };
59
60 xbt_os_timer_t xbt_os_timer_new(void) {
61    return xbt_new0(struct s_xbt_os_timer,1);
62 }
63 void xbt_os_timer_free(xbt_os_timer_t timer) {
64    free (timer);
65 }
66 void xbt_os_timer_start(xbt_os_timer_t timer) {
67 #ifdef HAVE_GETTIMEOFDAY
68   gettimeofday(&(timer->start), NULL);
69 #else 
70   timer->start = (unsigned long int)(time(NULL));
71 #endif
72 }
73 void xbt_os_timer_stop(xbt_os_timer_t timer) {
74 #ifdef HAVE_GETTIMEOFDAY
75   gettimeofday(&(timer->stop), NULL);
76 #else 
77   timer->stop = (unsigned long int)(time(NULL));
78 #endif
79 }
80 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
81 #ifdef HAVE_GETTIMEOFDAY
82    return  ((double)timer->stop.tv_sec)  - ((double)timer->start.tv_sec) +
83          ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
84 #else 
85    return  (double)timer->stop  - (double)timer->start;
86 #endif
87 }
88