lib: Add a tiny utility function to compute averages
PATCH 1/4 lib: Add a tiny utility function to compute averages
The master plan would be to get a bit more stats in it, at least the standard deviation and confidence interval. Just need the average for now.
--- lib/Makefile.sources | 2 ++ lib/igt_stats.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ lib/igt_stats.h | 36 +++++++++++++++++++++++++++++ lib/tests/.gitignore | 1 + lib/tests/Makefile.sources | 1 + lib/tests/igt_stats.c | 49 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+) create mode 100644 lib/igt_stats.c create mode 100644 lib/igt_stats.h create mode 100644 lib/tests/igt_stats.c
lib/Makefile.sources +2 -0
@@ -12,6 +12,8 @@ libintel_tools_la_SOURCES = \ igt_aux.h \ igt_gt.c \ igt_gt.h \ + igt_stats.c \ + igt_stats.h \ instdone.c \ instdone.h \ intel_batchbuffer.c \
lib/igt_stats.c new file +56 -0
56 lines not on this page · raw message
lib/igt_stats.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* Copyright © 2015 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#include <stdint.h> + +typedef struct { +uint64_t *values; +unsigned int capacity; +unsigned int n_values; +}igt_stats_t ; + +void igt_stats_init (igt_stats_t *stats,unsigned int capacity); +void igt_stats_fini (igt_stats_t *stats); +void igt_stats_push (igt_stats_t *stats,uint64_t value); +double igt_stats_get_average (igt_stats_t *stats);
lib/tests/.gitignore +1 -0
@@ -7,4 +7,5 @@ igt_no_exit_list_only igt_no_subtest igt_simple_test_subtests igt_simulation +igt_stats igt_timeout
lib/tests/Makefile.sources +1 -0
@@ -6,6 +6,7 @@ check_PROGRAMS = \ igt_no_subtest \ igt_simulation \ igt_simple_test_subtests \ + igt_stats \ igt_timeout \ igt_invalid_subtest_name \ $(NULL)
lib/tests/igt_stats.c new file +49 -0
49 lines not on this page · raw message
Signature · 6 lines
2.1.0 _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 2/4 skl_compute_wrpll: Print the average deviation
It's interesting to watch the effect of some algorithm tweaks on the average deviation between the central freq and the dco freq. A metric we'd like to minimize.
--- tools/skl_compute_wrpll.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
tools/skl_compute_wrpll.c +10 -0
@@ -29,6 +29,8 @@#include <stdlib.h> #include <string.h> +#include "igt_stats.h" +#define ARRAY_SIZE (arr) (sizeof(arr)/sizeof(arr[0]))#define WARN (cond, msg) printf(msg)
@@ -844,6 +846,9 @@ static void test_run(struct test_ops *test) {unsigned int m;unsigned p_odd_even[2 ] = {0 ,0 }; +igt_stats_t stats; + +igt_stats_init (&stats,ARRAY_SIZE (modes));for (m =0 ; m <ARRAY_SIZE (modes); m++) {struct skl_wrpll_params params = {};
@@ -871,6 +876,8 @@ static void test_run(struct test_ops *test) diff =abs_diff (dco_freq, central_freq); deviation =div64_u64 (10000 * diff, central_freq); +igt_stats_push (&stats, deviation); +if (dco_freq > central_freq) {if (deviation >100 ) printf("failed constraint for %dHz "
@@ -892,6 +899,9 @@ static void test_run(struct test_ops *test) }printf ("even/odd dividers: %d/%d\n" , p_odd_even[0 ], p_odd_even[1 ]); +printf ("average deviation: %.2lf\n" ,igt_stats_get_average (&stats)); + +igt_stats_fini (&stats); }int main (int argc,char **argv)
Signature · 6 lines
2.1.0 _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 3/4 skl_compute_wrpll: Cycle through dividers, then central freqs
In reply to Damien LespiauFollow Paulo's comment on the corresponding kernel patch.
This means we also have to move the break when we have cycled through the even dividers as well.
This improves the number of even dividers used across the tested frequencies (373) (at the expense of a slightly worse average deviation, but "even dividers take precedence over a lower deviation".
before:
even/odd dividers: 338/35
average deviation: 206.52
after:
even/odd dividers: 363/10
average deviation: 215.13
--- tools/skl_compute_wrpll.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
tools/skl_compute_wrpll.c +9 -9
@@ -422,8 +422,8 @@ skl_ddi_calculate_wrpll2(int clock /* in Hz */,skl_wrpll_context_init (&ctx); -for (dco =0 ; dco <ARRAY_SIZE (dco_central_freq); dco++) { -for (d =0 ; d <ARRAY_SIZE (dividers); d++) { +for (d =0 ; d <ARRAY_SIZE (dividers); d++) { +for (dco =0 ; dco <ARRAY_SIZE (dco_central_freq); dco++) {for (i =0 ; i < dividers[d].n_dividers; i++) {unsigned int p = dividers[d].list[i];uint64_t dco_freq = p * afe_clock;
@@ -433,14 +433,14 @@ skl_ddi_calculate_wrpll2(int clock /* in Hz */, dco_freq, p); } - -/* -* If a solution is found with an even divider, prefer -* this one. -*/ -if (d ==0 && ctx.p) -break ; } + +/* +* If a solution is found with an even divider, prefer +* this one. +*/ +if (d ==0 && ctx.p) +break ; }if (!ctx.p)
Signature · 6 lines
2.1.0 _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 4/4 skl_compute_wrpll: Fix the mininum deviation computation
In reply to Damien LespiauPaulo noticed that, because we were only comparing positive deviations with positive deviations and negative deviations with negative deviations, we weren't actually always using the absolute minimal deviation at all.
This improves the average deviation across all tested frequencies (373):
before: average deviation: 215.13
after: average deviation: 194.47
--- tools/skl_compute_wrpll.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
tools/skl_compute_wrpll.c +13 -9
@@ -23,6 +23,7 @@#include <assert.h> #include <inttypes.h> +#include <limits.h> #include <stdio.h> #include <stdbool.h> #include <stdint.h>
@@ -31,6 +32,7 @@#include "igt_stats.h" +#define U64_MAX ((uint64_t)~0ULL)#define ARRAY_SIZE (arr) (sizeof(arr)/sizeof(arr[0]))#define WARN (cond, msg) printf(msg)
@@ -255,8 +257,7 @@ found: }struct skl_wrpll_context { -uint32_t min_pdeviation;/* record the minimum deviations to */ -uint32_t min_ndeviation;/* compare candidates */ +uint64_t min_deviation;/* current minimal deviation */ uint64_t central_freq;/* chosen central freq */ uint64_t dco_freq;/* chosen dco freq */ unsigned int p;/* chosen divider */
@@ -266,11 +267,12 @@ static void skl_wrpll_context_init(struct skl_wrpll_context *ctx) {memset (ctx,0 ,sizeof (*ctx)); -/* DCO freq must be within +1%/-6% of the DCO central freq */ - ctx->min_pdeviation =100 ; - ctx-> min_ndeviation =600 ; + ctx->min_deviation =U64_MAX ; } +#define SKL_MAX_PDEVIATION 100 +#define SKL_MAX_NDEVIATION 600 + static void skl_wrpll_try_divider(struct skl_wrpll_context *ctx, uint64_t central_freq, uint64_t dco_freq,
@@ -284,8 +286,9 @@ static void skl_wrpll_try_divider(struct skl_wrpll_context *ctx,/* positive deviation */ if (dco_freq >= central_freq) { -if (deviation < ctx->min_pdeviation) { - ctx->min_pdeviation = deviation; +if (deviation <SKL_MAX_PDEVIATION && + deviation < ctx->min_deviation) { + ctx->min_deviation = deviation; ctx->central_freq = central_freq; ctx->dco_freq = dco_freq; ctx->p = divider;
@@ -294,8 +297,9 @@ static void skl_wrpll_try_divider(struct skl_wrpll_context *ctx,#endif }/* negative deviation */ - }else if (deviation < ctx->min_ndeviation) { - ctx->min_ndeviation = deviation; + }else if (deviation <SKL_MAX_NDEVIATION && + deviation < ctx->min_deviation) { + ctx->min_deviation = deviation; ctx->central_freq = central_freq; ctx->dco_freq = dco_freq; ctx->p = divider;
Signature · 6 lines
2.1.0 _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Quoted 3 lines · Damien Lespiau
The master plan would be to get a bit more stats in it, at least the standard deviation and confidence interval. Just need the average for now.
Definitely, I wouldn't even print the average without the std.dev and how normal the distribution is. (Otherwise the information can be very misleading.) Without them, I'd rather have the median than the mean.
lib/igt_stats.c line 43
Quoted 3 lines
lib/igt_stats.c new file +3
-0+void igt_stats_push (igt_stats_t *stats,uint64_t value) +{ +igt_assert (stats->n_values < stats->capacity);
Might as well make this a realloced array from the start.
lib/igt_stats.c line 53
Quoted 10 lines
lib/igt_stats.c new file +10
-0+ stats->values[stats->n_values++] = value; +} + +double igt_stats_get_average (igt_stats_t *stats) +{ +unsigned int i; +double a =0.0 ; + +for (i =0 ; i < stats->n_values; i++) + a += (double )stats->values[i] / stats->n_values;
For fun, we could write numerically more stable versions. Welcome to the bikeshed.
static double kahan_summation(igt_stats_t *stats)
{
double sum = 0;
double error = 0;
unsigned i;
for (i = 0; i < stats->n_values; i++) {
double x = stats->values[i] - error;
double tmp = sum + x;
error = (tmp - sum) - x;
sum = tmp;
}
return sum;
}double igt_stats_get_average(igt_stats_t *stats)
{
return kahan_summation(stats) / stats->n_values;
}or
static knuth_avergae(igt_stats_t *stats)
{
double mean = 0;
unsigned i;
for (i = 0; i < stats->n_values; i++)
mean += (stats->values[i] - mean) / (i + 1);
return mean;
}double igt_stats_get_average(igt_stats_t *stats)
{
return knuth_average(stats);
}or if we are sorting to get the median anyway, doing a pairwise summation would also reduce the numerical errors... -Chris
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Quoted 5 lines · Damien Lespiau
The master plan would be to get a bit more stats in it, at least the standard deviation and confidence interval. Just need the average for now.
Signed-off-by Damien Lespiau <[email protected]>
gtkdoc is missing for this.
-Daniel
Quoted 213 lines
--- lib/Makefile.sources | 2 ++ lib/igt_stats.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ lib/igt_stats.h | 36 +++++++++++++++++++++++++++++ lib/tests/.gitignore | 1 + lib/tests/Makefile.sources | 1 + lib/tests/igt_stats.c | 49 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+) create mode 100644 lib/igt_stats.c create mode 100644 lib/igt_stats.h create mode 100644 lib/tests/igt_stats.clib/Makefile.sources +2
-0@@ -12,6 +12,8 @@ libintel_tools_la_SOURCES = \ igt_aux.h \ igt_gt.c \ igt_gt.h \ + igt_stats.c \ + igt_stats.h \ instdone.c \ instdone.h \ intel_batchbuffer.c \lib/igt_stats.c new file +56
-0@@ -0,0 +1,56 @@ +/* +* Copyright © 2015 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#include "igt_core.h" +#include "igt_stats.h" + +void igt_stats_init (igt_stats_t *stats,unsigned int capacity) +{ + stats->values =calloc (capacity,sizeof (*stats->values)); +igt_assert (stats->values); + stats->capacity = capacity; + stats->n_values =0 ; +} + +void igt_stats_fini (igt_stats_t *stats) +{ +free (stats->values); +} + +void igt_stats_push (igt_stats_t *stats,uint64_t value) +{ +igt_assert (stats->n_values < stats->capacity); + stats->values[stats->n_values++] = value; +} + +double igt_stats_get_average (igt_stats_t *stats) +{ +unsigned int i; +double a =0.0 ; + +for (i =0 ; i < stats->n_values; i++) + a += (double )stats->values[i] / stats->n_values; + +return a; +}lib/igt_stats.h new file +36
-0@@ -0,0 +1,36 @@ +/* +* Copyright © 2015 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#include <stdint.h> + +typedef struct { +uint64_t *values; +unsigned int capacity; +unsigned int n_values; +}igt_stats_t ; + +void igt_stats_init (igt_stats_t *stats,unsigned int capacity); +void igt_stats_fini (igt_stats_t *stats); +void igt_stats_push (igt_stats_t *stats,uint64_t value); +double igt_stats_get_average (igt_stats_t *stats);lib/tests/.gitignore +1
-0@@ -7,4 +7,5 @@ igt_no_exit_list_only igt_no_subtest igt_simple_test_subtests igt_simulation +igt_stats igt_timeoutlib/tests/Makefile.sources +1
-0@@ -6,6 +6,7 @@ check_PROGRAMS = \ igt_no_subtest \ igt_simulation \ igt_simple_test_subtests \ + igt_stats \ igt_timeout \ igt_invalid_subtest_name \ $(NULL)lib/tests/igt_stats.c new file +49
-0@@ -0,0 +1,49 @@ +/* +* Copyright © 2015 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#include "igt_core.h" +#include "igt_stats.h" + +static void test_average (void ) +{ +igt_stats_t stats; +double average; + +igt_stats_init (&stats,5 ); + +igt_stats_push (&stats,2 ); +igt_stats_push (&stats,4 ); +igt_stats_push (&stats,6 ); +igt_stats_push (&stats,8 ); +igt_stats_push (&stats,10 ); + + average =igt_stats_get_average (&stats); + +igt_assert (average == (2 +4 +6 +8 +10 ) /5. ); +} + +igt_simple_main +{ +test_average (); +}Signature · 6 lines
2.1.0 _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 7 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch _______________________________________________ Intel-gfx mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/intel-gfx