I'm using kstat for drawing CPU part of boot chart (see my previous posts). I need cpu values for "user","nice","system","idle","iowait" in such order (sample output):
I've written the small kstat utility and added its call in a bootchartd script (utility writes /var/log/bootchart/proc_stat.log):
Sample boot chart for MilaX:
cpu 8 0 141 313 230 0 0
I've written the small kstat utility and added its call in a bootchartd script (utility writes /var/log/bootchart/proc_stat.log):
#include <kstat.h> #include <sys/sysinfo.h> #include <stdio.h> #include <string.h> static kstat_ctl_t *kc; static kstat_t *ksp; static unsigned int cpu_user, cpu_system, cpu_nice, cpu_idle, cpu_iowait; int main(int argc, char **argv) { cpu_stat_t *cpu_stat; kc = kstat_open(); if (kc != NULL) { ksp = kstat_lookup(kc, "cpu_stat", 0, "cpu_stat0"); if (ksp != NULL && ksp->ks_type == KSTAT_TYPE_RAW) { if (kstat_read(kc, ksp, NULL) != -1 && ksp->ks_data_size == sizeof(cpu_stat_t)) { cpu_stat = (cpu_stat_t *)ksp->ks_data; cpu_user=cpu_stat->cpu_sysinfo.cpu[CPU_USER]; cpu_nice=cpu_stat->cpu_sysinfo.cpu[CPU_WAIT]; cpu_system = cpu_stat->cpu_sysinfo.cpu[CPU_KERNEL] ; cpu_iowait=cpu_stat->cpu_syswait.iowait; cpu_idle=cpu_stat->cpu_sysinfo.cpu[CPU_IDLE]; printf("cpu %d %d %d %d %d 0 0\n", cpu_user,cpu_nice,cpu_system,cpu_idle,cpu_iowait); } } } }
Sample boot chart for MilaX: