From 8713638687de24d833d0787ba75520a608090008 Mon Sep 17 00:00:00 2001 From: yuanyongjian Date: Sun, 4 Feb 2024 21:59:20 +0800 Subject: [PATCH] syslog/syslog_rpmsg.c: improve syslog_rpmsg performance use copy buffer instead of single assignment Signed-off-by: yuanyongjian --- drivers/syslog/syslog_rpmsg.c | 76 ++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/drivers/syslog/syslog_rpmsg.c b/drivers/syslog/syslog_rpmsg.c index 6cbce4a010..10852ec15b 100644 --- a/drivers/syslog/syslog_rpmsg.c +++ b/drivers/syslog/syslog_rpmsg.c @@ -80,8 +80,8 @@ struct syslog_rpmsg_s ****************************************************************************/ static void syslog_rpmsg_work(FAR void *priv_); -static void syslog_rpmsg_putchar(FAR struct syslog_rpmsg_s *priv, int ch, - bool last); +static void syslog_rpmsg_addbuf(FAR struct syslog_rpmsg_s *priv, + FAR const char *buffer, size_t len); static void syslog_rpmsg_device_created(FAR struct rpmsg_device *rdev, FAR void *priv_); static void syslog_rpmsg_device_destroy(FAR struct rpmsg_device *rdev, @@ -189,10 +189,19 @@ static void syslog_rpmsg_work(FAR void *priv_) } } -static void syslog_rpmsg_putchar(FAR struct syslog_rpmsg_s *priv, int ch, - bool last) +static void syslog_rpmsg_addbuf(FAR struct syslog_rpmsg_s *priv, + FAR const char *buffer, size_t len) { - if (priv->head + 1 - priv->tail >= priv->size) + bool overwritten = false; + size_t offset; + size_t tail; + + if (len <= 0) + { + return; + } + + if (priv->head + len - priv->tail >= priv->size) { bool ret = false; @@ -203,26 +212,42 @@ static void syslog_rpmsg_putchar(FAR struct syslog_rpmsg_s *priv, int ch, if (!ret) { - /* Overwrite */ - - priv->buffer[SYSLOG_RPMSG_TAILOFF(priv)] = 0; - priv->tail++; + overwritten = true; } } - priv->buffer[SYSLOG_RPMSG_HEADOFF(priv)] = ch & 0xff; - priv->head++; + offset = SYSLOG_RPMSG_HEADOFF(priv); + tail = priv->size - offset; + + if (len > tail) + { + memcpy(&priv->buffer[offset], buffer, tail); + memcpy(priv->buffer, buffer + tail, len - tail); + } + else + { + memcpy(&priv->buffer[offset], buffer, len); + } + + priv->head += len; + if (overwritten) + { + priv->tail = priv->head - priv->size; + priv->buffer[priv->tail] = 0; + priv->tail++; + } if (priv->flush) { #if defined(CONFIG_ARCH_LOWPUTC) - up_putc(ch); + up_nputs(buffer, len); #endif - priv->flush++; + + priv->flush += len; return; } - if (last && !priv->suspend && is_rpmsg_ept_ready(&priv->ept)) + if (!priv->suspend && is_rpmsg_ept_ready(&priv->ept)) { clock_t delay = SYSLOG_RPMSG_WORK_DELAY; size_t space = SYSLOG_RPMSG_SPACE(priv); @@ -340,11 +365,12 @@ static ssize_t syslog_rpmsg_file_write(FAR struct file *filep, int syslog_rpmsg_putc(FAR syslog_channel_t *channel, int ch) { - FAR struct syslog_rpmsg_s *priv = &g_syslog_rpmsg; irqstate_t flags; + char tmp = ch; + UNUSED(channel); flags = enter_critical_section(); - syslog_rpmsg_putchar(priv, ch, true); + syslog_rpmsg_addbuf(&g_syslog_rpmsg, &tmp, 1); leave_critical_section(flags); return ch; @@ -363,12 +389,13 @@ int syslog_rpmsg_flush(FAR syslog_channel_t *channel) priv->flush = priv->tail; } - while (priv->flush < priv->head) + if (priv->flush < priv->head) { + size_t len = priv->head - priv->flush; #if defined(CONFIG_ARCH_LOWPUTC) - up_putc(priv->buffer[SYSLOG_RPMSG_FLUSHOFF(priv)]); + up_nputs(&priv->buffer[SYSLOG_RPMSG_FLUSHOFF(priv)], len); #endif - priv->flush++; + priv->flush += len; } leave_critical_section(flags); @@ -380,15 +407,8 @@ ssize_t syslog_rpmsg_write(FAR syslog_channel_t *channel, FAR const char *buffer, size_t buflen) { FAR struct syslog_rpmsg_s *priv = &g_syslog_rpmsg; - irqstate_t flags; - size_t nwritten; - - flags = enter_critical_section(); - for (nwritten = 1; nwritten <= buflen; nwritten++) - { - syslog_rpmsg_putchar(priv, *buffer++, nwritten == buflen); - } - + irqstate_t flags = enter_critical_section(); + syslog_rpmsg_addbuf(priv, buffer, buflen); leave_critical_section(flags); return buflen;