timer.c 6.05 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

5
  Copyright (C) 2003-2012 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
  See COPYING file for copying and redistribution conditions.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
19
20
21
#if  defined  (HAVE_CONFIG_H)
#  include "config.h"
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
23
24
#include <stdio.h>
#include <string.h>
#include <unistd.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
#if defined (HAVE_SYS_TIMES_H)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
#include <sys/times.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
29
30
31


void cdoProcessTime(double *utime, double *stime)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
#if defined (HAVE_SYS_TIMES_H)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
34
35
  struct tms tbuf;
  double clock_ticks = 0;

36
  if ( (int)times(&tbuf) == -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    {
      *utime = 0;
      *stime = 0;
    }
  else
    {
#if defined (_SC_CLK_TCK)
      clock_ticks = (double) sysconf(_SC_CLK_TCK);
#endif
      if ( clock_ticks > 0 )
	{
	  *utime = ((double) tbuf.tms_utime) / clock_ticks; 
	  *stime = ((double) tbuf.tms_stime) / clock_ticks;
	  /*
	    printf("utime  %g\n", ((double) tbuf.tms_utime) / clock_ticks);
	    printf("stime  %g\n", ((double) tbuf.tms_stime) / clock_ticks);
	    printf("cutime %g\n", ((double) tbuf.tms_cutime) / clock_ticks);
	    printf("cstime %g\n", ((double) tbuf.tms_cstime) / clock_ticks);
	  */
	}
      else
	{
	  *utime = 0;
	  *stime = 0;
	}
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
63
64
65
66
#else
  *utime = 0;
  *stime = 0;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68
69
70
71
72
73
74
}


void util_init_real_time(void);
void util_read_real_time(void *it);
void util_diff_real_time(void *it1, void *it2, double *t);


Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
FILE *rt_unit = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76
77
78

enum {rt_stat_undef, rt_stat_on, rt_stat_off};

Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
/* minimal internal time needed to do one measurement */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80

Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
double tm_shift = 0.0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
83
84

/* average total overhead for one timer_start & timer_stop pair */

Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
double tm_overhead = 0.0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103


#define  MAX_TIMER    128  /* max number of timers allowed */


typedef struct {
  int    reserved;
  int    calls;
  int    stat;
  double tot;
  double min;
  double max;
  double last;
  char   mark1[32]; /* max: 16 on IBM; 8 for double (all other) */
  char   text[128];
}
RT_TYPE;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
106
107
RT_TYPE rt[MAX_TIMER];
RT_TYPE rt_init = {0, 0, 0, 0, 1.e30, 0, 0, "", "noname"};
int timer_need_init = 1;
int top_timer = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
108

Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
static
void set_time_mark(void *mark)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
112
113
114
{
  util_read_real_time(mark);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
116
static
double get_time_val(void *mark0)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
118
119
120
121
122
123
124
125
126
127
128
{
  double dt;
  char mark[32];

  util_read_real_time(mark);
  util_diff_real_time(mark0, mark, &dt);

  dt -= tm_shift;

  return (dt);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
int ntests = 100; /* tests need about n microsecs on pwr4 */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130

Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
static
double m1(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{
  double dt, dt0;
  int i;
  char mark[32];

  dt0 = 1.0;
  for ( i = 0; i < ntests; i++ )
    {
      set_time_mark(mark);
      dt = get_time_val(mark);
      if ( dt < dt0 ) dt0 = dt;
    }

  return (dt0);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
150
static
double m2(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{
  char mark1[32], mark2[32];
  double dt1, dt2, dt0;
  int i;

  dt0 = 1.0;
  for ( i = 0; i < ntests; i++ )
    {
      set_time_mark(mark2);
      set_time_mark(mark1);
      dt1 = get_time_val(mark1);
      dt2 = get_time_val(mark2);
      if ( dt2 < dt0 ) dt0 = dt2;
      if ( dt2 < dt1 ) fprintf(rt_unit, "estimate_overhead: internal error\n");
    }

  return (dt0);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
static
void estimate_overhead(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172
173
174
175
176
177
{
  tm_shift    = m1();
  tm_overhead = m2();
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
179
static
void timer_init(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
180
181
182
183
184
185
186
187
188
189
190
{
  rt_unit = stderr;

  util_init_real_time();

  estimate_overhead();

  timer_need_init = 0;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
int timer_new(const char *text)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
192
193
194
195
196
197
198
199
200
201
{
  int it;

  if ( timer_need_init ) timer_init();

  if ( top_timer > MAX_TIMER )
    {
      for ( it = 0; it < MAX_TIMER; it++ )
	fprintf(stderr, "timer %3d:  %s\n", it, rt[it].text);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
      fprintf(stderr, "timer_new: MAX_TIMER too small!\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
204
205
206
207
208
209
210
211
212
213
214
215
    }

  it = top_timer;
  top_timer++;

  rt[it] = rt_init;
  rt[it].reserved = 1;

  if ( text ) strcpy(rt[it].text, text);

  return (it);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
218
219
220
221
222
void timer_check(int it)
{
  if ( it < 0 || it > (top_timer-1) )
    fprintf(rt_unit, "timer: invalid timer id %d\n", it);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
223

Uwe Schulzweida's avatar
Uwe Schulzweida committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
double timer_val(int it)
{
  double val, dt;

  timer_check(it);

  val = rt[it].tot;

  if ( rt[it].stat == rt_stat_on )
    {
      dt = get_time_val(rt[it].mark1);
      val += dt;
    }

  return (val);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
void timer_header(void)
{
  fprintf(rt_unit, "\nTimer report:  shift = %g\n", tm_shift);
  fprintf(rt_unit,
	  "timer  calls        t_min    t_average        t_max      t_total  text\n");
}


void timer_report(void)
{
  int it;
  double total, avg;

  timer_header();

  for ( it = 0; it < top_timer; it++ )
    {
      total = timer_val(it);

      avg = rt[it].tot;
      if ( rt[it].calls > 0 ) avg /= rt[it].calls;

      if ( rt[it].stat != rt_stat_undef )
	fprintf(rt_unit, "%4d %7d %12.4g %12.4g %12.4g %12.4g  %s\n",
		it, rt[it].calls, rt[it].min, avg, rt[it].max, total, rt[it].text);
    }

}


void timer_start(int it)
{
  timer_check(it);

  if ( rt[it].stat == rt_stat_on )
    fprintf(rt_unit, "timer_start: timer_stop call missing\n");

  set_time_mark(rt[it].mark1);

  rt[it].stat = rt_stat_on;
}


void timer_stop(int it)
{
  double dt;

  timer_check(it);

  if ( rt[it].stat != rt_stat_on )
    {
      if ( rt[it].stat == rt_stat_off )
        fprintf(rt_unit, "timer_stop: timer_start call missing\n");
      else
        fprintf(rt_unit, "timer_stop: undefined timer >%s<\n", rt[it].text);
    }

  dt = get_time_val(rt[it].mark1);

  rt[it].last  = dt;
  rt[it].tot  += dt;
  rt[it].calls++;
  if ( dt < rt[it].min ) rt[it].min = dt;
  if ( dt > rt[it].max ) rt[it].max = dt;

  rt[it].stat = rt_stat_off;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340



#include "counter.h"

static
void counter_init(counter_t *counter)
{
  counter->cputime = 0;
}


void counter_start(counter_t *counter)
{
  counter_init(counter);

  if ( timer_need_init ) timer_init();

  set_time_mark(counter->mark);
}


void counter_stop(counter_t *counter)
{
  counter->cputime = get_time_val(counter->mark);
}


double counter_cputime(counter_t counter)
{
  return (counter.cputime);
}