All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mathieu Desnoyers via lttng-dev <lttng-dev@lists.lttng.org>
To: Norbert Lange <nolange79@gmail.com>
Cc: lttng-dev <lttng-dev@lists.lttng.org>
Subject: Re: [lttng-dev] [PATCH] Avoid using the heap for small strings with tracef/tracelog
Date: Wed, 19 May 2021 15:00:41 -0400 (EDT)	[thread overview]
Message-ID: <1498635684.51674.1621450841774.JavaMail.zimbra@efficios.com> (raw)
In-Reply-To: <20210519165648.792939-1-nolange79@gmail.com>

----- On May 19, 2021, at 12:56 PM, lttng-dev lttng-dev@lists.lttng.org wrote:

> Try to use vsnprintf with a 512 Byte buffer on the Stack,
> if that fails allocate a larger one.

I agree with the approach.

Can we move the hardcoded "512" to a #define within src/common/tracer.h ?

Thanks,

Mathieu

> 
> Signed-off-by: Norbert Lange <nolange79@gmail.com>
> ---
> liblttng-ust/tracef.c   | 13 ++++++++++---
> liblttng-ust/tracelog.c | 12 +++++++++---
> 2 files changed, 19 insertions(+), 6 deletions(-)
> 
> diff --git a/liblttng-ust/tracef.c b/liblttng-ust/tracef.c
> index ea98e43e..18c29e25 100644
> --- a/liblttng-ust/tracef.c
> +++ b/liblttng-ust/tracef.c
> @@ -32,17 +32,24 @@
> void _lttng_ust_tracef(const char *fmt, ...)
> {
> 	va_list ap;
> -	char *msg;
> +	char local_buf[512];
> +	char *msg = local_buf;
> 	int len;
> 
> 	va_start(ap, fmt);
> -	len = vasprintf(&msg, fmt, ap);
> +	len = vsnprintf(local_buf, sizeof(local_buf), fmt, ap);
> +	if (len >= sizeof(local_buf)) {
> +		msg = (char *)malloc(len + 1);
> +		len = msg ? vsnprintf(msg, len + 1, fmt, ap) : -1;
> +	}
> +
> 	/* len does not include the final \0 */
> 	if (len < 0)
> 		goto end;
> 	__tracepoint_cb_lttng_ust_tracef___event(msg, len,
> 		LTTNG_UST_CALLER_IP());
> -	free(msg);
> end:
> +	if (msg != local_buf)
> +		free(msg);
> 	va_end(ap);
> }
> diff --git a/liblttng-ust/tracelog.c b/liblttng-ust/tracelog.c
> index 65fc87ed..a5d110fa 100644
> --- a/liblttng-ust/tracelog.c
> +++ b/liblttng-ust/tracelog.c
> @@ -35,19 +35,25 @@
> 			const char *fmt, ...) \
> 	{ \
> 		va_list ap; \
> -		char *msg; \
> +		char local_buf[512]; \
> +		char *msg = local_buf; \
> 		int len; \
> 		\
> 		va_start(ap, fmt); \
> -		len = vasprintf(&msg, fmt, ap); \
> +		len = vsnprintf(local_buf, sizeof(local_buf), fmt, ap); \
> +		if (len >= sizeof(local_buf)) { \
> +			msg = (char *)malloc(len + 1); \
> +			len = msg ? vsnprintf(msg, len + 1, fmt, ap) : -1; \
> +		} \
> 		/* len does not include the final \0 */ \
> 		if (len < 0) \
> 			goto end; \
> 		__tracepoint_cb_lttng_ust_tracelog___##level(file, \
> 			line, func, msg, len, \
> 			LTTNG_UST_CALLER_IP()); \
> -		free(msg); \
> 	end: \
> +		if (msg != local_buf) \
> +			free(msg); \
> 		va_end(ap); \
> 	}
> 
> --
> 2.30.2
> 
> _______________________________________________
> lttng-dev mailing list
> lttng-dev@lists.lttng.org
> https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

-- 
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
lttng-dev@lists.lttng.org
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

  reply	other threads:[~2021-05-19 19:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-19 16:56 [lttng-dev] [PATCH] Avoid using the heap for small strings with tracef/tracelog Norbert Lange via lttng-dev
2021-05-19 19:00 ` Mathieu Desnoyers via lttng-dev [this message]
2021-05-20 12:20   ` Norbert Lange via lttng-dev
2021-05-20 13:33     ` Mathieu Desnoyers via lttng-dev
2021-05-20 13:44       ` Norbert Lange via lttng-dev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1498635684.51674.1621450841774.JavaMail.zimbra@efficios.com \
    --to=lttng-dev@lists.lttng.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=nolange79@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.