/*
* i-scream libstatgrab
* http://www.i-scream.org
* Copyright (C) 2000-2004 i-scream
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: cpu_stats.c,v 1.25 2004/11/01 18:30:17 tdb Exp $
*/
#ifdef HAVE_CONFIG_H
#endif
#ifdef SOLARIS
#endif
#if defined(LINUX) || defined(CYGWIN)
#endif
#if defined(FREEBSD) || defined(DFBSD)
#endif
#ifdef NETBSD
#endif
#ifdef OPENBSD
#endif
#ifdef HPUX
#endif
static sg_cpu_stats cpu_now;
static int cpu_now_uninit=1;
sg_cpu_stats *sg_get_cpu_stats(){
#ifdef HPUX
struct pst_dynamic pstat_dynamic;
int i;
#endif
#ifdef SOLARIS
kstat_ctl_t *kc;
kstat_t *ksp;
cpu_stat_t cs;
#endif
#if defined(LINUX) || defined(CYGWIN)
FILE *f;
#endif
#ifdef ALLBSD
#if defined(NETBSD) || defined(OPENBSD)
int mib[2];
#endif
#ifdef NETBSD
u_int64_t cp_time[CPUSTATES];
#else
long cp_time[CPUSTATES];
#endif
size_t size;
#endif
cpu_now.user=0;
/* Not stored in linux or freebsd */
cpu_now.iowait=0;
cpu_now.kernel=0;
cpu_now.idle=0;
/* Not stored in linux, freebsd, or hpux */
cpu_now.swap=0;
cpu_now.total=0;
/* Not stored in solaris */
cpu_now.nice=0;
#ifdef HPUX
return NULL;
}
cpu_now.user = pstat_dynamic.psd_cpu_time[CP_USER];
cpu_now.iowait = pstat_dynamic.psd_cpu_time[CP_WAIT];
cpu_now.kernel = pstat_dynamic.psd_cpu_time[CP_SSYS] + pstat_dynamic.psd_cpu_time[CP_SYS];
cpu_now.idle = pstat_dynamic.psd_cpu_time[CP_IDLE];
cpu_now.nice = pstat_dynamic.psd_cpu_time[CP_NICE];
for (i = 0; i < PST_MAX_CPUSTATES; i++) {
cpu_now.total += pstat_dynamic.psd_cpu_time[i];
}
#endif
#ifdef SOLARIS
return NULL;
}
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
if ((strcmp(ksp->ks_module,
"cpu_stat")) !=
0) continue;
continue;
}
cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
}
cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
-
#endif
#if defined(LINUX) || defined(CYGWIN)
if ((f=
fopen("/proc/stat",
"r" ))==
NULL) { return NULL;
}
/* The very first line should be cpu */
if((fscanf(f,
"cpu %lld %lld %lld %lld", \
&cpu_now.user, \
&cpu_now.nice, \
&cpu_now.kernel, \
&cpu_now.idle)) != 4){
return NULL;
}
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
#endif
#ifdef ALLBSD
#if defined(FREEBSD) || defined(DFBSD)
size = sizeof cp_time;
if (sysctlbyname("kern.cp_time", &cp_time, &size,
NULL,
0) <
0){ return NULL;
}
#else
mib[0] = CTL_KERN;
#ifdef NETBSD
mib[1] = KERN_CP_TIME;
#else
mib[1] = KERN_CPTIME;
#endif
size = sizeof cp_time;
if (sysctl(mib,
2, &cp_time, &size,
NULL,
0) <
0) { #ifdef NETBSD
"CTL_KERN.KERN_CP_TIME");
#else
"CTL_KERN.KERN_CPTIME");
#endif
return NULL;
}
#endif
cpu_now.user=cp_time[CP_USER];
cpu_now.nice=cp_time[CP_NICE];
cpu_now.kernel=cp_time[CP_SYS];
cpu_now.idle=cp_time[CP_IDLE];
-
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
#endif
cpu_now.
systime=
time(NULL);
cpu_now_uninit=0;
return &cpu_now;
}
sg_cpu_stats *sg_get_cpu_stats_diff(){
static sg_cpu_stats cpu_diff;
sg_cpu_stats cpu_then, *cpu_tmp;
if (cpu_now_uninit){
/* Should sg_get_cpu_stats fail */
return NULL;
}
return cpu_tmp;
}
cpu_then.user=cpu_now.user;
cpu_then.kernel=cpu_now.kernel;
cpu_then.idle=cpu_now.idle;
cpu_then.iowait=cpu_now.iowait;
cpu_then.swap=cpu_now.swap;
cpu_then.nice=cpu_now.nice;
cpu_then.total=cpu_now.total;
cpu_then.systime=cpu_now.systime;
return NULL;
}
cpu_diff.user = cpu_now.user - cpu_then.user;
cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel;
cpu_diff.idle = cpu_now.idle - cpu_then.idle;
cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait;
cpu_diff.swap = cpu_now.swap - cpu_then.swap;
cpu_diff.nice = cpu_now.nice - cpu_then.nice;
cpu_diff.total = cpu_now.total - cpu_then.total;
cpu_diff.systime = cpu_now.systime - cpu_then.systime;
return &cpu_diff;
}
sg_cpu_percents *sg_get_cpu_percents(){
static sg_cpu_percents cpu_usage;
sg_cpu_stats *cs_ptr;
if(cs_ptr==NULL){
return NULL;
}
cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100;
cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100;
cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100;
cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100;
cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100;
cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100;
cpu_usage.time_taken = cs_ptr->systime;
return &cpu_usage;
}
-