xref: /openbsd-src/usr.sbin/unbound/util/timehist.h (revision 2be9e038cdf8e9bb60fc3afb4a2f01fcd3263713)
1933707f3Ssthen /*
2933707f3Ssthen  * util/timehist.h - 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 
42933707f3Ssthen #ifndef UTIL_TIMEHIST_H
43933707f3Ssthen #define UTIL_TIMEHIST_H
44933707f3Ssthen 
45933707f3Ssthen /** Number of buckets in a histogram */
46933707f3Ssthen #define NUM_BUCKETS_HIST 40
47933707f3Ssthen 
48933707f3Ssthen /**
49933707f3Ssthen  * Bucket of time history information
50933707f3Ssthen  */
51933707f3Ssthen struct th_buck {
52933707f3Ssthen 	/** lower bound */
53933707f3Ssthen 	struct timeval lower;
54933707f3Ssthen 	/** upper bound */
55933707f3Ssthen 	struct timeval upper;
56933707f3Ssthen 	/** number of items */
57933707f3Ssthen 	size_t count;
58933707f3Ssthen };
59933707f3Ssthen 
60933707f3Ssthen /**
61933707f3Ssthen  * Keep histogram of time values.
62933707f3Ssthen  */
63933707f3Ssthen struct timehist {
64933707f3Ssthen 	/** number of buckets */
65933707f3Ssthen 	size_t num;
66933707f3Ssthen 	/** bucket array */
67933707f3Ssthen 	struct th_buck* buckets;
68933707f3Ssthen };
69933707f3Ssthen 
70933707f3Ssthen /**
71933707f3Ssthen  * Setup a histogram, default
72933707f3Ssthen  * @return histogram or NULL on malloc failure.
73933707f3Ssthen  */
74933707f3Ssthen struct timehist* timehist_setup(void);
75933707f3Ssthen 
76933707f3Ssthen /**
77933707f3Ssthen  * Delete histogram
78933707f3Ssthen  * @param hist: to delete
79933707f3Ssthen  */
80933707f3Ssthen void timehist_delete(struct timehist* hist);
81933707f3Ssthen 
82933707f3Ssthen /**
83933707f3Ssthen  * Clear histogram
84933707f3Ssthen  * @param hist: to clear all data from
85933707f3Ssthen  */
86933707f3Ssthen void timehist_clear(struct timehist* hist);
87933707f3Ssthen 
88933707f3Ssthen /**
89933707f3Ssthen  * Add time value to histogram.
90933707f3Ssthen  * @param hist: histogram
91933707f3Ssthen  * @param tv: time value
92933707f3Ssthen  */
93933707f3Ssthen void timehist_insert(struct timehist* hist, struct timeval* tv);
94933707f3Ssthen 
95933707f3Ssthen /**
96933707f3Ssthen  * Find time value for given quartile, such as 0.25, 0.50, 0.75.
97933707f3Ssthen  * The looks up the value for the i-th element in the sorted list of time
98933707f3Ssthen  * values, as approximated using the histogram.
99933707f3Ssthen  * @param hist: histogram. Interpolated information is used from it.
100933707f3Ssthen  * @param q: quartile, 0.50 results in the median. Must be >0 and <1.
101933707f3Ssthen  * @return: the time in seconds for that percentage.
102933707f3Ssthen  */
103933707f3Ssthen double timehist_quartile(struct timehist* hist, double q);
104933707f3Ssthen 
105933707f3Ssthen /**
106933707f3Ssthen  * Printout histogram
107933707f3Ssthen  * @param hist: histogram
108933707f3Ssthen  */
109933707f3Ssthen void timehist_print(struct timehist* hist);
110933707f3Ssthen 
111933707f3Ssthen /**
112933707f3Ssthen  * Log histogram, print it to the logfile.
113933707f3Ssthen  * @param hist: histogram
114933707f3Ssthen  * @param name: the name of the value column
115933707f3Ssthen  */
116933707f3Ssthen void timehist_log(struct timehist* hist, const char* name);
117933707f3Ssthen 
118933707f3Ssthen /**
119933707f3Ssthen  * Export histogram to an array.
120933707f3Ssthen  * @param hist: histogram
121933707f3Ssthen  * @param array: the array to export to.
122933707f3Ssthen  * @param sz: number of items in array.
123933707f3Ssthen  */
124*2be9e038Ssthen void timehist_export(struct timehist* hist, long long* array, size_t sz);
125933707f3Ssthen 
126933707f3Ssthen /**
127933707f3Ssthen  * Import histogram from an array.
128933707f3Ssthen  * @param hist: histogram
129933707f3Ssthen  * @param array: the array to import from.
130933707f3Ssthen  * @param sz: number of items in array.
131933707f3Ssthen  */
132*2be9e038Ssthen void timehist_import(struct timehist* hist, long long* array, size_t sz);
133933707f3Ssthen 
134933707f3Ssthen #endif /* UTIL_TIMEHIST_H */
135