1933707f3Ssthen /*
2933707f3Ssthen * util/timehist.c - make histogram of time values.
3933707f3Ssthen *
4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen *
6933707f3Ssthen * This software is open source.
7933707f3Ssthen *
8933707f3Ssthen * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen * modification, are permitted provided that the following conditions
10933707f3Ssthen * are met:
11933707f3Ssthen *
12933707f3Ssthen * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen * this list of conditions and the following disclaimer.
14933707f3Ssthen *
15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen * and/or other materials provided with the distribution.
18933707f3Ssthen *
19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen * be used to endorse or promote products derived from this software without
21933707f3Ssthen * specific prior written permission.
22933707f3Ssthen *
23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen */
35933707f3Ssthen
36933707f3Ssthen /**
37933707f3Ssthen * \file
38933707f3Ssthen *
39933707f3Ssthen * This file contains functions to make a histogram of time values.
40933707f3Ssthen */
41933707f3Ssthen #include "config.h"
42933707f3Ssthen #ifdef HAVE_TIME_H
43933707f3Ssthen #include <time.h>
44933707f3Ssthen #endif
45933707f3Ssthen #include <sys/time.h>
465d76a658Ssthen #include <sys/types.h>
47933707f3Ssthen #include "util/timehist.h"
48933707f3Ssthen #include "util/log.h"
49*8b7325afSsthen #include "util/timeval_func.h"
50933707f3Ssthen
51933707f3Ssthen /** special timestwo operation for time values in histogram setup */
52933707f3Ssthen static void
timestwo(struct timeval * v)53933707f3Ssthen timestwo(struct timeval* v)
54933707f3Ssthen {
55933707f3Ssthen #ifndef S_SPLINT_S
56933707f3Ssthen if(v->tv_sec == 0 && v->tv_usec == 0) {
57933707f3Ssthen v->tv_usec = 1;
58933707f3Ssthen return;
59933707f3Ssthen }
60933707f3Ssthen v->tv_sec *= 2;
61933707f3Ssthen v->tv_usec *= 2;
62933707f3Ssthen if(v->tv_usec == 1024*1024) {
63933707f3Ssthen /* nice values and easy to compute */
64933707f3Ssthen v->tv_sec = 1;
65933707f3Ssthen v->tv_usec = 0;
66933707f3Ssthen }
67933707f3Ssthen #endif
68933707f3Ssthen }
69933707f3Ssthen
70933707f3Ssthen /** do setup exponentially */
71933707f3Ssthen static void
dosetup(struct timehist * hist)72933707f3Ssthen dosetup(struct timehist* hist)
73933707f3Ssthen {
74933707f3Ssthen struct timeval last;
75933707f3Ssthen size_t i;
76933707f3Ssthen memset(&last, 0, sizeof(last));
77933707f3Ssthen for(i=0; i<hist->num; i++) {
78933707f3Ssthen hist->buckets[i].lower = last;
79933707f3Ssthen timestwo(&last);
80933707f3Ssthen hist->buckets[i].upper = last;
81933707f3Ssthen hist->buckets[i].count = 0;
82933707f3Ssthen }
83933707f3Ssthen }
84933707f3Ssthen
timehist_setup(void)85933707f3Ssthen struct timehist* timehist_setup(void)
86933707f3Ssthen {
87933707f3Ssthen struct timehist* hist = (struct timehist*)calloc(1,
88933707f3Ssthen sizeof(struct timehist));
89933707f3Ssthen if(!hist)
90933707f3Ssthen return NULL;
91933707f3Ssthen hist->num = NUM_BUCKETS_HIST;
92933707f3Ssthen hist->buckets = (struct th_buck*)calloc(hist->num,
93933707f3Ssthen sizeof(struct th_buck));
94933707f3Ssthen if(!hist->buckets) {
95933707f3Ssthen free(hist);
96933707f3Ssthen return NULL;
97933707f3Ssthen }
98933707f3Ssthen /* setup the buckets */
99933707f3Ssthen dosetup(hist);
100933707f3Ssthen return hist;
101933707f3Ssthen }
102933707f3Ssthen
timehist_delete(struct timehist * hist)103933707f3Ssthen void timehist_delete(struct timehist* hist)
104933707f3Ssthen {
105933707f3Ssthen if(!hist)
106933707f3Ssthen return;
107933707f3Ssthen free(hist->buckets);
108933707f3Ssthen free(hist);
109933707f3Ssthen }
110933707f3Ssthen
timehist_clear(struct timehist * hist)111933707f3Ssthen void timehist_clear(struct timehist* hist)
112933707f3Ssthen {
113933707f3Ssthen size_t i;
114933707f3Ssthen for(i=0; i<hist->num; i++)
115933707f3Ssthen hist->buckets[i].count = 0;
116933707f3Ssthen }
117933707f3Ssthen
timehist_insert(struct timehist * hist,struct timeval * tv)118933707f3Ssthen void timehist_insert(struct timehist* hist, struct timeval* tv)
119933707f3Ssthen {
120933707f3Ssthen size_t i;
121933707f3Ssthen for(i=0; i<hist->num; i++) {
122933707f3Ssthen if(timeval_smaller(tv, &hist->buckets[i].upper)) {
123933707f3Ssthen hist->buckets[i].count++;
124933707f3Ssthen return;
125933707f3Ssthen }
126933707f3Ssthen }
127933707f3Ssthen /* dump in last bucket */
128933707f3Ssthen hist->buckets[hist->num-1].count++;
129933707f3Ssthen }
130933707f3Ssthen
timehist_print(struct timehist * hist)131933707f3Ssthen void timehist_print(struct timehist* hist)
132933707f3Ssthen {
133933707f3Ssthen #ifndef S_SPLINT_S
134933707f3Ssthen size_t i;
135933707f3Ssthen for(i=0; i<hist->num; i++) {
136933707f3Ssthen if(hist->buckets[i].count != 0) {
137933707f3Ssthen printf("%4d.%6.6d %4d.%6.6d %u\n",
138933707f3Ssthen (int)hist->buckets[i].lower.tv_sec,
139933707f3Ssthen (int)hist->buckets[i].lower.tv_usec,
140933707f3Ssthen (int)hist->buckets[i].upper.tv_sec,
141933707f3Ssthen (int)hist->buckets[i].upper.tv_usec,
142933707f3Ssthen (unsigned)hist->buckets[i].count);
143933707f3Ssthen }
144933707f3Ssthen }
145933707f3Ssthen #endif
146933707f3Ssthen }
147933707f3Ssthen
timehist_log(struct timehist * hist,const char * name)148933707f3Ssthen void timehist_log(struct timehist* hist, const char* name)
149933707f3Ssthen {
150933707f3Ssthen #ifndef S_SPLINT_S
151933707f3Ssthen size_t i;
152933707f3Ssthen log_info("[25%%]=%g median[50%%]=%g [75%%]=%g",
153933707f3Ssthen timehist_quartile(hist, 0.25),
154933707f3Ssthen timehist_quartile(hist, 0.50),
155933707f3Ssthen timehist_quartile(hist, 0.75));
156933707f3Ssthen /* 0000.000000 0000.000000 0 */
157933707f3Ssthen log_info("lower(secs) upper(secs) %s", name);
158933707f3Ssthen for(i=0; i<hist->num; i++) {
159933707f3Ssthen if(hist->buckets[i].count != 0) {
160933707f3Ssthen log_info("%4d.%6.6d %4d.%6.6d %u",
161933707f3Ssthen (int)hist->buckets[i].lower.tv_sec,
162933707f3Ssthen (int)hist->buckets[i].lower.tv_usec,
163933707f3Ssthen (int)hist->buckets[i].upper.tv_sec,
164933707f3Ssthen (int)hist->buckets[i].upper.tv_usec,
165933707f3Ssthen (unsigned)hist->buckets[i].count);
166933707f3Ssthen }
167933707f3Ssthen }
168933707f3Ssthen #endif
169933707f3Ssthen }
170933707f3Ssthen
171933707f3Ssthen /** total number in histogram */
172933707f3Ssthen static size_t
timehist_count(struct timehist * hist)173933707f3Ssthen timehist_count(struct timehist* hist)
174933707f3Ssthen {
175933707f3Ssthen size_t i, res = 0;
176933707f3Ssthen for(i=0; i<hist->num; i++)
177933707f3Ssthen res += hist->buckets[i].count;
178933707f3Ssthen return res;
179933707f3Ssthen }
180933707f3Ssthen
181933707f3Ssthen double
timehist_quartile(struct timehist * hist,double q)182933707f3Ssthen timehist_quartile(struct timehist* hist, double q)
183933707f3Ssthen {
184933707f3Ssthen double lookfor, passed, res;
185933707f3Ssthen double low = 0, up = 0;
186933707f3Ssthen size_t i;
187933707f3Ssthen if(!hist || hist->num == 0)
188933707f3Ssthen return 0.;
189933707f3Ssthen /* look for i'th element, interpolated */
190933707f3Ssthen lookfor = (double)timehist_count(hist);
191933707f3Ssthen if(lookfor < 4)
192933707f3Ssthen return 0.; /* not enough elements for a good estimate */
193933707f3Ssthen lookfor *= q;
194933707f3Ssthen passed = 0;
195933707f3Ssthen i = 0;
196933707f3Ssthen while(i+1 < hist->num &&
197933707f3Ssthen passed+(double)hist->buckets[i].count < lookfor) {
198933707f3Ssthen passed += (double)hist->buckets[i++].count;
199933707f3Ssthen }
200933707f3Ssthen /* got the right bucket */
201933707f3Ssthen #ifndef S_SPLINT_S
202933707f3Ssthen low = (double)hist->buckets[i].lower.tv_sec +
203933707f3Ssthen (double)hist->buckets[i].lower.tv_usec/1000000.;
204933707f3Ssthen up = (double)hist->buckets[i].upper.tv_sec +
205933707f3Ssthen (double)hist->buckets[i].upper.tv_usec/1000000.;
206933707f3Ssthen #endif
207933707f3Ssthen res = (lookfor - passed)*(up-low)/((double)hist->buckets[i].count);
208933707f3Ssthen return low+res;
209933707f3Ssthen }
210933707f3Ssthen
211933707f3Ssthen void
timehist_export(struct timehist * hist,long long * array,size_t sz)2122be9e038Ssthen timehist_export(struct timehist* hist, long long* array, size_t sz)
213933707f3Ssthen {
214933707f3Ssthen size_t i;
215933707f3Ssthen if(!hist) return;
216933707f3Ssthen if(sz > hist->num)
217933707f3Ssthen sz = hist->num;
218933707f3Ssthen for(i=0; i<sz; i++)
2192be9e038Ssthen array[i] = (long long)hist->buckets[i].count;
220933707f3Ssthen }
221933707f3Ssthen
222933707f3Ssthen void
timehist_import(struct timehist * hist,long long * array,size_t sz)2232be9e038Ssthen timehist_import(struct timehist* hist, long long* array, size_t sz)
224933707f3Ssthen {
225933707f3Ssthen size_t i;
226933707f3Ssthen if(!hist) return;
227933707f3Ssthen if(sz > hist->num)
228933707f3Ssthen sz = hist->num;
229933707f3Ssthen for(i=0; i<sz; i++)
2302be9e038Ssthen hist->buckets[i].count = (size_t)array[i];
231933707f3Ssthen }
232