Allow reqclock to be slightly less than the transmitted messages's
deadline. That is, delay messages with a reqclock far in the future
to slightly past (1<<31) ticks from its deadline. Use (3<<29)
instead, which gives an additional (1<<29) grace period to avoid clock
overflows.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The MIN_REQTIME_DELTA parameter controls when the host will flush
incomplete message blocks to the mcu. If the message had a target
time less than 250ms it would result in a flush even if a message
block was not completely full.
In the situation where the host generates lots of queue_step commands
to the point that it fills the mcu move_queue, then it would be
possible for individual queue_step commands to become eligible for
transmit only microseconds apart. It could also lead to a situation
where the target time was less than 250ms in the future. The result
could lead to many small message blocks as each became flushed
individually.
Tune the MIN_REQTIME_DELTA to 100ms to reduce the chance of this.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Keep moving messages from the pending queues to the ready queues even
if it's not currently valid to transmit messages to the mcu. This
improves the statistics when debugging.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Only hold the lock in the serialqueue thread when moving messages to
the ready queue and when setting the next need_kick_clock.
Only set the need_kick_clock just prior to sleeping.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Maintain the next needed wakeup time for entries in the pending
queues. This avoids needing to walk the upcoming queues when it is
known that nothing is ready to be released.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
There's no reason to attempt to handle multiple buffer transmissions
in a single command_event() call. Handle the transmit case outside of
the command building loop.
If data is transmitted, then get a new timestamp from the pollreactor
and retry before sleeping.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Simply describe how the cmdqueue is moved between states.
This is commit d7da45e1 resubmitted with a slighly different
implementation. It is thought that the previous implementation was
causing a gcc compiliation issue on gcc v12.0-v12.4 compilers. (It
may be related to gcc bug report 107467).
Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This reverts commit aea8d8e0a1.
This reverts commit 493271697f.
This reverts commit d7da45e152.
There are reports of a regression since making this change. Revert
for now until the root cause can be found.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Klipper logs an error on a failed CANbus write. Unfortunately, if the
bus becomes permanently disabled (eg, due to a user removing power to
devices on the CANbus) then it can result in the logs filling with
error messages.
Permanently disable the low-level processing of messages if CANbus
writes continually fail for at least 10 seconds. This avoids filling
the log with redundant messages.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The stalled_bytes counter refers to the number of bytes that are not
yet eligible for transmission. However, the naming leads to confusion
as it could be interpretted as an inability to transmit data. Rename
to upcoming_bytes to try to avoid that confusion.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add ability to run C code directly from the low-level socket reading
thread. This enables host based low-latency handlers.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If `klippy` loses connection to a device, the next `read()` from the
device file descriptor will result in a zero-byte result, i.e. an `EOF`.
Right now this gives a confusing error message, so this simply handles
the special case of `EOF` and outputs a better log message.
Signed-off-by: Lasse Dalegaard <dalegaard@gmail.com>
Some communication protocols are more efficient if fewer write() calls
are invoked. If multiple message blocks can be sent at the same time
then batch them into a single write() call.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If any callback is invoked from the pollreactor then obtain a new
eventtime before calculating a sleep duration. This improves the
timing of events.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
When in debug output mode, make sure to register the fd correctly.
Otherwise, the poll() call will use the 0 fd which could cause random
results.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add ability for the host code to get a notification when the ack for a
command sent to the micro-controller is received. This is in
preparation for improved detection of message loss between mcu and
host.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Use the -fwhole-program option when compiling the host C code. This
makes it easier to support inlining across C files.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If the mcu is using a traditional serial port, then only send a new
message block if there is space available in the mcu receive buffer.
This should make it significantly less likely that high load on the
mcu will result in retransmits.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Send the ack after processing commands - this gives the host code more
information on serial buffer utilization.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
In order to properly delay BACKGROUND_PRIORITY_CLOCK messages, they
must be 5ms beyond the minimum defined by MIN_REQTIME_DELTA. So, add
both MIN_REQTIME_DELTA and MIN_BACKGROUND_DELTA to the timing of these
messages.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Move the C code out of the main klippy/ directory and into its own
directory. This reduces the clutter in the main klippy directory.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>