2 * util/timehist.c - make histogram of time values.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions to make a histogram of time values.
46 #include "util/timehist.h"
49 /** special timestwo operation for time values in histogram setup */
51 timestwo(struct timeval* v)
54 if(v->tv_sec == 0 && v->tv_usec == 0) {
60 if(v->tv_usec == 1024*1024) {
61 /* nice values and easy to compute */
68 /** do setup exponentially */
70 dosetup(struct timehist* hist)
74 memset(&last, 0, sizeof(last));
75 for(i=0; i<hist->num; i++) {
76 hist->buckets[i].lower = last;
78 hist->buckets[i].upper = last;
79 hist->buckets[i].count = 0;
83 struct timehist* timehist_setup(void)
85 struct timehist* hist = (struct timehist*)calloc(1,
86 sizeof(struct timehist));
89 hist->num = NUM_BUCKETS_HIST;
90 hist->buckets = (struct th_buck*)calloc(hist->num,
91 sizeof(struct th_buck));
96 /* setup the buckets */
101 void timehist_delete(struct timehist* hist)
109 void timehist_clear(struct timehist* hist)
112 for(i=0; i<hist->num; i++)
113 hist->buckets[i].count = 0;
116 /** histogram compare of time values */
118 timeval_smaller(const struct timeval* x, const struct timeval* y)
121 if(x->tv_sec < y->tv_sec)
123 else if(x->tv_sec == y->tv_sec) {
124 if(x->tv_usec <= y->tv_usec)
133 void timehist_insert(struct timehist* hist, struct timeval* tv)
136 for(i=0; i<hist->num; i++) {
137 if(timeval_smaller(tv, &hist->buckets[i].upper)) {
138 hist->buckets[i].count++;
142 /* dump in last bucket */
143 hist->buckets[hist->num-1].count++;
146 void timehist_print(struct timehist* hist)
150 for(i=0; i<hist->num; i++) {
151 if(hist->buckets[i].count != 0) {
152 printf("%4d.%6.6d %4d.%6.6d %u\n",
153 (int)hist->buckets[i].lower.tv_sec,
154 (int)hist->buckets[i].lower.tv_usec,
155 (int)hist->buckets[i].upper.tv_sec,
156 (int)hist->buckets[i].upper.tv_usec,
157 (unsigned)hist->buckets[i].count);
163 void timehist_log(struct timehist* hist, const char* name)
167 log_info("[25%%]=%g median[50%%]=%g [75%%]=%g",
168 timehist_quartile(hist, 0.25),
169 timehist_quartile(hist, 0.50),
170 timehist_quartile(hist, 0.75));
171 /* 0000.000000 0000.000000 0 */
172 log_info("lower(secs) upper(secs) %s", name);
173 for(i=0; i<hist->num; i++) {
174 if(hist->buckets[i].count != 0) {
175 log_info("%4d.%6.6d %4d.%6.6d %u",
176 (int)hist->buckets[i].lower.tv_sec,
177 (int)hist->buckets[i].lower.tv_usec,
178 (int)hist->buckets[i].upper.tv_sec,
179 (int)hist->buckets[i].upper.tv_usec,
180 (unsigned)hist->buckets[i].count);
186 /** total number in histogram */
188 timehist_count(struct timehist* hist)
191 for(i=0; i<hist->num; i++)
192 res += hist->buckets[i].count;
197 timehist_quartile(struct timehist* hist, double q)
199 double lookfor, passed, res;
200 double low = 0, up = 0;
202 if(!hist || hist->num == 0)
204 /* look for i'th element, interpolated */
205 lookfor = (double)timehist_count(hist);
207 return 0.; /* not enough elements for a good estimate */
211 while(i+1 < hist->num &&
212 passed+(double)hist->buckets[i].count < lookfor) {
213 passed += (double)hist->buckets[i++].count;
215 /* got the right bucket */
217 low = (double)hist->buckets[i].lower.tv_sec +
218 (double)hist->buckets[i].lower.tv_usec/1000000.;
219 up = (double)hist->buckets[i].upper.tv_sec +
220 (double)hist->buckets[i].upper.tv_usec/1000000.;
222 res = (lookfor - passed)*(up-low)/((double)hist->buckets[i].count);
227 timehist_export(struct timehist* hist, size_t* array, size_t sz)
234 array[i] = hist->buckets[i].count;
238 timehist_import(struct timehist* hist, size_t* array, size_t sz)
245 hist->buckets[i].count = array[i];