Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A few more tiers file and a convenient perl script for the alnem project. I
[simgrid.git] / src / xbt / sysdep.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_portability.h" /* private */
14 #include "xbt/log.h"
15 #include "portable.h"
16
17
18 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus 
19    disabled in SDL source code */ 
20
21
22 /* \defgroup XBT_sysdep All system dependency
23  * \brief This section describes many macros/functions that can serve as
24  *  an OS abstraction.
25  */
26
27 double xbt_os_time(void) {
28 #ifdef HAVE_GETTIMEOFDAY
29   struct timeval tv;
30
31   gettimeofday(&tv, NULL);
32
33   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
34 #else
35   /* Poor resolution */
36   return (double)(time(NULL));
37 #endif /* HAVE_GETTIMEOFDAY? */         
38 }
39
40 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");*/
41
42
43 struct s_xbt_os_timer {
44 #ifdef HAVE_GETTIMEOFDAY
45    struct timeval start,stop;
46 #else
47    unsigned long int start,stop;
48 #endif
49 };
50
51 xbt_os_timer_t xbt_os_timer_new(void) {
52    return xbt_new0(struct s_xbt_os_timer,1);
53 }
54 void xbt_os_timer_free(xbt_os_timer_t timer) {
55    free (timer);
56 }
57 void xbt_os_timer_start(xbt_os_timer_t timer) {
58 #ifdef HAVE_GETTIMEOFDAY
59   gettimeofday(&(timer->start), NULL);
60 #else 
61   timer->start = (unsigned long int)(time(NULL));
62 #endif
63 }
64 void xbt_os_timer_stop(xbt_os_timer_t timer) {
65 #ifdef HAVE_GETTIMEOFDAY
66   gettimeofday(&(timer->stop), NULL);
67 #else 
68   timer->stop = (unsigned long int)(time(NULL));
69 #endif
70 }
71 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
72 #ifdef HAVE_GETTIMEOFDAY
73    return  ((double)timer->stop.tv_sec)  - ((double)timer->start.tv_sec) +
74          ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
75 #else 
76    return  (double)timer->stop  - (double)timer->start;
77 #endif
78 }
79