1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #ifndef __IGT_STATS_H__
26 #define __IGT_STATS_H__
27 
28 #include <stdint.h>
29 #include <stdbool.h>
30 #include <math.h>
31 
32 /**
33  * igt_stats_t:
34  * @values_u64: An array containing pushed integer values
35  * @is_float: Whether @values_f or @values_u64 is valid
36  * @values_f: An array containing pushed float values
37  * @n_values: The number of pushed values
38  */
39 typedef struct {
40 	unsigned int n_values;
41 	unsigned int is_float : 1;
42 	union {
43 		uint64_t *values_u64;
44 		double *values_f;
45 	};
46 
47 	/*< private >*/
48 	unsigned int capacity;
49 	unsigned int is_population  : 1;
50 	unsigned int mean_variance_valid : 1;
51 	unsigned int sorted_array_valid : 1;
52 
53 	uint64_t min, max;
54 	double range[2];
55 	double mean, variance;
56 
57 	union {
58 		uint64_t *sorted_u64;
59 		double *sorted_f;
60 	};
61 } igt_stats_t;
62 
63 void igt_stats_init(igt_stats_t *stats);
64 void igt_stats_init_with_size(igt_stats_t *stats, unsigned int capacity);
65 void igt_stats_fini(igt_stats_t *stats);
66 bool igt_stats_is_population(igt_stats_t *stats);
67 void igt_stats_set_population(igt_stats_t *stats, bool full_population);
68 void igt_stats_push(igt_stats_t *stats, uint64_t value);
69 void igt_stats_push_float(igt_stats_t *stats, double value);
70 void igt_stats_push_array(igt_stats_t *stats,
71 			  const uint64_t *values, unsigned int n_values);
72 uint64_t igt_stats_get_min(igt_stats_t *stats);
73 uint64_t igt_stats_get_max(igt_stats_t *stats);
74 uint64_t igt_stats_get_range(igt_stats_t *stats);
75 void igt_stats_get_quartiles(igt_stats_t *stats,
76 			     double *q1, double *q2, double *q3);
77 double igt_stats_get_iqr(igt_stats_t *stats);
78 double igt_stats_get_iqm(igt_stats_t *stats);
79 double igt_stats_get_mean(igt_stats_t *stats);
80 double igt_stats_get_trimean(igt_stats_t *stats);
81 double igt_stats_get_median(igt_stats_t *stats);
82 double igt_stats_get_variance(igt_stats_t *stats);
83 double igt_stats_get_std_deviation(igt_stats_t *stats);
84 
85 /**
86  * igt_mean:
87  *
88  * Structure to compute running statistical numbers. Needs to be initialized
89  * with igt_mean_init(). Read out data using igt_mean_get() and
90  * igt_mean_get_variance().
91  */
92 struct igt_mean {
93 	/*< private >*/
94 	double mean, sq, min, max;
95 	unsigned long count;
96 };
97 
98 void igt_mean_init(struct igt_mean *m);
99 void igt_mean_add(struct igt_mean *m, double v);
100 double igt_mean_get(struct igt_mean *m);
101 double igt_mean_get_variance(struct igt_mean *m);
102 
103 #endif /* __IGT_STATS_H__ */
104