diff --git a/src/system_info.c b/src/system_info.c index 16c9040..63b0846 100644 --- a/src/system_info.c +++ b/src/system_info.c @@ -502,6 +502,32 @@ int get_processes(struct fast_process_info **processes, int *count) return result; } +int get_sysinfo(struct fast_sysinfo*info) +{ + struct sysinfo si; + if (sysinfo(&si) != 0) + { + logError("file: "__FILE__", line: %d, " \ + "call sysinfo fail, " \ + "errno: %d, error info: %s", \ + __LINE__, errno, STRERROR(errno)); + return errno != 0 ? errno : EPERM; + } + + info->loads[0] = si.loads[0] / (double)(1 << SI_LOAD_SHIFT); + info->loads[1] = si.loads[1] / (double)(1 << SI_LOAD_SHIFT), + info->loads[2] = si.loads[2] / (double)(1 << SI_LOAD_SHIFT); + info->uptime = si.uptime; + info->totalram = si.totalram; + info->freeram = si.freeram; + info->sharedram = si.sharedram; + info->bufferram = si.bufferram; + info->totalswap = si.totalswap; + info->freeswap = si.freeswap; + info->procs = si.procs; + return 0; +} + #elif defined(OS_FREEBSD) int get_processes(struct fast_process_info **processes, int *count) @@ -620,7 +646,7 @@ int get_processes(struct fast_process_info **processes, int *count) return 0; } -int sysinfo(struct sysinfo *info) +int get_sysinfo(struct fast_sysinfo*info) { time_t uptime; int mib[4]; diff --git a/src/system_info.h b/src/system_info.h index cb9930c..9636a53 100644 --- a/src/system_info.h +++ b/src/system_info.h @@ -51,21 +51,19 @@ extern "C" { char f_mntonname[MNAMELEN]; /* directory on which mounted */ } FastStatFS; -#if defined(OS_FREEBSD) - struct sysinfo { - long uptime; /* Seconds since boot */ - unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ - unsigned long totalram; /* Total usable main memory size */ - unsigned long freeram; /* Available memory size */ - unsigned long sharedram; /* Amount of shared memory */ - unsigned long bufferram; /* Memory used by buffers */ - unsigned long totalswap; /* Total swap space size */ - unsigned long freeswap; /* swap space still available */ - unsigned short procs; /* Number of current processes */ - }; -#endif - #if defined(OS_LINUX) || defined(OS_FREEBSD) + struct fast_sysinfo { + long uptime; /* Seconds since boot */ + double loads[3]; /* 1, 5, and 15 minute load averages */ + unsigned long totalram; /* Total usable main memory size */ + unsigned long freeram; /* Available memory size */ + unsigned long sharedram; /* Amount of shared memory */ + unsigned long bufferram; /* Memory used by buffers */ + unsigned long totalswap; /* Total swap space size */ + unsigned long freeswap; /* swap space still available */ + unsigned short procs; /* Number of current processes */ + }; + typedef struct fast_process_info { int field_count; //field count in /proc/$pid/stat int pid; @@ -153,10 +151,8 @@ int get_mounted_filesystems(struct fast_statfs *stats, const int size, int *coun * return: error no , 0 success, != 0 fail */ int get_processes(struct fast_process_info **processes, int *count); -#endif -#if defined(OS_FREEBSD) -int sysinfo(struct sysinfo *info); +int get_sysinfo(struct fast_sysinfo*info); #endif #ifdef __cplusplus diff --git a/src/tests/test_mblock.c b/src/tests/test_mblock.c index 7241160..5c60923 100644 --- a/src/tests/test_mblock.c +++ b/src/tests/test_mblock.c @@ -86,7 +86,7 @@ int main(int argc, char *argv[]) #if defined(OS_LINUX) || defined(OS_FREEBSD) { FastProcessInfo *processes; - struct sysinfo info; + struct fast_sysinfo info; get_processes(&processes, &count); printf("process count: %d\n", count); @@ -100,10 +100,11 @@ int main(int argc, char *argv[]) free(processes); } - if (sysinfo(&info) == 0) + if (get_sysinfo(&info) == 0) { printf("uptime: %ld\n", info.uptime); - printf("loads: %ld, %ld, %ld\n", info.loads[0], info.loads[1], info.loads[2]); + printf("loads: %.2f, %.2f, %.2f\n", + info.loads[0], info.loads[1], info.loads[2]); printf("totalram: %ld\n", info.totalram); printf("freeram: %ld\n", info.freeram); printf("sharedram: %ld\n", info.sharedram);