From 0f7344a3a81265497b2d4893767d6f93b4794018 Mon Sep 17 00:00:00 2001 From: Mykyta Poturai Date: Fri, 12 Apr 2024 13:18:47 +0300 Subject: [PATCH] xen-console: Optimize console_display_thrd Optimize console_display_thrd by removing the inner loop that copies data from the console buffer to the output buffer. Instead, calculate the size of the data to be copied and print it in one go. Signed-off-by: Mykyta Poturai Reviewed-by: Dmytro Firsov Reviewed-by: Volodymyr Babchuk --- xen-console-srv/src/xen_console.c | 34 +++++++++++++++++-------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/xen-console-srv/src/xen_console.c b/xen-console-srv/src/xen_console.c index fdc4513a..c5dd3038 100644 --- a/xen-console-srv/src/xen_console.c +++ b/xen-console-srv/src/xen_console.c @@ -367,9 +367,8 @@ static void console_display_thrd(void *p1, void *p2, void *p3) ARG_UNUSED(p3); struct xen_domain_console *console = p1; const struct shell *shell = p2; - /* Buffer input a little */ - char buf[32]; - int read; + size_t buf_pos, prod_buf_pos; + int size; shell_info(shell, "Attached to a domain console"); @@ -387,19 +386,24 @@ static void console_display_thrd(void *p1, void *p2, void *p3) } while (console->int_cons < console->int_prod) { - read = 0; - memset(buf, 0, sizeof(buf)); - - /* TODO: There is a room for optimization.... */ - while (console->int_cons < console->int_prod && - read < sizeof(buf) - 1) { - size_t buf_pos = (console->int_cons++) & - (XEN_CONSOLE_BUFFER_SZ - 1); - buf[read++] = console->int_buf[buf_pos]; - } - if (read) { - shell_fprintf(shell, SHELL_NORMAL, "%s", buf); + buf_pos = (console->int_cons) & + (XEN_CONSOLE_BUFFER_SZ - 1); + prod_buf_pos = (console->int_prod) & + (XEN_CONSOLE_BUFFER_SZ - 1); + /* int_buf is a circular buffer so we need to check for + * the wrap around condition to print it safely. + * In the case of the wrap around condition, we first + * print from the buf_pos to the end of the buffer, + * and then continue printing from the beginning of the buffer. + */ + if (buf_pos < prod_buf_pos) { + size = prod_buf_pos - buf_pos; + } else { + size = XEN_CONSOLE_BUFFER_SZ - buf_pos; } + shell_fprintf(shell, SHELL_NORMAL, "%.*s", size, + &console->int_buf[buf_pos]); + console->int_cons += size; } k_mutex_unlock(&console->lock); }