~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to preprocessor/util/ecs_timer.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*============================================================================
 
2
 * Program timing information
 
3
 *============================================================================*/
 
4
 
 
5
/*
 
6
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
7
 
 
8
  Copyright (C) 1998-2011 EDF S.A.
 
9
 
 
10
  This program is free software; you can redistribute it and/or modify it under
 
11
  the terms of the GNU General Public License as published by the Free Software
 
12
  Foundation; either version 2 of the License, or (at your option) any later
 
13
  version.
 
14
 
 
15
  This program is distributed in the hope that it will be useful, but WITHOUT
 
16
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
18
  details.
 
19
 
 
20
  You should have received a copy of the GNU General Public License along with
 
21
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
22
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
23
*/
 
24
 
 
25
/*----------------------------------------------------------------------------*/
 
26
 
 
27
#include "cs_config.h"
 
28
 
 
29
/*
 
30
 * Standard C library headers
 
31
 */
 
32
 
 
33
#include <time.h>
 
34
 
 
35
#if defined (HAVE_GETTIMEOFDAY)
 
36
#include <sys/time.h>
 
37
#endif
 
38
 
 
39
#if defined (HAVE_GETRUSAGE)
 
40
#include <sys/time.h>
 
41
#include <sys/resource.h>
 
42
#include <unistd.h>
 
43
#elif defined(_POSIX_SOURCE)
 
44
#include <sys/times.h>
 
45
#include <unistd.h>
 
46
#endif
 
47
 
 
48
/*
 
49
 * Optional library and ECS headers
 
50
 */
 
51
 
 
52
#include "ecs_def.h"
 
53
#include "ecs_timer.h"
 
54
 
 
55
/*-----------------------------------------------------------------------------*/
 
56
 
 
57
#ifdef __cplusplus
 
58
extern "C" {
 
59
#if 0
 
60
} /* Fake brace to force back Emacs auto-indentation back to column 0 */
 
61
#endif
 
62
#endif /* __cplusplus */
 
63
 
 
64
/*-----------------------------------------------------------------------------*/
 
65
 
 
66
/*-----------------------------------------------------------------------------
 
67
 * Local type definitions
 
68
 *-----------------------------------------------------------------------------*/
 
69
 
 
70
/*-----------------------------------------------------------------------------
 
71
 * Local function prototypes
 
72
 *-----------------------------------------------------------------------------*/
 
73
 
 
74
/*-----------------------------------------------------------------------------
 
75
 * Local static variable definitions
 
76
 *-----------------------------------------------------------------------------*/
 
77
 
 
78
static _Bool _ecs_timer_initialized = false;
 
79
 
 
80
/* Wall-clock time */
 
81
 
 
82
#if defined (HAVE_GETTIMEOFDAY)
 
83
static struct timeval  _ecs_timer_wtime_tv_start;
 
84
#else
 
85
static time_t _ecs_timer_wtime_start;
 
86
#endif
 
87
 
 
88
/* CPU time */
 
89
 
 
90
#if defined (HAVE_GETRUSAGE)
 
91
#elif defined(_POSIX_SOURCE)
 
92
static time_t _ecs_timer_unit = 0;
 
93
#else
 
94
static clock_t _ecs_timer_clock_start;
 
95
#endif
 
96
 
 
97
/*-----------------------------------------------------------------------------
 
98
 * Local function definitions
 
99
 *-----------------------------------------------------------------------------*/
 
100
 
 
101
static void
 
102
_ecs_timer_initialize(void)
 
103
{
 
104
#if defined (HAVE_GETTIMEOFDAY)
 
105
  (void)gettimeofday(&_ecs_timer_wtime_tv_start, NULL);
 
106
#else
 
107
  (void)time(&_ecs_timer_wtime_start);
 
108
#endif
 
109
 
 
110
#if defined (HAVE_GETRUSAGE)
 
111
#elif defined(_POSIX_SOURCE)
 
112
  _ecs_timer_unit = (double)sysconf(_SC_CLK_TCK);
 
113
#else
 
114
  _ecs_timer_clock_start = clock();
 
115
#endif
 
116
 
 
117
  _ecs_timer_initialized = true;
 
118
}
 
119
 
 
120
/*============================================================================
 
121
 * Public function definitions
 
122
 *============================================================================*/
 
123
 
 
124
/*!
 
125
 * \brief Return Wall clock time
 
126
 *
 
127
 * \return elapsed time from first call of a function of the ecs_timer_...()
 
128
 *         series, or -1 if unable to compute.
 
129
 */
 
130
 
 
131
double
 
132
ecs_timer_wtime(void)
 
