-
libgtop2:
I have a while(1) loop, and the error is:
glibtop: open (/proc/stat): Too many open files
This error occurs after about a half hour to an hour of running.
I've tried running this multiple times, both with using glib_close() at the end of the loop, using glib_init() and glib_close() at beginning/end, and just using glib_init(). The strange thing is these have no effect on the actual glib_get functions.
Anybody familiar with libgtop and have a solution?
-
Please post your code here. It sounds like you are not releasing resources (file handles) appropriately.
-
my code:
Code:
#include <glibtop.h>
#include <glibtop/cpu.h>
#include <glibtop/mem.h>
#include <glibtop/uptime.h>
...
int main()
{
...
glibtop_init();
...
while(1)
{
glibtop_get_cpu (&cpu);
glibtop_get_mem(&memory);
glibtop_get_uptime(&uptime);
...
}
return;
}
As far as I can see, it uses a global server, which it only allocates on the init(). I've tried putting a glib_close() inside of the while loop (assuming it spawns a new server every time it calls the functions) but that has no effect.
The documentation for libgtop2, while complete, is very lacking in useful examples, so I'm really not sure how to properly use the other functions to specify a server handle which I control.
-
Well, that shows how you are using glibtop in your code, but not the rest of it. Something is consuming file descriptors and you still are not providing enough information to rule out either your code or the glibtop code.
-
More code...
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <cmath>
#include <glibtop.h>
#include <glibtop/cpu.h>
#include <glibtop/mem.h>
#include <glibtop/uptime.h>
...
static glibtop_cpu cpu;
static glibtop_mem memory;
static glibtop_uptime uptime;
static unsigned long long lastCpuTotalSum = 0;
static unsigned long long lastCpuUsedSum = 0;
static unsigned long long lastCpuIOWaitSum = 0;
static bool firstCpuCalc = true;
...
int main()
{
...
glibtop_init();
...
while( 1 )
{
...
//Get CPU/Memory status
glibtop_get_cpu (&cpu);
glibtop_get_mem(&memory);
glibtop_get_uptime(&uptime);
...
int memUsed = (int)(memory.user/(1024*1024));
int totalMem = (int)(memory.total/(1024*1024));
double memPercentFree = (double)(memUsed) /
(double)(totalMem);
...
if( !firstCpuCalc )
{
unsigned long long cpuusedsum = cpu.user + cpu.sys + cpu.nice + cpu.iowait + cpu.irq + cpu.softirq;
unsigned long long cpuusedsumdiff = cpuusedsum - lastCpuUsedSum;
unsigned long long cpuiowaitsumdiff = cpu.iowait - lastCpuIOWaitSum;
unsigned long long cputotalsumdiff = cpu.total - lastCpuTotalSum;
float percentUsed = (float)(cpuusedsumdiff) / (float)(cputotalsumdiff);
float percentIOWait = (float)(cpuiowaitsumdiff) / (float)(cputotalsumdiff);
int currentPercentUsed = round(percentUsed * 100);
//printf("Percent Used: %d\n", currentPercentUsed);
int currentIOWaitUsed = round(percentIOWait * 100);
...
lastCpuUsedSum = cpuusedsum;
lastCpuIOWaitSum = cpu.iowait;
lastCpuTotalSum = cpu.total;
}
else
{
firstCpuCalc = false;
...
lastCpuUsedSum = cpu.user + cpu.sys + cpu.nice;
lastCpuTotalSum = cpu.total;
}
//Uptime
double uptimeSec = uptime.uptime;
int uptimeMin = (int)uptimeSec / 60;
int uptimeMinClock = uptimeMin % 60;
int uptimeHour = uptimeMin / 60;
int uptimeHourClock = uptimeHour % 24;
int uptimeDay = uptimeHour / 24;
int uptimeDayClock = uptimeDay % 365;
...
glibtop_close();
sleep(CYCLE_TIME);
}
}
The ... stuff definitely has nothing to do with libgtop2 (all just working with copied variables), so this should pretty much be the complete code.
-
Well, I was trying to see if something you were doing would open file descriptors/sockets and not release them. Unless it is in the code you didn't provide, indicated by the elipses ..., then I can't say what is causing your problem. Have you tried updating the glibtop libraries?