Cpu now works on FreeBSD. Thanks to sndev for the help and to Stanislav Sedov for the patch (don't ask why am I commiting this..)

SVN revision: 25695
This commit is contained in:
manowarrior 2006-09-10 18:00:31 +00:00 committed by manowarrior
parent a977b8f4f7
commit 87cb60beab
1 changed files with 38 additions and 0 deletions

View File

@ -1,6 +1,12 @@
#include <e.h>
#include "e_mod_main.h"
#ifdef __FreeBSD__
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/resource.h>
#endif
typedef struct _Instance Instance;
typedef struct _Cpu Cpu;
@ -196,25 +202,56 @@ _get_cpu_count(void)
char tmp[4];
int cpu = -1;
#ifdef __FreeBSD__
cpu = 1;
#else
if (!(f = fopen("/proc/stat", "r"))) return cpu;
while (fscanf(f, "cp%s %*u %*u %*u %*u %*u %*u %*u %*u\n", (char *) &tmp) == 1)
cpu++;
fclose(f);
#endif
return cpu;
}
static int
_get_cpu_load(void)
{
#ifdef __FreeBSD__
long cp_time[CPUSTATES];
static long old_used, old_tot;
long new_used, new_tot;
size_t len;
#else
FILE *stat;
static unsigned long old_u[4], old_n[4], old_s[4], old_i[4], old_wa[4], old_hi[4], old_si[4];
unsigned long new_u, new_n, new_s, new_i, new_wa = 0, new_hi = 0, new_si = 0, dummy2, ticks_past;
int tmp_u = 0, tmp_n = 0, tmp_s = 0;
char dummy[16];
int i = 0;
#endif
#ifdef __FreeBSD__
len = sizeof(cp_time);
if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0)
{
warn("sysctl()");
return 0;
}
new_used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS];
new_tot = new_used + cp_time[CP_IDLE];
cpu_stats[0] = 100 * (float)(new_used - old_used) / (float)(new_tot - old_tot);
old_tot = new_tot;
old_used = new_used;
cpu_stats[0]=(cpu_stats[0]>100?100:cpu_stats[0]);
#else
if (!(stat = fopen("/proc/stat", "r"))) return -1;
/* since if there are more than 1 CPUs, the first entry is the summary:
@ -264,6 +301,7 @@ _get_cpu_load(void)
i++;
}
fclose (stat);
#endif
return 0;
}