Skip to content

Commit

Permalink
Moved uptime and memory usage stat lookup code from statusbox.cpp to …
Browse files Browse the repository at this point in the history
…util.cpp.

Added three query strings and RemoteGet* routines to query the
backend for these stats, along with HandleQuery* routines to
handle them (and call util.cpp) in the backend's mainserver.cpp
statusbox.cpp now calls the RemoteGet routines
if frontend is not running on backend machine


git-svn-id: http://svn.mythtv.org/svn/trunk@5019 7dbf422c-18fa-0310-86e9-fd20926502f2
  • Loading branch information
NigelPearson committed Dec 30, 2004
1 parent 97cb59b commit 420af85
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 152 deletions.
133 changes: 133 additions & 0 deletions mythtv/libs/libmyth/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
#include <unistd.h>
#include <fcntl.h>

#ifdef __linux__
#include <sys/statvfs.h>
#include <sys/sysinfo.h>
#endif
#if defined(__FreeBSD__) || defined(CONFIG_DARWIN)
#include <sys/sysctl.h>
#include <sys/param.h>
#include <sys/mount.h>
#endif
#ifdef CONFIG_DARWIN
#include <mach/mach.h>
#endif

#include <iostream>
using namespace std;

Expand Down Expand Up @@ -744,3 +757,123 @@ QString longLongToString(long long ll)
str[20] = '\0';
return str;
}

bool getUptime(time_t &uptime)
{
#ifdef __linux__
struct sysinfo sinfo;
if (sysinfo(&sinfo) == -1)
{
VERBOSE(VB_ALL, "sysinfo() error");
return false;
}
else
uptime = sinfo.uptime;

#elif defined(__FreeBSD__) || defined(CONFIG_DARWIN)

int mib[2];
struct timeval bootTime;
size_t len;

// Uptime is calculated. Get this machine's boot time
// and subtract it from the current machine time
len = sizeof(bootTime);
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
if (sysctl(mib, 2, &bootTime, &len, NULL, 0) == -1)
{
VERBOSE(VB_ALL, "sysctl() error");
return false;
}
else
uptime = time(NULL) - bootTime.tv_sec;

#else
// Hmmm. Not Linux, not FreeBSD or Darwin. What else is there :-)
VERBOSE(VB_ALL, "Unknown platform. How do I get the uptime?");
return false;
#endif

return true;
}

#define MB (1024*1024)

static bool diskUsage(const char *fs,
double &total, double &used, double &free)
{
// stat the file system

#ifdef __linux__
#define STAT statvfs
#elif defined(__FreeBSD__) || defined(CONFIG_DARWIN)
#define STAT statfs
#endif

struct STAT sbuff;
if (STAT(fs, &sbuff) == -1)
return false;

// see http://en.wikipedia.org/wiki/Megabyte

total = (double)sbuff.f_blocks * sbuff.f_bsize / MB;
free = (double)sbuff.f_bfree * sbuff.f_bsize / MB;
used = total - free;
return true;
}

bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
{
#ifdef __linux__
struct sysinfo sinfo;
if (sysinfo(&sinfo) == -1)
{
VERBOSE(VB_ALL, "sysinfo() error");
return false;
}
else
totalMB = sinfo.totalram/MB,
freeMB = sinfo.freeram/MB,
totalVM = sinfo.totalswap/MB,
freeVM = sinfo.freeswap/MB;

#elif defined(CONFIG_DARWIN)
mach_port_t mp;
mach_msg_type_number_t count, pageSize;
vm_statistics_data_t s;

mp = mach_host_self();

// VM page size
if (host_page_size(mp, &pageSize) != KERN_SUCCESS)
pageSize = 4096; // If we can't look it up, 4K is a good guess

count = HOST_VM_INFO_COUNT;
if (host_statistics(mp, HOST_VM_INFO,
(host_info_t)&s, &count) != KERN_SUCCESS)
{
VERBOSE(VB_ALL, "Failed to get vm statistics.");
return false;
}

pageSize >>= 10; // This gives usages in KB
totalMB = (s.active_count + s.inactive_count
+ s.wire_count + s.free_count) * pageSize / 1024;
freeMB = s.free_count * pageSize / 1024;


// This is a real hack. I have not found a way to ask the kernel how much
// swap it is using, and the dynamic_pager daemon doesn't even seem to be
// able to report what filesystem it is using for the swapfiles. So, we do:
double total, used, free;
diskUsage("/private/var/vm", total, used, free);
totalVM = (int)total, freeVM = (int)free;

#else
VERBOSE(VB_ALL, "Unknown platform. How do I get the memory stats?");
return false;
#endif

return true;
}
4 changes: 4 additions & 0 deletions mythtv/libs/libmyth/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ QString cutDownString(QString text, QFont *testFont, int maxwidth);

