/* * CPU usage as ticks spent in different states, * see <tt>sg_get_cpu_stats(3)</tt> manpage. */ static VALUE statgrab_cpu_stats(VALUE self) { sg_cpu_stats *stats; VALUE info, time_at; if ((stats = sg_get_cpu_stats()) == NULL) statgrab_handle_error(); info = rb_hash_new(); rb_hash_aset(info, ID2SYM(rb_intern("user")), INT2NUM(stats->user)); rb_hash_aset(info, ID2SYM(rb_intern("kernel")), INT2NUM(stats->kernel)); rb_hash_aset(info, ID2SYM(rb_intern("idle")), INT2NUM(stats->idle)); rb_hash_aset(info, ID2SYM(rb_intern("iowait")), INT2NUM(stats->iowait)); rb_hash_aset(info, ID2SYM(rb_intern("swap")), INT2NUM(stats->swap)); rb_hash_aset(info, ID2SYM(rb_intern("nice")), INT2NUM(stats->nice)); rb_hash_aset(info, ID2SYM(rb_intern("systime")), INT2NUM(stats->systime)); time_at = rb_funcall(rb_cTime, rb_intern("at"), 1, INT2NUM(stats->systime)); rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at); return info; }
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){ if((cpu_tmp=sg_get_cpu_stats())==NULL){ /* 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; if((cpu_tmp=sg_get_cpu_stats())==NULL){ 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; }
void populate_cpu() { if (use_cpu_percent) { sg_cpu_percents *cpu_p = sg_get_cpu_percents(); if (cpu_p != NULL) { add_stat(FLOAT, &cpu_p->user, "cpu", "user", NULL); add_stat(FLOAT, &cpu_p->kernel, "cpu", "kernel", NULL); add_stat(FLOAT, &cpu_p->idle, "cpu", "idle", NULL); add_stat(FLOAT, &cpu_p->iowait, "cpu", "iowait", NULL); add_stat(FLOAT, &cpu_p->swap, "cpu", "swap", NULL); add_stat(FLOAT, &cpu_p->nice, "cpu", "nice", NULL); add_stat(TIME_T, &cpu_p->time_taken, "cpu", "time_taken", NULL); } } else { sg_cpu_stats *cpu_s; cpu_s = use_diffs ? sg_get_cpu_stats_diff() : sg_get_cpu_stats(); if (cpu_s != NULL) { add_stat(LONG_LONG, &cpu_s->user, "cpu", "user", NULL); add_stat(LONG_LONG, &cpu_s->kernel, "cpu", "kernel", NULL); add_stat(LONG_LONG, &cpu_s->idle, "cpu", "idle", NULL); add_stat(LONG_LONG, &cpu_s->iowait, "cpu", "iowait", NULL); add_stat(LONG_LONG, &cpu_s->swap, "cpu", "swap", NULL); add_stat(LONG_LONG, &cpu_s->nice, "cpu", "nice", NULL); add_stat(LONG_LONG, &cpu_s->total, "cpu", "total", NULL); add_stat(TIME_T, &cpu_s->systime, "cpu", "systime", NULL); } } }
static void populate_cpu(void) { sg_cpu_stats *cpu_s; cpu_s = use_diffs ? sg_get_cpu_stats_diff(NULL) : sg_get_cpu_stats(NULL); if (use_cpu_percent) { sg_cpu_percent_source cps = use_diffs ? sg_last_diff_cpu_percent : sg_entire_cpu_percent; sg_cpu_percents *cpu_p = sg_get_cpu_percents_of(cps, NULL); if (cpu_p != NULL) { add_stat(DOUBLE, &cpu_p->user, "cpu", "user", NULL); add_stat(DOUBLE, &cpu_p->kernel, "cpu", "kernel", NULL); add_stat(DOUBLE, &cpu_p->idle, "cpu", "idle", NULL); add_stat(DOUBLE, &cpu_p->iowait, "cpu", "iowait", NULL); add_stat(DOUBLE, &cpu_p->swap, "cpu", "swap", NULL); add_stat(DOUBLE, &cpu_p->nice, "cpu", "nice", NULL); add_stat(TIME_T, &cpu_p->time_taken, "cpu", "time_taken", NULL); } } else { if (cpu_s != NULL) { add_stat(UNSIGNED_LONG_LONG, &cpu_s->user, "cpu", "user", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->kernel, "cpu", "kernel", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->idle, "cpu", "idle", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->iowait, "cpu", "iowait", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->swap, "cpu", "swap", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->nice, "cpu", "nice", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->total, "cpu", "total", NULL); add_stat(TIME_T, &cpu_s->systime, "cpu", "systime", NULL); } } if (cpu_s != NULL) { add_stat(UNSIGNED_LONG_LONG, &cpu_s->context_switches, "cpu", "ctxsw", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->voluntary_context_switches, "cpu", "vctxsw", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->involuntary_context_switches, "cpu", "nvctxsw", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->syscalls, "cpu", "syscalls", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->interrupts, "cpu", "intrs", NULL); add_stat(UNSIGNED_LONG_LONG, &cpu_s->soft_interrupts, "cpu", "softintrs", NULL); } }
static int cpu_read (void) { #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE int cpu; kern_return_t status; #if PROCESSOR_CPU_LOAD_INFO processor_cpu_load_info_data_t cpu_info; mach_msg_type_number_t cpu_info_len; #endif #if PROCESSOR_TEMPERATURE processor_info_data_t cpu_temp; mach_msg_type_number_t cpu_temp_len; #endif host_t cpu_host; for (cpu = 0; cpu < cpu_list_len; cpu++) { #if PROCESSOR_CPU_LOAD_INFO derive_t derives[CPU_SUBMIT_MAX] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; memset(derives, -1, sizeof(derives)); cpu_host = 0; cpu_info_len = PROCESSOR_BASIC_INFO_COUNT; if ((status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host, (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS) { ERROR ("cpu plugin: processor_info failed with status %i", (int) status); continue; } if (cpu_info_len < CPU_STATE_MAX) { ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len); continue; } derives[CPU_SUBMIT_USER] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_USER]; derives[CPU_SUBMIT_NICE] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_NICE]; derives[CPU_SUBMIT_SYSTEM] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]; derives[CPU_SUBMIT_IDLE] = (derive_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]; submit (cpu, derives); #endif /* PROCESSOR_CPU_LOAD_INFO */ #if PROCESSOR_TEMPERATURE /* * Not all Apple computers do have this ability. To minimize * the messages sent to the syslog we do an exponential * stepback if `processor_info' fails. We still try ~once a day * though.. */ if (cpu_temp_retry_counter > 0) { cpu_temp_retry_counter--; continue; } cpu_temp_len = PROCESSOR_INFO_MAX; status = processor_info (cpu_list[cpu], PROCESSOR_TEMPERATURE, &cpu_host, cpu_temp, &cpu_temp_len); if (status != KERN_SUCCESS) { ERROR ("cpu plugin: processor_info failed: %s", mach_error_string (status)); cpu_temp_retry_counter = cpu_temp_retry_step; cpu_temp_retry_step *= 2; if (cpu_temp_retry_step > cpu_temp_retry_max) cpu_temp_retry_step = cpu_temp_retry_max; continue; } if (cpu_temp_len != 1) { DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?", (int) cpu_temp_len); continue; } cpu_temp_retry_counter = 0; cpu_temp_retry_step = 1; #endif /* PROCESSOR_TEMPERATURE */ } submit_flush (); /* #endif PROCESSOR_CPU_LOAD_INFO */ #elif defined(KERNEL_LINUX) int cpu; FILE *fh; char buf[1024]; char *fields[9]; int numfields; if ((fh = fopen ("/proc/stat", "r")) == NULL) { char errbuf[1024]; ERROR ("cpu plugin: fopen (/proc/stat) failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } while (fgets (buf, 1024, fh) != NULL) { derive_t derives[CPU_SUBMIT_MAX] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; if (strncmp (buf, "cpu", 3)) continue; if ((buf[3] < '0') || (buf[3] > '9')) continue; numfields = strsplit (buf, fields, 9); if (numfields < 5) continue; cpu = atoi (fields[0] + 3); derives[CPU_SUBMIT_USER] = atoll(fields[1]); derives[CPU_SUBMIT_NICE] = atoll(fields[2]); derives[CPU_SUBMIT_SYSTEM] = atoll(fields[3]); derives[CPU_SUBMIT_IDLE] = atoll(fields[4]); if (numfields >= 8) { derives[CPU_SUBMIT_WAIT] = atoll(fields[5]); derives[CPU_SUBMIT_INTERRUPT] = atoll(fields[6]); derives[CPU_SUBMIT_SOFTIRQ] = atoll(fields[6]); if (numfields >= 9) derives[CPU_SUBMIT_STEAL] = atoll(fields[8]); } submit(cpu, derives); } submit_flush(); fclose (fh); /* #endif defined(KERNEL_LINUX) */ #elif defined(HAVE_LIBKSTAT) int cpu; static cpu_stat_t cs; if (kc == NULL) return (-1); for (cpu = 0; cpu < numcpu; cpu++) { derive_t derives[CPU_SUBMIT_MAX] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; if (kstat_read (kc, ksp[cpu], &cs) == -1) continue; /* error message? */ memset(derives, -1, sizeof(derives)); derives[CPU_SUBMIT_IDLE] = cs.cpu_sysinfo.cpu[CPU_IDLE]; derives[CPU_SUBMIT_USER] = cs.cpu_sysinfo.cpu[CPU_USER]; derives[CPU_SUBMIT_SYSTEM] = cs.cpu_sysinfo.cpu[CPU_KERNEL]; derives[CPU_SUBMIT_WAIT] = cs.cpu_sysinfo.cpu[CPU_WAIT]; submit (ksp[cpu]->ks_instance, derives); } submit_flush (); /* #endif defined(HAVE_LIBKSTAT) */ #elif CAN_USE_SYSCTL uint64_t cpuinfo[numcpu][CPUSTATES]; size_t cpuinfo_size; int status; int i; if (numcpu < 1) { ERROR ("cpu plugin: Could not determine number of " "installed CPUs using sysctl(3)."); return (-1); } memset (cpuinfo, 0, sizeof (cpuinfo)); #if defined(KERN_CPTIME2) if (numcpu > 1) { for (i = 0; i < numcpu; i++) { int mib[] = {CTL_KERN, KERN_CPTIME2, i}; cpuinfo_size = sizeof (cpuinfo[0]); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), cpuinfo[i], &cpuinfo_size, NULL, 0); if (status == -1) { char errbuf[1024]; ERROR ("cpu plugin: sysctl failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } } } else #endif /* defined(KERN_CPTIME2) */ { int mib[] = {CTL_KERN, KERN_CPTIME}; long cpuinfo_tmp[CPUSTATES]; cpuinfo_size = sizeof(cpuinfo_tmp); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &cpuinfo_tmp, &cpuinfo_size, NULL, 0); if (status == -1) { char errbuf[1024]; ERROR ("cpu plugin: sysctl failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } for(i = 0; i < CPUSTATES; i++) { cpuinfo[0][i] = cpuinfo_tmp[i]; } } for (i = 0; i < numcpu; i++) { derive_t derives[CPU_SUBMIT_MAX] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; derives[CPU_SUBMIT_USER] = cpuinfo[i][CP_USER]; derives[CPU_SUBMIT_NICE] = cpuinfo[i][CP_NICE]; derives[CPU_SUBMIT_SYSTEM] = cpuinfo[i][CP_SYS]; derives[CPU_SUBMIT_IDLE] = cpuinfo[i][CP_IDLE]; derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[i][CP_INTR]; submit(i, derives); } submit_flush(); /* #endif CAN_USE_SYSCTL */ #elif defined(HAVE_SYSCTLBYNAME) && defined(HAVE_SYSCTL_KERN_CP_TIMES) long cpuinfo[maxcpu][CPUSTATES]; size_t cpuinfo_size; int i; memset (cpuinfo, 0, sizeof (cpuinfo)); cpuinfo_size = sizeof (cpuinfo); if (sysctlbyname("kern.cp_times", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) { char errbuf[1024]; ERROR ("cpu plugin: sysctlbyname failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } for (i = 0; i < numcpu; i++) { derive_t derives[CPU_SUBMIT_MAX] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; derives[CPU_SUBMIT_USER] = cpuinfo[i][CP_USER]; derives[CPU_SUBMIT_NICE] = cpuinfo[i][CP_NICE]; derives[CPU_SUBMIT_SYSTEM] = cpuinfo[i][CP_SYS]; derives[CPU_SUBMIT_IDLE] = cpuinfo[i][CP_IDLE]; derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[i][CP_INTR]; submit(i, derives); } submit_flush(); /* #endif HAVE_SYSCTL_KERN_CP_TIMES */ #elif defined(HAVE_SYSCTLBYNAME) long cpuinfo[CPUSTATES]; size_t cpuinfo_size; derive_t derives[CPU_SUBMIT_MAX] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; cpuinfo_size = sizeof (cpuinfo); if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) { char errbuf[1024]; ERROR ("cpu plugin: sysctlbyname failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } derives[CPU_SUBMIT_USER] = cpuinfo[CP_USER]; derives[CPU_SUBMIT_SYSTEM] = cpuinfo[CP_SYS]; derives[CPU_SUBMIT_NICE] = cpuinfo[CP_NICE]; derives[CPU_SUBMIT_IDLE] = cpuinfo[CP_IDLE]; derives[CPU_SUBMIT_INTERRUPT] = cpuinfo[CP_INTR]; submit(0, derives); submit_flush(); /* #endif HAVE_SYSCTLBYNAME */ #elif defined(HAVE_LIBSTATGRAB) sg_cpu_stats *cs; derive_t derives[CPU_SUBMIT_MAX] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; cs = sg_get_cpu_stats (); if (cs == NULL) { ERROR ("cpu plugin: sg_get_cpu_stats failed."); return (-1); } derives[CPU_SUBMIT_IDLE] = (derive_t) cs->idle; derives[CPU_SUBMIT_NICE] = (derive_t) cs->nice; derives[CPU_SUBMIT_SWAP] = (derive_t) cs->swap; derives[CPU_SUBMIT_SYSTEM] = (derive_t) cs->kernel; derives[CPU_SUBMIT_USER] = (derive_t) cs->user; derives[CPU_SUBMIT_WAIT] = (derive_t) cs->iowait; submit(0, derives); submit_flush(); /* #endif HAVE_LIBSTATGRAB */ #elif defined(HAVE_PERFSTAT) perfstat_id_t id; int i, cpus; numcpu = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0); if(numcpu == -1) { char errbuf[1024]; WARNING ("cpu plugin: perfstat_cpu: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } if (pnumcpu != numcpu || perfcpu == NULL) { if (perfcpu != NULL) free(perfcpu); perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t)); } pnumcpu = numcpu; id.name[0] = '\0'; if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0) { char errbuf[1024]; WARNING ("cpu plugin: perfstat_cpu: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } for (i = 0; i < cpus; i++) { derive_t derives[CPU_SUBMIT_MAX] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; derives[CPU_SUBMIT_IDLE] = perfcpu[i].idle; derives[CPU_SUBMIT_SYSTEM] = perfcpu[i].sys; derives[CPU_SUBMIT_USER] = perfcpu[i].user; derives[CPU_SUBMIT_WAIT] = perfcpu[i].wait; submit(i, derives); } submit_flush(); #endif /* HAVE_PERFSTAT */ return (0); }
static int cpu_read (void) { #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE int cpu; kern_return_t status; #if PROCESSOR_CPU_LOAD_INFO processor_cpu_load_info_data_t cpu_info; mach_msg_type_number_t cpu_info_len; #endif #if PROCESSOR_TEMPERATURE processor_info_data_t cpu_temp; mach_msg_type_number_t cpu_temp_len; #endif host_t cpu_host; for (cpu = 0; cpu < cpu_list_len; cpu++) { #if PROCESSOR_CPU_LOAD_INFO cpu_host = 0; cpu_info_len = PROCESSOR_BASIC_INFO_COUNT; if ((status = processor_info (cpu_list[cpu], PROCESSOR_CPU_LOAD_INFO, &cpu_host, (processor_info_t) &cpu_info, &cpu_info_len)) != KERN_SUCCESS) { ERROR ("cpu plugin: processor_info failed with status %i", (int) status); continue; } if (cpu_info_len < CPU_STATE_MAX) { ERROR ("cpu plugin: processor_info returned only %i elements..", cpu_info_len); continue; } submit (cpu, "user", (counter_t) cpu_info.cpu_ticks[CPU_STATE_USER]); submit (cpu, "nice", (counter_t) cpu_info.cpu_ticks[CPU_STATE_NICE]); submit (cpu, "system", (counter_t) cpu_info.cpu_ticks[CPU_STATE_SYSTEM]); submit (cpu, "idle", (counter_t) cpu_info.cpu_ticks[CPU_STATE_IDLE]); #endif /* PROCESSOR_CPU_LOAD_INFO */ #if PROCESSOR_TEMPERATURE /* * Not all Apple computers do have this ability. To minimize * the messages sent to the syslog we do an exponential * stepback if `processor_info' fails. We still try ~once a day * though.. */ if (cpu_temp_retry_counter > 0) { cpu_temp_retry_counter--; continue; } cpu_temp_len = PROCESSOR_INFO_MAX; status = processor_info (cpu_list[cpu], PROCESSOR_TEMPERATURE, &cpu_host, cpu_temp, &cpu_temp_len); if (status != KERN_SUCCESS) { ERROR ("cpu plugin: processor_info failed: %s", mach_error_string (status)); cpu_temp_retry_counter = cpu_temp_retry_step; cpu_temp_retry_step *= 2; if (cpu_temp_retry_step > cpu_temp_retry_max) cpu_temp_retry_step = cpu_temp_retry_max; continue; } if (cpu_temp_len != 1) { DEBUG ("processor_info (PROCESSOR_TEMPERATURE) returned %i elements..?", (int) cpu_temp_len); continue; } cpu_temp_retry_counter = 0; cpu_temp_retry_step = 1; DEBUG ("cpu_temp = %i", (int) cpu_temp); #endif /* PROCESSOR_TEMPERATURE */ } /* #endif PROCESSOR_CPU_LOAD_INFO */ #elif defined(KERNEL_LINUX) int cpu; counter_t user, nice, syst, idle; counter_t wait, intr, sitr; /* sitr == soft interrupt */ FILE *fh; char buf[1024]; char *fields[9]; int numfields; if ((fh = fopen ("/proc/stat", "r")) == NULL) { char errbuf[1024]; ERROR ("cpu plugin: fopen (/proc/stat) failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } while (fgets (buf, 1024, fh) != NULL) { if (strncmp (buf, "cpu", 3)) continue; if ((buf[3] < '0') || (buf[3] > '9')) continue; numfields = strsplit (buf, fields, 9); if (numfields < 5) continue; cpu = atoi (fields[0] + 3); user = atoll (fields[1]); nice = atoll (fields[2]); syst = atoll (fields[3]); idle = atoll (fields[4]); submit (cpu, "user", user); submit (cpu, "nice", nice); submit (cpu, "system", syst); submit (cpu, "idle", idle); if (numfields >= 8) { wait = atoll (fields[5]); intr = atoll (fields[6]); sitr = atoll (fields[7]); submit (cpu, "wait", wait); submit (cpu, "interrupt", intr); submit (cpu, "softirq", sitr); if (numfields >= 9) submit (cpu, "steal", atoll (fields[8])); } } fclose (fh); /* #endif defined(KERNEL_LINUX) */ #elif defined(HAVE_LIBKSTAT) int cpu; counter_t user, syst, idle, wait; static cpu_stat_t cs; if (kc == NULL) return (-1); for (cpu = 0; cpu < numcpu; cpu++) { if (kstat_read (kc, ksp[cpu], &cs) == -1) continue; /* error message? */ idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE]; user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER]; syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL]; wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT]; submit (ksp[cpu]->ks_instance, "user", user); submit (ksp[cpu]->ks_instance, "system", syst); submit (ksp[cpu]->ks_instance, "idle", idle); submit (ksp[cpu]->ks_instance, "wait", wait); } /* #endif defined(HAVE_LIBKSTAT) */ #elif CAN_USE_SYSCTL uint64_t cpuinfo[numcpu][CPUSTATES]; size_t cpuinfo_size; int status; int i; if (numcpu < 1) { ERROR ("cpu plugin: Could not determine number of " "installed CPUs using sysctl(3)."); return (-1); } memset (cpuinfo, 0, sizeof (cpuinfo)); #if defined(KERN_CPTIME2) if (numcpu > 1) { for (i = 0; i < numcpu; i++) { int mib[] = {CTL_KERN, KERN_CPTIME2, i}; cpuinfo_size = sizeof (cpuinfo[0]); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), cpuinfo[i], &cpuinfo_size, NULL, 0); if (status == -1) { char errbuf[1024]; ERROR ("cpu plugin: sysctl failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } } } else #endif /* defined(KERN_CPTIME2) */ { int mib[] = {CTL_KERN, KERN_CPTIME}; long cpuinfo_tmp[CPUSTATES]; cpuinfo_size = sizeof(cpuinfo_tmp); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &cpuinfo_tmp, &cpuinfo_size, NULL, 0); if (status == -1) { char errbuf[1024]; ERROR ("cpu plugin: sysctl failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } for(i = 0; i < CPUSTATES; i++) { cpuinfo[0][i] = cpuinfo_tmp[i]; } } for (i = 0; i < numcpu; i++) { submit (i, "user", cpuinfo[i][CP_USER]); submit (i, "nice", cpuinfo[i][CP_NICE]); submit (i, "system", cpuinfo[i][CP_SYS]); submit (i, "idle", cpuinfo[i][CP_IDLE]); submit (i, "interrupt", cpuinfo[i][CP_INTR]); } /* #endif CAN_USE_SYSCTL */ #elif defined(HAVE_SYSCTLBYNAME) long cpuinfo[CPUSTATES]; size_t cpuinfo_size; cpuinfo_size = sizeof (cpuinfo); if (sysctlbyname("kern.cp_time", &cpuinfo, &cpuinfo_size, NULL, 0) < 0) { char errbuf[1024]; ERROR ("cpu plugin: sysctlbyname failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } submit (0, "user", cpuinfo[CP_USER]); submit (0, "nice", cpuinfo[CP_NICE]); submit (0, "system", cpuinfo[CP_SYS]); submit (0, "idle", cpuinfo[CP_IDLE]); submit (0, "interrupt", cpuinfo[CP_INTR]); /* #endif HAVE_SYSCTLBYNAME */ #elif defined(HAVE_LIBSTATGRAB) sg_cpu_stats *cs; cs = sg_get_cpu_stats (); if (cs == NULL) { ERROR ("cpu plugin: sg_get_cpu_stats failed."); return (-1); } submit (0, "idle", (counter_t) cs->idle); submit (0, "nice", (counter_t) cs->nice); submit (0, "swap", (counter_t) cs->swap); submit (0, "system", (counter_t) cs->kernel); submit (0, "user", (counter_t) cs->user); submit (0, "wait", (counter_t) cs->iowait); #endif /* HAVE_LIBSTATGRAB */ return (0); }