1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
24 #include "cpu_timer.h"
26 static void to_time(int *sec
, int *nsec
, struct timespec
*start
,
27 struct timespec
*stop
)
29 if (stop
->tv_nsec
< start
->tv_nsec
) {
30 *sec
= stop
->tv_sec
- start
->tv_sec
- 1;
31 *nsec
= stop
->tv_nsec
+ 1000000000 - start
->tv_nsec
;
33 *sec
= stop
->tv_sec
- start
->tv_sec
;
34 *nsec
= stop
->tv_nsec
- start
->tv_nsec
;
38 static int timers_disabled
= 1;
40 void cpu_timer_switch(int enable
)
42 timers_disabled
= !enable
;
45 void cpu_timer_start(struct cpu_timer
*self
, const char *name
)
52 clock_gettime(CLOCK_PROCESS_CPUTIME_ID
, &self
->t_cpu_start
);
53 clock_gettime(CLOCK_MONOTONIC
, &self
->t_real_start
);
56 void cpu_timer_stop(struct cpu_timer
*self
)
61 clock_gettime(CLOCK_PROCESS_CPUTIME_ID
, &self
->t_cpu_stop
);
62 clock_gettime(CLOCK_MONOTONIC
, &self
->t_real_stop
);
70 to_time(&cpu_sec
, &cpu_nsec
, &self
->t_cpu_start
, &self
->t_cpu_stop
);
71 to_time(&real_sec
, &real_nsec
, &self
->t_real_start
, &self
->t_real_stop
);
73 printf("TIMER '%s' CPU=%i.%09is REAL=%i.%09is\n", self
->name
,
74 cpu_sec
, cpu_nsec
, real_sec
, real_nsec
);