From patchwork Sat Jun 27 17:07:47 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Lespiau, Damien" X-Patchwork-Id: 6685351 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 07670C05AC for ; Sat, 27 Jun 2015 17:08:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 186C4204E0 for ; Sat, 27 Jun 2015 17:08:00 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 2470D20145 for ; Sat, 27 Jun 2015 17:07:59 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 677246E755; Sat, 27 Jun 2015 10:07:58 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by gabe.freedesktop.org (Postfix) with ESMTP id 38A666E74D for ; Sat, 27 Jun 2015 10:07:55 -0700 (PDT) Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga103.fm.intel.com with ESMTP; 27 Jun 2015 10:07:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.13,690,1427785200"; d="scan'208";a="595849321" Received: from kczmudzi-mobl4.amr.corp.intel.com (HELO strange.ger.corp.intel.com) ([10.252.53.243]) by orsmga003.jf.intel.com with ESMTP; 27 Jun 2015 10:07:53 -0700 From: Damien Lespiau To: intel-gfx@lists.freedesktop.org Date: Sat, 27 Jun 2015 18:07:47 +0100 Message-Id: <1435424869-17455-2-git-send-email-damien.lespiau@intel.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1435424869-17455-1-git-send-email-damien.lespiau@intel.com> References: <1435424869-17455-1-git-send-email-damien.lespiau@intel.com> Subject: [Intel-gfx] [PATCH i-g-t 1/3] stats: Allow the underlying arrays to grow at will X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Chris mentioned he wanted to be able to measure a variable "for one second" and use igt_stats to store them. That's one case where we don't know the number of data points upfront. We should really support that, so here it is. Signed-off-by: Damien Lespiau --- lib/igt_stats.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/igt_stats.c b/lib/igt_stats.c index caf3f37..cd97ab0 100644 --- a/lib/igt_stats.c +++ b/lib/igt_stats.c @@ -63,6 +63,41 @@ * ]| */ +static unsigned int get_new_capacity(int need) +{ + unsigned int new_capacity; + + /* taken from Python's list */ + new_capacity = (need >> 6) + (need < 9 ? 3 : 6); + new_capacity += need; + + return new_capacity; +} + +static void igt_stats_ensure_capacity(igt_stats_t *stats, + unsigned int n_additional_values) +{ + unsigned int new_n_values = stats->n_values + n_additional_values; + unsigned int new_capacity; + + if (new_n_values <= stats->capacity) + return; + + new_capacity = get_new_capacity(new_n_values); + stats->values = realloc(stats->values, + sizeof(*stats->values) * new_capacity); + igt_assert(stats->values); + + stats->capacity = new_capacity; + + if (!stats->sorted) + return; + + stats->sorted = realloc(stats->sorted, + sizeof(*stats->values) * new_capacity); + igt_assert(stats->sorted); +} + /** * igt_stats_init: * @stats: An #igt_stats_t instance @@ -78,9 +113,7 @@ void igt_stats_init(igt_stats_t *stats, unsigned int capacity) { memset(stats, 0, sizeof(*stats)); - stats->values = calloc(capacity, sizeof(*stats->values)); - igt_assert(stats->values); - stats->capacity = capacity; + igt_stats_ensure_capacity(stats, capacity); stats->min = U64_MAX; stats->max = 0; @@ -156,7 +189,8 @@ void igt_stats_set_population(igt_stats_t *stats, bool full_population) */ void igt_stats_push(igt_stats_t *stats, uint64_t value) { - igt_assert(stats->n_values < stats->capacity); + igt_stats_ensure_capacity(stats, 1); + stats->values[stats->n_values++] = value; stats->mean_variance_valid = false; @@ -181,6 +215,8 @@ void igt_stats_push_array(igt_stats_t *stats, { unsigned int i; + igt_stats_ensure_capacity(stats, n_values); + for (i = 0; i < n_values; i++) igt_stats_push(stats, values[i]); } @@ -240,6 +276,14 @@ static void igt_stats_ensure_sorted_values(igt_stats_t *stats) return; if (!stats->sorted) { + /* + * We could always allocate sorted, but doing it lazily will + * consume a bit less memory if the user doesn't use any of the + * functions needing to sort the data points. + * + * igt_stats_ensure_capacity() will take care of reallocating + * sorted when needed after this initial allocation. + */ stats->sorted = calloc(stats->capacity, sizeof(*stats->values)); igt_assert(stats->sorted); }