Implemented bug #781: Easier tracking of result sets. We only do this
[idzebra-moved-to-github.git] / util / timing.c
1 /* $Id: timing.c,v 1.1 2006-12-11 09:50:36 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdlib.h>
24
25 #if HAVE_SYS_TIMES_H
26 #include <sys/times.h>
27 #endif
28 #if HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
31
32 #include <yaz/xmalloc.h>
33 #include <idzebra/util.h>
34
35 struct zebra_timing {
36     struct tms tms1, tms2;
37     struct timeval start_time, end_time;
38     double real_sec, user_sec, sys_sec;
39 };
40
41 zebra_timing_t zebra_timing_create(void)
42 {
43     zebra_timing_t t = xmalloc(sizeof(*t));
44     zebra_timing_start(t);
45     return t;
46 }
47
48 void zebra_timing_start(zebra_timing_t t)
49 {
50 #if HAVE_SYS_TIMES_H
51 #if HAVE_SYS_TIME_H
52     times(&t->tms1);
53     gettimeofday(&t->start_time, 0);
54 #endif
55 #endif
56     t->real_sec = -1.0;
57     t->user_sec = -1.0;
58     t->sys_sec = -1.0;
59 }
60
61 void zebra_timing_stop(zebra_timing_t t)
62 {
63     t->real_sec = 0.0;
64     t->user_sec = 0.0;
65     t->sys_sec = 0.0;
66 #if HAVE_SYS_TIMES_H
67 #if HAVE_SYS_TIME_H
68     gettimeofday(&t->end_time, 0);
69     times(&t->tms2);
70     
71     t->real_sec = ((t->end_time.tv_sec - t->start_time.tv_sec) * 1000000.0 +
72                    t->end_time.tv_usec - t->start_time.tv_usec) / 1000000;
73     
74     t->user_sec = (double) (t->tms2.tms_utime - t->tms1.tms_utime)/100;
75     t->sys_sec = (double) (t->tms2.tms_stime - t->tms1.tms_stime)/100;
76 #endif
77 #endif
78 }
79
80 double zebra_timing_get_real(zebra_timing_t t)
81 {
82     return t->real_sec;
83 }
84
85 double zebra_timing_get_user(zebra_timing_t t)
86 {
87     return t->user_sec;
88 }
89
90 double zebra_timing_get_sys(zebra_timing_t t)
91 {
92     return t->sys_sec;
93 }
94
95 void zebra_timing_destroy(zebra_timing_t *tp)
96 {
97     if (*tp)
98     {
99         xfree(*tp);
100         *tp = 0;
101     }
102 }
103
104 /*
105  * Local variables:
106  * c-basic-offset: 4
107  * indent-tabs-mode: nil
108  * End:
109  * vim: shiftwidth=4 tabstop=8 expandtab
110  */
111