Skip to content

Commit

Permalink
lguest: implement deferred interrupts in example Launcher
Browse files Browse the repository at this point in the history
Rather than sending an interrupt on every buffer, we only send an interrupt
when we're about to wait for the Guest to send us a new one.  The console
input and network input still send interrupts manually, but the block device,
network and console output queues can simply rely on this logic to send
interrupts to the Guest at the right time.

The patch is cluttered by moving trigger_irq() higher in the code.

In practice, two factors make this optimization less interesting:
(1) we often only get one input at a time, even for networking,
(2) triggering an interrupt rapidly tends to get coalesced anyway.

Before:				Secs	RxIRQS	TxIRQs
 1G TCP Guest->Host:		3.72	32784	32771
 1M normal pings:		99	1000004	995541
 100,000 1k pings (-l 120):	5	49510	49058

After:
 1G TCP Guest->Host:		3.69	32809	32769
 1M normal pings:		99	1000004	996196
 100,000 1k pings (-l 120):	5	52435	52361

(Note the interrupt count on 100k pings goes *up*: see next patch).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
Rusty Russell committed Jun 12, 2009
1 parent 5dac051 commit 38bc2b8
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions Documentation/lguest/lguest.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,21 @@ static unsigned next_desc(struct virtqueue *vq, unsigned int i)
return next;
}

/* This actually sends the interrupt for this virtqueue */
static void trigger_irq(struct virtqueue *vq)
{
unsigned long buf[] = { LHREQ_IRQ, vq->config.irq };

/* If they don't want an interrupt, don't send one, unless empty. */
if ((vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
&& lg_last_avail(vq) != vq->vring.avail->idx)
return;

/* Send the Guest an interrupt tell them we used something up. */
if (write(lguest_fd, buf, sizeof(buf)) != 0)
err(1, "Triggering irq %i", vq->config.irq);
}

/* This looks in the virtqueue and for the first available buffer, and converts
* it to an iovec for convenient access. Since descriptors consist of some
* number of output then some number of input descriptors, it's actually two
Expand All @@ -567,6 +582,9 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
while (last_avail == vq->vring.avail->idx) {
u64 event;

/* OK, tell Guest about progress up to now. */
trigger_irq(vq);

/* Nothing new? Wait for eventfd to tell us they refilled. */
if (read(vq->eventfd, &event, sizeof(event)) != sizeof(event))
errx(1, "Event read failed?");
Expand Down Expand Up @@ -631,21 +649,6 @@ static void add_used(struct virtqueue *vq, unsigned int head, int len)
vq->vring.used->idx++;
}

/* This actually sends the interrupt for this virtqueue */
static void trigger_irq(struct virtqueue *vq)
{
unsigned long buf[] = { LHREQ_IRQ, vq->config.irq };

/* If they don't want an interrupt, don't send one, unless empty. */
if ((vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
&& lg_last_avail(vq) != vq->vring.avail->idx)
return;

/* Send the Guest an interrupt tell them we used something up. */
if (write(lguest_fd, buf, sizeof(buf)) != 0)
err(1, "Triggering irq %i", vq->config.irq);
}

/* And here's the combo meal deal. Supersize me! */
static void add_used_and_trigger(struct virtqueue *vq, unsigned head, int len)
{
Expand Down Expand Up @@ -730,7 +733,7 @@ static void console_output(struct virtqueue *vq)
err(1, "Write to stdout gave %i", len);
iov_consume(iov, out, len);
}
add_used_and_trigger(vq, head, 0);
add_used(vq, head, 0);
}

/*
Expand All @@ -754,7 +757,7 @@ static void net_output(struct virtqueue *vq)
errx(1, "Input buffers in net output queue?");
if (writev(net_info->tunfd, iov, out) < 0)
errx(1, "Write to tun failed?");
add_used_and_trigger(vq, head, 0);
add_used(vq, head, 0);
}

/* This is where we handle packets coming in from the tun device to our
Expand Down Expand Up @@ -1422,7 +1425,7 @@ static void blk_request(struct virtqueue *vq)
if (out->type & VIRTIO_BLK_T_BARRIER)
fdatasync(vblk->fd);

add_used_and_trigger(vq, head, wlen);
add_used(vq, head, wlen);
}

/*L:198 This actually sets up a virtual block device. */
Expand Down Expand Up @@ -1496,7 +1499,7 @@ static void rng_input(struct virtqueue *vq)
}

/* Tell the Guest about the new input. */
add_used_and_trigger(vq, head, totlen);
add_used(vq, head, totlen);
}

/* And this creates a "hardware" random number device for the Guest. */
Expand Down

0 comments on commit 38bc2b8

Please sign in to comment.