add get_sysinfo

pull/5/head
yuqing 2016-01-28 12:26:11 +08:00
parent 0758be6e88
commit e4876f0c61
3 changed files with 44 additions and 21 deletions

View File

@ -502,6 +502,32 @@ int get_processes(struct fast_process_info **processes, int *count)
return result; 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) #elif defined(OS_FREEBSD)
int get_processes(struct fast_process_info **processes, int *count) 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; return 0;
} }
int sysinfo(struct sysinfo *info) int get_sysinfo(struct fast_sysinfo*info)
{ {
time_t uptime; time_t uptime;
int mib[4]; int mib[4];

View File

@ -51,21 +51,19 @@ extern "C" {
char f_mntonname[MNAMELEN]; /* directory on which mounted */ char f_mntonname[MNAMELEN]; /* directory on which mounted */
} FastStatFS; } 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) #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 { typedef struct fast_process_info {
int field_count; //field count in /proc/$pid/stat int field_count; //field count in /proc/$pid/stat
int pid; 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 * return: error no , 0 success, != 0 fail
*/ */
int get_processes(struct fast_process_info **processes, int *count); int get_processes(struct fast_process_info **processes, int *count);
#endif
#if defined(OS_FREEBSD) int get_sysinfo(struct fast_sysinfo*info);
int sysinfo(struct sysinfo *info);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -86,7 +86,7 @@ int main(int argc, char *argv[])
#if defined(OS_LINUX) || defined(OS_FREEBSD) #if defined(OS_LINUX) || defined(OS_FREEBSD)
{ {
FastProcessInfo *processes; FastProcessInfo *processes;
struct sysinfo info; struct fast_sysinfo info;
get_processes(&processes, &count); get_processes(&processes, &count);
printf("process count: %d\n", count); printf("process count: %d\n", count);
@ -100,10 +100,11 @@ int main(int argc, char *argv[])
free(processes); free(processes);
} }
if (sysinfo(&info) == 0) if (get_sysinfo(&info) == 0)
{ {
printf("uptime: %ld\n", info.uptime); 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("totalram: %ld\n", info.totalram);
printf("freeram: %ld\n", info.freeram); printf("freeram: %ld\n", info.freeram);
printf("sharedram: %ld\n", info.sharedram); printf("sharedram: %ld\n", info.sharedram);