Skip to content

fixed: serial reads waited for a full buffer, delaying every adapter message by up to 50ms - #770

Open
proilyxa wants to merge 1 commit into
Pulse-Eight:masterfrom
proilyxa:serial-read-available
Open

proilyxa wants to merge 1 commit into
Pulse-Eight:masterfrom
proilyxa:serial-read-available

Conversation

@proilyxa

Copy link
Copy Markdown

Problem

On POSIX, CSerialSocket::Read() goes through SocketRead(), which keeps calling select() and read() until len bytes are in or the timeout passes. CUSBCECAdapterCommunication::Process() reads the Pulse-Eight adapter with ReadFromDevice(50, 5): 5 bytes, 50 ms timeout. Most adapter messages are 4 bytes (MSGSTART, code, value, MSGEND), so each one sits in the buffer until the next byte arrives or the 50 ms run out. The message carrying the EOM flag is the last of every received frame, so every frame reaches the client late, by up to 50 ms. With a TV remote that is added to every key press.

Measurement

Pulse-Eight USB-CEC adapter (2548:1002) on /dev/ttyACM0, LG TV, Debian testing, libCEC 8.1.7 (8.1.7+dfsg-1), cec-client -t r -d 15. Method: strace -f -ttt -e trace=read,write on cec-client; for each received frame, the time from the read() that completes the adapter message with the EOM flag to the write() of that frame's TRAFFIC line.

frames median mean 90th percentile max
all received frames, before 1203 38.6 ms 31.0 ms 49.8 ms 175.5 ms
all received frames, after 1209 0.6 ms 0.6 ms 1.0 ms 5.4 ms
remote key presses (User Control Pressed), before 245 46.4 ms 43.4 ms 47.8 ms 48.8 ms
remote key presses, after 46 0.7 ms 0.7 ms 1.1 ms 1.5 ms

Most frames are the TV's once-a-second polls. Before the change the polls show the delay drifting between 0 and 50 ms, since the 50 ms read window is not aligned with the bus.

Idle CPU of cec-client is unchanged: 96 clock ticks per 60 s before and 95 after (about 1.6% of one core). When idle the thread still blocks in select() for up to 50 ms, then sleeps 5 ms, as before.

Change

CSerialSocket::Read() on POSIX now waits until the port is readable (same timeout semantics: 0 waits forever) and returns what a single read() gives. Its callers already handle partial data:

  • ReadFromDevice() hands whatever it gets to CCECAdapterMessageQueue::AddData(), which parses a byte stream;
  • ClearInputBytes() reads until the input is quiet and the last byte was MSGEND.

SocketRead() itself is unchanged: CCDevSocket uses it too, and the TDA995x and IMX backends read whole structs through it. Only the Pulse-Eight backend uses CSerialPort.

Windows is not changed. CSerialSocket::SetTimeouts() sets only ReadTotalTimeoutConstant, so ReadFile() probably waits for the full buffer in the same way, but I can't test it there.

Testing

  • src/ is the same at libcec-8.1.7 and master; the patch applies to both.
  • Built as a Debian package from 8.1.7+dfsg-1 with this patch: the exported dynamic symbols of libcec.so.8 match the Debian build (1327 symbols).
  • With the adapter above, running for 30+ minutes: TV remote OK, arrows and Back work with no missed or doubled presses, cec-client stays on the bus with no reconnects, and Kodi 22 linked against the rebuilt libcec.so.8 starts and exits normally.

…message by up to 50ms

CSerialSocket::Read() on POSIX went through SocketRead(), which keeps calling
select() and read() until len bytes are in or the timeout passes. The
Pulse-Eight communication thread reads 5 bytes with a 50ms timeout, and most
adapter messages are 4 bytes, so each one - the end of every received frame
included - sat in the buffer until the next byte came in or the 50ms ran out.

Read() now waits until the port is readable and returns what read() gives.
ReadFromDevice() already hands whatever it gets to the message parser, and
ClearInputBytes() reads until the input is quiet, so neither needs a full
buffer. SocketRead() is unchanged: the cdev based adapters (TDA995x, IMX)
read whole structs through it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant