diff --git a/device/device.go b/device/device.go index 766cdd4..0b7289b 100644 --- a/device/device.go +++ b/device/device.go @@ -307,6 +307,10 @@ func NewDevice(tunDevice tun.Device, bind conn.Bind, logger *Logger) *Device { device.rate.limiter.Init() device.indexTable.Init() + if MaxBatchSizeOverride > 0 { + device.batchSizeOverride.Store(int32(MaxBatchSizeOverride)) + } + device.PopulatePools() // create queues diff --git a/device/pool_size.go b/device/pool_size.go index 8910a4a..70b55f8 100644 --- a/device/pool_size.go +++ b/device/pool_size.go @@ -5,6 +5,12 @@ package device +// MaxBatchSizeOverride, when nonzero, replaces the per-Device batch size used +// to size eager buffer allocations in RoutineReceiveIncoming and +// RoutineReadFromTUN. Changes affect Devices created after this assignment. +// Use SetMaxBatchSizeOverride to change it safely at runtime. +var MaxBatchSizeOverride uint32 = 0 + // SetPreallocatedBuffersPerPool sets the cap on the number of buffers held by // each per-Device pool. Zero disables the cap (upstream default on // non-mobile platforms). Changes affect Devices created after this call. @@ -13,6 +19,13 @@ func SetPreallocatedBuffersPerPool(n uint32) { PreallocatedBuffersPerPool = n } +// SetMaxBatchSizeOverride sets the global batch size override applied to +// Devices created after this call. Zero disables the override. Existing +// Devices are unaffected; use Device.SetMaxBatchSize for per-instance. +func SetMaxBatchSizeOverride(n uint32) { + MaxBatchSizeOverride = n +} + // SetPreallocatedBuffersPerPool updates the cap on this Device's pools in // place. Takes effect immediately; goroutines blocked in Get are unblocked if // the cap was raised. Has no effect if the Device was created with