From 75cd6b03703c9817390b4ab57bbd89673b09718c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 26 Oct 2026 07:07:09 +0000 Subject: [PATCH] Agere: 8 bulk transfers in flight each way; the IN round trips timed in the log The device starts its next IN packet only once the last has gone; with Agere's driver's 2 in flight an app's round trip through libusb seems to leave it waiting (2459 samples a second, at every ARM clock). The log's transfer counts now say how long the IN transfers take to come back. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs --- app/src/main/cpp/usb/usb_modem.cpp | 2 +- app/src/main/cpp/usb/usb_transfers.cpp | 39 ++++++++++++++++++- .../com/neodesktop/dmodem/modem/UsbModems.kt | 6 ++- external/d-modem-re | 2 +- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/app/src/main/cpp/usb/usb_modem.cpp b/app/src/main/cpp/usb/usb_modem.cpp index 0628bda..0cac8f9 100644 --- a/app/src/main/cpp/usb/usb_modem.cpp +++ b/app/src/main/cpp/usb/usb_modem.cpp @@ -113,7 +113,7 @@ int UsbModem::run() u64 now = dcore::monotonic_ms(); if (now >= next_say) { u64 wall = now - t0, clock = line_->now_ms(); - char stats[256]; + char stats[384]; usb_transfer_stats(stats, sizeof(stats)); if (!warned && clock < wall / 2) { ERR("usb: the modem's samples %s: its clock %llu ms " diff --git a/app/src/main/cpp/usb/usb_transfers.cpp b/app/src/main/cpp/usb/usb_transfers.cpp index e4b224a..69e53ca 100644 --- a/app/src/main/cpp/usb/usb_transfers.cpp +++ b/app/src/main/cpp/usb/usb_transfers.cpp @@ -10,6 +10,7 @@ #include #include +#include #include @@ -24,6 +25,18 @@ struct Way { int last_status = 0; }; Way in_, out_; +/* the IN transfers' round trips (sent to back) and the longest gap + between two coming back, since the last usb_transfer_stats() */ +std::unordered_map sent_at_; +uint64_t trips_ = 0, trip_us_ = 0, trip_max_us_ = 0, gap_max_us_ = 0, last_back_us_ = 0; + +uint64_t now_us() +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return static_cast(ts.tv_sec) * 1000000u + + static_cast(ts.tv_nsec) / 1000u; +} /* the first IN transfers back: logged, their octets' head */ const int IN_SAID = 3; int in_said_ = 0; @@ -39,6 +52,20 @@ void LIBUSB_CALL counted(libusb_transfer *t) { Way &w = is_in(t) ? in_ : out_; w.done++; + if (is_in(t)) { + uint64_t now = now_us(); + auto s = sent_at_.find(t); + if (s != sent_at_.end()) { + uint64_t trip = now - s->second; + trips_++; + trip_us_ += trip; + if (trip > trip_max_us_) + trip_max_us_ = trip; + } + if (last_back_us_ && now - last_back_us_ > gap_max_us_) + gap_max_us_ = now - last_back_us_; + last_back_us_ = now; + } if (t->status == LIBUSB_TRANSFER_COMPLETED) { w.octets += static_cast(t->actual_length); /* (an isochronous one's are its packets') */ @@ -74,6 +101,8 @@ void usb_transfers_reset() out_ = Way(); in_said_ = 0; callbacks_.clear(); + sent_at_.clear(); + trips_ = trip_us_ = trip_max_us_ = gap_max_us_ = last_back_us_ = 0; } void usb_transfer_stats(char *buf, size_t len) @@ -82,11 +111,15 @@ void usb_transfer_stats(char *buf, size_t len) "IN %" PRIu64 " sent, %" PRIu64 " back (%" PRIu64 " octets, %" PRIu64 " failed, last %d)%s; " "OUT %" PRIu64 " sent, %" PRIu64 " back (%" PRIu64 - " failed, last %d)%s", + " failed, last %d)%s; IN round trips %.1f ms, at most %.1f, " + "the longest wait %.1f", in_.sent, in_.done, in_.octets, in_.failed, in_.last_status, in_.refused ? ", some refused" : "", out_.sent, out_.done, out_.failed, out_.last_status, - out_.refused ? ", some refused" : ""); + out_.refused ? ", some refused" : "", + trips_ ? trip_us_ / 1000.0 / static_cast(trips_) : 0.0, + trip_max_us_ / 1000.0, gap_max_us_ / 1000.0); + trips_ = trip_us_ = trip_max_us_ = gap_max_us_ = 0; } } // namespace dmodem_android @@ -99,6 +132,8 @@ int dmodem_libusb_submit_transfer(struct libusb_transfer *t) callbacks_[t] = t->callback; t->callback = counted; } + if (is_in(t)) + sent_at_[t] = now_us(); int err = libusb_submit_transfer(t); Way &w = is_in(t) ? in_ : out_; if (err == 0) diff --git a/app/src/main/java/com/neodesktop/dmodem/modem/UsbModems.kt b/app/src/main/java/com/neodesktop/dmodem/modem/UsbModems.kt index c7ebb34..a4e6359 100644 --- a/app/src/main/java/com/neodesktop/dmodem/modem/UsbModems.kt +++ b/app/src/main/java/com/neodesktop/dmodem/modem/UsbModems.kt @@ -73,8 +73,10 @@ object UsbModems { if (!out.exists() || !out.readBytes().contentEquals(bytes)) out.writeBytes(bytes) } android.system.Os.setenv("DMODEM_AGERE_FIRMWARE", dir.path, true) - // (while the Agere is brought up: its samples' rate at each ARM clock, logged at start, ~13 s) - android.system.Os.setenv("DMODEM_AGERE_CLOCK_SWEEP", "1", true) + // (8 bulk transfers in flight each way, not Agere's driver's 2: a packet is 6 ms, and the + // device starts the next only once the last has gone, so an app's round trip through libusb + // must not leave it waiting) + android.system.Os.setenv("DMODEM_AGERE_TRANSFERS", "8", true) return dir } diff --git a/external/d-modem-re b/external/d-modem-re index ad29f8d..dde6b6d 160000 --- a/external/d-modem-re +++ b/external/d-modem-re @@ -1 +1 @@ -Subproject commit ad29f8dd4381e5d7abcf2325516210b9608499a0 +Subproject commit dde6b6da25944b690cb6632156f026a82d1fe388