1 /*
2  * Copyright (c) 2012, 2013, 2020
3  *      Inferno Nettverk A/S, Norway.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. The above copyright notice, this list of conditions and the following
9  *    disclaimer must appear in all copies of the software, derivative works
10  *    or modified versions, and any portions thereof, aswell as in all
11  *    supporting documentation.
12  * 2. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *      This product includes software developed by
15  *      Inferno Nettverk A/S, Norway.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Inferno Nettverk A/S requests users of this software to return to
31  *
32  *  Software Distribution Coordinator  or  sdc@inet.no
33  *  Inferno Nettverk A/S
34  *  Oslo Research Park
35  *  Gaustadall�en 21
36  *  NO-0349 Oslo
37  *  Norway
38  *
39  * any improvements or extensions that they make and grant Inferno Nettverk A/S
40  * the rights to redistribute these changes.
41  *
42  */
43 
44 #include "common.h"
45 
46 static const char rcsid[] =
47 "$Id: math.c,v 1.16.10.2 2020/11/11 16:11:59 karls Exp $";
48 
49 #include <math.h>
50 
51 unsigned long
medtv(struct timeval * tvarr,size_t tvsize)52 medtv(struct timeval *tvarr, size_t tvsize)
53 {
54    struct timeval med;
55 
56    if (tvsize == 0)
57       return 0;
58    else if (tvsize == 1)
59       med = tvarr[tvsize - 1];
60    else if (tvsize % 2 == 1)
61       med = tvarr[(tvsize - 1) / 2];
62    else {
63       timeradd(&tvarr[(tvsize - 1) / 2], &tvarr[(tvsize - 1) / 2 + 1], &med);
64                return tv2us(&med) / 2;
65    }
66 
67    return tv2us(&med);
68 }
69 
70 unsigned long
avgtv(struct timeval * tvarr,size_t tvsize)71 avgtv(struct timeval *tvarr, size_t tvsize)
72 {
73    struct timeval sum, nsum;
74    size_t i;
75 
76    if (tvsize == 0)
77       return 0;
78 
79    timerclear(&sum);
80    for (i = 0; i < tvsize; ++i) {
81       /* don't assume multiple arguments to timer*() can point to same mem. */
82       timeradd(&sum, &tvarr[i], &nsum);
83       sum = nsum;
84    }
85 
86    return tv2us(&sum) / tvsize;
87 }
88 
89 unsigned long
stddevtv(struct timeval * tvarr,size_t tvsize,unsigned long avg)90 stddevtv(struct timeval *tvarr, size_t tvsize, unsigned long avg)
91 {
92    unsigned long long diffsum;
93    size_t i;
94 
95    if (tvsize <= 1)
96       return 0;
97 
98    /* get the squared sum of differences from the mean */
99    for (i = 0, diffsum = 0; i < tvsize; ++i) {
100       const unsigned long long diff = tv2us(&tvarr[i]) - avg;
101 
102       diffsum += diff * diff;
103    }
104 
105    return (unsigned long)lround(sqrt(((double)diffsum) / tvsize));
106 }
107