[PATCH v5 8/8] lib: sbi: Speed-up sbi_printf() and friends using nputs()

Anup Patel apatel at ventanamicro.com
Fri Jan 13 03:41:10 PST 2023


The sbi_printf() is slow for semihosting because it prints one
character at a time. To speed-up sbi_printf() for semihosting,
we use a temporary buffer and nputs().

Signed-off-by: Anup Patel <apatel at ventanamicro.com>
---
 lib/sbi/sbi_console.c | 52 ++++++++++++++++++++++++++++---------------
 1 file changed, 34 insertions(+), 18 deletions(-)

diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
index c1b9f73..41881d7 100644
--- a/lib/sbi/sbi_console.c
+++ b/lib/sbi/sbi_console.c
@@ -42,31 +42,29 @@ void sbi_putc(char ch)
 	}
 }
 
-void sbi_puts(const char *str)
+static void nputs(const char *str, unsigned long len)
 {
-	spin_lock(&console_out_lock);
+	unsigned long i;
+
 	if (console_dev && console_dev->console_puts) {
-		console_dev->console_puts(str, sbi_strlen(str));
+		console_dev->console_puts(str, len);
 	} else {
-		while (*str) {
-			sbi_putc(*str);
-			str++;
-		}
+		for (i = 0; i < len; i++)
+			sbi_putc(str[i]);
 	}
+}
+
+void sbi_puts(const char *str)
+{
+	spin_lock(&console_out_lock);
+	nputs(str, sbi_strlen(str));
 	spin_unlock(&console_out_lock);
 }
 
 void sbi_nputs(const char *str, unsigned long len)
 {
-	unsigned long i;
-
 	spin_lock(&console_out_lock);
-	if (console_dev && console_dev->console_puts) {
-		console_dev->console_puts(str, len);
-	} else {
-		for (i = 0; i < len; i++)
-			sbi_putc(str[i]);
-	}
+	nputs(str, len);
 	spin_unlock(&console_out_lock);
 }
 
@@ -102,6 +100,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)
 #define PAD_ZERO 2
 #define PAD_ALTERNATE 4
 #define PRINT_BUF_LEN 64
+#define PRINT_TBUF_MAX 128
 
 #define va_start(v, l) __builtin_va_start((v), l)
 #define va_end __builtin_va_end
@@ -217,12 +216,26 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg,
 
 static int print(char **out, u32 *out_len, const char *format, va_list args)
 {
-	int width, flags;
-	int pc = 0;
-	char scr[2];
+	u32 tbuf_len;
+	int width, flags, pc = 0;
+	char scr[2], *tout, tbuf[PRINT_TBUF_MAX];
+	bool use_tbuf = (!out) ? true : false;
 	unsigned long long tmp;
 
+	if (use_tbuf) {
+		tbuf_len = PRINT_TBUF_MAX;
+		tout = tbuf;
+		out = &tout;
+		out_len = &tbuf_len;
+	}
+
 	for (; *format != 0; ++format) {
+		if (use_tbuf && !tbuf_len) {
+			nputs(tbuf, PRINT_TBUF_MAX);
+			tbuf_len = PRINT_TBUF_MAX;
+			tout = tbuf;
+		}
+
 		if (*format == '%') {
 			++format;
 			width = flags = 0;
@@ -348,6 +361,9 @@ literal:
 		}
 	}
 
+	if (use_tbuf && tbuf_len < PRINT_TBUF_MAX)
+		nputs(tbuf, PRINT_TBUF_MAX - tbuf_len);
+
 	return pc;
 }
 
-- 
2.34.1




More information about the opensbi mailing list