gdb: read a serial's error fd once per input chunk, not per character
Checks
| Context |
Check |
Description |
| linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_gdb_build--master-arm |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 |
success
|
Test passed
|
| linaro-tcwg-bot/tcwg_gdb_check--master-arm |
success
|
Test passed
|
Commit Message
generic_readchar reads the error fd after every character it returns.
For a pipe target ("target remote | command"), that fd is the
command's stderr.
ser-pipe.c has no "avail" method, so each of these reads is a read(2)
on a non-blocking socket, which nearly always fails with EAGAIN.
Reading memory therefore costs one extra system call per byte.
Read the error fd only when the character is the first one of a chunk
read from the device, or is an error or end of file.
Error output written before a chunk's data is still printed before
that data is used.
Error output that arrives while GDB works through a chunk is printed
with the next chunk, at the next one-second wait in
do_ser_base_readchar, or by handle_error_fd in the event loop, as when
GDB is idle.
TCP and serial connections have no error fd and are unchanged.
---
This is my first GDB patch, so I'm happy to learn and to rework it as
needed. I came across it by accident while doing embedded work: I was
speeding up a GDB connection to a board over "| ssh" layer by layer
(the stub, the transport, GDB itself), and GDB's side turned out to
make one system call per byte.
I don't have write access; please push it if approved.
Thank you for taking a look.
Measured on x86-64 GNU/Linux, GDB master 10ae2343:
1. A program holding 2 MiB of data, run by gdbserver on a pipe:
target remote | exec gdbserver - prog
2. "dump binary memory" of the 2 MiB.
3. Before and after, alternated, 3 runs each.
Before: 1.86 to 1.89 s, 2,689,827 read calls.
After: 0.034 to 0.035 s, 1,793 read calls.
The dumped bytes are identical.
Over "| ssh host stub" to a board (STM32H753, 1.875 MiB of flash,
GDB 18.1): 5.2 to 5.4 s before, 4.0 to 4.4 s after.
Tested: make check TESTS="gdb.server/*.exp" with
--target_board=native-stdio-gdbserver and with native-gdbserver,
before and after the patch.
The results are the same: 1027 and 1124 expected passes, and the
same one FAIL in both (gdb.server/non-existing-program.exp:
gdbserver exits cleanly).
I have no FSF copyright assignment.
This is a tiny change: 13 lines, most of them a comment.
gdb/ser-base.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
Comments
>>>>> Josip Seketa <josip.seketa@gmail.com> writes:
> generic_readchar reads the error fd after every character it returns.
> For a pipe target ("target remote | command"), that fd is the
> command's stderr.
> ser-pipe.c has no "avail" method, so each of these reads is a read(2)
> on a non-blocking socket, which nearly always fails with EAGAIN.
> Reading memory therefore costs one extra system call per byte.
Thank you for the patch.
I hadn't previously taken any notice of this code. Ugly stuff :-(
> Read the error fd only when the character is the first one of a chunk
> read from the device, or is an error or end of file.
I find it a bit hard to untangle all this but I think there are two
cases here:
1. In async mode, the error fd is handled via the main select/poll loop.
2. In sync mode, the error fd is handled when reading from the main
serial fd as well.
And (again IIUC) your patch addresses an issue with scenario 2.
And I guess this makes sense. We can't always handle the error fd via
the poll loop, because in sync mode gdb will be blocked in some read()
in remote.c.
Ok so far. But then, consider this comment from ser-base.c:
/* We also need to check and consume the stderr because it could
come before the stdout for some stubs. If we just sit and wait
for stdout, we would hit a deadlock for that case. */
From this I think the idea is to consider a remote that's writing to
both its stdout and stderr. And, if the stderr pipe fills up, then a
read attempt by gdb on the remote's stdout will never finish -- because
the remote will be blocked while writing.
So, gdb has to be sure to drain the error output while reading.
And so my question is whether your patch preserves this. I somewhat
suspect it does not.
Also I was wondering why you changed the call in generic_readchar (which
passes "1" as the close_fd parameter) and not the call in
do_ser_base_readchar.
I didn't dig much deeper than this. The code here is a bit confusing I
think.
Anyway, I do think some kind of change here would be good. I'm just not
sure exactly what.
Maybe one idea would be if the read from the remote's stdout fails (like
if we're doing a non-blocking read, which I didn't look into), then that
might be a good time to read from the error fd?
I also have a crazier idea involving threads but TBH I'm not sure that
is sensible at all.
Finally I wanted to say:
> Before: 1.86 to 1.89 s, 2,689,827 read calls.
> After: 0.034 to 0.035 s, 1,793 read calls.
This is a nice find, thank you.
Here at AdaCore, we've noticed that remote testing over ssh is
noticeably slower than remote testing over a socket. Investigating this
has never gotten to the top of my to-do list -- but I wonder if you've
found the problem.
Tom
@@ -428,6 +428,11 @@ generic_readchar (struct serial *scb, int timeout,
int (do_readchar) (struct serial *scb, int timeout))
{
int ch;
+ /* Whether CH will be the first character of a chunk read from
+ SCB->fd (here or by fd_event), or an error, rather than the next
+ one of a chunk already begun. */
+ bool chunk_start = scb->bufcnt <= 0 || scb->bufp == scb->buf;
+
if (scb->bufcnt > 0)
{
ch = *scb->bufp;
@@ -458,8 +463,14 @@ generic_readchar (struct serial *scb, int timeout,
}
}
- /* Read any error output we might have. */
- ser_base_read_error_fd (scb, 1);
+ /* Read any error output we might have: once a chunk, when its
+ first character is taken, so that error output written before
+ the chunk's data is shown before any of that data is used. Not
+ for every character: for a pipe, whose ops have no avail, each
+ call is a read on SCB->error_fd that nearly always fails with
+ EAGAIN, and a large reply made one such call per byte. */
+ if (chunk_start)
+ ser_base_read_error_fd (scb, 1);
reschedule (scb);
return ch;