[Intel-gfx] [PATCH i-g-t 1/3] stats: Allow the underlying arrays to grow at will

Chris Wilson chris at chris-wilson.co.uk
Sat Jun 27 10:15:42 PDT 2015


On Sat, Jun 27, 2015 at 06:07:47PM +0100, Damien Lespiau wrote:
> 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 <damien.lespiau at intel.com>
> ---
>  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);

There isn't any point in preserving the sorted array when pushing new
values. Might as well just free here and allocate again when required.
-Chris

-- 
Chris Wilson, Intel Open Source Technology Centre


More information about the Intel-gfx mailing list