1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00
vdr/ringbuffer.c

505 lines
11 KiB
C
Raw Permalink Normal View History

/*
* ringbuffer.c: A ring buffer
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* Parts of this file were inspired by the 'ringbuffy.c' from the
* LinuxDVB driver (see linuxtv.org).
*
* $Id: ringbuffer.c 4.2 2017/03/19 12:43:36 kls Exp $
*/
#include "ringbuffer.h"
2002-07-28 12:48:44 +02:00
#include <stdlib.h>
#include <unistd.h>
#include "tools.h"
// --- cRingBuffer -----------------------------------------------------------
#define OVERFLOWREPORTDELTA 5 // seconds between reports
2004-10-16 09:36:28 +02:00
#define PERCENTAGEDELTA 10
#define PERCENTAGETHRESHOLD 70
#define IOTHROTTLELOW 20
#define IOTHROTTLEHIGH 50
cRingBuffer::cRingBuffer(int Size, bool Statistics)
{
size = Size;
statistics = Statistics;
2004-10-16 09:36:28 +02:00
getThreadTid = 0;
maxFill = 0;
lastPercent = 0;
putTimeout = getTimeout = 0;
lastOverflowReport = 0;
overflowCount = overflowBytes = 0;
ioThrottle = NULL;
2001-08-05 12:23:24 +02:00
}
cRingBuffer::~cRingBuffer()
{
delete ioThrottle;
2001-08-05 12:23:24 +02:00
if (statistics)
2002-05-13 16:35:49 +02:00
dsyslog("buffer stats: %d (%d%%) used", maxFill, maxFill * 100 / (size - 1));
2001-08-05 12:23:24 +02:00
}
2004-10-16 09:36:28 +02:00
void cRingBuffer::UpdatePercentage(int Fill)
{
2004-10-16 09:36:28 +02:00
if (Fill > maxFill)
maxFill = Fill;
int percent = Fill * 100 / (Size() - 1) / PERCENTAGEDELTA * PERCENTAGEDELTA; // clamp down to nearest quantum
2004-10-16 09:36:28 +02:00
if (percent != lastPercent) {
if (percent >= PERCENTAGETHRESHOLD && percent > lastPercent || percent < PERCENTAGETHRESHOLD && lastPercent >= PERCENTAGETHRESHOLD) {
dsyslog("buffer usage: %d%% (tid=%d)", percent, getThreadTid);
2004-10-16 09:36:28 +02:00
lastPercent = percent;
}
}
if (ioThrottle) {
if (percent >= IOTHROTTLEHIGH)
ioThrottle->Activate();
else if (percent < IOTHROTTLELOW)
ioThrottle->Release();
}
}
2004-10-16 09:36:28 +02:00
void cRingBuffer::WaitForPut(void)
{
if (putTimeout)
readyForPut.Wait(putTimeout);
}
void cRingBuffer::WaitForGet(void)
{
2004-10-16 09:36:28 +02:00
if (getTimeout)
readyForGet.Wait(getTimeout);
}
void cRingBuffer::EnablePut(void)
{
if (putTimeout && Free() > Size() / 10)
2004-10-16 09:36:28 +02:00
readyForPut.Signal();
}
void cRingBuffer::EnableGet(void)
{
if (getTimeout && Available() > Size() / 10)
2004-10-16 09:36:28 +02:00
readyForGet.Signal();
}
void cRingBuffer::SetTimeouts(int PutTimeout, int GetTimeout)
{
putTimeout = PutTimeout;
getTimeout = GetTimeout;
}
void cRingBuffer::SetIoThrottle(void)
{
if (!ioThrottle)
ioThrottle = new cIoThrottle;
}
void cRingBuffer::ReportOverflow(int Bytes)
{
overflowCount++;
overflowBytes += Bytes;
if (time(NULL) - lastOverflowReport > OVERFLOWREPORTDELTA) {
esyslog("ERROR: %d ring buffer overflow%s (%d bytes dropped)", overflowCount, overflowCount > 1 ? "s" : "", overflowBytes);
overflowCount = overflowBytes = 0;
lastOverflowReport = time(NULL);
}
}
// --- cRingBufferLinear -----------------------------------------------------
2001-08-05 12:23:24 +02:00
2004-10-16 09:36:28 +02:00
#ifdef DEBUGRINGBUFFERS
#define MAXRBLS 30
#define DEBUGRBLWIDTH 45
cRingBufferLinear *cRingBufferLinear::RBLS[MAXRBLS] = { NULL };
void cRingBufferLinear::AddDebugRBL(cRingBufferLinear *RBL)
{
for (int i = 0; i < MAXRBLS; i++) {
if (!RBLS[i]) {
RBLS[i] = RBL;
break;
}
}
}
void cRingBufferLinear::DelDebugRBL(cRingBufferLinear *RBL)
{
for (int i = 0; i < MAXRBLS; i++) {
if (RBLS[i] == RBL) {
RBLS[i] = NULL;
break;
}
}
}
void cRingBufferLinear::PrintDebugRBL(void)
{
bool printed = false;
for (int i = 0; i < MAXRBLS; i++) {
cRingBufferLinear *p = RBLS[i];
if (p) {
printed = true;
int lh = p->lastHead;
int lt = p->lastTail;
int h = lh * DEBUGRBLWIDTH / p->Size();
int t = lt * DEBUGRBLWIDTH / p->Size();
char buf[DEBUGRBLWIDTH + 10];
memset(buf, '-', DEBUGRBLWIDTH);
if (lt <= lh)
memset(buf + t, '*', max(h - t, 1));
else {
memset(buf, '*', h);
memset(buf + t, '*', DEBUGRBLWIDTH - t);
}
buf[t] = '<';
buf[h] = '>';
buf[DEBUGRBLWIDTH] = 0;
printf("%2d %s %8d %8d %s\n", i, buf, p->lastPut, p->lastGet, p->description);
}
}
if (printed)
printf("\n");
}
#endif
cRingBufferLinear::cRingBufferLinear(int Size, int Margin, bool Statistics, const char *Description)
2001-08-05 12:23:24 +02:00
:cRingBuffer(Size, Statistics)
{
2004-10-16 09:36:28 +02:00
description = Description ? strdup(Description) : NULL;
tail = head = margin = Margin;
gotten = 0;
2001-08-05 12:23:24 +02:00
buffer = NULL;
if (Size > 1) { // 'Size - 1' must not be 0!
2004-10-16 09:36:28 +02:00
if (Margin <= Size / 2) {
buffer = MALLOC(uchar, Size);
if (!buffer)
esyslog("ERROR: can't allocate ring buffer (size=%d)", Size);
Clear();
}
else
esyslog("ERROR: invalid margin for ring buffer (%d > %d)", Margin, Size / 2);
}
else
esyslog("ERROR: invalid size for ring buffer (%d)", Size);
2004-10-16 09:36:28 +02:00
#ifdef DEBUGRINGBUFFERS
lastHead = head;
lastTail = tail;
lastPut = lastGet = -1;
AddDebugRBL(this);
#endif
}
2001-08-05 12:23:24 +02:00
cRingBufferLinear::~cRingBufferLinear()
{
2004-10-16 09:36:28 +02:00
#ifdef DEBUGRINGBUFFERS
DelDebugRBL(this);
#endif
free(buffer);
2004-10-16 09:36:28 +02:00
free(description);
}
int cRingBufferLinear::DataReady(const uchar *Data, int Count)
{
return Count >= margin ? Count : 0;
}
2001-08-05 12:23:24 +02:00
int cRingBufferLinear::Available(void)
{
int diff = head - tail;
return (diff >= 0) ? diff : Size() + diff - margin;
}
2001-08-05 12:23:24 +02:00
void cRingBufferLinear::Clear(void)
{
int Head = head;
tail = Head;
2004-10-16 09:36:28 +02:00
#ifdef DEBUGRINGBUFFERS
lastHead = Head;
2004-10-16 09:36:28 +02:00
lastTail = tail;
lastPut = lastGet = -1;
#endif
maxFill = 0;
EnablePut();
2004-10-16 09:36:28 +02:00
}
int cRingBufferLinear::Read(int FileHandle, int Max)
{
int Tail = tail;
int diff = Tail - head;
int free = (diff > 0) ? diff - 1 : Size() - head;
if (Tail <= margin)
free--;
int Count = -1;
errno = EAGAIN;
2004-10-16 09:36:28 +02:00
if (free > 0) {
if (0 < Max && Max < free)
free = Max;
Count = safe_read(FileHandle, buffer + head, free);
if (Count > 0) {
int Head = head + Count;
if (Head >= Size())
Head = margin;
head = Head;
if (statistics) {
int fill = head - Tail;
if (fill < 0)
fill = Size() + fill;
else if (fill >= Size())
fill = Size() - 1;
UpdatePercentage(fill);
}
}
}
#ifdef DEBUGRINGBUFFERS
lastHead = head;
lastPut = Count;
#endif
EnableGet();
2004-10-16 09:36:28 +02:00
if (free == 0)
WaitForPut();
return Count;
}
int cRingBufferLinear::Read(cUnbufferedFile *File, int Max)
{
int Tail = tail;
int diff = Tail - head;
int free = (diff > 0) ? diff - 1 : Size() - head;
if (Tail <= margin)
free--;
int Count = -1;
errno = EAGAIN;
if (free > 0) {
if (0 < Max && Max < free)
free = Max;
Count = File->Read(buffer + head, free);
if (Count > 0) {
int Head = head + Count;
if (Head >= Size())
Head = margin;
head = Head;
if (statistics) {
int fill = head - Tail;
if (fill < 0)
fill = Size() + fill;
else if (fill >= Size())
fill = Size() - 1;
UpdatePercentage(fill);
}
}
}
#ifdef DEBUGRINGBUFFERS
lastHead = head;
lastPut = Count;
#endif
EnableGet();
if (free == 0)
WaitForPut();
return Count;
}
2001-08-05 12:23:24 +02:00
int cRingBufferLinear::Put(const uchar *Data, int Count)
{
if (Count > 0) {
2004-10-16 09:36:28 +02:00
int Tail = tail;
2001-08-05 12:23:24 +02:00
int rest = Size() - head;
2004-10-16 09:36:28 +02:00
int diff = Tail - head;
int free = ((Tail < margin) ? rest : (diff > 0) ? diff : Size() + diff - margin) - 1;
if (statistics) {
2001-08-05 12:23:24 +02:00
int fill = Size() - free - 1 + Count;
if (fill >= Size())
fill = Size() - 1;
2004-10-16 09:36:28 +02:00
UpdatePercentage(fill);
}
if (free > 0) {
if (free < Count)
Count = free;
if (Count >= rest) {
memcpy(buffer + head, Data, rest);
if (Count - rest)
memcpy(buffer + margin, Data + rest, Count - rest);
head = margin + Count - rest;
}
else {
memcpy(buffer + head, Data, Count);
head += Count;
}
}
else
Count = 0;
2004-10-16 09:36:28 +02:00
#ifdef DEBUGRINGBUFFERS
lastHead = head;
lastPut = Count;
#endif
EnableGet();
if (Count == 0)
WaitForPut();
}
return Count;
}
uchar *cRingBufferLinear::Get(int &Count)
{
2004-10-16 09:36:28 +02:00
int Head = head;
if (getThreadTid <= 0)
getThreadTid = cThread::ThreadId();
int rest = Size() - tail;
2004-10-16 09:36:28 +02:00
if (rest < margin && Head < tail) {
int t = margin - rest;
memcpy(buffer + t, buffer + tail, rest);
tail = t;
2004-10-16 09:36:28 +02:00
rest = Head - tail;
}
2004-10-16 09:36:28 +02:00
int diff = Head - tail;
int cont = (diff >= 0) ? diff : Size() + diff - margin;
if (cont > rest)
cont = rest;
uchar *p = buffer + tail;
if ((cont = DataReady(p, cont)) > 0) {
2004-10-16 09:36:28 +02:00
Count = gotten = cont;
return p;
}
WaitForGet();
return NULL;
}
void cRingBufferLinear::Del(int Count)
{
2004-10-16 09:36:28 +02:00
if (Count > gotten) {
esyslog("ERROR: invalid Count in cRingBufferLinear::Del: %d (limited to %d)", Count, gotten);
Count = gotten;
}
if (Count > 0) {
int Tail = tail;
Tail += Count;
gotten -= Count;
if (Tail >= Size())
Tail = margin;
tail = Tail;
EnablePut();
}
2004-10-16 09:36:28 +02:00
#ifdef DEBUGRINGBUFFERS
lastTail = tail;
lastGet = Count;
#endif
}
2001-08-05 12:23:24 +02:00
// --- cFrame ----------------------------------------------------------------
cFrame::cFrame(const uchar *Data, int Count, eFrameType Type, int Index, uint32_t Pts, bool Independent)
{
count = abs(Count);
type = Type;
2001-08-05 12:23:24 +02:00
index = Index;
2009-03-13 14:45:12 +01:00
pts = Pts;
independent = Type == ftAudio ? true : Independent;
if (Count < 0)
data = (uchar *)Data;
else {
data = MALLOC(uchar, count);
if (data)
memcpy(data, Data, count);
else
esyslog("ERROR: can't allocate frame buffer (count=%d)", count);
}
2001-08-05 12:23:24 +02:00
next = NULL;
}
cFrame::~cFrame()
{
free(data);
2001-08-05 12:23:24 +02:00
}
// --- cRingBufferFrame ------------------------------------------------------
cRingBufferFrame::cRingBufferFrame(int Size, bool Statistics)
2001-08-05 12:23:24 +02:00
:cRingBuffer(Size, Statistics)
{
head = NULL;
currentFill = 0;
}
cRingBufferFrame::~cRingBufferFrame()
{
Clear();
}
void cRingBufferFrame::Clear(void)
{
Lock();
cFrame *p;
while ((p = Get()) != NULL)
2001-08-05 12:23:24 +02:00
Drop(p);
Unlock();
EnablePut();
EnableGet();
2001-08-05 12:23:24 +02:00
}
bool cRingBufferFrame::Put(cFrame *Frame)
{
if (Frame->Count() <= Free()) {
Lock();
if (head) {
Frame->next = head->next;
head->next = Frame;
head = Frame;
}
2001-08-05 12:23:24 +02:00
else {
head = Frame->next = Frame;
}
currentFill += Frame->Count();
Unlock();
EnableGet();
2001-08-05 12:23:24 +02:00
return true;
}
2001-08-05 12:23:24 +02:00
return false;
}
cFrame *cRingBufferFrame::Get(void)
{
2001-08-05 12:23:24 +02:00
Lock();
cFrame *p = head ? head->next : NULL;
Unlock();
return p;
}
void cRingBufferFrame::Delete(cFrame *Frame)
{
2001-08-05 12:23:24 +02:00
currentFill -= Frame->Count();
delete Frame;
}
void cRingBufferFrame::Drop(cFrame *Frame)
2001-08-05 12:23:24 +02:00
{
Lock();
if (head) {
if (Frame == head->next) {
if (head->next != head) {
head->next = Frame->next;
Delete(Frame);
}
else {
Delete(head);
head = NULL;
}
}
else
2002-05-13 16:35:49 +02:00
esyslog("ERROR: attempt to drop wrong frame from ring buffer!");
2001-08-05 12:23:24 +02:00
}
Unlock();
EnablePut();
}
2001-08-05 12:23:24 +02:00
int cRingBufferFrame::Available(void)
{
Lock();
int av = currentFill;
Unlock();
return av;
}