Skip to content

Commit 4b86806

Browse files
bhyveload: add a small ring buffer to the cb_putc function.
The console output descriptor is NONBLOCK. It often returns EAGIN when the console connects to nmdm(4). The cb_putc should buffer the output characters and retry writing. If the buffer becomes full, the first character will be discarded. Signed-off-by: Yuichiro NAITO <naito.yuichiro@gmail.com>
1 parent 50caa0e commit 4b86806

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

usr.sbin/bhyveload/bhyveload.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <sys/stat.h>
5858
#include <sys/disk.h>
5959
#include <sys/queue.h>
60+
#include <sys/uio.h>
6061

6162
#include <machine/specialreg.h>
6263
#include <machine/vmm.h>
@@ -89,6 +90,10 @@
8990

9091
#define NDISKS 32
9192

93+
#define BUF_SIZE 256
94+
#define PRODUCE(u, p, n) {(u) += (n); (p) = ((p) + (n)) % BUF_SIZE;}
95+
#define CONSUME(u, c, n) {(u) -= (n); (c) = ((c) + (n)) % BUF_SIZE;}
96+
9297
/*
9398
* Reason for our loader reload and reentry, though these aren't really used
9499
* at the moment.
@@ -125,9 +130,34 @@ static void cb_exit(void *arg, int v);
125130
static void
126131
cb_putc(void *arg __unused, int ch)
127132
{
128-
char c = ch;
133+
static char buf[BUF_SIZE];
134+
static uint cons = 0, prod = 0, used = 0, niov;
135+
struct iovec iov[2];
136+
ssize_t rc;
137+
138+
/* If the buffer is full, discard one character. */
139+
if (used == BUF_SIZE)
140+
CONSUME(used, cons, 1);
141+
142+
buf[prod] = ch;
143+
PRODUCE(used, prod, 1);
144+
145+
if (prod > cons) {
146+
iov[0].iov_base = &buf[cons];
147+
iov[0].iov_len = prod - cons;
148+
niov = 1;
149+
} else {
150+
iov[0].iov_base = &buf[cons];
151+
iov[0].iov_len = BUF_SIZE - cons;
152+
iov[1].iov_base = buf;
153+
iov[1].iov_len = prod;
154+
niov = 2;
155+
}
156+
157+
if ((rc = writev(consout_fd, iov, niov)) < 0)
158+
return;
129159

130-
(void) write(consout_fd, &c, 1);
160+
CONSUME(used, cons, rc);
131161
}
132162

133163
static int

0 commit comments

Comments
 (0)