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