char * loadavg(void) { double avgs[3]; if (getloadavg(avgs, 3) < 0) { perror("getloadavg"); exit(1); } return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); }
void load_check(void) { /* * XXX: I know that checking loadavg is probably a bad yardstick to * measure system usage with, but it's a good indicator of how busy a * system really is, even though it can generate some nasty false * positives that are hard to explain. With measuring load avg a * system can seem loaded while it in fact is just doing nothing. */ double currload[3]; char *buf; getloadavg(currload, 3); #if 0 if (nodaemon) { fprintf(stderr, "Current load: %6.2f\n", currload[0]); } #endif if (maxload == 0) { dbprintf("maxload was 0, setting it to %d\n", numcpus); maxload = numcpus; } if (currload[0] > maxload) { if (nodaemon) { dbprintf("%6.2f > %6.2f : maxload triggered\n", currload[0], maxload); } status.alert_load = true; } else { if (status.alert_load) { dbprintf("Alert passed, resetting things...\n"); } status.alert_load = false; if (alert_once) { /* reset toggle */ status_alerted.alert_load = false; } } if ((asprintf(&buf, "%6.2f", currload[0])) < 0) { perror("load_check: asprintf"); } if (!database_insert_stat("load", buf, status_alerted.alert_load)) { dbprintf("Skipping due to condition\n"); } free(buf); return; }
void Platform_getLoadAverage(double* one, double* five, double* fifteen) { double results[3]; if(3 == getloadavg(results, 3)) { *one = results[0]; *five = results[1]; *fifteen = results[2]; } else { *one = 0; *five = 0; *fifteen = 0; } }
void qstime::stop() { getrusage(0,&ru); t_stop=ru; qstime_tovector.clock_user=t_stop.ru_utime.tv_usec-t_start.ru_utime.tv_usec; qstime_tovector.clock_system=t_stop.ru_stime.tv_usec-t_start.ru_stime.tv_usec; qstime_tovector.ru_stop=ru; getloadavg(qstime_tovector.cpuload,3); zeitblock.push_back(qstime_tovector); m++; }
CAMLprim value getloadavg_stub (value v_unit __unused) { CAMLparam0(); CAMLlocal1(v_ret); double loadavg[3]; int ret = getloadavg(loadavg,3); if (ret < 0) uerror("getloadavg",Nothing); v_ret = caml_alloc_tuple(3); Store_field(v_ret, 2, caml_copy_double(ret >= 3 ? loadavg[2] : NAN)); Store_field(v_ret, 1, caml_copy_double(ret >= 2 ? loadavg[1] : NAN)); Store_field(v_ret, 0, caml_copy_double(ret >= 1 ? loadavg[0] : NAN)); CAMLreturn(v_ret); }
/* set new number of workers */ int adjust_number_of_worker(int min, int max, int cur_workers, int cur_jobs) { int perc_running; int idle; int target = min; double load[3]; if(cur_workers == 0) { gm_log( GM_LOG_TRACE3, "adjust_number_of_worker(min %d, max %d, worker %d, jobs %d) -> %d\n", min, max, cur_workers, cur_jobs, mod_gm_opt->min_worker); return mod_gm_opt->min_worker; } perc_running = (int)cur_jobs*100/cur_workers; idle = (int)cur_workers - cur_jobs; gm_log( GM_LOG_TRACE3, "adjust_number_of_worker(min %d, max %d, worker %d, jobs %d) = %d%% running\n", min, max, cur_workers, cur_jobs, perc_running); if(cur_workers == max) return max; /* > 90% workers running */ if(cur_jobs > 0 && ( perc_running > 90 || idle <= 2 )) { if (getloadavg(load, 3) == -1) { gm_log( GM_LOG_ERROR, "failed to get current load\n"); perror("getloadavg"); } if(mod_gm_opt->load_limit1 > 0 && load[0] >= mod_gm_opt->load_limit1) { gm_log( GM_LOG_TRACE, "load limit 1min hit, not starting any more workers: %1.2f > %1.2f\n", load[0], mod_gm_opt->load_limit1); return cur_workers; } if(mod_gm_opt->load_limit5 > 0 && load[1] >= mod_gm_opt->load_limit5) { gm_log( GM_LOG_TRACE, "load limit 5min hit, not starting any more workers: %1.2f > %1.2f\n", load[1], mod_gm_opt->load_limit5); return cur_workers; } if(mod_gm_opt->load_limit15 > 0 && load[2] >= mod_gm_opt->load_limit15) { gm_log( GM_LOG_TRACE, "load limit 15min hit, not starting any more workers: %1.2f > %1.2f\n", load[2], mod_gm_opt->load_limit15); return cur_workers; } /* increase target number by spawn rate */ gm_log( GM_LOG_TRACE, "starting %d new workers\n", mod_gm_opt->spawn_rate); target = cur_workers + mod_gm_opt->spawn_rate; } /* dont go over the top */ if(target > max) { target = max; } if(target != cur_workers) gm_log( GM_LOG_TRACE3, "adjust_number_of_worker(min %d, max %d, worker %d, jobs %d) = %d%% running -> %d\n", min, max, cur_workers, cur_jobs, perc_running, target); return target; }
// Returns load struct with average system loads for the last // 1, 5 and 15 minutes respectively. // Load values should be interpreted as usual average loads from // uptime(1). inline Try<Load> loadavg() { double loadArray[3]; if (getloadavg(loadArray, 3) == -1) { return ErrnoError("Failed to determine system load averages"); } Load load; load.one = loadArray[0]; load.five = loadArray[1]; load.fifteen = loadArray[2]; return load; }
/* * la: print the load average. */ int main() { unsigned vec[3]; if (getloadavg(vec, 3) < 0) { perror("la: getloadavg"); return 1; } printf("load %u.%02u %u.%02u %u.%02u\n", vec[0] / 100, vec[0] % 100, vec[1] / 100, vec[1] % 100, vec[2] / 100, vec[2] % 100); return 0; }
static int _sysinfo_loads_generic(struct sysinfo * info) { double la[3]; if(getloadavg(la, 3) != 3) { memset(info->loads, 0, sizeof(info->loads)); return 1; } info->loads[0] = la[0]; info->loads[1] = la[1]; info->loads[2] = la[2]; return 0; }
int CpuSummary() { double monoload[3]; if ( getloadavg(monoload,3)<1) return INVALID_VALUE; double numcpus=1; if (HaveSystemInfo) numcpus=(double)SystemInfo_Cpus(); LKASSERT(numcpus>0); int summary=(int)(monoload[0]*100/numcpus); if (summary>999 || summary<0) summary=INVALID_VALUE; return summary; }
size_t loadavg(char* dest, size_t n) { size_t ret; double avgs[3]; if (!getloadavg(avgs, 3)) die("getloadavg failed: %s\n", strerror(errno)); if ((ret = snprintf(dest, n, "%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2])) > n) ret = n; return ret; }
/* get memory stats */ void getload(long flag) { /* struct loadavg load; size_t load_s; load_s = sizeof(load); if ( sysctlbyname("vm.loadavg", &load, &load_s, NULL, 0) == -1 ) exit -1; */ double load[3]; getloadavg(load, flag); printf("%.2f\t%.2f\t%.2f\n", load[0], load[1], load[2]); }
void print_load(yajl_gen json_gen, char *buffer, const char *format, const int max_threshold) { char *outwalk = buffer; /* Get load */ #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__) double loadavg[3]; const char *walk; bool colorful_output = false; if (getloadavg(loadavg, 3) == -1) goto error; for (walk = format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; continue; } if (loadavg[0] >= max_threshold) { START_COLOR("color_bad"); colorful_output = true; } if (BEGINS_WITH(walk+1, "1min")) { outwalk += sprintf(outwalk, "%1.2f", loadavg[0]); walk += strlen("1min"); } if (BEGINS_WITH(walk+1, "5min")) { outwalk += sprintf(outwalk, "%1.2f", loadavg[1]); walk += strlen("5min"); } if (BEGINS_WITH(walk+1, "15min")) { outwalk += sprintf(outwalk, "%1.2f", loadavg[2]); walk += strlen("15min"); } if (colorful_output) END_COLOR; } *outwalk = '\0'; OUTPUT_FULL_TEXT(buffer); return; error: #endif OUTPUT_FULL_TEXT("cant read load"); (void)fputs("i3status: Cannot read system load using getloadavg()\n", stderr); }
void display(void) { uint64_t arc_stat; int i, j; /* Get the load average over the last minute. */ (void) getloadavg(avenrun, nitems(avenrun)); (*curcmd->c_fetch)(); if (curcmd->c_flags & CF_LOADAV) { j = 5.0*avenrun[0] + 0.5; dellave -= avenrun[0]; if (dellave >= 0.0) c = '<'; else { c = '>'; dellave = -dellave; } if (dellave < 0.1) c = '|'; dellave = avenrun[0]; wmove(wload, 0, 0); wclrtoeol(wload); for (i = MIN(j, 50); i > 0; i--) waddch(wload, c); if (j > 50) wprintw(wload, " %4.1f", avenrun[0]); } if (curcmd->c_flags & CF_ZFSARC) { uint64_t arc[7] = {}; size_t size = sizeof(arc[0]); if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arc[0], &size, NULL, 0) == 0 ) { GETSYSCTL("vfs.zfs.mfu_size", arc[1]); GETSYSCTL("vfs.zfs.mru_size", arc[2]); GETSYSCTL("vfs.zfs.anon_size", arc[3]); GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc[4]); GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc[5]); GETSYSCTL("kstat.zfs.misc.arcstats.bonus_size", arc[6]); GETSYSCTL("kstat.zfs.misc.arcstats.dnode_size", arc_stat); arc[6] += arc_stat; GETSYSCTL("kstat.zfs.misc.arcstats.dbuf_size", arc_stat); arc[6] += arc_stat; wmove(wload, 0, 0); wclrtoeol(wload); for (i = 0 ; i < nitems(arc); i++) { if (arc[i] > 10llu * 1024 * 1024 * 1024 ) { wprintw(wload, "%7lluG", arc[i] >> 30); } else if (arc[i] > 10 * 1024 * 1024 ) {
static ngx_int_t ngx_getloadavg(ngx_int_t avg[], ngx_int_t nelem, ngx_log_t *log) { double loadavg[3]; ngx_int_t i; if (getloadavg(loadavg, nelem) == -1) { return NGX_ERROR; } for (i = 0; i < nelem; i ++) { avg[i] = loadavg[i] * 1000; } return NGX_OK; }
static double getloadavg_15s() { double avg[3]; int loads; loads = getloadavg(avg, 3); if(loads > 2) return avg[2]; else if(loads > 1) return avg[1]; else if(loads > 0) return avg[0]; else return -1; }
void glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf) { #ifndef HAVE_GETLOADAVG kstat_ctl_t *kc; kstat_t *ksp; size_t i; static const char avestrings[][14] = { "avenrun_1min", "avenrun_5min", "avenrun_15min" }; #endif memset (buf, 0, sizeof (glibtop_loadavg)); #ifdef HAVE_GETLOADAVG if (getloadavg (buf->loadavg, 3) != 3) return; #else if(!(kc = server->machine->kc)) return; switch(kstat_chain_update(kc)) { case -1: assert(0); /* Debugging, shouldn't happen */ case 0: break; default: glibtop_get_kstats(server); } if(!(ksp = server->machine->system)) return; if(kstat_read(kc, ksp, NULL) < 0) return; for(i = 0; i < 3; ++i) /* Do we have a countof macro? */ { kstat_named_t *kn; kn = (kstat_named_t *)kstat_data_lookup(ksp, avestrings[i]); if(kn) buf->loadavg[i] = (double)kn->value.ul / FSCALE; } #endif /* HAVE_GETLOADAVG */ buf->flags = _glibtop_sysdeps_loadavg; }
/** * This routine returns 'nelem' double precision floats containing * the load averages in 'loadv'; at most 3 values will be returned. * @param loadv destination of the load averages * @param nelem number of averages * @return: 0 if successful, -1 if failed (and all load averages are 0). */ int getloadavg_sysdep(double *loadv, int nelem) { #ifdef HAVE_GETLOADAVG return getloadavg(loadv, nelem); #else char buf[STRLEN]; double load[3]; if (! read_proc_file(buf, sizeof(buf), "loadavg", -1, NULL)) return -1; if (sscanf(buf, "%lf %lf %lf", &load[0], &load[1], &load[2]) != 3) { DEBUG("system statistic error -- cannot get load average\n"); return -1; } for (int i = 0; i < nelem; i++) loadv[i] = load[i]; return 0; #endif }
int readCpuCounters(SFLHost_cpu_counters *cpu) { int gotData = NO; uint64_t val64; size_t len; double load[3]; long cp_time[CPUSTATES]; uint32_t stathz = sysconf(_SC_CLK_TCK); #define STATHZ_TO_MS(t) (((t) * 1000) / stathz) getloadavg(load, 3); cpu->load_one = (float)load[0]; cpu->load_five = (float)load[1]; cpu->load_fifteen = (float)load[2]; cpu->proc_run = (uint32_t)getRunningProcesses(); cpu->proc_total = (uint32_t)getTotalProcesses(); if (getSys64("hw.ncpu", &val64)) cpu->cpu_num = (uint32_t)val64; cpu->cpu_speed = (uint32_t) getCpuSpeed(); /* puts kern.cp_time array into cp_time */ /* constants are defined in sys/dkstat.h */ len = sizeof(cp_time); if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) != -1) { // len should be 20. is it really an array of long, though? // might want to just read up to 40 bytes and then see what we get. // myLog(LOG_INFO, "kerm.cp_time len=%u", len); cpu->cpu_user = STATHZ_TO_MS(cp_time[CP_USER]); cpu->cpu_nice = STATHZ_TO_MS(cp_time[CP_NICE]); cpu->cpu_system = STATHZ_TO_MS(cp_time[CP_SYS]); cpu->cpu_idle = STATHZ_TO_MS(cp_time[CP_IDLE]); cpu->cpu_intr = STATHZ_TO_MS(cp_time[CP_INTR]); } cpu->cpu_wio = (uint32_t)-1; // unsupported // note "vm.stats.sys.v_soft" gives us the number of soft interrupts, not the time spent cpu->cpu_sintr = (uint32_t)-1; // unsupported if(getSys64("vm.stats.sys.v_intr", &val64)) cpu->interrupts = (uint32_t)val64; if(getSys64("vm.stats.sys.v_swtch", &val64)) cpu->contexts = (uint32_t)val64; gotData = YES; return gotData; }
void BalanccClient::SendLoad( ) { if( !isConnected( )) return; double load[3]; if( getloadavg( load, 3 ) == -1 ) { LogError( "cannot get loadavg" ); } char buf[32]; snprintf( buf, sizeof( buf ), "load %f\n", load[0] ); if( !Send( buf, strlen( buf ))) { LogError( "error sending load avg" ); } }
int wproc_can_spawn(struct load_control *lc) { unsigned int old = 0; time_t now; /* if no load control is enabled, we can safely run this job */ if (!(lc->options & LOADCTL_ENABLED)) return 1; now = time(NULL); if (lc->last_check + lc->check_interval > now) { lc->last_check = now; if (getloadavg(lc->load, 3) < 0) return lc->jobs_limit > lc->jobs_running; if (lc->load[0] > lc->backoff_limit) { old = lc->jobs_limit; lc->jobs_limit -= lc->backoff_change; } else if (lc->load[0] < lc->rampup_limit) { old = lc->jobs_limit; lc->jobs_limit += lc->rampup_change; } if (lc->jobs_limit > lc->jobs_max) { lc->jobs_limit = lc->jobs_max; } else if (lc->jobs_limit < lc->jobs_min) { logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Tried to set jobs_limit to %u, below jobs_min (%u)\n", lc->jobs_limit, lc->jobs_min); lc->jobs_limit = lc->jobs_min; } if (old && old != lc->jobs_limit) { if (lc->jobs_limit < old) { logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: loadctl.jobs_limit changed from %u to %u\n", old, lc->jobs_limit); } else { logit(NSLOG_INFO_MESSAGE, FALSE, "wproc: loadctl.jobs_limit changed from %u to %u\n", old, lc->jobs_limit); } } } return lc->jobs_limit > lc->jobs_running; }
/** * Wrapper for getloadavg() that tries to return all 3 samples, and reports * -1 for those samples that are not available. * * Averages are over the last 1, 5, and 15 minutes, respectively. **/ void dcc_getloadavg(double loadavg[3]) { int num; int i; #if defined(HAVE_GETLOADAVG) num = getloadavg(loadavg, 3); #else num = 0; #endif /* If getloadavg() didn't return 3 we want to fill * in the invalid elements with -1 */ if (num < 0) num = 0; for (i=num; i < 3; ++i) loadavg[i] = -1; }
JNIEXPORT double JNICALL Java_com_caucho_v5_server_util_JniCauchoSystemImpl_nativeGetLoadAvg(JNIEnv *env, jobject obj) { double avg[3]; #ifndef WIN32 getloadavg(avg, 3); #else avg[0] = -1; #endif return avg[0]; }
static int SYSTEM_CPU_LOAD15(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { double load[3]; assert(result); init_result(result); if(getloadavg(load, 3)) { SET_DBL_RESULT(result, load[2]); return SYSINFO_RET_OK; } else { return SYSINFO_RET_FAIL; } }
/** * \addtogroup myth_network_protocol * \par QUERY_LOAD * Returns the Unix load on this backend * (three floats - the average over 1, 5 and 15 mins). */ bool BaseRequestHandler::HandleQueryLoad(SocketHandler *sock) { QStringList strlist; double loads[3]; if (getloadavg(loads,3) == -1) { strlist << "ERROR"; strlist << "getloadavg() failed"; } else strlist << QString::number(loads[0]) << QString::number(loads[1]) << QString::number(loads[2]); sock->WriteStringList(strlist); return true; }
/* when copying local-to-local, make sure to cut estimate by half */ int pfl_getnfreecores(int want) { int nstr = want; #ifdef HAVE_GETLOADAVG { double avg; if (getloadavg(&avg, 1) == -1) psclog_warn("getloadavg"); // XXX round up nstr = MAX(1, nstr - avg); } #endif return (nstr); }
static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) { #if defined(HAVE_SYSINFO) struct sysinfo sys_info; if (sysinfo(&sys_info)) { ast_log(LOG_ERROR, "FAILED to retrieve system information\n"); return -1; } #endif if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n"); return -1; } else if (!strcasecmp("loadavg", data)) { double curloadavg; getloadavg(&curloadavg, 1); snprintf(buf, len, "%f", curloadavg); } else if (!strcasecmp("numcalls", data)) { snprintf(buf, len, "%d", ast_active_calls()); } #if defined(HAVE_SYSINFO) else if (!strcasecmp("uptime", data)) { /* in hours */ snprintf(buf, len, "%ld", sys_info.uptime/3600); } else if (!strcasecmp("totalram", data)) { /* in KiB */ snprintf(buf, len, "%lu",(sys_info.totalram * sys_info.mem_unit)/1024); } else if (!strcasecmp("freeram", data)) { /* in KiB */ snprintf(buf, len, "%lu",(sys_info.freeram * sys_info.mem_unit)/1024); } else if (!strcasecmp("bufferram", data)) { /* in KiB */ snprintf(buf, len, "%lu",(sys_info.bufferram * sys_info.mem_unit)/1024); } else if (!strcasecmp("totalswap", data)) { /* in KiB */ snprintf(buf, len, "%lu",(sys_info.totalswap * sys_info.mem_unit)/1024); } else if (!strcasecmp("freeswap", data)) { /* in KiB */ snprintf(buf, len, "%lu",(sys_info.freeswap * sys_info.mem_unit)/1024); } else if (!strcasecmp("numprocs", data)) { snprintf(buf, len, "%d", sys_info.procs); } #endif else { ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data); return -1; } return 0; }
static void get_loadavg(Enna_Buffer *b) { float load; #ifdef __FreeBSD__ double loadavg[3]; if (getloadavg(loadavg, 3) == -1) return; load = loadavg[0] * 100.0; enna_buffer_append(b, "<hilight>"); enna_buffer_append(b, _("CPU Load:")); enna_buffer_appendf(b, "</hilight> %d%%<br>", (int) load); #else /* __FreeBSD__ */ FILE *f; char buf[256] = { 0 }; char *ld, *x; f = fopen("/proc/loadavg", "r"); if (!f) return; x = fgets(buf, sizeof(buf), f); x = strchr(buf, ' '); if (!x) goto err_loadavg; ld = strndup(buf, sizeof(x)); load = enna_util_atof(ld) * 100.0; enna_buffer_append(b, "<hilight>"); enna_buffer_append(b, _("CPU Load:")); enna_buffer_appendf(b, "</hilight> %d%%<br>", (int) load); err_loadavg: if (ld) free(ld); if (f) fclose(f); #endif /* !__FreeBSD__ */ }
int SYSTEM_CPU_LOAD(AGENT_REQUEST *request, AGENT_RESULT *result) { char *tmp; int mode, per_cpu = 1, cpu_num; double load[ZBX_AVG_COUNT], value; if (2 < request->nparam) return SYSINFO_RET_FAIL; tmp = get_rparam(request, 0); if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "all")) per_cpu = 0; else if (0 != strcmp(tmp, "percpu")) return SYSINFO_RET_FAIL; tmp = get_rparam(request, 1); if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "avg1")) mode = ZBX_AVG1; else if (0 == strcmp(tmp, "avg5")) mode = ZBX_AVG5; else if (0 == strcmp(tmp, "avg15")) mode = ZBX_AVG15; else return SYSINFO_RET_FAIL; if (mode >= getloadavg(load, 3)) return SYSINFO_RET_FAIL; value = load[mode]; if (1 == per_cpu) { if (0 >= (cpu_num = get_cpu_num(1))) return SYSINFO_RET_FAIL; value /= cpu_num; } SET_DBL_RESULT(result, value); return SYSINFO_RET_OK; }
EXTL_EXPORT ExtlTab statusd_getloadavg() { ExtlTab t=extl_create_table(); #ifndef CF_NO_GETLOADAVG double l[3]; int n; n=getloadavg(l, 3); if(n>=1) extl_table_sets_d(t, "1min", l[0]); if(n>=2) extl_table_sets_d(t, "5min", l[1]); if(n>=3) extl_table_sets_d(t, "15min", l[2]); #endif return t; }