732b4ef01b578b3b57c1a4e0ca16b53ad572851d
[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.5 2002/04/08 00:16:18 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 int clock_is_running()
45 {
46     return clock_running;
47 }
48
49 void startclock()
50 {
51     amanda_timezone dontcare;
52
53     clock_running = 1;
54     amanda_gettimeofday(&start_time.r, &dontcare);
55 }
56
57 times_t stopclock()
58 {
59     times_t diff;
60     struct timeval end_time;
61     amanda_timezone dontcare;
62
63     if(!clock_running) {
64         fprintf(stderr,"stopclock botch\n");
65         exit(1);
66     }
67     amanda_gettimeofday(&end_time, &dontcare);
68     diff.r = timesub(end_time,start_time.r);
69     clock_running = 0;
70     return diff;
71 }
72
73 times_t curclock()
74 {
75     times_t diff;
76     struct timeval end_time;
77     amanda_timezone dontcare;
78
79     if(!clock_running) {
80         fprintf(stderr,"curclock botch\n");
81         exit(1);
82     }
83     amanda_gettimeofday(&end_time, &dontcare);
84     diff.r = timesub(end_time,start_time.r);
85     return diff;
86 }
87
88 times_t timesadd(a,b)
89 times_t a,b;
90 {
91     times_t sum;
92
93     sum.r = timeadd(a.r,b.r);
94     return sum;
95 }
96
97 times_t timessub(a,b)
98 times_t a,b;
99 {
100     times_t dif;
101
102     dif.r = timesub(a.r,b.r);
103     return dif;
104 }
105
106 char *times_str(t)
107 times_t t;
108 {
109     static char str[10][NUM_STR_SIZE+10];
110     static int n = 0;
111     char *s;
112
113     /* tv_sec/tv_usec are longs on some systems */
114     snprintf(str[n], sizeof(str[n]),
115                 "rtime %d.%03d", (int)t.r.tv_sec, (int)t.r.tv_usec/1000);
116     s = str[n++];
117     n %= am_countof(str);
118     return s;
119 }
120
121 char *walltime_str(t)
122 times_t t;
123 {
124     static char str[10][NUM_STR_SIZE+10];
125     static int n = 0;
126     char *s;
127
128     /* tv_sec/tv_usec are longs on some systems */
129     snprintf(str[n], sizeof(str[n]),
130                 "%d.%03d", (int)t.r.tv_sec, (int)t.r.tv_usec/1000);
131     s = str[n++];
132     n %= am_countof(str);
133     return s;
134 }
135
136 static struct timeval timesub(end,start)
137 struct timeval end,start;
138 {
139     struct timeval diff;
140
141     if(end.tv_usec < start.tv_usec) { /* borrow 1 sec */
142         end.tv_sec -= 1;
143         end.tv_usec += 1000000;
144     }
145     diff.tv_usec = end.tv_usec - start.tv_usec;
146     diff.tv_sec = end.tv_sec - start.tv_sec;
147     return diff;
148 }
149
150 static struct timeval timeadd(a,b)
151 struct timeval a,b;
152 {
153     struct timeval sum;
154
155     sum.tv_sec = a.tv_sec + b.tv_sec;
156     sum.tv_usec = a.tv_usec + b.tv_usec;
157
158     if(sum.tv_usec >= 1000000) {
159         sum.tv_usec -= 1000000;
160         sum.tv_sec += 1;
161     }
162     return sum;
163 }