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