From patchwork Mon Apr 12 21:11:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 42956 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C7C643982071; Mon, 12 Apr 2021 21:11:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7C643982071 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618261888; bh=SiKv0rwct10uA7A0aHblOgYcigPZjTc/4xTxpuJD9PU=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Px9CHdACv8dT4+/sOUL47xtEpbPtdZ330/VCxAKK58vHyAuj6a7Bj+pT2PJQWA03H WobBMudlgjmtusQ7Bj6Yxhc5zuLLmM+nU0rzd0x+39X220Oohww35esPWz57lXI3uL hfVcRs3DQ2e0uDlYjuDJSgv1XR13BxRox4ge4CF0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by sourceware.org (Postfix) with ESMTPS id 7C73C398206C for ; Mon, 12 Apr 2021 21:11:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7C73C398206C Received: by mail-qv1-xf35.google.com with SMTP id iu14so7062417qvb.4 for ; Mon, 12 Apr 2021 14:11:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SiKv0rwct10uA7A0aHblOgYcigPZjTc/4xTxpuJD9PU=; b=IHrzD308yVSaSwv8GjJT0nYggDKWcalGZSoU2Miu7mHjXSmNC4yTaJhVKSTHUDIrHe gLZegD2c2NK+hbYROW3NT64k1bLrjUz9Hpba+x4yFULBGr9e7FbkvQ32byLSLyvv0DgM Ish668lfQACos3tH5jx8oNdDoF00mhxPZuWd68nPQCW9nGHDZMXMvLmMnQHgSwO3f/sS iAcEQmxQ2LR04JvGfeNmRycQxmJXlpYPFP8Cuf4NX5RO8BpNyXGkHNDeu22+K3oDa+Pt Ve9u3ZPHKKzUbKDaCmP7bzhcS70xJpy57ygD6eeX9CtOsKEQv/KyYT35HxPaql2z0j9H 2Qlw== X-Gm-Message-State: AOAM533LXoyCF/fkfC8+HkKTaG+xZZdBdyckD/P2pVRN6XLygx92THWM 9pOYilju02WZ11gT9wPFy05oeMbYgk4FnHRB X-Google-Smtp-Source: ABdhPJy9HlwZUC9Lv/LzisPfhFb278nI53opnTmlHHEJmjWXktcEM2Yc1FzTc11rBuQK3422C0DucA== X-Received: by 2002:a05:6214:8c4:: with SMTP id da4mr30259780qvb.57.1618261884845; Mon, 12 Apr 2021 14:11:24 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id i22sm3073392qko.135.2021.04.12.14.11.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:11:24 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 07/11] misc: syslog: Use static buffer Date: Mon, 12 Apr 2021 18:11:09 -0300 Message-Id: <20210412211113.393120-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210412211113.393120-1-adhemerval.zanella@linaro.org> References: <20210412211113.393120-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The static buffer is used instead of memstream for messages up to 1024 bytes to avoid the potential BUFSIZ (8K) malloc and free for each syslog call. The memstream is still used as fallback for larger messages. Checked on x86_64-linux-gnu. --- misc/syslog.c | 102 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 34 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index cb608766cb..a310eba476 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -115,12 +115,11 @@ void __vsyslog_internal (int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - FILE *f; - char *buf = 0; + char *buf = NULL; size_t bufsize = 0; + bool buf_to_free = false; int msgoff; int saved_errno = errno; - char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID /* Check for invalid bits. */ @@ -144,42 +143,77 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, if ((pri & LOG_FACMASK) == 0) pri |= LogFacility; - /* Build the message in a memory-buffer stream. */ - f = __open_memstream (&buf, &bufsize); - if (f != NULL) + pid_t pid = LogStat & LOG_PID ? __getpid () : 0; + + enum + { + timebuf_size = 3+1 /* "%h " */ + + 2+1 /* "%e " */ + + 2+1 + 2+1 + 2+1 + 1, /* "%T " */ + + bufs_size = 1024 + }; + + /* "%h %e %H:%M:%S " */ + char timestamp[timebuf_size]; + time_t now = time_now (); + struct tm now_tm; + __localtime_r (&now, &now_tm); + __strftime_l (timestamp, sizeof (timestamp), "%h %e %T ", &now_tm, + _nl_C_locobj_ptr); + +#define SYSLOG_HEADER(__pri, __timestamp, __msgoff, pid) \ + "<%d>%s %n%s%s%.0d%s: ", \ + __pri, __timestamp, __msgoff, \ + LogTag == NULL ? __progname : LogTag, \ + pid != 0 ? "[" : "", pid, pid != 0 ? "]" : "" + + /* Try to use a static buffer as an optimization. */ + char bufs[bufs_size]; + int l = __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + if (l < sizeof (bufs)) { - __fsetlocking (f, FSETLOCKING_BYCALLER); - - /* "%h %e %H:%M:%S " */ - char timebuf[3+1 /* "%h " */ - + 2+1 /* "%e " */ - + 2+1 + 2+1 + 2+1 + 1 /* "%T " */]; - time_t now = time_now (); - struct tm now_tm; - __localtime_r (&now, &now_tm); - __strftime_l (timebuf, sizeof (timebuf), "%h %e %T ", &now_tm, - _nl_C_locobj_ptr); - - pid_t pid = LogStat & LOG_PID ? __getpid () : 0; - - fprintf (f, "<%d>%s %n%s%s%.0d%s: ", pri, timebuf, &msgoff, - LogTag == NULL ? __progname : LogTag, - pid != 0 ? "[" : "", pid, pid != 0 ? "]" : ""); + va_list apc; + va_copy (apc, ap); + /* Restore errno for %m format. */ __set_errno (saved_errno); - __vfprintf_internal (f, fmt, ap, mode_flags); - fclose (f); + int vl = __vsnprintf_internal (bufs + l, sizeof (bufs) - l, fmt, apc, + mode_flags); + if (l + vl < sizeof (bufs)) + { + buf = bufs; + bufsize = l + vl; + } - /* Tell the cancellation handler to free this buffer. */ - clarg.buf = buf; + va_end (apc); } - else + + /* If the required size is larger than buffer size fallbacks to + open_memstream. */ + if (buf == NULL) { - /* We cannot get a stream. There is not much we can do but emitting an - error messages. */ - bufsize = __snprintf (failbuf, sizeof failbuf, "out of memory[%d]", - __getpid ()); - buf = failbuf; + FILE *f = __open_memstream (&buf, &bufsize); + if (f != NULL) + { + __fsetlocking (f, FSETLOCKING_BYCALLER); + fprintf (f, SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + /* Restore errno for %m format. */ + __set_errno (saved_errno); + __vfprintf_internal (f, fmt, ap, mode_flags); + fclose (f); + + /* Tell the cancellation handler to free this buffer. */ + buf_to_free = true; + clarg.buf = buf; + } + else + { + bufsize = __snprintf (bufs, sizeof bufs, + "out of memory[%d]", __getpid ()); + buf = bufs; + } } /* Output to stderr if requested. */ @@ -229,7 +263,7 @@ out: __libc_cleanup_pop (0); __libc_lock_unlock (syslog_lock); - if (buf != failbuf) + if (buf_to_free) free (buf); }