Skip to content

Commit 17e5b4f

Browse files
amit3srustyrussell
authored andcommitted
virtio: console: add port stats for bytes received, sent and discarded
This commit adds port-specific stats for the number of bytes received, sent and discarded. They're exposed via the debugfs interface. This data can be used to check for data loss bugs (or disprove such claims). It can also be used for accounting, if there's such a need. The stats remain valid throughout the lifetime of the port. Unplugging a port will reset the stats. The numbers are not reset across port opens/closes. Signed-off-by: Amit Shah <amit.shah@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 2d24cda commit 17e5b4f

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎drivers/char/virtio_console.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ struct ports_device {
152152
int chr_major;
153153
};
154154

155+
struct port_stats {
156+
unsigned long bytes_sent, bytes_received, bytes_discarded;
157+
};
158+
155159
/* This struct holds the per-port data */
156160
struct port {
157161
/* Next port in the list, head is in the ports_device */
@@ -179,6 +183,13 @@ struct port {
179183
/* File in the debugfs directory that exposes this port's information */
180184
struct dentry *debugfs_file;
181185

186+
/*
187+
* Keep count of the bytes sent, received and discarded for
188+
* this port for accounting and debugging purposes. These
189+
* counts are not reset across port open / close events.
190+
*/
191+
struct port_stats stats;
192+
182193
/*
183194
* The entries in this struct will be valid if this port is
184195
* hooked up to an hvc console
@@ -360,6 +371,7 @@ static struct port_buffer *get_inbuf(struct port *port)
360371
if (buf) {
361372
buf->len = len;
362373
buf->offset = 0;
374+
port->stats.bytes_received += len;
363375
}
364376
return buf;
365377
}
@@ -396,6 +408,7 @@ static void discard_port_data(struct port *port)
396408

397409
err = 0;
398410
while (buf) {
411+
port->stats.bytes_discarded += buf->len - buf->offset;
399412
if (add_inbuf(port->in_vq, buf) < 0) {
400413
err++;
401414
free_buf(buf);
@@ -519,6 +532,8 @@ static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
519532
cpu_relax();
520533
done:
521534
spin_unlock_irqrestore(&port->outvq_lock, flags);
535+
536+
port->stats.bytes_sent += in_count;
522537
/*
523538
* We're expected to return the amount of data we wrote -- all
524539
* of it
@@ -1048,6 +1063,14 @@ static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
10481063
"host_connected: %d\n", port->host_connected);
10491064
out_offset += snprintf(buf + out_offset, out_count - out_offset,
10501065
"outvq_full: %d\n", port->outvq_full);
1066+
out_offset += snprintf(buf + out_offset, out_count - out_offset,
1067+
"bytes_sent: %lu\n", port->stats.bytes_sent);
1068+
out_offset += snprintf(buf + out_offset, out_count - out_offset,
1069+
"bytes_received: %lu\n",
1070+
port->stats.bytes_received);
1071+
out_offset += snprintf(buf + out_offset, out_count - out_offset,
1072+
"bytes_discarded: %lu\n",
1073+
port->stats.bytes_discarded);
10511074
out_offset += snprintf(buf + out_offset, out_count - out_offset,
10521075
"is_console: %s\n",
10531076
is_console_port(port) ? "yes" : "no");
@@ -1133,6 +1156,7 @@ static int add_port(struct ports_device *portdev, u32 id)
11331156
port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
11341157

11351158
port->host_connected = port->guest_connected = false;
1159+
port->stats = (struct port_stats) { 0 };
11361160

11371161
port->outvq_full = false;
11381162

0 commit comments

Comments
 (0)