diff mbox

[i-g-t,2/3] stats: Spwan igt_init_with_size() from igt_init()

Message ID 1435424869-17455-3-git-send-email-damien.lespiau@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Lespiau, Damien June 27, 2015, 5:07 p.m. UTC
It's all about good looking APIs.

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 lib/igt_stats.c           | 27 ++++++++++++++++++++++-----
 lib/igt_stats.h           |  3 ++-
 lib/tests/igt_stats.c     | 20 ++++++++++----------
 tools/skl_compute_wrpll.c |  2 +-
 4 files changed, 35 insertions(+), 17 deletions(-)
diff mbox

Patch

diff --git a/lib/igt_stats.c b/lib/igt_stats.c
index cd97ab0..6b867e3 100644
--- a/lib/igt_stats.c
+++ b/lib/igt_stats.c
@@ -101,15 +101,32 @@  static void igt_stats_ensure_capacity(igt_stats_t *stats,
 /**
  * igt_stats_init:
  * @stats: An #igt_stats_t instance
+ *
+ * Initializes an #igt_stats_t instance. igt_stats_fini() must be called once
+ * finished with @stats.
+ */
+void igt_stats_init(igt_stats_t *stats)
+{
+	memset(stats, 0, sizeof(*stats));
+
+	igt_stats_ensure_capacity(stats, 128);
+
+	stats->min = U64_MAX;
+	stats->max = 0;
+}
+
+/**
+ * igt_stats_init_with_size:
+ * @stats: An #igt_stats_t instance
  * @capacity: Number of data samples @stats can contain
  *
- * Initializes an #igt_stats_t instance to hold @capacity samples.
- * igt_stats_fini() must be called once finished with @stats.
+ * Like igt_stats_init() but with a size to avoid reallocating the underlying
+ * array(s) when pushing new values. Useful if we have a good idea of the
+ * number of data points we want @stats to hold.
  *
- * We currently assume the user knows how many data samples upfront and there's
- * no need to grow the array of values.
+ * igt_stats_fini() must be called once finished with @stats.
  */
-void igt_stats_init(igt_stats_t *stats, unsigned int capacity)
+void igt_stats_init_with_size(igt_stats_t *stats, unsigned int capacity)
 {
 	memset(stats, 0, sizeof(*stats));
 
diff --git a/lib/igt_stats.h b/lib/igt_stats.h
index 6ee3ae6..dd04097 100644
--- a/lib/igt_stats.h
+++ b/lib/igt_stats.h
@@ -47,7 +47,8 @@  typedef struct {
 	uint64_t *sorted;
 } igt_stats_t;
 
-void igt_stats_init(igt_stats_t *stats, unsigned int capacity);
+void igt_stats_init(igt_stats_t *stats);
+void igt_stats_init_with_size(igt_stats_t *stats, unsigned int capacity);
 void igt_stats_fini(igt_stats_t *stats);
 bool igt_stats_is_population(igt_stats_t *stats);
 void igt_stats_set_population(igt_stats_t *stats, bool full_population);
diff --git a/lib/tests/igt_stats.c b/lib/tests/igt_stats.c
index dfdc507..6e4b233 100644
--- a/lib/tests/igt_stats.c
+++ b/lib/tests/igt_stats.c
@@ -42,7 +42,7 @@  static void test_init_zero(void)
 	igt_stats_t stats;
 
 	stats.mean = 1.;
-	igt_stats_init(&stats, 2);
+	igt_stats_init(&stats);
 	igt_assert_eq_double(stats.mean, 0.);
 }
 
@@ -50,7 +50,7 @@  static void test_init(void)
 {
 	igt_stats_t stats;
 
-	igt_stats_init(&stats, 2);
+	igt_stats_init(&stats);
 
 	/*
 	 * Make sure we default to representing only a sample of a bigger
@@ -63,7 +63,7 @@  static void test_min_max(void)
 {
 	igt_stats_t stats;
 
-	igt_stats_init(&stats, 5);
+	igt_stats_init(&stats);
 	push_fixture_1(&stats);
 
 	igt_assert(igt_stats_get_min(&stats) == 2);
@@ -74,7 +74,7 @@  static void test_range(void)
 {
 	igt_stats_t stats;
 
-	igt_stats_init(&stats, 5);
+	igt_stats_init(&stats);
 	push_fixture_1(&stats);
 
 	igt_assert(igt_stats_get_range(&stats) == 8);
@@ -94,7 +94,7 @@  static void test_quartiles(void)
 	double q1, q2, q3;
 
 	/* s1, odd number of data points */
-	igt_stats_init(&stats, ARRAY_SIZE(s1));
+	igt_stats_init(&stats);
 	igt_stats_push_array(&stats, s1, ARRAY_SIZE(s1));
 
 	igt_stats_get_quartiles(&stats, &q1, &q2, &q3);
@@ -107,7 +107,7 @@  static void test_quartiles(void)
 	igt_stats_fini(&stats);
 
 	/* s1, even number of data points */
-	igt_stats_init(&stats, ARRAY_SIZE(s2));
+	igt_stats_init(&stats);
 	igt_stats_push_array(&stats, s2, ARRAY_SIZE(s2));
 
 	igt_stats_get_quartiles(&stats, &q1, &q2, &q3);
@@ -127,7 +127,7 @@  static void test_invalidate_sorted(void)
 		{ 47, 49, 6, 7, 15, 36, 39, 40, 41, 42};
 	double median1, median2;
 
-	igt_stats_init(&stats, ARRAY_SIZE(s1_truncated) + 1);
+	igt_stats_init(&stats);
 	igt_stats_push_array(&stats, s1_truncated, ARRAY_SIZE(s1_truncated));
 	median1 = igt_stats_get_median(&stats);
 
@@ -143,7 +143,7 @@  static void test_mean(void)
 	igt_stats_t stats;
 	double mean;
 
-	igt_stats_init(&stats, 5);
+	igt_stats_init(&stats);
 	push_fixture_1(&stats);
 
 	mean = igt_stats_get_mean(&stats);
@@ -157,7 +157,7 @@  static void test_invalidate_mean(void)
 	igt_stats_t stats;
 	double mean1, mean2;
 
-	igt_stats_init(&stats, 6);
+	igt_stats_init(&stats);
 	push_fixture_1(&stats);
 
 	mean1 = igt_stats_get_mean(&stats);
@@ -180,7 +180,7 @@  static void test_std_deviation(void)
 	igt_stats_t stats;
 	double mean, variance, std_deviation;
 
-	igt_stats_init(&stats, 8);
+	igt_stats_init(&stats);
 	igt_stats_set_population(&stats, true);
 
 	igt_stats_push(&stats, 2);
diff --git a/tools/skl_compute_wrpll.c b/tools/skl_compute_wrpll.c
index 8b6fcd3..0e5965c 100644
--- a/tools/skl_compute_wrpll.c
+++ b/tools/skl_compute_wrpll.c
@@ -866,7 +866,7 @@  static void test_run(struct test_ops *test)
 	unsigned p_odd_even[2] = { 0, 0 };
 	igt_stats_t stats;
 
-	igt_stats_init(&stats, ARRAY_SIZE(modes));
+	igt_stats_init_with_size(&stats, ARRAY_SIZE(modes));
 	igt_stats_set_population(&stats, true);
 
 	for (m = 0; m < ARRAY_SIZE(modes); m++) {