stream/syslog: use internal buffer to decoupling syslog with iob

Signed-off-by: chao an <anchao@xiaomi.com>
This commit is contained in:
chao an
2023-05-23 20:14:58 +08:00
committed by Xiang Xiao
parent 1bc4b8d7b2
commit 0455167457
3 changed files with 61 additions and 27 deletions
+10 -1
View File
@@ -72,7 +72,6 @@ endif
config SYSLOG_BUFFER config SYSLOG_BUFFER
bool "Use buffered output" bool "Use buffered output"
default n default n
select MM_IOB
---help--- ---help---
Enables an buffering logic that will be used to serialize debug Enables an buffering logic that will be used to serialize debug
output from concurrent tasks. This enables allocation of one buffer output from concurrent tasks. This enables allocation of one buffer
@@ -82,6 +81,16 @@ config SYSLOG_BUFFER
then the output from multiple tasks that attempt to generate SYSLOG then the output from multiple tasks that attempt to generate SYSLOG
output may be interleaved and difficult to read. output may be interleaved and difficult to read.
if SYSLOG_BUFFER
config SYSLOG_BUFSIZE
int "Syslog buffer size"
default 64
---help---
The size of the syslog buffer in bytes.
endif
config SYSLOG_INTBUFFER config SYSLOG_INTBUFFER
bool "Use interrupt buffer" bool "Use interrupt buffer"
default n default n
+7
View File
@@ -236,8 +236,15 @@ struct lib_syslogstream_s
{ {
struct lib_outstream_s public; struct lib_outstream_s public;
#ifdef CONFIG_SYSLOG_BUFFER #ifdef CONFIG_SYSLOG_BUFFER
# ifdef CONFIG_MM_IOB
FAR struct iob_s *iob; FAR struct iob_s *iob;
# else
char buffer[CONFIG_SYSLOG_BUFSIZE];
# endif
#endif #endif
FAR char *base;
int size;
int offset;
int last_ch; int last_ch;
}; };
+44 -26
View File
@@ -34,42 +34,40 @@
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_SYSLOG_BUFFER
/**************************************************************************** /****************************************************************************
* Name: syslogstream_flush * Name: syslogstream_flush
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_SYSLOG_BUFFER static int syslogstream_flush(FAR struct lib_outstream_s *ostream)
static int syslogstream_flush(FAR struct lib_syslogstream_s *stream)
{ {
FAR struct iob_s *iob; FAR struct lib_syslogstream_s *stream = (FAR void *)ostream;
int ret = OK; int ret = OK;
DEBUGASSERT(stream != NULL); DEBUGASSERT(stream != NULL);
iob = stream->iob;
/* Do we have an IO buffer? Is there anything buffered? */ /* Do we have an IO buffer? Is there anything buffered? */
if (iob != NULL && iob->io_len > 0) if (stream->base != NULL && stream->offset > 0)
{ {
/* Yes write the buffered data */ /* Yes write the buffered data */
do do
{ {
ssize_t nbytes = syslog_write((FAR const char *)iob->io_data, ssize_t nbytes = syslog_write(stream->base, stream->offset);
iob->io_len);
if (nbytes < 0) if (nbytes < 0)
{ {
ret = nbytes; ret = nbytes;
} }
else else
{ {
iob->io_len = 0;
ret = OK; ret = OK;
} }
} }
while (ret == -EINTR); while (ret == -EINTR);
} }
stream->offset = 0;
return ret; return ret;
} }
#endif #endif
@@ -82,12 +80,10 @@ static int syslogstream_flush(FAR struct lib_syslogstream_s *stream)
static void syslogstream_addchar(FAR struct lib_syslogstream_s *stream, static void syslogstream_addchar(FAR struct lib_syslogstream_s *stream,
int ch) int ch)
{ {
FAR struct iob_s *iob = stream->iob;
/* Add the incoming character to the buffer */ /* Add the incoming character to the buffer */
iob->io_data[iob->io_len] = ch; stream->base[stream->offset] = ch;
iob->io_len++; stream->offset++;
/* Increment the total number of bytes buffered. */ /* Increment the total number of bytes buffered. */
@@ -95,35 +91,38 @@ static void syslogstream_addchar(FAR struct lib_syslogstream_s *stream,
/* Is the buffer full? */ /* Is the buffer full? */
if (iob->io_len >= CONFIG_IOB_BUFSIZE) if (stream->offset >= stream->size)
{ {
/* Yes.. then flush the buffer */ /* Yes.. then flush the buffer */
syslogstream_flush(stream); syslogstream_flush(&stream->public);
} }
} }
/****************************************************************************
* Name: syslogstream_addstring
****************************************************************************/
static int syslogstream_addstring(FAR struct lib_syslogstream_s *stream, static int syslogstream_addstring(FAR struct lib_syslogstream_s *stream,
FAR const char *buff, int len) FAR const char *buff, int len)
{ {
FAR struct iob_s *iob = stream->iob;
int ret = 0; int ret = 0;
do do
{ {
int remain = CONFIG_IOB_BUFSIZE - iob->io_len; int remain = stream->size - stream->offset;
remain = remain > len - ret ? len - ret : remain; remain = remain > len - ret ? len - ret : remain;
memcpy(iob->io_data + iob->io_len, buff + ret, remain); memcpy(stream->base + stream->offset, buff + ret, remain);
iob->io_len += remain; stream->offset += remain;
ret += remain; ret += remain;
/* Is the buffer enough? */ /* Is the buffer enough? */
if (iob->io_len >= CONFIG_IOB_BUFSIZE) if (stream->offset >= stream->size)
{ {
/* Yes.. then flush the buffer */ /* Yes.. then flush the buffer */
syslogstream_flush(stream); syslogstream_flush(&stream->public);
} }
} }
while (ret < len); while (ret < len);
@@ -151,17 +150,17 @@ static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch)
if (ch != '\r') if (ch != '\r')
{ {
#ifdef CONFIG_SYSLOG_BUFFER # ifdef CONFIG_SYSLOG_BUFFER
/* Do we have an IO buffer? */ /* Do we have an IO buffer? */
if (stream->iob != NULL) if (stream->base != NULL)
{ {
/* Add the incoming character to the buffer */ /* Add the incoming character to the buffer */
syslogstream_addchar(stream, ch); syslogstream_addchar(stream, ch);
} }
else else
#endif # endif
{ {
int ret; int ret;
@@ -208,9 +207,10 @@ static int syslogstream_puts(FAR struct lib_outstream_s *this,
stream->last_ch = ((FAR const char *)buff)[len - 1]; stream->last_ch = ((FAR const char *)buff)[len - 1];
#ifdef CONFIG_SYSLOG_BUFFER #ifdef CONFIG_SYSLOG_BUFFER
/* Do we have an IO buffer? */ /* Do we have an IO buffer? */
if (stream->iob != NULL) if (stream->base != NULL)
{ {
/* Add the incoming string to the buffer */ /* Add the incoming string to the buffer */
@@ -275,14 +275,28 @@ void lib_syslogstream_open(FAR struct lib_syslogstream_s *stream)
stream->public.putc = syslogstream_putc; stream->public.putc = syslogstream_putc;
stream->public.puts = syslogstream_puts; stream->public.puts = syslogstream_puts;
stream->public.flush = lib_noflush;
stream->public.nput = 0; stream->public.nput = 0;
#ifdef CONFIG_SYSLOG_BUFFER #ifdef CONFIG_SYSLOG_BUFFER
stream->public.flush = syslogstream_flush;
/* Allocate an IOB */ /* Allocate an IOB */
# ifdef CONFIG_MM_IOB
stream->iob = iob_tryalloc(true); stream->iob = iob_tryalloc(true);
if (stream->iob != NULL)
{
stream->base = (FAR void *)stream->iob->io_data;
stream->size = sizeof(stream->iob->io_data);
}
# else
stream->base = stream->buffer;
stream->size = sizeof(stream->buffer);
# endif
#else
stream->public.flush = lib_noflush;
#endif #endif
stream->offset = 0;
} }
/**************************************************************************** /****************************************************************************
@@ -307,16 +321,20 @@ void lib_syslogstream_close(FAR struct lib_syslogstream_s *stream)
/* Verify that there is an IOB attached (there should be) */ /* Verify that there is an IOB attached (there should be) */
# ifdef CONFIG_MM_IOB
if (stream->iob != NULL) if (stream->iob != NULL)
{ {
/* Flush the output buffered in the IOB */ /* Flush the output buffered in the IOB */
syslogstream_flush(stream); syslogstream_flush(&stream->public);
/* Free the IOB */ /* Free the IOB */
iob_free(stream->iob); iob_free(stream->iob);
stream->iob = NULL; stream->iob = NULL;
} }
# else
syslogstream_flush(&stream->public);
# endif
} }
#endif #endif