diff --git a/HISTORY b/HISTORY index a5aaae5..35a2bd6 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.24 2016-01-27 +Version 1.24 2016-01-28 * php extension compiled on PHP 7 * add skiplist which support stable sort * make.sh: use sed to replace perl @@ -10,6 +10,7 @@ Version 1.24 2016-01-27 * ini_file_reader add iniGetSectionNames and iniGetSectionItems * add fast_blocked_queue.[hc] * iovent bug fixed for FreeBSD + * sysinfo for FreeBSD Version 1.23 2015-11-16 * sched_thread.c: task can execute in a new thread diff --git a/src/system_info.c b/src/system_info.c index 6b3e1a1..16c9040 100644 --- a/src/system_info.c +++ b/src/system_info.c @@ -31,6 +31,7 @@ #ifdef OS_FREEBSD #include #include +#include #endif #endif @@ -618,6 +619,92 @@ int get_processes(struct fast_process_info **processes, int *count) *count = nproc; return 0; } + +int sysinfo(struct sysinfo *info) +{ + time_t uptime; + int mib[4]; + size_t size; + struct loadavg loads; + struct vmtotal vm; + struct xsw_usage sw_usage; + + memset(info, 0, sizeof(struct sysinfo)); + get_uptime(&uptime); + info->uptime = uptime; + + mib[0] = CTL_VM; + mib[1] = VM_LOADAVG; + size = sizeof(loads); + if (sysctl(mib, 2, &loads, &size, NULL, 0) != 0) + { + logError("file: "__FILE__", line: %d, " \ + "call sysctl fail, " \ + "errno: %d, error info: %s", \ + __LINE__, errno, STRERROR(errno)); + } + else if (loads.fscale > 0) + { + info->loads[0] = loads.ldavg[0] / loads.fscale; + info->loads[1] = loads.ldavg[1] / loads.fscale; + info->loads[2] = loads.ldavg[2] / loads.fscale; + } + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_ALL; + mib[3] = 0; + size = 0; + if (sysctl(mib, 4, NULL, &size, NULL, 0) != 0) + { + logError("file: "__FILE__", line: %d, " \ + "call sysctl fail, " \ + "errno: %d, error info: %s", \ + __LINE__, errno, STRERROR(errno)); + } + else + { + info->procs = size / sizeof(struct kinfo_proc); + } + + get_sys_total_mem_size((int64_t *)&info->totalram); + + mib[0] = CTL_VM; + mib[1] = VM_METER; + size = sizeof(vm); + if (sysctl(mib, 2, &vm, &size, NULL, 0) != 0) + { + logError("file: "__FILE__", line: %d, " \ + "call sysctl fail, " \ + "errno: %d, error info: %s", \ + __LINE__, errno, STRERROR(errno)); + } + else + { + info->freeram = vm.t_free; + info->sharedram = vm.t_rmshr; + //info->bufferram = vm. //TODO: + } + + mib[0] = CTL_VM; + mib[1] = VM_SWAPUSAGE; + size = sizeof(sw_usage); + if (sysctl(mib, 2, &sw_usage, &size, NULL, 0) != 0) + { + logError("file: "__FILE__", line: %d, " \ + "call sysctl fail, " \ + "errno: %d, error info: %s", \ + __LINE__, errno, STRERROR(errno)); + } + else + { + info->totalswap = sw_usage.xsu_total; + info->freeswap = sw_usage.xsu_avail; + } + + return 0; +} + #endif #endif diff --git a/src/system_info.h b/src/system_info.h index 41f2d81..cb9930c 100644 --- a/src/system_info.h +++ b/src/system_info.h @@ -19,6 +19,10 @@ #include #include "common_define.h" +#ifdef OS_LINUX +#include +#endif + #ifdef __cplusplus extern "C" { #endif @@ -47,6 +51,20 @@ 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) typedef struct fast_process_info { int field_count; //field count in /proc/$pid/stat @@ -137,6 +155,10 @@ int get_mounted_filesystems(struct fast_statfs *stats, const int size, int *coun int get_processes(struct fast_process_info **processes, int *count); #endif +#if defined(OS_FREEBSD) +int sysinfo(struct sysinfo *info); +#endif + #ifdef __cplusplus } #endif diff --git a/src/tests/test_mblock.c b/src/tests/test_mblock.c index b033516..7241160 100644 --- a/src/tests/test_mblock.c +++ b/src/tests/test_mblock.c @@ -86,6 +86,8 @@ int main(int argc, char *argv[]) #if defined(OS_LINUX) || defined(OS_FREEBSD) { FastProcessInfo *processes; + struct sysinfo info; + get_processes(&processes, &count); printf("process count: %d\n", count); for (i=0; i