sysinfo for FreeBSD

pull/5/head
Yu Qing 2016-01-28 11:30:07 +08:00
parent 7b61231508
commit 0758be6e88
4 changed files with 126 additions and 1 deletions

View File

@ -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

View File

@ -31,6 +31,7 @@
#ifdef OS_FREEBSD
#include <sys/sysctl.h>
#include <sys/ucred.h>
#include <sys/vmmeter.h>
#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

View File

@ -19,6 +19,10 @@
#include <sys/mount.h>
#include "common_define.h"
#ifdef OS_LINUX
#include <sys/sysinfo.h>
#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

View File

@ -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<count; i++)
@ -97,6 +99,19 @@ int main(int argc, char *argv[])
{
free(processes);
}
if (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("totalram: %ld\n", info.totalram);
printf("freeram: %ld\n", info.freeram);
printf("sharedram: %ld\n", info.sharedram);
printf("bufferram: %ld\n", info.bufferram);
printf("totalswap: %ld\n", info.totalswap);
printf("freeswap: %ld\n", info.freeswap);
printf("procs: %d\n", info.procs);
}
}
#endif