Skip to content

Commit

Permalink
xen-console: Optimize console_display_thrd
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Dmytro Firsov <[email protected]>
Reviewed-by: Volodymyr Babchuk <[email protected]>
  • Loading branch information
Deedone committed Apr 16, 2024
1 parent 7029516 commit 0f7344a
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions xen-console-srv/src/xen_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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);
}
Expand Down

0 comments on commit 0f7344a

Please sign in to comment.