@@ -49,6 +49,54 @@ RecordThread::~RecordThread()
4949{
5050}
5151
52+ void RecordThread::adjustWriteThresholdsBasedOnBufferUsage (float bufferUsage)
53+ {
54+ // Dynamic adjustment strategy:
55+ // - Low usage (0-40%): Use larger batches for efficiency
56+ // - Medium usage (40-70%): Use moderate batches
57+ // - High usage (70-85%): Use smaller batches, write more frequently
58+ // - Critical usage (85%+): Minimize delay, write as soon as possible
59+
60+ int oldMin = m_minWriteSamples;
61+ int oldMax = m_maxWriteSamples;
62+
63+ if (bufferUsage < 0 .4f )
64+ {
65+ // Low buffer usage - optimize for disk efficiency with larger batches
66+ m_minWriteSamples = BLOCK_DEFAULT_MIN_WRITE_SAMPLES ;
67+ m_maxWriteSamples = BLOCK_DEFAULT_MAX_WRITE_SAMPLES ;
68+ }
69+ else if (bufferUsage < 0 .7f )
70+ {
71+ // Medium buffer usage - balanced approach
72+ m_minWriteSamples = BLOCK_DEFAULT_MIN_WRITE_SAMPLES / 2 ; // 256
73+ m_maxWriteSamples = BLOCK_DEFAULT_MAX_WRITE_SAMPLES / 2 ; // 2048
74+ }
75+ else if (bufferUsage < 0 .85f )
76+ {
77+ // High buffer usage - prioritize emptying buffer
78+ m_minWriteSamples = BLOCK_DEFAULT_MIN_WRITE_SAMPLES / 4 ; // 128
79+ m_maxWriteSamples = BLOCK_DEFAULT_MAX_WRITE_SAMPLES / 4 ; // 1024
80+ }
81+ else
82+ {
83+ // Critical buffer usage - write immediately with any available data
84+ m_minWriteSamples = 64 ; // Very low threshold
85+ m_maxWriteSamples = 512 ; // Smaller batches to reduce latency
86+ }
87+
88+ // Ensure max >= min to avoid deadlock
89+ if (m_maxWriteSamples < m_minWriteSamples)
90+ m_maxWriteSamples = m_minWriteSamples;
91+
92+ // Log threshold changes to help monitor adaptive behavior
93+ if (oldMin != m_minWriteSamples || oldMax != m_maxWriteSamples)
94+ {
95+ LOGC (" Buffer usage: " , int (bufferUsage * 100 ), " % - Adjusted write thresholds: MIN=" ,
96+ m_minWriteSamples, " MAX=" , m_maxWriteSamples);
97+ }
98+ }
99+
52100void RecordThread::setEngine (RecordEngine* engine)
53101{
54102 m_engine = engine;
@@ -170,7 +218,28 @@ void RecordThread::run()
170218
171219 // 4 - Normal loop
172220 while (! threadShouldExit ())
221+ {
222+ // Calculate maximum buffer usage across all streams to adjust write strategy
223+ float maxBufferUsage = 0 .0f ;
224+ for (int streamIdx = 0 ; streamIdx < numStreams; streamIdx++)
225+ {
226+ DataQueue* queue = (*m_dataQueues)[streamIdx];
227+ if (queue != nullptr )
228+ {
229+ // Calculate buffer usage based on samples ready vs total capacity
230+ int samplesReady = queue->getNumSamplesReady ();
231+ int totalCapacity = queue->getBlockSize () * DATA_BUFFER_NBLOCKS ;
232+ float usage = (float ) samplesReady / (float ) totalCapacity;
233+ if (usage > maxBufferUsage)
234+ maxBufferUsage = usage;
235+ }
236+ }
237+
238+ // Dynamically adjust write thresholds based on buffer fullness
239+ adjustWriteThresholdsBasedOnBufferUsage (maxBufferUsage);
240+
173241 writeData (m_minWriteSamples, m_maxWriteSamples, BLOCK_MAX_WRITE_EVENTS , BLOCK_MAX_WRITE_SPIKES );
242+ }
174243
175244 // LOGD(__FUNCTION__, " Exiting record thread");
176245 // 5 - Before closing the thread, try to write the remaining samples
0 commit comments