5362ac8562440a101186fef04104d95b65b5f51f
[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 times_t times_zero;
38 times_t start_time;
39 static int clock_running = 0;
40
41 int
42 clock_is_running(void)
43 {
44     return clock_running;
45 }
46
47 void
48 startclock(void)
49 {
50     clock_running = 1;
51     
52     g_get_current_time(&start_time);
53 }
54
55 times_t
56 stopclock(void)
57 {
58     GTimeVal diff;
59
60     diff = curclock();
61
62     clock_running = 0;
63     return diff;
64 }
65
66 times_t
67 curclock(void)
68 {
69     GTimeVal end_time;
70
71     if(!clock_running) {
72         g_fprintf(stderr,_("curclock botch\n"));
73         exit(1);
74     }
75
76     g_get_current_time(&end_time);
77     return timesub(end_time,start_time);
78 }
79
80 char *
81 walltime_str(
82     times_t     t)
83 {
84     static char str[10][NUM_STR_SIZE+10];
85     static size_t n = 0;
86     char *s;
87
88     /* tv_sec/tv_usec are longs on some systems */
89     g_snprintf(str[n], SIZEOF(str[n]), "%lu.%03lu",
90              (unsigned long)t.tv_sec,
91              (unsigned long)t.tv_usec/1000);
92     s = str[n++];
93     n %= am_countof(str);
94     return s;
95 }
96
97 GTimeVal timesub(GTimeVal end, GTimeVal start) {
98     GTimeVal diff;
99
100     if(end.tv_usec < start.tv_usec) { /* borrow 1 sec */
101         if (end.tv_sec > 0)
102             end.tv_sec -= 1;
103         end.tv_usec += 1000000;
104     }
105     diff.tv_usec = end.tv_usec - start.tv_usec;
106
107     if (end.tv_sec > start.tv_sec)
108         diff.tv_sec = end.tv_sec - start.tv_sec;
109     else
110         diff.tv_sec = 0;
111
112     return diff;
113 }
114
115 GTimeVal timeadd(GTimeVal a, GTimeVal b) {
116     GTimeVal sum;
117
118     sum.tv_sec = a.tv_sec + b.tv_sec;
119     sum.tv_usec = a.tv_usec + b.tv_usec;
120
121     if(sum.tv_usec >= 1000000) {
122         sum.tv_usec -= 1000000;
123         sum.tv_sec += 1;
124     }
125     return sum;
126 }
127
128 double g_timeval_to_double(GTimeVal v) {
129     return v.tv_sec + ((double)v.tv_usec) / G_USEC_PER_SEC;
130 }
131
132 void amanda_gettimeofday(struct timeval * timeval_time) {
133     GTimeVal gtimeval_time;
134
135     g_get_current_time(&gtimeval_time);
136     timeval_time->tv_sec = gtimeval_time.tv_sec;
137     timeval_time->tv_usec = gtimeval_time.tv_usec;
138 }