blob: 67ee94734ab6e2b55efaa6f02ae64b9f217af156 [file] [log] [blame]
Damien Lespiau06f5f702015-06-25 12:07:56 +01001/*
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
Damien Lespiau515cec12015-06-25 23:59:21 +010025#include <math.h>
Damien Lespiau203c3842015-06-26 13:55:01 +010026#include <string.h>
Damien Lespiau515cec12015-06-25 23:59:21 +010027
Damien Lespiau06f5f702015-06-25 12:07:56 +010028#include "igt_core.h"
29#include "igt_stats.h"
30
31void igt_stats_init(igt_stats_t *stats, unsigned int capacity)
32{
Damien Lespiau203c3842015-06-26 13:55:01 +010033 memset(stats, 0, sizeof(*stats));
34
Damien Lespiau06f5f702015-06-25 12:07:56 +010035 stats->values = calloc(capacity, sizeof(*stats->values));
36 igt_assert(stats->values);
37 stats->capacity = capacity;
Damien Lespiau06f5f702015-06-25 12:07:56 +010038}
39
40void igt_stats_fini(igt_stats_t *stats)
41{
42 free(stats->values);
43}
44
45void igt_stats_push(igt_stats_t *stats, uint64_t value)
46{
47 igt_assert(stats->n_values < stats->capacity);
48 stats->values[stats->n_values++] = value;
Damien Lespiau05c10f92015-06-25 23:57:49 +010049 stats->mean_variance_valid = false;
50}
51
52/*
53 * Algorithm popularised by Knuth in:
54 *
55 * The Art of Computer Programming, volume 2: Seminumerical Algorithms,
56 * 3rd edn., p. 232. Boston: Addison-Wesley
57 *
58 * Source: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
59 */
60static void igt_stats_knuth_mean_variance(igt_stats_t *stats)
61{
62 double mean = 0., m2 = 0.;
63 unsigned int i;
64
65 if (stats->mean_variance_valid)
66 return;
67
68 for (i = 0; i < stats->n_values; i++) {
69 double delta = stats->values[i] - mean;
70
71 mean += delta / (i + 1);
72 m2 += delta * (stats->values[i] - mean);
73 }
74
75 stats->mean = mean;
76 stats->variance = m2 / stats->n_values;
77 stats->mean_variance_valid = true;
Damien Lespiau06f5f702015-06-25 12:07:56 +010078}
79
Damien Lespiaue55a11d2015-06-25 23:44:20 +010080double igt_stats_get_mean(igt_stats_t *stats)
Damien Lespiau06f5f702015-06-25 12:07:56 +010081{
Damien Lespiau05c10f92015-06-25 23:57:49 +010082 igt_stats_knuth_mean_variance(stats);
Damien Lespiau06f5f702015-06-25 12:07:56 +010083
Damien Lespiau05c10f92015-06-25 23:57:49 +010084 return stats->mean;
85}
Damien Lespiau06f5f702015-06-25 12:07:56 +010086
Damien Lespiau05c10f92015-06-25 23:57:49 +010087double igt_stats_get_variance(igt_stats_t *stats)
88{
89 igt_stats_knuth_mean_variance(stats);
90
91 return stats->variance;
Damien Lespiau06f5f702015-06-25 12:07:56 +010092}
Damien Lespiau515cec12015-06-25 23:59:21 +010093
94double igt_stats_get_std_deviation(igt_stats_t *stats)
95{
96 igt_stats_knuth_mean_variance(stats);
97
98 return sqrt(stats->variance);
99}