840992f86378f45c85cf06960eee3667e799c718
[debian/amanda] / common-src / clock.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /*
28  * $Id: clock.c,v 1.2.2.2 2002/03/31 21:01:33 jrjackson Exp $
29  *
30  * timing functions
31  */
32 #include "amanda.h"
33
34 #include "clock.h"
35
36 /* local functions */
37 static struct timeval timesub P((struct timeval end, struct timeval start));
38 static struct timeval timeadd P((struct timeval a, struct timeval b));
39
40 times_t times_zero = {{0,0}};
41 times_t start_time;
42 static int clock_running = 0;
43
44 #ifdef HAVE_TWO_ARG_GETTIMEOFDAY
45 #  define amanda_gettimeofday(x, y) gettimeofday((x), (y))
46 #else
47 #  define amanda_gettimeofday(x, y) gettimeofday((x))
48 #endif
49
50 int clock_is_running()
51 {
52     return clock_running;
53 }
54
55 void startclock()
56 {
57 #ifdef HAVE_TWO_ARG_GETTIMEOFDAY
58     struct timezone dontcare;
59 #endif
60
61     clock_running = 1;
62     amanda_gettimeofday(&start_time.r, &dontcare);
63 }
64
65 times_t stopclock()
66 {
67     times_t diff;
68     struct timeval end_time;
69
70 #ifdef HAVE_TWO_ARG_GETTIMEOFDAY
71     struct timezone dontcare;
72 #endif
73
74     if(!clock_running) {
75         fprintf(stderr,"stopclock botch\n");
76         exit(1);
77     }
78     amanda_gettimeofday(&end_time, &dontcare);
79     diff.r = timesub(end_time,start_time.r);
80     clock_running = 0;
81     return diff;
82 }
83
84 times_t curclock()
85 {
86     times_t diff;
87     struct timeval end_time;
88
89 #ifdef HAVE_TWO_ARG_GETTIMEOFDAY
90     struct timezone dontcare;
91 #endif
92
93     if(!clock_running) {
94         fprintf(stderr,"curclock botch\n");
95         exit(1);
96     }
97     amanda_gettimeofday(&end_time, &dontcare);
98     diff.r = timesub(end_time,start_time.r);
99     return diff;
100 }
101
102 times_t timesadd(a,b)
103 times_t a,b;
104 {
105     times_t sum;
106
107     sum.r = timeadd(a.r,b.r);
108     return sum;
109 }
110
111 times_t timessub(a,b)
112 times_t a,b;
113 {
114     times_t dif;
115
116     dif.r = timesub(a.r,b.r);
117     return dif;
118 }
119
120 char *times_str(t)
121 times_t t;
122 {
123     static char str[10][NUM_STR_SIZE+10];
124     static int n = 0;
125     char *s;
126
127     /* tv_sec/tv_usec are longs on some systems */
128     ap_snprintf(str[n], sizeof(str[n]),
129                 "rtime %d.%03d", (int)t.r.tv_sec, (int)t.r.tv_usec/1000);
130     s = str[n++];
131     n %= am_countof(str);
132     return s;
133 }
134
135 char *walltime_str(t)
136 times_t t;
137 {
138     static char str[10][NUM_STR_SIZE+10];
139     static int n = 0;
140     char *s;
141
142     /* tv_sec/tv_usec are longs on some systems */
143     ap_snprintf(str[n], sizeof(str[n]),
144                 "%d.%03d", (int)t.r.tv_sec, (int)t.r.tv_usec/1000);
145     s = str[n++];
146     n %= am_countof(str);
147     return s;
148 }
149
150 static struct timeval timesub(end,start)
151 struct timeval end,start;
152 {
153     struct timeval diff;
154
155     if(end.tv_usec < start.tv_usec) { /* borrow 1 sec */
156         end.tv_sec -= 1;
157         end.tv_usec += 1000000;
158     }
159     diff.tv_usec = end.tv_usec - start.tv_usec;
160     diff.tv_sec = end.tv_sec - start.tv_sec;
161     return diff;
162 }
163
164 static struct timeval timeadd(a,b)
165 struct timeval a,b;
166 {
167     struct timeval sum;
168
169     sum.tv_sec = a.tv_sec + b.tv_sec;
170     sum.tv_usec = a.tv_usec + b.tv_usec;
171
172     if(sum.tv_usec >= 1000000) {
173         sum.tv_usec -= 1000000;
174         sum.tv_sec += 1;
175     }
176     return sum;
177 }