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