From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55976) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agZlQ-0004zS-St for qemu-devel@nongnu.org; Thu, 17 Mar 2016 11:23:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1agZlP-0000Lx-Rg for qemu-devel@nongnu.org; Thu, 17 Mar 2016 11:23:04 -0400 Received: from mail-ig0-x243.google.com ([2607:f8b0:4001:c05::243]:33580) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agZlP-0000Lt-Lk for qemu-devel@nongnu.org; Thu, 17 Mar 2016 11:23:03 -0400 Received: by mail-ig0-x243.google.com with SMTP id nt3so8121683igb.0 for ; Thu, 17 Mar 2016 08:23:03 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20160317145338.GO14062@stefanha-x1.localdomain> References: <1458066313-12203-1-git-send-email-dhannawatpooja1@gmail.com> <20160317145338.GO14062@stefanha-x1.localdomain> Date: Thu, 17 Mar 2016 20:53:02 +0530 Message-ID: From: Pooja Dhannawat Content-Type: multipart/alternative; boundary=047d7b2e15cbd80faa052e403425 Subject: Re: [Qemu-devel] [PATCH v3] net: Allocating Large sized arrays to heap List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: qemu-devel@nongnu.org --047d7b2e15cbd80faa052e403425 Content-Type: text/plain; charset=UTF-8 On Thu, Mar 17, 2016 at 8:23 PM, Stefan Hajnoczi wrote: > On Tue, Mar 15, 2016 at 11:55:13PM +0530, Pooja Dhannawat wrote: > > nc_sendv_compat has a huge stack usage of 69680 bytes approx. > > Moving large arrays to heap to reduce stack usage. > > > > Signed-off-by: Pooja Dhannawat > > --- > > net/net.c | 14 ++++++++++---- > > 1 file changed, 10 insertions(+), 4 deletions(-) > > > > diff --git a/net/net.c b/net/net.c > > index b0c832e..f03c571 100644 > > --- a/net/net.c > > +++ b/net/net.c > > @@ -709,23 +709,29 @@ ssize_t qemu_send_packet_raw(NetClientState *nc, > const uint8_t *buf, int size) > > static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec > *iov, > > int iovcnt, unsigned flags) > > { > > - uint8_t buf[NET_BUFSIZE]; > > + uint8_t *buf; > > uint8_t *buffer; > > size_t offset; > > + ssize_t ret; > > + > > + buf = g_new(uint8_t, NET_BUFSIZE); > > The linear buffer is only needed when iovcnt > 1. I suggest the > following instead: > > uint8_t *buf = NULL; > > if (iovcnt == 1) { > buffer = iov[0].iov_base; > offset = iov[0].iov_len; > } else { > buf = g_new(uint8_t, NET_BUFSIZE); > buffer = buf; > offset = iov_to_buf(iov, iovcnt, 0, buf, NET_BUFSIZE); > } > > This way the allocation is only made when we actually need to linearize > the buffer. > Thanks Stefan for pointing out this one :) I will make desired changes and push patch. --047d7b2e15cbd80faa052e403425 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On Thu, Mar 17, 2016 at 8:23 PM, Stefan Hajnoczi <= stefanha@gmail.com<= /a>> wrote:
On= Tue, Mar 15, 2016 at 11:55:13PM +0530, Pooja Dhannawat wrote:
> nc_sendv_compat has a huge stack usage of 69680 bytes approx.
> Moving large arrays to heap to reduce stack usage.
>
> Signed-off-by: Pooja Dhannawat <
dhannawatpooja1@gmail.com>
> ---
>=C2=A0 net/net.c | 14 ++++++++++----
>=C2=A0 1 file changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/net/net.c b/net/net.c
> index b0c832e..f03c571 100644
> --- a/net/net.c
> +++ b/net/net.c
> @@ -709,23 +709,29 @@ ssize_t qemu_send_packet_raw(NetClientState *nc,= const uint8_t *buf, int size)
>=C2=A0 static ssize_t nc_sendv_compat(NetClientState *nc, const struct = iovec *iov,
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0int iovcnt, unsigned flags)=
>=C2=A0 {
> -=C2=A0 =C2=A0 uint8_t buf[NET_BUFSIZE];
> +=C2=A0 =C2=A0 uint8_t *buf;
>=C2=A0 =C2=A0 =C2=A0 uint8_t *buffer;
>=C2=A0 =C2=A0 =C2=A0 size_t offset;
> +=C2=A0 =C2=A0 ssize_t ret;
> +
> +=C2=A0 =C2=A0 buf =3D g_new(uint8_t, NET_BUFSIZE);

The linear buffer is only needed when iovcnt > 1.=C2=A0 I suggest= the
following instead:

uint8_t *buf =3D NULL;

if (iovcnt =3D=3D 1) {
=C2=A0 =C2=A0 buffer =3D iov[0].iov_base;
=C2=A0 =C2=A0 offset =3D iov[0].iov_len;
} else {
=C2=A0 =C2=A0 buf =3D g_new(uint8_t, NET_BUFSIZE);
=C2=A0 =C2=A0 buffer =3D buf;
=C2=A0 =C2=A0 offset =3D iov_to_buf(iov, iovcnt, 0, buf, N= ET_BUFSIZE);
}

This way the allocation is only made when we actually need to linear= ize
the buffer.

Thanks Stefan for pointing = out this one :)=C2=A0 I will make desired changes and push patch.

--047d7b2e15cbd80faa052e403425--