From 87cb60beaba6a074ca55067ba988af5d2fdbd0fc Mon Sep 17 00:00:00 2001 From: manowarrior Date: Sun, 10 Sep 2006 18:00:31 +0000 Subject: [PATCH] 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 --- e_mod_main.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/e_mod_main.c b/e_mod_main.c index c798d97..d9adfa2 100644 --- a/e_mod_main.c +++ b/e_mod_main.c @@ -1,6 +1,12 @@ #include #include "e_mod_main.h" +#ifdef __FreeBSD__ +#include +#include +#include +#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; }