1*8f76bb7dSCy Schubert /*
2*8f76bb7dSCy Schubert * util/timeval_func.c - helpers to work with struct timeval values.
3*8f76bb7dSCy Schubert *
4*8f76bb7dSCy Schubert * Copyright (c) 2023, NLnet Labs. All rights reserved.
5*8f76bb7dSCy Schubert *
6*8f76bb7dSCy Schubert * This software is open source.
7*8f76bb7dSCy Schubert *
8*8f76bb7dSCy Schubert * Redistribution and use in source and binary forms, with or without
9*8f76bb7dSCy Schubert * modification, are permitted provided that the following conditions
10*8f76bb7dSCy Schubert * are met:
11*8f76bb7dSCy Schubert *
12*8f76bb7dSCy Schubert * Redistributions of source code must retain the above copyright notice,
13*8f76bb7dSCy Schubert * this list of conditions and the following disclaimer.
14*8f76bb7dSCy Schubert *
15*8f76bb7dSCy Schubert * Redistributions in binary form must reproduce the above copyright notice,
16*8f76bb7dSCy Schubert * this list of conditions and the following disclaimer in the documentation
17*8f76bb7dSCy Schubert * and/or other materials provided with the distribution.
18*8f76bb7dSCy Schubert *
19*8f76bb7dSCy Schubert * Neither the name of the NLNET LABS nor the names of its contributors may
20*8f76bb7dSCy Schubert * be used to endorse or promote products derived from this software without
21*8f76bb7dSCy Schubert * specific prior written permission.
22*8f76bb7dSCy Schubert *
23*8f76bb7dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*8f76bb7dSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*8f76bb7dSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*8f76bb7dSCy Schubert * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*8f76bb7dSCy Schubert * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*8f76bb7dSCy Schubert * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*8f76bb7dSCy Schubert * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*8f76bb7dSCy Schubert * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*8f76bb7dSCy Schubert * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*8f76bb7dSCy Schubert * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*8f76bb7dSCy Schubert * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*8f76bb7dSCy Schubert */
35*8f76bb7dSCy Schubert
36*8f76bb7dSCy Schubert /**
37*8f76bb7dSCy Schubert * \file
38*8f76bb7dSCy Schubert *
39*8f76bb7dSCy Schubert * This file contains helpers to manipulate struct timeval values.
40*8f76bb7dSCy Schubert */
41*8f76bb7dSCy Schubert
42*8f76bb7dSCy Schubert #include "config.h"
43*8f76bb7dSCy Schubert #include "timeval_func.h"
44*8f76bb7dSCy Schubert
45*8f76bb7dSCy Schubert /** subtract timers and the values do not overflow or become negative */
46*8f76bb7dSCy Schubert void
timeval_subtract(struct timeval * d,const struct timeval * end,const struct timeval * start)47*8f76bb7dSCy Schubert timeval_subtract(struct timeval* d, const struct timeval* end, const struct timeval* start)
48*8f76bb7dSCy Schubert {
49*8f76bb7dSCy Schubert #ifndef S_SPLINT_S
50*8f76bb7dSCy Schubert time_t end_usec = end->tv_usec;
51*8f76bb7dSCy Schubert d->tv_sec = end->tv_sec - start->tv_sec;
52*8f76bb7dSCy Schubert if(end_usec < start->tv_usec) {
53*8f76bb7dSCy Schubert end_usec += 1000000;
54*8f76bb7dSCy Schubert d->tv_sec--;
55*8f76bb7dSCy Schubert }
56*8f76bb7dSCy Schubert d->tv_usec = end_usec - start->tv_usec;
57*8f76bb7dSCy Schubert #endif
58*8f76bb7dSCy Schubert }
59*8f76bb7dSCy Schubert
60*8f76bb7dSCy Schubert /** add timers and the values do not overflow or become negative */
61*8f76bb7dSCy Schubert void
timeval_add(struct timeval * d,const struct timeval * add)62*8f76bb7dSCy Schubert timeval_add(struct timeval* d, const struct timeval* add)
63*8f76bb7dSCy Schubert {
64*8f76bb7dSCy Schubert #ifndef S_SPLINT_S
65*8f76bb7dSCy Schubert d->tv_sec += add->tv_sec;
66*8f76bb7dSCy Schubert d->tv_usec += add->tv_usec;
67*8f76bb7dSCy Schubert if(d->tv_usec >= 1000000 ) {
68*8f76bb7dSCy Schubert d->tv_usec -= 1000000;
69*8f76bb7dSCy Schubert d->tv_sec++;
70*8f76bb7dSCy Schubert }
71*8f76bb7dSCy Schubert #endif
72*8f76bb7dSCy Schubert }
73*8f76bb7dSCy Schubert
74*8f76bb7dSCy Schubert /** divide sum of timers to get average */
75*8f76bb7dSCy Schubert void
timeval_divide(struct timeval * avg,const struct timeval * sum,long long d)76*8f76bb7dSCy Schubert timeval_divide(struct timeval* avg, const struct timeval* sum, long long d)
77*8f76bb7dSCy Schubert {
78*8f76bb7dSCy Schubert #ifndef S_SPLINT_S
79*8f76bb7dSCy Schubert long long leftover;
80*8f76bb7dSCy Schubert if(d <= 0) {
81*8f76bb7dSCy Schubert avg->tv_sec = 0;
82*8f76bb7dSCy Schubert avg->tv_usec = 0;
83*8f76bb7dSCy Schubert return;
84*8f76bb7dSCy Schubert }
85*8f76bb7dSCy Schubert avg->tv_sec = sum->tv_sec / d;
86*8f76bb7dSCy Schubert avg->tv_usec = sum->tv_usec / d;
87*8f76bb7dSCy Schubert /* handle fraction from seconds divide */
88*8f76bb7dSCy Schubert leftover = sum->tv_sec - avg->tv_sec*d;
89*8f76bb7dSCy Schubert if(leftover <= 0)
90*8f76bb7dSCy Schubert leftover = 0;
91*8f76bb7dSCy Schubert avg->tv_usec += (((long long)leftover)*((long long)1000000))/d;
92*8f76bb7dSCy Schubert if(avg->tv_sec < 0)
93*8f76bb7dSCy Schubert avg->tv_sec = 0;
94*8f76bb7dSCy Schubert if(avg->tv_usec < 0)
95*8f76bb7dSCy Schubert avg->tv_usec = 0;
96*8f76bb7dSCy Schubert #endif
97*8f76bb7dSCy Schubert }
98*8f76bb7dSCy Schubert
99*8f76bb7dSCy Schubert /** histogram compare of time values */
100*8f76bb7dSCy Schubert int
timeval_smaller(const struct timeval * x,const struct timeval * y)101*8f76bb7dSCy Schubert timeval_smaller(const struct timeval* x, const struct timeval* y)
102*8f76bb7dSCy Schubert {
103*8f76bb7dSCy Schubert #ifndef S_SPLINT_S
104*8f76bb7dSCy Schubert if(x->tv_sec < y->tv_sec)
105*8f76bb7dSCy Schubert return 1;
106*8f76bb7dSCy Schubert else if(x->tv_sec == y->tv_sec) {
107*8f76bb7dSCy Schubert if(x->tv_usec <= y->tv_usec)
108*8f76bb7dSCy Schubert return 1;
109*8f76bb7dSCy Schubert else return 0;
110*8f76bb7dSCy Schubert }
111*8f76bb7dSCy Schubert else return 0;
112*8f76bb7dSCy Schubert #endif
113*8f76bb7dSCy Schubert }
114