[PATCH] Avoid some memory waste while allocating tcbtab.

Dmitry V. Levin ldv at altlinux.org
Tue Feb 23 17:26:42 UTC 2016


On Tue, Feb 23, 2016 at 05:21:02PM +0100, Nahim El Atmani wrote:
> * strace.c: add create_tcbtab() that allocate the minimum required amount of
> memory to create the tcbtab. Add a tcbtab existance check in expand_tcbtab()
> that will be called by alloctcb() when no tcb are available (first call and
> subsequent calls when tcbtab is full). Remove the always done at first tcbtab
> allocation in init(), now replaced by create_tcbtab() and called when needed.
> 
> Signed-off-by: Nahim El Atmani <nahim+dev at naam.me>
> Reviewed-By: Gabriel Laskar <gabriel at lse.epita.fr>
> Reported-by: haris iqbal <haris.phnx at gmail.com>
> ---
>  strace.c | 35 ++++++++++++++++++++++-------------
>  1 file changed, 22 insertions(+), 13 deletions(-)
> 
> diff --git a/strace.c b/strace.c
> index 785cc60..d18aa7e 100644
> --- a/strace.c
> +++ b/strace.c
> @@ -689,6 +689,18 @@ newoutf(struct tcb *tcp)
>  }
>  
>  static void
> +create_tcbtab(void)
> +{
> +	struct tcb *tcp;
> +
> +	/* Allocate the initial tcbtab.  */
> +	tcbtabsize = 1; /* We don't need much more at first */
> +	tcbtab = xcalloc(tcbtabsize, sizeof(tcbtab[0]));
> +	tcp = xcalloc(tcbtabsize, sizeof(*tcp));
> +	tcbtab[0] = tcp;
> +}
> +
> +static void
>  expand_tcbtab(void)
>  {
>  	/* Allocate some more TCBs and expand the table.
> @@ -696,10 +708,16 @@ expand_tcbtab(void)
>  	   callers have pointers and it would be a pain.
>  	   So tcbtab is a table of pointers.  Since we never
>  	   free the TCBs, we allocate a single chunk of many.  */
> -	unsigned int i = tcbtabsize;
> -	struct tcb *newtcbs = xcalloc(tcbtabsize, sizeof(newtcbs[0]));
> -	struct tcb **newtab = xreallocarray(tcbtab, tcbtabsize * 2,
> -					    sizeof(tcbtab[0]));
> +	unsigned int i;
> +	struct tcb *newtcbs;
> +	struct tcb **newtab;
> +
> +	if (!tcbtabsize)
> +		create_tcbtab();
> +
> +	i = tcbtabsize;
> +	newtcbs = xcalloc(tcbtabsize, sizeof(newtcbs[0]));
> +	newtab = xreallocarray(tcbtab, tcbtabsize * 2, sizeof(tcbtab[0]));

This means that the first expand_tcbtab() would end up with two calloc
calls (via create_tcbtab) immediately followed by calloc and realloc.
I think we could do better than this.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20160223/b54b99bc/attachment.bin>


More information about the Strace-devel mailing list