commaai / msgq

MSGQ: A lock free single producer multi consumer message queue
184 stars 251 forks source link

msgq: connect immediately #588

Open sshane opened 8 months ago

sshane commented 8 months ago

When the publisher is created, it calls msgq_init_publisher which resets the reader count. If you send data (in msgq_msg_send) before the subcriber connects, you will have no readers to signal to here:

https://github.com/commaai/cereal/blob/bd31b25aacc5b39f36cedcb0dabd05db471da59f/messaging/msgq.cc#L295-L299

This PR finds all current readers and sends a SIGUSR2 signal. Verified that the subscriber polling doesn't finish early when a publisher is created.


Subscriber ```python from cereal.messaging import SubMaster, PubMaster, new_message, sub_sock from cereal import log import time sock = sub_sock('sendcan') sock.setTimeout(100) t = time.time() while True: print('recv', sock.receive()) print(time.time() - t) print() ```
Publisher ``` from cereal.messaging import PubMaster, new_message from cereal import log import time times = [] for _ in range(1000): pm = PubMaster(['sendcan']) t = time.monotonic() while 1: if pm.sock['sendcan'].all_readers_updated(): times.append(time.monotonic() - t) break print(times) print(sum(times) / len(times)) ```

Time for all readers to connect (should be half of subscriber dt on average):

0.05434682522984804 mean, 0.049833057644082523 std

After change:

2.469095983542502e-06 mean, 7.680831147408531e-07 std