36 #if defined(OS_SOLARIS) 45 #if defined(OS_FREEBSD) 69 #define JCPU_SLOPE 100 72 static char *cpustatenames[] =
73 {
"idle",
"user",
"nice",
"kernel",
"wait", NULL };
84 static bool gather_cpu_load_info();
86 #define CPUSTAT_INITIALIZER {FALSE, (time_t ) 0, {0, 0, 0, 0, 0}} 94 static int cpu_ptr = 0;
95 static bool init_ok =
FALSE;
97 static pthread_mutex_t cpu_mutex = PTHREAD_MUTEX_INITIALIZER;
99 #define CPU_LOAD_LOCK() MUTEX_LOCK(&cpu_mutex) 101 #define CPU_LOAD_UNLOCK() MUTEX_UNLOCK(&cpu_mutex) 103 static time_t dt_stat = 10;
120 memset(cpu_ary, 0,
sizeof (cpu_ary));
142 if ((old == NULL) || (
new == NULL) || (pct == NULL))
147 total += new->
load[i] - old->
load[i];
154 oldslope = pct->
slope;
156 pct->
date = time(NULL);
158 pct->
load[i] = ((new->
load[i] - old->
load[i]) * 1000 + half) / total;
163 pct->
slope = ++oldslope;
167 cpu_ary[cpu_ptr++] = *pct;
187 gather_cpu_load_info()
189 static kstat_ctl_t *kc = NULL;
190 static kstat_t *ksp = NULL;
192 static time_t
last = 0;
193 time_t now = time(NULL);
197 if ((kc = kstat_open()) == NULL)
206 if ((kid = kstat_chain_update(kc)) < 0)
219 if (last + dt_stat > now)
227 memset(&cpu_new, 0,
sizeof (cpu_new));
228 cpu_new.
date = time(NULL);
230 for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next)
232 if (strncasecmp(ksp->ks_module,
"cpu_stat", 8) == 0)
234 static cpu_stat_t cpu_buf;
235 static cpu_sysinfo_t *cpuinfo = &cpu_buf.cpu_sysinfo;
237 if (kstat_read(kc, ksp, &cpu_buf) < 0)
273 #define PROC_STAT_FILE "/proc/stat" 276 gather_cpu_load_info()
279 static time_t
last = 0;
280 time_t now = time(NULL);
281 static char buf[4096];
284 if (last + dt_stat > now)
288 if ((fd = open(PROC_STAT_FILE, O_RDONLY)) < 0)
297 memset(&cpu_new, 0,
sizeof (cpu_new));
298 cpu_new.
date = time(NULL);
301 if (read(fd, buf,
sizeof (buf)) > 0)
308 for (iarg = 0; iarg < largc; iarg++)
317 u = n = s = i = x = y = z = 0;
411 gather_cpu_load_info()
413 static time_t
last = 0;
414 time_t now = time(NULL);
416 if (last + dt_stat > now)
424 memset(&cpu_new, 0,
sizeof (cpu_new));
425 cpu_new.
date = time(NULL);
453 #define MAX_KVM_ERR 32 457 gather_cpu_load_info()
459 static time_t
last = 0;
460 time_t now = time(NULL);
464 if ((MAX_KVM_ERR > 0) && (nerr > MAX_KVM_ERR))
467 if (last + dt_stat > now)
472 #if HAVE_SYSCTLBYNAME 477 memset(&cpu_new, 0,
sizeof (cpu_new));
478 cpu_new.
date = time(NULL);
481 long cp_time[CPUSTATES];
482 size_t len =
sizeof (cp_time);
484 if (sysctlbyname(
"kern.cp_time", &cp_time, &len, NULL, 0) == 0)
502 static kvm_t *kd = NULL;
503 static struct nlist namelist[] = { {
"_cp_time"}, {
""} };
504 static unsigned long nameaddr = 0;
510 if ((kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL)
516 if (kvm_nlist(kd, namelist) != 0)
523 nameaddr = namelist[0].n_value;
531 memset(&cpu_new, 0,
sizeof (cpu_new));
532 cpu_new.
date = time(NULL);
537 if (kvm_read(kd, nameaddr, cp_time,
sizeof (cp_time)) ==
sizeof (cp_time))
556 if (kvm_close(kd) != 0)
583 gather_cpu_load_info()
585 static time_t
last = 0;
586 time_t now = time(NULL);
588 if (last + dt_stat > now)
596 memset(&cpu_new, 0,
sizeof (cpu_new));
597 cpu_new.
date = time(NULL);
628 gather_cpu_load_info()
630 static time_t
last = 0;
631 time_t now = time(NULL);
633 if (last + dt_stat > now)
641 memset(&cpu_new, 0,
sizeof (cpu_new));
642 cpu_new.
date = time(NULL);
669 if ((cpu_new.
date == (time_t) 0) || (cpu_old.
date == (time_t) 0))
676 if (u + n + s + i > 0)
678 printf(
"*** %ld", (
long) cpu_new.
date);
679 printf(
" - user : %5.2f", ((
double) u) / (u + n + s + i));
680 printf(
" - nice : %5.2f", ((
double) n) / (u + n + s + i));
681 printf(
" - kernel : %5.2f", ((
double) s) / (u + n + s + i));
682 printf(
" - idle : %5.2f", ((
double) i) / (u + n + s + i));
700 "SYSTEM LOAD - idle : %5.1f - user : %5.1f - kernel : %5.1f - wait %5.1f - %3d";
710 fmt =
"SYSTEM LOAD - idle/kernel/user/nice = %5.1f %5.1f %5.1f %5.1f %3d";
720 fmt =
"SYSTEM LOAD - idle/kernel/user/nice = %5.1f %5.1f %5.1f %5.1f %3d";
757 return (
double) cpu_pct.
slope;
763 return cpu_pct.
load[which] / 10.;
775 #define DT_SYSLOAD 10 777 static bool cpuloadgo =
TRUE;
778 static bool cpuloadok =
FALSE;
788 tid = pthread_self();
789 (void) pthread_detach(tid);
796 old = now = time(NULL);
805 if (gather_cpu_load_info())
809 "System Load increasing for more than %ld sec - Last Idle = %5.1f",
854 if ((r = pthread_create(&tid, NULL,
cpuload_thread, (
void *) NULL)) != 0)
862 if ((r = pthread_create(&tid, NULL,
cpuload_thread, (
void *) NULL)) != 0)
870 if ((r = pthread_create(&tid, NULL,
cpuload_thread, (
void *) NULL)) != 0)
878 if ((r = pthread_create(&tid, NULL,
cpuload_thread, (
void *) NULL)) != 0)
886 if ((r = pthread_create(&tid, NULL,
cpuload_thread, (
void *) NULL)) != 0)
loadtype_T load[JCPU_STATES]
void * cpuload_thread(void *data)
#define ZE_LogMsgError(level,...)
bool zeStrRegex(char *, char *, long *, long *, bool)
int zeStr2Tokens(char *, int, char **, char *)
#define CPU_LOAD_UNLOCK()
unsigned long long zeStr2ulonglong(char *s, int *error, unsigned long long dval)
int evaluate_load_pct(cpustats_t *pct, cpustats_t *old, cpustats_t *new)
void * load_measure_thread(void *)
#define ZE_MessageInfo(level,...)
#define ZE_LogSysError(...)
#define CPUSTAT_INITIALIZER