Imported Upstream version 3.3.3
[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  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: James da Silva, Systems Design and Analysis Group
25  *                         Computer Science Department
26  *                         University of Maryland at College Park
27  */
28 /*
29  * $Id: clock.c,v 1.7 2006/07/27 18:12:10 martinea Exp $
30  *
31  * timing functions
32  */
33 #include "amanda.h"
34
35 #include "clock.h"
36
37 /* local functions */
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 }