Damien Lespiau | 06f5f70 | 2015-06-25 12:07:56 +0100 | [diff] [blame] | 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 | |
Damien Lespiau | 515cec1 | 2015-06-25 23:59:21 +0100 | [diff] [blame] | 25 | #include <math.h> |
Damien Lespiau | 203c384 | 2015-06-26 13:55:01 +0100 | [diff] [blame^] | 26 | #include <string.h> |
Damien Lespiau | 515cec1 | 2015-06-25 23:59:21 +0100 | [diff] [blame] | 27 | |
Damien Lespiau | 06f5f70 | 2015-06-25 12:07:56 +0100 | [diff] [blame] | 28 | #include "igt_core.h" |
| 29 | #include "igt_stats.h" |
| 30 | |
| 31 | void igt_stats_init(igt_stats_t *stats, unsigned int capacity) |
| 32 | { |
Damien Lespiau | 203c384 | 2015-06-26 13:55:01 +0100 | [diff] [blame^] | 33 | memset(stats, 0, sizeof(*stats)); |
| 34 | |
Damien Lespiau | 06f5f70 | 2015-06-25 12:07:56 +0100 | [diff] [blame] | 35 | stats->values = calloc(capacity, sizeof(*stats->values)); |
| 36 | igt_assert(stats->values); |
| 37 | stats->capacity = capacity; |
Damien Lespiau | 06f5f70 | 2015-06-25 12:07:56 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void igt_stats_fini(igt_stats_t *stats) |
| 41 | { |
| 42 | free(stats->values); |
| 43 | } |
| 44 | |
| 45 | void 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 Lespiau | 05c10f9 | 2015-06-25 23:57:49 +0100 | [diff] [blame] | 49 | 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 | */ |
| 60 | static 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 Lespiau | 06f5f70 | 2015-06-25 12:07:56 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Damien Lespiau | e55a11d | 2015-06-25 23:44:20 +0100 | [diff] [blame] | 80 | double igt_stats_get_mean(igt_stats_t *stats) |
Damien Lespiau | 06f5f70 | 2015-06-25 12:07:56 +0100 | [diff] [blame] | 81 | { |
Damien Lespiau | 05c10f9 | 2015-06-25 23:57:49 +0100 | [diff] [blame] | 82 | igt_stats_knuth_mean_variance(stats); |
Damien Lespiau | 06f5f70 | 2015-06-25 12:07:56 +0100 | [diff] [blame] | 83 | |
Damien Lespiau | 05c10f9 | 2015-06-25 23:57:49 +0100 | [diff] [blame] | 84 | return stats->mean; |
| 85 | } |
Damien Lespiau | 06f5f70 | 2015-06-25 12:07:56 +0100 | [diff] [blame] | 86 | |
Damien Lespiau | 05c10f9 | 2015-06-25 23:57:49 +0100 | [diff] [blame] | 87 | double igt_stats_get_variance(igt_stats_t *stats) |
| 88 | { |
| 89 | igt_stats_knuth_mean_variance(stats); |
| 90 | |
| 91 | return stats->variance; |
Damien Lespiau | 06f5f70 | 2015-06-25 12:07:56 +0100 | [diff] [blame] | 92 | } |
Damien Lespiau | 515cec1 | 2015-06-25 23:59:21 +0100 | [diff] [blame] | 93 | |
| 94 | double igt_stats_get_std_deviation(igt_stats_t *stats) |
| 95 | { |
| 96 | igt_stats_knuth_mean_variance(stats); |
| 97 | |
| 98 | return sqrt(stats->variance); |
| 99 | } |