long long stringToLongLong(const QString &str);
QString longLongToString(long long ll);

bool diskUsage(const char *path, double &total, double &used, double &free);
bool getUptime(time_t &uptime);
bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM);
#endif
51 changes: 51 additions & 0 deletions mythtv/libs/libmythtv/remoteutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,57 @@ void RemoteGetFreeSpace(int &totalspace, int &usedspace)
}
}

bool RemoteGetLoad(float load[3])
{
QStringList strlist = QString("QUERY_LOAD");

if (gContext->SendReceiveStringList(strlist))
{
load[0] = strlist[0].toFloat();
load[1] = strlist[1].toFloat();
load[2] = strlist[2].toFloat();
return true;
}

return false;
}

bool RemoteGetUptime(time_t &uptime)
{
QStringList strlist = QString("QUERY_UPTIME");

if (!gContext->SendReceiveStringList(strlist))
return false;

if (!strlist[0].at(0).isNumber())
return false;

if (sizeof(time_t) == sizeof(int))
uptime = strlist[0].toUInt();
else if (sizeof(time_t) == sizeof(long))
uptime = strlist[0].toULong();
else if (sizeof(time_t) == sizeof(long long))
uptime = strlist[0].toULongLong();

return false;
}

bool RemoteGetMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
{
QStringList strlist = QString("QUERY_MEMSTATS");

if (gContext->SendReceiveStringList(strlist))
{
totalMB = strlist[0].toInt();
freeMB = strlist[1].toInt();
totalVM = strlist[2].toInt();
freeVM = strlist[3].toInt();
return true;
}

return false;
}

bool RemoteCheckFile(ProgramInfo *pginfo)
{
QStringList strlist = "QUERY_CHECKFILE";
Expand Down
3 changes: 3 additions & 0 deletions mythtv/libs/libmythtv/remoteutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class RemoteEncoder;

vector<ProgramInfo *> *RemoteGetRecordedList(bool deltype);
void RemoteGetFreeSpace(int &totalspace, int &usedspace);
bool RemoteGetLoad(float load[3]);
bool RemoteGetUptime(time_t &uptime);
bool RemoteGetMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM);
bool RemoteCheckFile(ProgramInfo *pginfo);
void RemoteStopRecording(ProgramInfo *pginfo);
void RemoteDeleteRecording(ProgramInfo *pginfo, bool forgetHistory);
Expand Down
58 changes: 58 additions & 0 deletions mythtv/programs/mythbackend/mainserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ void MainServer::ProcessRequest(RefSocket *sock)
{
HandleQueryFreeSpace(pbs);
}
else if (command == "QUERY_LOAD")
{
HandleQueryLoad(pbs);
}
else if (command == "QUERY_UPTIME")
{
HandleQueryUptime(pbs);
}
else if (command == "QUERY_MEMSTATS")
{
HandleQueryMemStats(pbs);
}
else if (command == "QUERY_CHECKFILE")
{
HandleQueryCheckFile(listline, pbs);
Expand Down Expand Up @@ -1522,6 +1534,52 @@ void MainServer::HandleQueryFreeSpace(PlaybackSock *pbs)
SendResponse(pbssock, strlist);
}

void MainServer::HandleQueryLoad(PlaybackSock *pbs)
{
QSocket *pbssock = pbs->getSocket();

QStringList strlist;

double loads[3];
if (getloadavg(loads,3) == -1)
strlist << "getloadavg() failed";
else
strlist << QString::number(loads[0])
<< QString::number(loads[1])
<< QString::number(loads[2]);

SendResponse(pbssock, strlist);
}

void MainServer::HandleQueryUptime(PlaybackSock *pbs)
{
QSocket *pbssock = pbs->getSocket();
QStringList strlist;
time_t uptime;

if (getUptime(uptime))
strlist << QString::number(uptime);
else
strlist << "Could not determine uptime.";

SendResponse(pbssock, strlist);
}

void MainServer::HandleQueryMemStats(PlaybackSock *pbs)
{
QSocket *pbssock = pbs->getSocket();
QStringList strlist;
int totalMB, freeMB, totalVM, freeVM;

if (getMemStats(totalMB, freeMB, totalVM, freeVM))
strlist << QString::number(totalMB) << QString::number(freeMB)
<< QString::number(totalVM) << QString::number(freeVM);
else
strlist << "Could not determine memory stats.";

SendResponse(pbssock, strlist);
}

void MainServer::HandleQueryCheckFile(QStringList &slist, PlaybackSock *pbs)
{
QSocket *pbssock = pbs->getSocket();
Expand Down
Loading

0 comments on commit 420af85

Please sign in to comment.