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