static int _get_mem_usage (pctx_t ctx, double *fp) { uint64_t kfree, ktot; if (proc_meminfo (ctx, &ktot, &kfree) < 0) { err ("error reading memory usage from proc"); return -1; } *fp = ((double)(ktot - kfree) / (double)(ktot)) * 100.0; return 0; }
static int _sysstat (pctx_t ctx, char *buf, int len) { static uint64_t cpuusage = 0, cputot = 0; double cpupct, mempct; uint64_t ktot, kfree; int ret = -1; if (proc_stat2 (ctx, &cpuusage, &cputot, &cpupct) < 0) goto done; if (proc_meminfo (ctx, &ktot, &kfree) < 0) goto done; mempct = ((double)(ktot - kfree) / (double)(ktot)) * 100.0; snprintf (buf, len, "cpu_util: %.2f%% mem_util: %.2f%%", cpupct, mempct); ret = 0; done: return ret; }