Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
DeviceReadBuffer: Try to optimize some of the busy-waiting.
Wait for readThreshold or 20 milliseconds before returning to the stream
handler.

Move the throttling "sleep" from the stream handler into the DRB run loop.

Original patch by Rune Petersen.
Closes #11252
  • Loading branch information
jpoet committed Jan 27, 2013
1 parent 4e16c1a commit 4f0bfd9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
10 changes: 7 additions & 3 deletions mythtv/libs/libmythtv/recorders/DeviceReadBuffer.cpp
Expand Up @@ -34,7 +34,7 @@ DeviceReadBuffer::DeviceReadBuffer(

size(0), used(0),
read_quanta(0),
dev_read_size(0), min_read(0),
dev_read_size(0), readThreshold(0),

buffer(NULL), readPtr(NULL),
writePtr(NULL), endPtr(NULL),
Expand Down Expand Up @@ -96,7 +96,7 @@ bool DeviceReadBuffer::Setup(const QString &streamName, int streamfd,
dev_read_size = read_quanta * (using_poll ? 256 : 48);
dev_read_size = (deviceBufferSize) ?
min(dev_read_size, (size_t)deviceBufferSize) : dev_read_size;
min_read = read_quanta * 4;
readThreshold = read_quanta * 128;

buffer = new (nothrow) unsigned char[size + dev_read_size];
readPtr = buffer;
Expand Down Expand Up @@ -382,6 +382,10 @@ void DeviceReadBuffer::run(void)
memcpy(buffer, endPtr, writePtr + len - endPtr);
IncrWritePointer(len);
}

// Slow down reading if not under load
if (len < static_cast<int32_t>(dev_read_size / 2))
usleep(1000);
}

ClosePipes();
Expand Down Expand Up @@ -625,7 +629,7 @@ bool DeviceReadBuffer::CheckForErrors(
*/
uint DeviceReadBuffer::Read(unsigned char *buf, const uint count)
{
uint avail = WaitForUsed(min(count, (uint)dev_read_size), 20);
uint avail = WaitForUsed(min(count, (uint)readThreshold), 20);
size_t cnt = min(count, avail);

if (!cnt)
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/recorders/DeviceReadBuffer.h
Expand Up @@ -103,7 +103,7 @@ class DeviceReadBuffer : protected MThread
size_t used;
size_t read_quanta;
size_t dev_read_size;
size_t min_read;
size_t readThreshold;
unsigned char *buffer;
unsigned char *readPtr;
unsigned char *writePtr;
Expand Down
6 changes: 0 additions & 6 deletions mythtv/libs/libmythtv/recorders/asistreamhandler.cpp
Expand Up @@ -206,12 +206,6 @@ void ASIStreamHandler::run(void)
_error = true;
}

if ((0 == len) || (-1 == len))
{
usleep(100);
continue;
}

len += remainder;

if (len < 10) // 10 bytes = 4 bytes TS header + 6 bytes PES header
Expand Down
13 changes: 6 additions & 7 deletions mythtv/libs/libmythtv/recorders/dvbstreamhandler.cpp
Expand Up @@ -208,8 +208,7 @@ void DVBStreamHandler::RunTS(void)

if (drb)
{
len = drb->Read(
&(buffer[remainder]), buffer_size - remainder);
len = drb->Read(&(buffer[remainder]), buffer_size - remainder);

// Check for DRB errors
if (drb->IsErrored())
Expand Down Expand Up @@ -238,12 +237,12 @@ void DVBStreamHandler::RunTS(void)
len = read(dvr_fd, &(buffer[remainder]),
buffer_size - remainder);
}
}

if ((0 == len) || (-1 == len))
{
usleep(100);
continue;
if ((0 == len) || (-1 == len))
{
usleep(100);
continue;
}
}

len += remainder;
Expand Down

0 comments on commit 4f0bfd9

Please sign in to comment.