Logo AND Algorithmique Numérique Distribuée

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