mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-20 04:27:48 -05:00
#2672 Polyphase channelizer enhancements to address buffer sizing and management
#2672 Bounds the polyphase buffer queue by sample duration instead of buffer count The polyphase buffer processor queue was capped at 500 native buffers, which is only about 85 ms of samples for an RSP at 6 MSPS (about 1,000 samples per buffer) versus about 6.8 s for an RTL-SDR (32,768 samples per buffer). Short processing stalls overflowed the queue and dropped buffers on RSP tuners. - Dispatcher: adds a weight-bounded queue option (element weigher plus an adjustable maximum queued weight). Elements count toward the weight until the listener has processed them. - PolyphaseChannelManager: bounds the buffer queue at 1 second of samples and updates the bound when the tuner sample rate changes. - RspNativeBuffer: sampleCount() returned twice the complex sample count. This also corrects the tuner sample drop monitor and the spectral display buffer manager for RSP tuners. Co-authored-by: denny <dennis.sheirer@gmail.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* *****************************************************************************
|
||||
* Copyright (C) 2014-2025 Dennis Sheirer
|
||||
* Copyright (C) 2014-2026 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -31,6 +31,8 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.ToLongFunction;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -54,6 +56,9 @@ public class Dispatcher<E> implements Listener<E>
|
||||
private HeartbeatManager mHeartbeatManager;
|
||||
private long mDroppedCount = 0;
|
||||
private long mLastOverflowLogTimestamp = 0;
|
||||
private final ToLongFunction<E> mWeigher;
|
||||
private volatile long mMaximumQueuedWeight = Long.MAX_VALUE;
|
||||
private final AtomicLong mQueuedWeight = new AtomicLong();
|
||||
|
||||
/**
|
||||
* Constructs an instance of a Dispatcher with integrated heartbeat support.
|
||||
@@ -90,6 +95,7 @@ public class Dispatcher<E> implements Listener<E>
|
||||
mThreadName = threadName;
|
||||
mInterval = interval;
|
||||
mQueue = new LinkedTransferQueue<>();
|
||||
mWeigher = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,6 +109,46 @@ public class Dispatcher<E> implements Listener<E>
|
||||
mThreadName = threadName;
|
||||
mInterval = interval;
|
||||
mQueue = new LinkedBlockingQueue<>(maximumQueueSize);
|
||||
mWeigher = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance that is bounded by the total weight of the queued elements instead of the element count,
|
||||
* for when element sizes vary widely, for example sample buffers from different tuners. An element is dropped
|
||||
* when adding it would exceed the maximum weight, except when nothing is queued, so that an element heavier than
|
||||
* the maximum is still delivered. Elements count toward the weight until the listener has processed them.
|
||||
*
|
||||
* @param threadName to name the dispatcher thread
|
||||
* @param interval for processing each batch in milliseconds.
|
||||
* @param weigher to calculate the weight of each element, for example a sample count.
|
||||
* @param maximumQueuedWeight for the queued elements.
|
||||
*/
|
||||
public Dispatcher(String threadName, long interval, ToLongFunction<E> weigher, long maximumQueuedWeight)
|
||||
{
|
||||
if(weigher == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Weigher cannot be null");
|
||||
}
|
||||
|
||||
mThreadName = threadName;
|
||||
mInterval = interval;
|
||||
mQueue = new LinkedTransferQueue<>();
|
||||
mWeigher = weigher;
|
||||
setMaximumQueuedWeight(maximumQueuedWeight);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum total weight of the queued elements, for a dispatcher that was constructed with a weigher.
|
||||
* @param maximumQueuedWeight for the queued elements.
|
||||
*/
|
||||
public void setMaximumQueuedWeight(long maximumQueuedWeight)
|
||||
{
|
||||
if(maximumQueuedWeight <= 0)
|
||||
{
|
||||
throw new IllegalArgumentException("Maximum queued weight must be positive: " + maximumQueuedWeight);
|
||||
}
|
||||
|
||||
mMaximumQueuedWeight = maximumQueuedWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,7 +195,27 @@ public class Dispatcher<E> implements Listener<E>
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean queued = mQueue.offer(e);
|
||||
boolean queued;
|
||||
|
||||
if(mWeigher != null)
|
||||
{
|
||||
long weight = mWeigher.applyAsLong(e);
|
||||
long queuedWeight = mQueuedWeight.addAndGet(weight);
|
||||
|
||||
if(queuedWeight > mMaximumQueuedWeight && queuedWeight > weight)
|
||||
{
|
||||
mQueuedWeight.addAndGet(-weight);
|
||||
queued = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
queued = mQueue.offer(e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
queued = mQueue.offer(e);
|
||||
}
|
||||
|
||||
if(!queued)
|
||||
{
|
||||
@@ -189,6 +255,7 @@ public class Dispatcher<E> implements Listener<E>
|
||||
}
|
||||
|
||||
mQueue.clear();
|
||||
mQueuedWeight.set(0);
|
||||
mExecutorService = Executors.newSingleThreadScheduledExecutor(new NamingThreadFactory(mThreadName));
|
||||
|
||||
Runnable r = (mHeartbeatManager != null ? new ProcessorWithHeartbeat() : new Processor());
|
||||
@@ -210,6 +277,7 @@ public class Dispatcher<E> implements Listener<E>
|
||||
mScheduledFuture.cancel(false);
|
||||
mScheduledFuture = null;
|
||||
mQueue.clear();
|
||||
mQueuedWeight.set(0);
|
||||
}
|
||||
|
||||
if(mExecutorService != null)
|
||||
@@ -259,6 +327,8 @@ public class Dispatcher<E> implements Listener<E>
|
||||
mListener.getClass() + "]", t);
|
||||
}
|
||||
}
|
||||
|
||||
release(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -294,6 +364,19 @@ public class Dispatcher<E> implements Listener<E>
|
||||
mListener.getClass() + "]", t);
|
||||
}
|
||||
}
|
||||
|
||||
release(element);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the weight of a dequeued element from the queued weight, once the element is processed.
|
||||
*/
|
||||
private void release(E element)
|
||||
{
|
||||
if(mWeigher != null)
|
||||
{
|
||||
mQueuedWeight.addAndGet(-mWeigher.applyAsLong(element));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user