use symbolic names for getloadavg() parameters, stricter error checks;
enable getloadavg() for Linux, a few little cleanups for Linux.
This commit is contained in:
@@ -4,6 +4,22 @@
|
|||||||
|
|
||||||
#include "shared/LL.h"
|
#include "shared/LL.h"
|
||||||
|
|
||||||
|
#ifndef LOADAVG_NSTATS
|
||||||
|
# define LOADAVG_NSTATS 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LOADAVG_1MIN
|
||||||
|
# define LOADAVG_1MIN 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LOADAVG_5MIN
|
||||||
|
# define LOADAVG_5MIN 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LOADAVG_15MIN
|
||||||
|
# define LOADAVG_15MIN 2
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -263,12 +263,12 @@ int machine_get_load(load_type *curr_load)
|
|||||||
|
|
||||||
int machine_get_loadavg(double *load)
|
int machine_get_loadavg(double *load)
|
||||||
{
|
{
|
||||||
double loadavg[1];
|
double loadavg[LOADAVG_NSTATS];
|
||||||
|
|
||||||
if (getloadavg(loadavg, 1) == -1)
|
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
|
||||||
return(FALSE);
|
return(FALSE);
|
||||||
|
|
||||||
*load = loadavg[0];
|
*load = loadavg[LOADAVG_1MIN];
|
||||||
|
|
||||||
return(TRUE);
|
return(TRUE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -232,12 +232,12 @@ int machine_get_load(load_type *curr_load)
|
|||||||
|
|
||||||
int machine_get_loadavg(double *load)
|
int machine_get_loadavg(double *load)
|
||||||
{
|
{
|
||||||
double loadavg[1];
|
double loadavg[LOADAVG_NSTATS];
|
||||||
|
|
||||||
if (getloadavg(loadavg, 1) == -1)
|
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
|
||||||
return(FALSE);
|
return(FALSE);
|
||||||
|
|
||||||
*load = loadavg[0];
|
*load = loadavg[LOADAVG_1MIN];
|
||||||
|
|
||||||
return(TRUE);
|
return(TRUE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,14 @@
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_GETLOADAVG
|
||||||
|
# define USE_GETLOADAVG
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef USE_GETLOADAVG
|
#ifdef USE_GETLOADAVG
|
||||||
# include <sys/loadavg.h>
|
# ifdef HAVE_SYS_LOADAVG_H
|
||||||
|
# include <sys/loadavg.h>
|
||||||
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_PROCFS_H
|
#ifdef HAVE_PROCFS_H
|
||||||
@@ -147,11 +153,12 @@ reread (int f, char *errmsg)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
long
|
||||||
getentry (const char *tag, const char *bufptr)
|
getentry (const char *tag, const char *bufptr)
|
||||||
{
|
{
|
||||||
char *tail;
|
char *tail;
|
||||||
int retval, len = strlen (tag);
|
int len = strlen(tag);
|
||||||
|
long retval;
|
||||||
|
|
||||||
while (bufptr != NULL) {
|
while (bufptr != NULL) {
|
||||||
if (*bufptr == '\n')
|
if (*bufptr == '\n')
|
||||||
@@ -292,7 +299,7 @@ int machine_get_load(load_type *curr_load)
|
|||||||
static load_type last_load = { 0, 0, 0, 0, 0 };
|
static load_type last_load = { 0, 0, 0, 0, 0 };
|
||||||
load_type load;
|
load_type load;
|
||||||
|
|
||||||
reread(load_fd, "get_load:");
|
reread(load_fd, "get_load");
|
||||||
sscanf(procbuf, "%*s %lu %lu %lu %lu\n", &load.user, &load.nice, &load.system, &load.idle);
|
sscanf(procbuf, "%*s %lu %lu %lu %lu\n", &load.user, &load.nice, &load.system, &load.idle);
|
||||||
load.total = load.user + load.nice + load.system + load.idle;
|
load.total = load.user + load.nice + load.system + load.idle;
|
||||||
|
|
||||||
@@ -311,14 +318,13 @@ int machine_get_loadavg(double *load)
|
|||||||
#ifdef USE_GETLOADAVG
|
#ifdef USE_GETLOADAVG
|
||||||
double loadavg[LOADAVG_NSTATS];
|
double loadavg[LOADAVG_NSTATS];
|
||||||
|
|
||||||
if (getloadavg(loadavg, LOADAVG_NSTATS) < 0) {
|
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN) {
|
||||||
perror("getloadavg"); /* ToDo: correct error reporting */
|
perror("getloadavg"); /* ToDo: correct error reporting */
|
||||||
*load = 1.;
|
|
||||||
return(FALSE);
|
return(FALSE);
|
||||||
}
|
}
|
||||||
*load = loadavg[LOADAVG_1MIN];
|
*load = loadavg[LOADAVG_1MIN];
|
||||||
#else
|
#else
|
||||||
reread(loadavg_fd, "get_load:");
|
reread(loadavg_fd, "get_loadavg");
|
||||||
sscanf(procbuf, "%lf", load);
|
sscanf(procbuf, "%lf", load);
|
||||||
#endif
|
#endif
|
||||||
return(TRUE);
|
return(TRUE);
|
||||||
@@ -326,7 +332,7 @@ int machine_get_loadavg(double *load)
|
|||||||
|
|
||||||
int machine_get_meminfo(meminfo_type *result)
|
int machine_get_meminfo(meminfo_type *result)
|
||||||
{
|
{
|
||||||
reread(meminfo_fd, "get_meminfo:");
|
reread(meminfo_fd, "get_meminfo");
|
||||||
result[0].total = getentry("MemTotal:", procbuf);
|
result[0].total = getentry("MemTotal:", procbuf);
|
||||||
result[0].free = getentry("MemFree:", procbuf);
|
result[0].free = getentry("MemFree:", procbuf);
|
||||||
result[0].shared = getentry("MemShared:", procbuf);
|
result[0].shared = getentry("MemShared:", procbuf);
|
||||||
@@ -452,7 +458,7 @@ int machine_get_smpload(load_type *result, int *numcpus)
|
|||||||
|
|
||||||
// Look for lines starting with "cpu0", "cpu1", etc.
|
// Look for lines starting with "cpu0", "cpu1", etc.
|
||||||
token = strtok(procbuf, "\n");
|
token = strtok(procbuf, "\n");
|
||||||
while (token) {
|
while (token != NULL) {
|
||||||
if ((strlen(token) > 3) && (!strncmp(token, "cpu", 3)) && isdigit(token[3])) {
|
if ((strlen(token) > 3) && (!strncmp(token, "cpu", 3)) && isdigit(token[3])) {
|
||||||
sscanf(token, "%*s %lu %lu %lu %lu", &curr_load[*numcpus].user, &curr_load[*numcpus].nice, &curr_load[*numcpus].system, &curr_load[*numcpus].idle);
|
sscanf(token, "%*s %lu %lu %lu %lu", &curr_load[*numcpus].user, &curr_load[*numcpus].nice, &curr_load[*numcpus].system, &curr_load[*numcpus].idle);
|
||||||
|
|
||||||
@@ -479,7 +485,7 @@ int machine_get_uptime(double *up, double *idle)
|
|||||||
{
|
{
|
||||||
double local_up, local_idle;
|
double local_up, local_idle;
|
||||||
|
|
||||||
reread(uptime_fd, "get_uptime:");
|
reread(uptime_fd, "get_uptime");
|
||||||
sscanf(procbuf, "%lf %lf", &local_up, &local_idle);
|
sscanf(procbuf, "%lf %lf", &local_up, &local_idle);
|
||||||
if (up != NULL)
|
if (up != NULL)
|
||||||
*up = local_up;
|
*up = local_up;
|
||||||
|
|||||||
@@ -229,12 +229,12 @@ int machine_get_load(load_type *curr_load)
|
|||||||
|
|
||||||
int machine_get_loadavg(double *load)
|
int machine_get_loadavg(double *load)
|
||||||
{
|
{
|
||||||
double loadavg[1];
|
double loadavg[LOADAVG_NSTATS];
|
||||||
|
|
||||||
if (getloadavg(loadavg, 1) == -1)
|
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
|
||||||
return(FALSE);
|
return(FALSE);
|
||||||
|
|
||||||
*load = loadavg[0];
|
*load = loadavg[LOADAVG_1MIN];
|
||||||
|
|
||||||
return(TRUE);
|
return(TRUE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -234,12 +234,12 @@ int machine_get_load(load_type *curr_load)
|
|||||||
|
|
||||||
int machine_get_loadavg(double *load)
|
int machine_get_loadavg(double *load)
|
||||||
{
|
{
|
||||||
double loadavg[1];
|
double loadavg[LOADAVG_NSTATS];
|
||||||
|
|
||||||
if (getloadavg(loadavg, 1) == -1)
|
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
|
||||||
return(FALSE);
|
return(FALSE);
|
||||||
|
|
||||||
*load = loadavg[0];
|
*load = loadavg[LOADAVG_1MIN];
|
||||||
|
|
||||||
return(TRUE);
|
return(TRUE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -193,12 +193,12 @@ int machine_get_load(load_type *curr_load)
|
|||||||
|
|
||||||
int machine_get_loadavg(double *load)
|
int machine_get_loadavg(double *load)
|
||||||
{
|
{
|
||||||
double loadavg[1];
|
double loadavg[LOADAVG_NSTATS];
|
||||||
|
|
||||||
if (getloadavg(loadavg, 1) == -1)
|
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
|
||||||
return(FALSE);
|
return(FALSE);
|
||||||
|
|
||||||
*load = loadavg[0];
|
*load = loadavg[LOADAVG_1MIN];
|
||||||
|
|
||||||
return(TRUE);
|
return(TRUE);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user