2007-10-05 21:00:44 +02:00
|
|
|
/*
|
|
|
|
* statistics.c: IPTV plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
2007-10-08 18:24:48 +02:00
|
|
|
* $Id: statistics.c,v 1.13 2007/10/08 16:24:48 rahrenbe Exp $
|
2007-10-05 21:00:44 +02:00
|
|
|
*/
|
|
|
|
|
2007-10-07 21:06:33 +02:00
|
|
|
#include <limits.h>
|
|
|
|
|
2007-10-05 21:00:44 +02:00
|
|
|
#include "common.h"
|
|
|
|
#include "statistics.h"
|
2007-10-07 21:06:33 +02:00
|
|
|
#include "config.h"
|
2007-10-05 21:00:44 +02:00
|
|
|
|
|
|
|
// Section statistic class
|
|
|
|
cIptvSectionStatistics::cIptvSectionStatistics()
|
2007-10-07 00:15:02 +02:00
|
|
|
: filteredData(0),
|
2007-10-07 21:06:33 +02:00
|
|
|
numberOfCalls(0),
|
2007-10-08 18:24:48 +02:00
|
|
|
timer(),
|
|
|
|
mutex()
|
2007-10-05 21:00:44 +02:00
|
|
|
{
|
2007-10-06 00:23:56 +02:00
|
|
|
//debug("cIptvSectionStatistics::cIptvSectionStatistics()\n");
|
2007-10-05 21:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cIptvSectionStatistics::~cIptvSectionStatistics()
|
|
|
|
{
|
2007-10-06 00:23:56 +02:00
|
|
|
//debug("cIptvSectionStatistics::~cIptvSectionStatistics()\n");
|
2007-10-05 21:00:44 +02:00
|
|
|
}
|
|
|
|
|
2007-10-07 21:06:33 +02:00
|
|
|
cString cIptvSectionStatistics::GetStatistic()
|
2007-10-05 21:00:44 +02:00
|
|
|
{
|
2007-10-08 00:54:09 +02:00
|
|
|
//debug("cIptvSectionStatistics::GetStatistic()\n");
|
2007-10-08 18:24:48 +02:00
|
|
|
mutex.Lock();
|
|
|
|
long tmpNumberOfCalls = numberOfCalls;
|
|
|
|
long tmpFilteredData = filteredData;
|
|
|
|
filteredData = numberOfCalls = 0;
|
2007-10-07 21:06:33 +02:00
|
|
|
uint64_t elapsed = timer.Elapsed();
|
|
|
|
timer.Set();
|
2007-10-08 18:24:48 +02:00
|
|
|
mutex.Unlock();
|
2007-10-07 21:06:33 +02:00
|
|
|
float divider = elapsed / 1000;
|
|
|
|
char unit[] = { ' ', 'B', '/', 's', '\0' };
|
2007-10-07 22:08:44 +02:00
|
|
|
if (IptvConfig.IsStatsUnitInKilos()) {
|
2007-10-07 21:06:33 +02:00
|
|
|
divider *= KILOBYTE(1);
|
|
|
|
unit[0] = 'k';
|
2007-10-07 22:08:44 +02:00
|
|
|
}
|
|
|
|
if (!IptvConfig.IsStatsUnitInBytes()) {
|
2007-10-07 21:29:09 +02:00
|
|
|
divider /= sizeof(unsigned short) * 8;
|
2007-10-07 21:06:33 +02:00
|
|
|
unit[1] = 'b';
|
2007-10-07 22:08:44 +02:00
|
|
|
}
|
2007-10-08 18:24:48 +02:00
|
|
|
cString info = cString::sprintf("%4ld (%4ld %s)", tmpNumberOfCalls, divider ?
|
|
|
|
(long)(tmpFilteredData / divider) : 0L, unit);
|
2007-10-08 00:54:09 +02:00
|
|
|
return info;
|
2007-10-05 21:00:44 +02:00
|
|
|
}
|
|
|
|
|
2007-10-08 18:24:48 +02:00
|
|
|
void cIptvSectionStatistics::AddStatistic(long Bytes, long Calls)
|
|
|
|
{
|
|
|
|
//debug("cIptvSectionStatistics::AddStatistic(Bytes=%ld, Calls=%ld)\n", Bytes, Calls);
|
|
|
|
mutex.Lock();
|
|
|
|
filteredData += Bytes;
|
|
|
|
numberOfCalls += Calls;
|
|
|
|
mutex.Unlock();
|
|
|
|
}
|
|
|
|
|
2007-10-07 00:15:02 +02:00
|
|
|
// --- cIptvDeviceStatistics -------------------------------------------------
|
|
|
|
|
2007-10-05 21:00:44 +02:00
|
|
|
// Device statistic class
|
|
|
|
cIptvDeviceStatistics::cIptvDeviceStatistics()
|
2007-10-07 21:06:33 +02:00
|
|
|
: dataBytes(0),
|
2007-10-08 18:24:48 +02:00
|
|
|
timer(),
|
|
|
|
mutex()
|
2007-10-05 21:00:44 +02:00
|
|
|
{
|
|
|
|
debug("cIptvDeviceStatistics::cIptvDeviceStatistics()\n");
|
|
|
|
memset(mostActivePids, '\0', sizeof(mostActivePids));
|
|
|
|
}
|
|
|
|
|
|
|
|
cIptvDeviceStatistics::~cIptvDeviceStatistics()
|
|
|
|
{
|
|
|
|
debug("cIptvDeviceStatistics::~cIptvDeviceStatistics()\n");
|
|
|
|
}
|
|
|
|
|
2007-10-07 21:06:33 +02:00
|
|
|
cString cIptvDeviceStatistics::GetStatistic()
|
2007-10-05 21:00:44 +02:00
|
|
|
{
|
2007-10-08 00:54:09 +02:00
|
|
|
//debug("cIptvDeviceStatistics::GetStatistic()\n");
|
2007-10-08 18:24:48 +02:00
|
|
|
mutex.Lock();
|
2007-10-07 00:15:02 +02:00
|
|
|
long tmpDataBytes = dataBytes;
|
2007-10-08 18:24:48 +02:00
|
|
|
dataBytes = 0;
|
|
|
|
pidStruct tmpMostActivePids[IPTV_STATS_ACTIVE_PIDS_COUNT];
|
|
|
|
memcpy(&tmpMostActivePids, &mostActivePids, sizeof(tmpMostActivePids));
|
|
|
|
memset(&mostActivePids, '\0', sizeof(mostActivePids));
|
2007-10-07 21:06:33 +02:00
|
|
|
uint64_t elapsed = timer.Elapsed();
|
|
|
|
timer.Set();
|
2007-10-08 18:24:48 +02:00
|
|
|
mutex.Unlock();
|
2007-10-07 21:06:33 +02:00
|
|
|
float divider = elapsed / 1000;
|
|
|
|
char unit[] = { ' ', 'B', '/', 's', '\0' };
|
2007-10-07 22:08:44 +02:00
|
|
|
if (IptvConfig.IsStatsUnitInKilos()) {
|
2007-10-07 21:06:33 +02:00
|
|
|
divider *= KILOBYTE(1);
|
|
|
|
unit[0] = 'k';
|
2007-10-07 22:08:44 +02:00
|
|
|
}
|
|
|
|
if (!IptvConfig.IsStatsUnitInBytes()) {
|
2007-10-07 21:29:09 +02:00
|
|
|
divider /= sizeof(unsigned short) * 8;
|
2007-10-07 21:06:33 +02:00
|
|
|
unit[1] = 'b';
|
2007-10-07 22:08:44 +02:00
|
|
|
}
|
2007-10-08 00:54:09 +02:00
|
|
|
cString info = cString::sprintf("Bitrate: %ld %s\n", divider ?
|
|
|
|
(long)(tmpDataBytes / divider) : 0L, unit);
|
|
|
|
for (unsigned int i = 0; i < IPTV_STATS_ACTIVE_PIDS_COUNT; ++i) {
|
2007-10-08 18:24:48 +02:00
|
|
|
if (tmpMostActivePids[i].pid)
|
2007-10-08 00:54:09 +02:00
|
|
|
info = cString::sprintf("%sPid %d: %4d (%4ld %s)%c", *info, i,
|
2007-10-08 18:24:48 +02:00
|
|
|
tmpMostActivePids[i].pid,
|
|
|
|
(long)(tmpMostActivePids[i].DataAmount / divider),
|
2007-10-08 00:54:09 +02:00
|
|
|
unit, ((i + 1) % 2) ? '\t' : '\n');
|
|
|
|
}
|
2007-10-08 14:30:53 +02:00
|
|
|
if (!endswith(*info, "\n"))
|
|
|
|
info = cString::sprintf("%s%c", *info, '\n');
|
2007-10-08 00:54:09 +02:00
|
|
|
return info;
|
2007-10-05 21:00:44 +02:00
|
|
|
}
|
|
|
|
|
2007-10-08 14:25:30 +02:00
|
|
|
int cIptvDeviceStatistics::SortPids(const void* data1, const void* data2)
|
2007-10-05 21:00:44 +02:00
|
|
|
{
|
2007-10-08 14:25:30 +02:00
|
|
|
//debug("cIptvDeviceStatistics::SortPids()\n");
|
2007-10-05 21:00:44 +02:00
|
|
|
pidStruct *comp1 = (pidStruct*)data1;
|
|
|
|
pidStruct *comp2 = (pidStruct*)data2;
|
|
|
|
if (comp1->DataAmount > comp2->DataAmount)
|
|
|
|
return -1;
|
2007-10-07 10:46:34 +02:00
|
|
|
if (comp1->DataAmount < comp2->DataAmount)
|
|
|
|
return 1;
|
2007-10-05 21:00:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-08 18:24:48 +02:00
|
|
|
void cIptvDeviceStatistics::AddStatistic(long Bytes, u_short pid, long payload)
|
2007-10-05 21:00:44 +02:00
|
|
|
{
|
2007-10-08 18:24:48 +02:00
|
|
|
//debug("cIptvDeviceStatistics::AddStatistic(Bytes=%ld, pid=%ld, payload=%ld)\n", Bytes, pid, payload);
|
|
|
|
mutex.Lock();
|
|
|
|
dataBytes += Bytes;
|
2007-10-05 21:00:44 +02:00
|
|
|
const int numberOfElements = sizeof(mostActivePids) / sizeof(pidStruct);
|
|
|
|
// If our statistic already is in the array, update it and quit
|
|
|
|
for (int i = 0; i < numberOfElements; ++i) {
|
2007-10-07 00:15:02 +02:00
|
|
|
if (mostActivePids[i].pid == pid) {
|
|
|
|
mostActivePids[i].DataAmount += payload;
|
|
|
|
// Now re-sort the array and quit
|
2007-10-08 14:25:30 +02:00
|
|
|
qsort(&mostActivePids, numberOfElements, sizeof(pidStruct), SortPids);
|
2007-10-07 00:15:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-10-05 21:00:44 +02:00
|
|
|
// Apparently our pid isn't in the array. Replace the last element with this
|
|
|
|
// one if new payload is greater
|
|
|
|
if (mostActivePids[numberOfElements - 1].DataAmount < payload) {
|
2007-10-07 00:15:02 +02:00
|
|
|
mostActivePids[numberOfElements - 1].pid = pid;
|
|
|
|
mostActivePids[numberOfElements - 1].DataAmount = payload;
|
2007-10-06 00:30:14 +02:00
|
|
|
// Re-sort
|
2007-10-08 14:25:30 +02:00
|
|
|
qsort(&mostActivePids, numberOfElements, sizeof(pidStruct), SortPids);
|
2007-10-06 00:30:14 +02:00
|
|
|
}
|
2007-10-08 18:24:48 +02:00
|
|
|
mutex.Unlock();
|
2007-10-05 21:00:44 +02:00
|
|
|
}
|
|
|
|
|
2007-10-07 00:15:02 +02:00
|
|
|
// --- cIptvStreamerStatistics -----------------------------------------------
|
2007-10-05 21:00:44 +02:00
|
|
|
|
|
|
|
// Streamer statistic class
|
|
|
|
cIptvStreamerStatistics::cIptvStreamerStatistics()
|
2007-10-07 21:06:33 +02:00
|
|
|
: dataBytes(0),
|
2007-10-08 18:24:48 +02:00
|
|
|
timer(),
|
|
|
|
mutex()
|
2007-10-05 21:00:44 +02:00
|
|
|
{
|
|
|
|
debug("cIptvStreamerStatistics::cIptvStreamerStatistics()\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
cIptvStreamerStatistics::~cIptvStreamerStatistics()
|
|
|
|
{
|
|
|
|
debug("cIptvStreamerStatistics::~cIptvStreamerStatistics()\n");
|
|
|
|
}
|
|
|
|
|
2007-10-07 21:06:33 +02:00
|
|
|
cString cIptvStreamerStatistics::GetStatistic()
|
2007-10-05 21:00:44 +02:00
|
|
|
{
|
2007-10-08 00:54:09 +02:00
|
|
|
//debug("cIptvStreamerStatistics::GetStatistic()\n");
|
2007-10-08 18:24:48 +02:00
|
|
|
mutex.Lock();
|
|
|
|
long tmpDataBytes = dataBytes;
|
|
|
|
dataBytes = 0;
|
2007-10-07 21:06:33 +02:00
|
|
|
uint64_t elapsed = timer.Elapsed();
|
|
|
|
timer.Set();
|
2007-10-08 18:24:48 +02:00
|
|
|
mutex.Unlock();
|
2007-10-07 21:06:33 +02:00
|
|
|
float divider = elapsed / 1000;
|
|
|
|
char unit[] = { ' ', 'B', '/', 's', '\0' };
|
2007-10-07 22:08:44 +02:00
|
|
|
if (IptvConfig.IsStatsUnitInKilos()) {
|
2007-10-07 21:06:33 +02:00
|
|
|
divider *= KILOBYTE(1);
|
|
|
|
unit[0] = 'k';
|
2007-10-07 22:08:44 +02:00
|
|
|
}
|
|
|
|
if (!IptvConfig.IsStatsUnitInBytes()) {
|
2007-10-07 21:29:09 +02:00
|
|
|
divider /= sizeof(unsigned short) * 8;
|
2007-10-07 21:06:33 +02:00
|
|
|
unit[1] = 'b';
|
2007-10-07 22:08:44 +02:00
|
|
|
}
|
2007-10-08 18:24:48 +02:00
|
|
|
return cString::sprintf("Streamer: %ld %s", divider ?
|
|
|
|
(long)(tmpDataBytes / divider) : 0L, unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cIptvStreamerStatistics::AddStatistic(long Bytes)
|
|
|
|
{
|
|
|
|
//debug("cIptvStreamerStatistics::AddStatistic(Bytes=%ld)\n", Bytes);
|
|
|
|
mutex.Lock();
|
|
|
|
dataBytes += Bytes;
|
|
|
|
mutex.Unlock();
|
2007-10-05 21:00:44 +02:00
|
|
|
}
|