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

« back to all changes in this revision

Viewing changes to src/bft/bft_timer.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-01 17:43:32 UTC
  • mto: (6.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: package-import@ubuntu.com-20111101174332-tl4vk45no0x3emc3
Tags: upstream-2.1.0
ImportĀ upstreamĀ versionĀ 2.1.0

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
#if defined(HAVE_CONFIG_H)
 
28
#include "cs_config.h"
 
29
#endif
 
30
 
 
31
#include "bft_config_defs.h"
 
32
 
 
33
/*
 
34
 * Standard C library headers
 
35
 */
 
36
 
 
37
#include <time.h>
 
38
 
 
39
#if defined (HAVE_GETTIMEOFDAY)
 
40
#include <sys/time.h>
 
41
#endif
 
42
 
 
43
#if defined (HAVE_GETRUSAGE)
 
44
#include <sys/time.h>
 
45
#include <sys/resource.h>
 
46
#include <unistd.h>
 
47
#elif defined(_POSIX_SOURCE)
 
48
#include <sys/times.h>
 
49
#include <unistd.h>
 
50
#endif
 
51
 
 
52
/*
 
53
 * Optional library and BFT headers
 
54
 */
 
55
 
 
56
#include "bft_timer.h"
 
57
 
 
58
/*-----------------------------------------------------------------------------*/
 
59
 
 
60
#ifdef __cplusplus
 
61
extern "C" {
 
62
#if 0
 
63
} /* Fake brace to force back Emacs auto-indentation back to column 0 */
 
64
#endif
 
65
#endif /* __cplusplus */
 
66
 
 
67
/*-----------------------------------------------------------------------------*/
 
68
 
 
69
/*-----------------------------------------------------------------------------
 
70
 * Local type definitions
 
71
 *-----------------------------------------------------------------------------*/
 
72
 
 
73
/*-----------------------------------------------------------------------------
 
74
 * Local function prototypes
 
75
 *-----------------------------------------------------------------------------*/
 
76
 
 
77
/*-----------------------------------------------------------------------------
 
78
 * Local static variable definitions
 
79
 *-----------------------------------------------------------------------------*/
 
80
 
 
81
static _Bool _bft_timer_initialized = false;
 
82
 
 
83
/* Wall-clock time */
 
84
 
 
85
#if defined (HAVE_GETTIMEOFDAY)
 
86
static struct timeval  _bft_timer_wtime_tv_start;
 
87
#else
 
88
static time_t _bft_timer_wtime_start;
 
89
#endif
 
90
 
 
91
/* CPU time */
 
92
 
 
93
#if defined (HAVE_GETRUSAGE)
 
94
#elif defined(_POSIX_SOURCE)
 
95
static time_t _bft_timer_unit = 0;
 
96
#else
 
97
static clock_t _bft_timer_clock_start;
 
98
#endif
 
99
 
 
100
/*-----------------------------------------------------------------------------
 
101
 * Local function definitions
 
102
 *-----------------------------------------------------------------------------*/
 
103
 
 
104
static void
 
105
_bft_timer_initialize(void)
 
106
{
 
107
#if defined (HAVE_GETTIMEOFDAY)
 
108
  (void)gettimeofday(&_bft_timer_wtime_tv_start, NULL);
 
109
#else
 
110
  (void)time(&_bft_timer_wtime_start);
 
111
#endif
 
112
 
 
113
#if defined (HAVE_GETRUSAGE)
 
114
#elif defined(_POSIX_SOURCE)
 
115
  _bft_timer_unit = (double)sysconf(_SC_CLK_TCK);
 
116
#else
 
117
  _bft_timer_clock_start = clock();
 
118
#endif
 
119
 
 
120
  _bft_timer_initialized = true;
 
121
}
 
122
 
 
123
/*============================================================================
 
124
 * Public function definitions
 
125
 *============================================================================*/
 
126
 
 
127
/*!
 
128
 * \brief Return Wall clock time
 
129
 *
 
130
 * \return elapsed time from first call of a function of the bft_timer_...()
 
131
 *         series, or -1 if unable to compute.
 
132
 */
 
133
 
 
134
double
 
135
bft_timer_wtime(void)
 
136
{
 
137
  double this_wtime = -1.;
 
138
 
 
139
  /* Ensure initialization */
 
140
 
 
141
  if (_bft_timer_initialized == false)
 
142
    _bft_timer_initialize();
 
143
 
 
144
  /* Compute elapsed time */
 
145
 
 
146
#if defined (HAVE_GETTIMEOFDAY)
 
147
 
 
148
 {
 
149
    struct timeval  wtime_tv_current;
 
150
 
 
151
    if (gettimeofday(&wtime_tv_current, NULL) == 0) {
 
152
 
 
153
      /* Perform carry for later subtraction */
 
154
      if (_bft_timer_wtime_tv_start.tv_usec > wtime_tv_current.tv_usec) {
 
155
        int nsec = (_bft_timer_wtime_tv_start.tv_usec - wtime_tv_current.tv_usec)
 
156
                   / 1000000 + 1;
 
157
        wtime_tv_current.tv_usec += 1000000 * nsec;
 
158
        wtime_tv_current.tv_sec -= nsec;
 
159
      }
 
160
      if (  wtime_tv_current.tv_usec - _bft_timer_wtime_tv_start.tv_usec
 
161
          > 1000000) {
 
162
        int nsec = (wtime_tv_current.tv_usec - _bft_timer_wtime_tv_start.tv_usec)
 
163
                   / 1000000;
 
164
        wtime_tv_current.tv_usec -= 1000000 * nsec;
 
165
        wtime_tv_current.tv_sec += nsec;
 
166
      }
 
167
 
 
168
      this_wtime =   (  wtime_tv_current.tv_sec
 
169
                      - _bft_timer_wtime_tv_start.tv_sec)
 
170
                   + (  wtime_tv_current.tv_usec
 
171
                      - _bft_timer_wtime_tv_start.tv_usec) * 1.e-6 ;
 
172
 
 
173
    }
 
174
 
 
175
 }
 
176
 
 
177
#else
 
178
 
 
179
 {
 
180
   time_t wtime_current;
 
181
 
 
182
   if (time(&wtime_current) != (time_t)-1)
 
183
     this_wtime = difftime(wtime_current, _bft_timer_wtime_start);
 
184
 }
 
185
 
 
186
#endif
 
187
 
 
188
  return this_wtime;
 
189
}
 
190
 
 
191
/*!
 
192
 * \brief Return CPU time.
 
193
 *
 
194
 * Note that in the rare case that only the minimal C library clock()
 
195
 * method is available (see bft_timer_cpu_time_method()), at least one of
 
196
 * the bft_timer_...() functions (possibly this one) must be called
 
197
 * upon program start for this function to be used. In addition,
 
198
 * in this case, time may "loop" back to 0 every multiple of
 
199
 * 2^size_t / CLOCKS_PER_SEC seconds.
 
200
 *
 
201
 * \return current CPU time usage, or -1 if unable to compute.
 
202
 */
 
203
 
 
204
double
 
205
bft_timer_cpu_time(void)
 
206
{
 
207
  double cpu_time = -1.;
 
208
 
 
209
  /* Ensure initialization */
 
210
 
 
211
  if (_bft_timer_initialized == 0)
 
212
    _bft_timer_initialize();
 
213
 
 
214
  /* Compute CPU time */
 
215
 
 
216
#if defined (HAVE_GETRUSAGE)
 
217
 
 
218
 {
 
219
   struct rusage  usage;
 
220
 
 
221
   if (getrusage(RUSAGE_SELF, &usage) == 0) {
 
222
     cpu_time  =    usage.ru_utime.tv_sec  + usage.ru_stime.tv_sec
 
223
                 + (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) * 1.e-6;
 
224
   }
 
225
 }
 
226
 
 
227
#elif defined(_POSIX_SOURCE)
 
228
 
 
229
 {
 
230
    static struct tms  ptimer;
 
231
 
 
232
    if (_bft_timer_unit != -1 && times(&ptimer) != -1) {
 
233
      cpu_time =   ((double)(ptimer.tms_utime + ptimer.tms_stime))
 
234
                 / _bft_timer_unit;
 
235
    }
 
236
 
 
237
 }
 
238
 
 
239
#else /* Use minimal C library function */
 
240
 
 
241
  if (_bft_timer_clock_start != -1) {
 
242
 
 
243
    static clock_t  clock_current;
 
244
 
 
245
    clock_current = clock();
 
246
    if (clock_current != (clock_t)-1)
 
247
      cpu_time
 
248
        = ((double)(clock_current - _bft_timer_clock_start)) / CLOCKS_PER_SEC;
 
249
 
 
250
  }
 
251
 
 
252
#endif
 
253
 
 
254
  return cpu_time;
 
255
}
 
256
 
 
257
/*!
 
258
 * \brief Return separate user and system CPU times.
 
259
 *
 
260
 * Note that in the rare case that only the minimal C library clock()
 
261
 * method is available, this function will return -1 values.
 
262
 *
 
263
 * \param [out] user_time current user CPU usage.
 
264
 * \param [out] system_time current system CPU usage.
 
265
 */
 
266
 
 
267
void
 
268
bft_timer_cpu_times(double *user_time,
 
269
                    double *system_time)
 
270
 
 
271
{
 
272
  /* Ensure initialization */
 
273
 
 
274
  if (_bft_timer_initialized == 0)
 
275
     _bft_timer_initialize();
 
276
 
 
277
  *user_time   = -1.;
 
278
  *system_time = -1.;
 
279
 
 
280
  /* Compute CPU time */
 
281
 
 
282
#if defined (HAVE_GETRUSAGE)
 
283
 
 
284
 {
 
285
   struct rusage  usage;
 
286
 
 
287
   if (getrusage(RUSAGE_SELF, &usage) == 0) {
 
288
     *user_time    = usage.ru_utime.tv_sec + usage.ru_utime.tv_usec * 1.e-6;
 
289
     *system_time  = usage.ru_stime.tv_sec + usage.ru_stime.tv_usec * 1.e-6;
 
290
   }
 
291
 }
 
292
 
 
293
#elif defined(_POSIX_SOURCE)
 
294
 
 
295
 {
 
296
    static struct tms  ptimer;
 
297
 
 
298
    if (_bft_timer_unit != -1 && times(&ptimer) != -1) {
 
299
      *user_time    = ((double)ptimer.tms_utime)  / _bft_timer_unit;
 
300
      *system_time  = ((double)ptimer.tms_stime)  / _bft_timer_unit;
 
301
    }
 
302
 
 
303
 }
 
304
 
 
305
#endif
 
306
}
 
307
 
 
308
/*!
 
309
 * \brief Return method used to return wall clock time.
 
310
 *
 
311
 * \return short description of method used to return wall clock time.
 
312
 */
 
313
 
 
314
const char *
 
315
bft_timer_wtime_method(void)
 
316
{
 
317
  if (_bft_timer_initialized == 0)
 
318
    _bft_timer_initialize();
 
319
 
 
320
#if defined (HAVE_GETTIMEOFDAY)
 
321
  return _("gettimeofday() function");
 
322
#else
 
323
  return _("Iso C time() function");
 
324
#endif
 
325
}
 
326
 
 
327
/*!
 
328
 * \brief Return method used to return CPU time.
 
329
 *
 
330
 * \return short description of method used to return CPU time.
 
331
 */
 
332
 
 
333
const char *
 
334
bft_timer_cpu_time_method(void)
 
335
{
 
336
  if (_bft_timer_initialized == 0)
 
337
    _bft_timer_initialize();
 
338
 
 
339
#if defined (HAVE_GETRUSAGE)
 
340
  return _("getrusage() function");
 
341
#elif defined(_POSIX_SOURCE)
 
342
  return _("Posix times() function");
 
343
#else
 
344
  return _("Iso C clock() function");
 
345
#endif
 
346
}
 
347
 
 
348
/*-----------------------------------------------------------------------------*/
 
349
 
 
350
#ifdef __cplusplus
 
351
}
 
352
#endif /* __cplusplus */