xref: /aosp_15_r20/external/ltp/testcases/realtime/include/libstats.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright © International Business Machines  Corp., 2006, 2008
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * NAME
20*49cdfc7eSAndroid Build Coastguard Worker  *       libstats.h
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
23*49cdfc7eSAndroid Build Coastguard Worker  *      Some basic statistical analysis convenience tools.
24*49cdfc7eSAndroid Build Coastguard Worker  *
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * USAGE:
27*49cdfc7eSAndroid Build Coastguard Worker  *      To be included in test cases
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * AUTHOR
30*49cdfc7eSAndroid Build Coastguard Worker  *        Darren Hart <[email protected]>
31*49cdfc7eSAndroid Build Coastguard Worker  *
32*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
33*49cdfc7eSAndroid Build Coastguard Worker  *      2006-Oct-17: Initial version by Darren Hart
34*49cdfc7eSAndroid Build Coastguard Worker  *
35*49cdfc7eSAndroid Build Coastguard Worker  * TODO: the save routine for gnuplot plotting should be more modular...
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  *****************************************************************************/
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #ifndef LIBSTATS_H
40*49cdfc7eSAndroid Build Coastguard Worker #define LIBSTATS_H
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <math.h>
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker #define MIN(A,B) ((A)<(B)?(A):(B))
50*49cdfc7eSAndroid Build Coastguard Worker #define MAX(A,B) ((A)>(B)?(A):(B))
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker typedef struct stats_record {
53*49cdfc7eSAndroid Build Coastguard Worker 	long x;
54*49cdfc7eSAndroid Build Coastguard Worker 	long y;
55*49cdfc7eSAndroid Build Coastguard Worker } stats_record_t;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker typedef struct stats_container {
58*49cdfc7eSAndroid Build Coastguard Worker 	long size;
59*49cdfc7eSAndroid Build Coastguard Worker 	long index;
60*49cdfc7eSAndroid Build Coastguard Worker 	stats_record_t *records;
61*49cdfc7eSAndroid Build Coastguard Worker } stats_container_t;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker enum stats_sort_method {
64*49cdfc7eSAndroid Build Coastguard Worker 	ASCENDING_ON_X,
65*49cdfc7eSAndroid Build Coastguard Worker 	ASCENDING_ON_Y,
66*49cdfc7eSAndroid Build Coastguard Worker 	DESCENDING_ON_X,
67*49cdfc7eSAndroid Build Coastguard Worker 	DESCENDING_ON_Y
68*49cdfc7eSAndroid Build Coastguard Worker };
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker typedef struct stats_quantiles {
71*49cdfc7eSAndroid Build Coastguard Worker 	int nines;
72*49cdfc7eSAndroid Build Coastguard Worker 	long *quantiles;
73*49cdfc7eSAndroid Build Coastguard Worker } stats_quantiles_t;
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker extern int save_stats;
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker /* function prototypes */
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker /* stats_container_init - allocate memory for a new container
80*49cdfc7eSAndroid Build Coastguard Worker  * size: number of records to allocate
81*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_container_t destination pointer
82*49cdfc7eSAndroid Build Coastguard Worker  */
83*49cdfc7eSAndroid Build Coastguard Worker int stats_container_init(stats_container_t *data, long size);
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker /* stats_container_resize - resize a container
86*49cdfc7eSAndroid Build Coastguard Worker  * data: container to resize
87*49cdfc7eSAndroid Build Coastguard Worker  * size: new number of records
88*49cdfc7eSAndroid Build Coastguard Worker  */
89*49cdfc7eSAndroid Build Coastguard Worker int stats_container_resize(stats_container_t *data, long size);
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker /* stats_container_free - free the records array
92*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_container_t to free records
93*49cdfc7eSAndroid Build Coastguard Worker  */
94*49cdfc7eSAndroid Build Coastguard Worker int stats_container_free(stats_container_t *data);
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker /* stats_sort - sort a container according to method
97*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_container_t to sort
98*49cdfc7eSAndroid Build Coastguard Worker  * method: which field and which order to sort
99*49cdfc7eSAndroid Build Coastguard Worker  */
100*49cdfc7eSAndroid Build Coastguard Worker int stats_sort(stats_container_t *data, enum stats_sort_method method);
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker /* stats_stddev - return the standard deviation of the y values in data
103*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_container_t data with y values for use in the calculation
104*49cdfc7eSAndroid Build Coastguard Worker  */
105*49cdfc7eSAndroid Build Coastguard Worker float stats_stddev(stats_container_t *data);
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker /* stats_avg - return the average (mean) of the y values in data
108*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_container_t data with y values for use in the calculation
109*49cdfc7eSAndroid Build Coastguard Worker  */
110*49cdfc7eSAndroid Build Coastguard Worker float stats_avg(stats_container_t *data);
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker /* stats_min - return the minimum of the y values in data
113*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_container_t data with y values for use in the calculation
114*49cdfc7eSAndroid Build Coastguard Worker  */
115*49cdfc7eSAndroid Build Coastguard Worker long stats_min(stats_container_t *data);
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker /* stats_max - return the maximum of the y values in data
118*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_container_t data with y values for use in the calculation
119*49cdfc7eSAndroid Build Coastguard Worker  */
120*49cdfc7eSAndroid Build Coastguard Worker long stats_max(stats_container_t *data);
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker /* stats_container_init - allocate memory for new quantiles
123*49cdfc7eSAndroid Build Coastguard Worker  * nines: int number of nines in most inclusive quantile
124*49cdfc7eSAndroid Build Coastguard Worker  * quantiles: stats_quantiles_t destination pointer
125*49cdfc7eSAndroid Build Coastguard Worker  */
126*49cdfc7eSAndroid Build Coastguard Worker int stats_quantiles_init(stats_quantiles_t *quantiles, int nines);
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker /* stats_quantiles_free - free the quantiles array
129*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_quantiles_t to free
130*49cdfc7eSAndroid Build Coastguard Worker  */
131*49cdfc7eSAndroid Build Coastguard Worker int stats_quantiles_free(stats_quantiles_t *quantiles);
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker /* stats_quantiles_calc - calculate the quantiles of the supplied container
134*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_container_t data with y values for use in the calculation
135*49cdfc7eSAndroid Build Coastguard Worker  * quantiles: stats_quantiles_t structure for storing the results
136*49cdfc7eSAndroid Build Coastguard Worker  */
137*49cdfc7eSAndroid Build Coastguard Worker int stats_quantiles_calc(stats_container_t *data, stats_quantiles_t *quantiles);
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker /* stats_quantiles_print - print the quantiles stored in quantiles
140*49cdfc7eSAndroid Build Coastguard Worker  * quantiles: stats_quantiles_t structure to print
141*49cdfc7eSAndroid Build Coastguard Worker  */
142*49cdfc7eSAndroid Build Coastguard Worker void stats_quantiles_print(stats_quantiles_t *quantiles);
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker /* stats_hist - calculate a histogram with hist->size divisions from data
145*49cdfc7eSAndroid Build Coastguard Worker  * hist: the destination of the histogram data
146*49cdfc7eSAndroid Build Coastguard Worker  * data: the source from which to calculate the histogram
147*49cdfc7eSAndroid Build Coastguard Worker  */
148*49cdfc7eSAndroid Build Coastguard Worker int stats_hist(stats_container_t *hist, stats_container_t *data);
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker /* stats_hist_print - print out a histogram in human readable format
151*49cdfc7eSAndroid Build Coastguard Worker  * hist: the stats_container containing the histogram data
152*49cdfc7eSAndroid Build Coastguard Worker  */
153*49cdfc7eSAndroid Build Coastguard Worker void stats_hist_print(stats_container_t *hist);
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker /* stats_container_save - save the x,y data to a file and create a gnuplot
156*49cdfc7eSAndroid Build Coastguard Worker  * runnable script.
157*49cdfc7eSAndroid Build Coastguard Worker  * filename: the filename to save the data as without an extension. A .dat
158*49cdfc7eSAndroid Build Coastguard Worker  * and a .plt file will be created.
159*49cdfc7eSAndroid Build Coastguard Worker  * title: the title of the graph
160*49cdfc7eSAndroid Build Coastguard Worker  * labelx: the x-axis label
161*49cdfc7eSAndroid Build Coastguard Worker  * labely: the y-axis label
162*49cdfc7eSAndroid Build Coastguard Worker  * mode: "points" "lines" "steps" etc, see gnuplot help for plotting types
163*49cdfc7eSAndroid Build Coastguard Worker  */
164*49cdfc7eSAndroid Build Coastguard Worker int stats_container_save(char *filename, char *title, char *labelx, char *labely, stats_container_t *data, char *mode);
165*49cdfc7eSAndroid Build Coastguard Worker 
166*49cdfc7eSAndroid Build Coastguard Worker /* stats_container_append - appends stats_record_t to data
167*49cdfc7eSAndroid Build Coastguard Worker  * data: stats_container_t structure for holding the records list, index of
168*49cdfc7eSAndroid Build Coastguard Worker  *       min and max elements in records list and the sum
169*49cdfc7eSAndroid Build Coastguard Worker  * rec: stats_record_t to be appended to the records list in data
170*49cdfc7eSAndroid Build Coastguard Worker  * Returns the index of the appended record on success and -1 on error
171*49cdfc7eSAndroid Build Coastguard Worker  */
172*49cdfc7eSAndroid Build Coastguard Worker int stats_container_append(stats_container_t *data, stats_record_t rec);
173*49cdfc7eSAndroid Build Coastguard Worker #endif /* LIBSTAT_H */
174