1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/* timeval.c - functions to perform operations on struct timevals */
/* Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
#include <config.h>
#if defined (HAVE_TIMEVAL)
#include <sys/types.h>
#include <posixtime.h>
#include <stdio.h>
struct timeval *
difftimeval (d, t1, t2)
struct timeval *d, *t1, *t2;
{
d->tv_sec = t2->tv_sec - t1->tv_sec;
d->tv_usec = t2->tv_usec - t1->tv_usec;
if (d->tv_usec < 0)
{
d->tv_usec += 1000000;
d->tv_sec -= 1;
if (d->tv_sec < 0) /* ??? -- BSD/OS does this */
{
d->tv_sec = 0;
d->tv_usec = 0;
}
}
return d;
}
struct timeval *
addtimeval (d, t1, t2)
struct timeval *d, *t1, *t2;
{
d->tv_sec = t1->tv_sec + t2->tv_sec;
d->tv_usec = t1->tv_usec + t2->tv_usec;
if (d->tv_usec >= 1000000)
{
d->tv_usec -= 1000000;
d->tv_sec += 1;
}
return d;
}
/* Do "cpu = ((user + sys) * 10000) / real;" with timevals.
Barely-tested code from Deven T. Corzine <deven@ties.org>. */
int
timeval_to_cpu (rt, ut, st)
struct timeval *rt, *ut, *st; /* real, user, sys */
{
struct timeval t1, t2;
register int i;
addtimeval (&t1, ut, st);
t2.tv_sec = rt->tv_sec;
t2.tv_usec = rt->tv_usec;
for (i = 0; i < 6; i++)
{
if ((t1.tv_sec > 99999999) || (t2.tv_sec > 99999999))
break;
t1.tv_sec *= 10;
t1.tv_sec += t1.tv_usec / 100000;
t1.tv_usec *= 10;
t1.tv_usec %= 1000000;
t2.tv_sec *= 10;
t2.tv_sec += t2.tv_usec / 100000;
t2.tv_usec *= 10;
t2.tv_usec %= 1000000;
}
for (i = 0; i < 4; i++)
{
if (t1.tv_sec < 100000000)
t1.tv_sec *= 10;
else
t2.tv_sec /= 10;
}
return ((t2.tv_sec == 0) ? 0 : t1.tv_sec / t2.tv_sec);
}
/* Convert a pointer to a struct timeval to seconds and thousandths of a
second, returning the values in *SP and *SFP, respectively. This does
rounding on the fractional part, not just truncation to three places. */
void
timeval_to_secs (tvp, sp, sfp)
struct timeval *tvp;
time_t *sp;
int *sfp;
{
int rest;
*sp = tvp->tv_sec;
*sfp = tvp->tv_usec % 1000000; /* pretty much a no-op */
rest = *sfp % 1000;
*sfp = (*sfp * 1000) / 1000000;
if (rest >= 500)
*sfp += 1;
/* Sanity check */
if (*sfp >= 1000)
{
*sp += 1;
*sfp -= 1000;
}
}
/* Print the contents of a struct timeval * in a standard way to stdio
stream FP. */
void
print_timeval (fp, tvp)
FILE *fp;
struct timeval *tvp;
{
time_t timestamp;
long minutes;
int seconds, seconds_fraction;
timeval_to_secs (tvp, ×tamp, &seconds_fraction);
minutes = timestamp / 60;
seconds = timestamp % 60;
fprintf (fp, "%ldm%d.%03ds", minutes, seconds, seconds_fraction);
}
#endif /* HAVE_TIMEVAL */
|