Home | History | Annotate | Line # | Download | only in util
      1 /*
      2  * util/timehist.h - make histogram of time values.
      3  *
      4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
      5  *
      6  * This software is open source.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  *
     12  * Redistributions of source code must retain the above copyright notice,
     13  * this list of conditions and the following disclaimer.
     14  *
     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.
     18  *
     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.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /**
     37  * \file
     38  *
     39  * This file contains functions to make a histogram of time values.
     40  */
     41 
     42 #ifndef UTIL_TIMEHIST_H
     43 #define UTIL_TIMEHIST_H
     44 
     45 #ifdef __QNX__
     46 /* For struct timeval */
     47 #include <sys/time.h>
     48 #endif /* __QNX__ */
     49 /** Number of buckets in a histogram */
     50 #define NUM_BUCKETS_HIST 40
     51 
     52 /**
     53  * Bucket of time history information
     54  */
     55 struct th_buck {
     56 	/** lower bound */
     57 	struct timeval lower;
     58 	/** upper bound */
     59 	struct timeval upper;
     60 	/** number of items */
     61 	size_t count;
     62 };
     63 
     64 /**
     65  * Keep histogram of time values.
     66  */
     67 struct timehist {
     68 	/** number of buckets */
     69 	size_t num;
     70 	/** bucket array */
     71 	struct th_buck* buckets;
     72 };
     73 
     74 /**
     75  * Setup a histogram, default
     76  * @return histogram or NULL on malloc failure.
     77  */
     78 struct timehist* timehist_setup(void);
     79 
     80 /**
     81  * Delete histogram
     82  * @param hist: to delete
     83  */
     84 void timehist_delete(struct timehist* hist);
     85 
     86 /**
     87  * Clear histogram
     88  * @param hist: to clear all data from
     89  */
     90 void timehist_clear(struct timehist* hist);
     91 
     92 /**
     93  * Add time value to histogram.
     94  * @param hist: histogram
     95  * @param tv: time value
     96  */
     97 void timehist_insert(struct timehist* hist, struct timeval* tv);
     98 
     99 /**
    100  * Find time value for given quartile, such as 0.25, 0.50, 0.75.
    101  * The looks up the value for the i-th element in the sorted list of time
    102  * values, as approximated using the histogram.
    103  * @param hist: histogram. Interpolated information is used from it.
    104  * @param q: quartile, 0.50 results in the median. Must be >0 and <1.
    105  * @return: the time in seconds for that percentage.
    106  */
    107 double timehist_quartile(struct timehist* hist, double q);
    108 
    109 /**
    110  * Printout histogram
    111  * @param hist: histogram
    112  */
    113 void timehist_print(struct timehist* hist);
    114 
    115 /**
    116  * Log histogram, print it to the logfile.
    117  * @param hist: histogram
    118  * @param name: the name of the value column
    119  */
    120 void timehist_log(struct timehist* hist, const char* name);
    121 
    122 /**
    123  * Export histogram to an array.
    124  * @param hist: histogram
    125  * @param array: the array to export to.
    126  * @param sz: number of items in array.
    127  */
    128 void timehist_export(struct timehist* hist, long long* array, size_t sz);
    129 
    130 /**
    131  * Import histogram from an array.
    132  * @param hist: histogram
    133  * @param array: the array to import from.
    134  * @param sz: number of items in array.
    135  */
    136 void timehist_import(struct timehist* hist, long long* array, size_t sz);
    137 
    138 #endif /* UTIL_TIMEHIST_H */
    139