5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen#ifndef STATS_DIST_H
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen#define STATS_DIST_H
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenstruct stats_dist *stats_dist_init(void);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenstruct stats_dist *stats_dist_init_with_size(unsigned int sample_count);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenvoid stats_dist_deinit(struct stats_dist **stats);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Reset all events. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenvoid stats_dist_reset(struct stats_dist *stats);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Add a new event. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenvoid stats_dist_add(struct stats_dist *stats, uint64_t value);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Returns number of events added. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenunsigned int stats_dist_get_count(const struct stats_dist *stats);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Returns the sum of all events. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenuint64_t stats_dist_get_sum(const struct stats_dist *stats);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Returns events' minimum. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenuint64_t stats_dist_get_min(const struct stats_dist *stats);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Returns events' maximum. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenuint64_t stats_dist_get_max(const struct stats_dist *stats);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Returns events' average. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenuint64_t stats_dist_get_avg(const struct stats_dist *stats);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Returns events' approximate (through random subsampling) median. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenuint64_t stats_dist_get_median(const struct stats_dist *stats);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Returns events' approximate (through random subsampling) percentile.
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen fraction parameter is in the range (0., 1.], so 95th %-ile is 0.95. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenuint64_t stats_dist_get_percentile(const struct stats_dist *stats, double fraction);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen/* Returns events' approximate (through random subsampling) 95th percentile. */
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainenstatic inline uint64_t stats_dist_get_95th(const struct stats_dist *stats)
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen{
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen return stats_dist_get_percentile(stats, 0.95);
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen}
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen
5f08b0309190ec818d46bfe0e497468b30714a93Timo Sirainen#endif