905c68e603a50d6048e4253089e735a1ea7f1021
[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.7 2006/07/27 18:12:10 martinea Exp $
29  *
30  * timing functions
31  */
32 #include "amanda.h"
33
34 #include "clock.h"
35
36 /* local functions */
37 static struct timeval timesub(struct timeval end, struct timeval start);
38 static struct timeval timeadd(struct timeval a, struct timeval b);
39
40 times_t times_zero;
41 times_t start_time;
42 static int clock_running = 0;
43
44 int
45 clock_is_running(void)
46 {
47     return clock_running;
48 }
49
50 void
51 startclock(void)
52 {
53     amanda_timezone dontcare;
54
55     clock_running = 1;
56     amanda_gettimeofday(&start_time.r, &dontcare);
57 }
58
59 times_t
60 stopclock(void)
61 {
62     times_t diff;
63     struct timeval end_time;
64     amanda_timezone dontcare;
65
66     if(!clock_running) {
67         fprintf(stderr,"stopclock botch\n");
68         exit(1);
69     }
70     amanda_gettimeofday(&end_time, &dontcare);
71     diff.r = timesub(end_time,start_time.r);
72     clock_running = 0;
73     return diff;
74 }
75
76 times_t
77 curclock(void)
78 {
79     times_t diff;
80     struct timeval end_time;
81     amanda_timezone dontcare;
82
83     if(!clock_running) {
84         fprintf(stderr,"curclock botch\n");
85         exit(1);
86     }
87     amanda_gettimeofday(&end_time, &dontcare);
88     diff.r = timesub(end_time,start_time.r);
89     return diff;
90 }
91
92 times_t
93 timesadd(
94     times_t     a,
95     times_t     b)
96 {
97     times_t sum;
98
99     sum.r = timeadd(a.r,b.r);
100     return sum;
101 }
102
103 times_t
104 timessub(
105     times_t     a,
106     times_t     b)
107 {
108     times_t dif;
109
110     dif.r = timesub(a.r,b.r);
111     return dif;
112 }
113
114 char *
115 times_str(
116     times_t     t)
117 {
118     static char str[10][NUM_STR_SIZE+10];
119     static size_t n = 0;
120     char *s;
121
122     /* tv_sec/tv_usec are longs on some systems */
123     snprintf(str[n], SIZEOF(str[n]), "rtime %lu.%03lu",
124              (unsigned long)t.r.tv_sec,
125              (unsigned long)t.r.tv_usec / 1000);
126     s = str[n++];
127     n %= am_countof(str);
128     return s;
129 }
130
131 char *
132 walltime_str(
133     times_t     t)
134 {
135     static char str[10][NUM_STR_SIZE+10];
136     static size_t n = 0;
137     char *s;
138
139     /* tv_sec/tv_usec are longs on some systems */
140     snprintf(str[n], SIZEOF(str[n]), "%lu.%03lu",
141              (unsigned long)t.r.tv_sec,
142              (unsigned long)t.r.tv_usec/1000);
143     s = str[n++];
144     n %= am_countof(str);
145     return s;
146 }
147
148 static struct timeval
149 timesub(
150     struct timeval      end,
151     struct timeval      start)
152 {
153     struct timeval diff;
154
155     if(end.tv_usec < start.tv_usec) { /* borrow 1 sec */
156         if (end.tv_sec > 0)
157             end.tv_sec -= 1;
158         end.tv_usec += 1000000;
159     }
160     diff.tv_usec = end.tv_usec - start.tv_usec;
161     diff.tv_sec = end.tv_sec - start.tv_sec;
162     return diff;
163 }
164
165 static struct timeval
166 timeadd(
167     struct timeval      a,
168     struct timeval      b)
169 {
170     struct timeval sum;
171
172     sum.tv_sec = a.tv_sec + b.tv_sec;
173     sum.tv_usec = a.tv_usec + b.tv_usec;
174
175     if(sum.tv_usec >= 1000000) {
176         sum.tv_usec -= 1000000;
177         sum.tv_sec += 1;
178     }
179     return sum;
180 }