USB buffering#

This document explains how the driver buffers streaming data from the USB host controller and how the buffer sizes were chosen.

How it works#

Each instrument streams over one bulk IN pipe. The USB backend keeps a queue of read transfers on that pipe with the operating system:

  • libusb on Linux and macOS (src/backend/libusb/backend.c)

  • WinUSB on Windows (src/backend/winusb/backend.c)

A transfer completes when it is full, or when the device ends a burst with a short or zero-length packet. The JS220 and JS320 send a zero-length packet when the IN pipe has been idle for long enough, so a partly filled transfer does not wait for more data.

The backend thread hands each completed transfer to the device driver without copying, and queues a fresh transfer in its place. Data that has reached the driver is safe. The queued transfers are the only protection against a host delay: if the backend thread does not run for longer than the queue holds, the device’s own buffer fills and the device drops samples.

The current settings, for every backend:

Setting

Value

Transfer size

32 KiB (64 frames of 512 bytes)

Transfers queued per instrument

32

Memory per instrument

1 MiB

Time held at full rate

about 130 ms

Every instrument streams about 8.1 MB/s at its full sample rate, the JS110 included. When the device sends less data, for example when it downsamples, a transfer takes longer to fill. The same queue then holds longer, and the latency below rises in proportion.

Transfer size sets the latency#

At full rate, data reaches the host one transfer at a time, so the transfer size sets the delay from a sample to the host. Measured median and 99th percentile delays, from the device timestamp of the last sample in each message to its arrival:

Transfer size

Median

99th percentile

32 KiB

3 ms

5 ms

64 KiB

5 ms

9 ms

128 KiB

10 ms

17 ms

256 KiB

18 ms

33 ms

1 MiB

68 ms

156 ms

The number of queued transfers has no effect on latency or CPU load. So the driver keeps 32 KiB transfers and sets the margin with the count.

Transfer count sets the margin#

The queue absorbs a host delay of up to transfer size x count / 8.1 MB/s. The test blocked the backend thread for a set time every 5 s while every instrument streamed at full rate. On macOS, Linux and Windows, x64 and ARM, each configuration lost no samples below its capacity and lost samples just above it:

Transfers queued (32 KiB)

Capacity

No loss

Loss

4

16 ms

10 ms

25 ms

16

64 ms

58 ms

80 ms

64

260 ms

230 ms

310 ms

256

1.03 s

900 ms

1250 ms

The JS110 has the least buffering on the device: with 16 transfers it lost samples at 58 ms, where the JS220 and JS320 did not.

The longest delays seen without injection, in 12-hour full-rate captures on the CI stations, were 17 to 19 ms on macOS after background-task bursts, and 20 to 30 ms on Windows. Before 2.5.9, Windows queued 4 transfers (16 ms) and lost samples during these delays. 32 transfers hold about 4 times the longest delay seen.

Platform limits#

  • Linux: the kernel limits the memory of all queued USB transfers, for all programs, to /sys/module/usbcore/parameters/usbfs_memory_mb, which is 16 MB by default (0 means no limit). Past the limit, libusb fails new transfers with LIBUSB_ERROR_NO_MEM, including the control transfers that open a device, so the next instrument cannot open. At 1 MiB per instrument, the default limit allows about 15 instruments, less any memory that other USB programs use. See More than 15 instruments on Linux.

  • Windows: WinUSB limits one transfer to the pipe’s MAXIMUM_TRANSFER_SIZE, 2 MiB on the tested hosts. WinUSB locks queued transfer buffers in memory. All transfers on a pipe share one event, so the 64-handle limit of WaitForMultipleObjects does not limit the count.

  • macOS: no limit was reached up to 8 MiB per instrument with 3 instruments.

More than 15 instruments on Linux#

Raise the usbfs limit before opening the instruments. 64 MB allows about 60 instruments, and 0 removes the limit. Check the current value:

cat /sys/module/usbcore/parameters/usbfs_memory_mb

To raise it until the next reboot:

sudo sh -c 'echo 64 > /sys/module/usbcore/parameters/usbfs_memory_mb'

To keep it across reboots, add usbcore.usbfs_memory_mb=64 to the kernel command line. On Debian and Ubuntu, add it to GRUB_CMDLINE_LINUX in /etc/default/grub, keeping any options already there, for example:

GRUB_CMDLINE_LINUX="usbcore.usbfs_memory_mb=64"

then run sudo update-grub and reboot. On Fedora and RHEL, run sudo grubby --update-kernel=ALL --args="usbcore.usbfs_memory_mb=64" and reboot.

An options usbcore usbfs_memory_mb=64 line in /etc/modprobe.d usually has no effect, since most distribution kernels build usbcore in rather than load it as a module. The kernel command line works either way.