133
{
 
134
  double this_wtime = -1.;
 
135
 
 
136
  /* Ensure initialization */
 
137
 
 
138
  if (_ecs_timer_initialized == false)
 
139
    _ecs_timer_initialize();
 
140
 
 
141
  /* Compute elapsed time */
 
142
 
 
143
#if defined (HAVE_GETTIMEOFDAY)
 
144
 
 
145
 {
 
146
    struct timeval  wtime_tv_current;
 
147
 
 
148
    if (gettimeofday(&wtime_tv_current, NULL) == 0) {
 
149
 
 
150
      /* Perform carry for later subtraction */
 
151
      if (_ecs_timer_wtime_tv_start.tv_usec > wtime_tv_current.tv_usec) {
 
152
        int nsec = (_ecs_timer_wtime_tv_start.tv_usec - wtime_tv_current.tv_usec)
 
153
                   / 1000000 + 1;
 
154
        wtime_tv_current.tv_usec += 1000000 * nsec;
 
155
        wtime_tv_current.tv_sec -= nsec;
 
156
      }
 
157
      if (  wtime_tv_current.tv_usec - _ecs_timer_wtime_tv_start.tv_usec
 
158
          > 1000000) {
 
159
        int nsec = (wtime_tv_current.tv_usec - _ecs_timer_wtime_tv_start.tv_usec)
 
160
                   / 1000000;
 
161
        wtime_tv_current.tv_usec -= 1000000 * nsec;
 
162
        wtime_tv_current.tv_sec += nsec;
 
163
      }
 
164
 
 
165
      this_wtime =   (  wtime_tv_current.tv_sec
 
166
                      - _ecs_timer_wtime_tv_start.tv_sec)
 
167
                   + (  wtime_tv_current.tv_usec
 
168
                      - _ecs_timer_wtime_tv_start.tv_usec) * 1.e-6 ;
 
169
 
 
170
    }
 
171
 
 
172
 }
 
173
 
 
174
#else
 
175
 
 
176
 {
 
177
   time_t wtime_current;
 
178
 
 
179
   if (time(&wtime_current) != (time_t)-1)
 
180
     this_wtime = difftime(wtime_current, _ecs_timer_wtime_start);
 
181
 }
 
182
 
 
183
#endif
 
184
 
 
185
  return this_wtime;
 
186
}
 
187
 
 
188
/*!
 
189
 * \brief Return CPU time.
 
190
 *
 
191
 * Note that in the rare case that only the minimal C library clock()
 
192
 * method is available (see ecs_timer_cpu_time_method()), at least one of
 
193
 * the ecs_timer_...() functions (possibly this one) must be called
 
194
 * upon program start for this function to be used. In addition,
 
195
 * in this case, time may "loop" back to 0 every multiple of
 
196
 * 2^size_t / CLOCKS_PER_SEC seconds.
 
197
 *
 
198
 * \return current CPU time usage, or -1 if unable to compute.
 
199
 */
 
200
 
 
201
double
 
202
ecs_timer_cpu_time(void)
 
203
{
 
204
  double cpu_time = -1.;
 
205
 
 
206
  /* Ensure initialization */
 
207
 
 
208
  if (_ecs_timer_initialized == 0)
 
209
    _ecs_timer_initialize();
 
210
 
 
211
  /* Compute CPU time */
 
212
 
 
213
#if defined (HAVE_GETRUSAGE)
 
214
 
 
215
 {
 
216
   struct rusage  usage;
 
217
 
 
218
   if (getrusage(RUSAGE_SELF, &usage) == 0) {
 
219
     cpu_time  =    usage.ru_utime.tv_sec  + usage.ru_stime.tv_sec
 
220
                 + (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) * 1.e-6;
 
221
   }
 
222
 }
 
223
 
 
224
#elif defined(_POSIX_SOURCE)
 
225
 
 
226
 {
 
227
    static struct tms  ptimer;
 
228
 
 
229
    if (_ecs_timer_unit != -1 && times(&ptimer) != -1) {
 
230
      cpu_time =   ((double)(ptimer.tms_utime + ptimer.tms_stime))
 
231
                 / _ecs_timer_unit;
 
232
    }
 
233
 
 
234
 }
 
235
 
 
236
#else /* Use minimal C library function */
 
237
 
 
238
  if (_ecs_timer_clock_start != -1) {
 
239
 
 
240
    static clock_t  clock_current;
 
241
 
 
242
    clock_current = clock();
 
243
    if (clock_current != (clock_t)-1)
 
244
      cpu_time
 
245
        = ((double)(clock_current - _ecs_timer_clock_start)) / CLOCKS_PER_SEC;
 
246
 
 
247
  }
 
248
 
 
249
#endif
 
250
 
 
251
  return cpu_time;
 
252
}
 
253
 
 
254
/*!
 
255
 * \brief Return separate user and system CPU times.
 
256
 *
 
257
 * Note that in the rare case that only the minimal C library clock()
 
258
 * method is available, this function will return -1 values.
 
259
 *
 
260
 * \param [out] user_time current user CPU usage.
 
261
 * \param [out] system_time current system CPU usage.
 
262
 */
 
263
 
 
264
void
 
265
ecs_timer_cpu_times(double *user_time,
 
266
                    double *system_time)
 
267
{
 
268
  /* Ensure initialization */
 
269
 
 
270
  if (_ecs_timer_initialized == 0)
 
271
     _ecs_timer_initialize();
 
272
 
 
273
  *user_time   = -1.;
 
274
  *system_time = -1.;
 
275
 
 
276
  /* Compute CPU time */
 
277
 
 
278
#if defined (HAVE_GETRUSAGE)
 
279
 
 
280
 {
 
281
   struct rusage  usage;
 
282
 
 
283
   if (getrusage(RUSAGE_SELF, &usage) == 0) {
 
284
     *user_time    = usage.ru_utime.tv_sec + usage.ru_utime.tv_usec * 1.e-6;
 
285
     *system_time  = usage.ru_stime.tv_sec + usage.ru_stime.tv_usec * 1.e-6;
 
286
   }
 
287
 }
 
288
 
 
289
#elif defined(_POSIX_SOURCE)
 
290
 
 
291
 {
 
292
    static struct tms  ptimer;
 
293
 
 
294
    if (_ecs_timer_unit != -1 && times(&ptimer) != -1) {
 
295
      *user_time    = ((double)ptimer.tms_utime)  / _ecs_timer_unit;
 
296
      *system_time  = ((double)ptimer.tms_stime)  / _ecs_timer_unit;
 
297
    }
 
298
 
 
299
 }
 
300
 
 
301
#endif
 
302
}
 
303
 
 
304
/*-----------------------------------------------------------------------------*/
 
305
 
 
306
#ifdef __cplusplus
 
307
}
 
308
#endif /* __cplusplus */