Skip to content

Commit

Permalink
Merge bfc6872 into 00311ac
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberDem0n committed Aug 9, 2019
2 parents 00311ac + bfc6872 commit 0eb5048
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 10 deletions.
26 changes: 18 additions & 8 deletions bg_mon.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,22 +217,32 @@ static void prepare_statistics_output(struct evbuffer *evb)
evbuffer_add_printf(evb, ", \"total\": %d, ", pg_stats_current.total_connections);
evbuffer_add_printf(evb, "\"active\": %d}, \"start_time\": %lu}, ", pg_stats_current.active_connections, pg_start_time);
evbuffer_add_printf(evb, "\"system_stats\": {\"uptime\": %d, \"load_average\": ", (int)(s.uptime / SC_CLK_TCK));
evbuffer_add_printf(evb, "[%4.6g, %4.6g, %4.6g], \"cpu\": ", la.run_1min, la.run_5min, la.run_15min);
evbuffer_add_printf(evb, "{\"user\": %2.1f, \"system\": %2.1f, \"idle\": ", c.utime_diff, c.stime_diff);
evbuffer_add_printf(evb, "%2.1f, \"iowait\": %2.1f}, \"ctxt\": %lu", c.idle_diff, c.iowait_diff, s.ctxt_diff);
evbuffer_add_printf(evb, ", \"processes\": {\"running\": %lu, \"blocked\": %lu}, ", s.procs_running, s.procs_blocked);
evbuffer_add_printf(evb, "\"memory\": {\"total\": %lu, \"free\": %lu, ", m.total, m.free);
evbuffer_add_printf(evb, "[%4.6g, %4.6g, %4.6g], \"cpu\": {\"user\": ", la.run_1min, la.run_5min, la.run_15min);
evbuffer_add_printf(evb, "%2.1f, \"nice\": %2.1f, \"system\": %2.1f, ", c.utime_diff, c.ntime_diff, c.stime_diff);
evbuffer_add_printf(evb, "\"idle\": %2.1f, \"iowait\": %2.1f, \"steal\": %2.1f", c.idle_diff, c.iowait_diff, c.steal_diff);
evbuffer_add_printf(evb, "}, \"ctxt\": %lu, \"processes\": {\"running\": %lu, \"blocked\":", s.ctxt_diff, s.procs_running);
evbuffer_add_printf(evb, " %lu}, \"memory\": {\"total\": %lu, \"free\": %lu, ", s.procs_blocked, m.total, m.free);
evbuffer_add_printf(evb, "\"buffers\": %lu, \"cached\": %lu, \"dirty\": %lu", m.buffers, m.cached, m.dirty);

if (m.overcommit.memory == 2) {
evbuffer_add_printf(evb, ", \"overcommit\": {\"ratio\": %u, ", m.overcommit.ratio);
evbuffer_add_printf(evb, "\"commit_limit\": %lu, \"committed_as\": %lu}", m.limit, m.as);
}

if (m.cgroup.available) {
if (m.cgroup.available || c.cgroup.available) {
cgroup_memory cm = m.cgroup;
evbuffer_add_printf(evb, ", \"cgroup\": {\"limit\": %lu, \"usage\": %lu", cm.limit, cm.usage);
evbuffer_add_printf(evb, ", \"rss\": %lu, \"cache\": %lu}", cm.rss, cm.cache);
cgroup_cpu cc = c.cgroup;
evbuffer_add_printf(evb, "}}, \"cgroup\": {");
if (cm.available) {
evbuffer_add_printf(evb, "\"memory\": {\"limit\": %lu, \"usage\": %lu", cm.limit, cm.usage);
evbuffer_add_printf(evb, ", \"rss\": %lu, \"cache\": %lu", cm.rss, cm.cache);
if (cc.available)
evbuffer_add_printf(evb, "}, ");
}
if (cc.available) {
evbuffer_add_printf(evb, "\"cpu\": {\"shares\": %llu, \"quota\": %lld", cc.shares, cc.quota);
evbuffer_add_printf(evb, ", \"user\": %2.1f, \"system\": %2.1f", cc.user_diff, cc.system_diff);
}
}

evbuffer_add_printf(evb, "}}, \"disk_stats\": {");
Expand Down
28 changes: 26 additions & 2 deletions bg_mon.html
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@
if (el != null) el.style.display = 'none';
} else
set_diskstats_values(data, 'wal');

var el = document.getElementById('cgroup_enabled');
if (el != null) el.style.display = 'cgroup' in data ? '' : 'none';
}

for (var key in data) {
Expand All @@ -402,6 +405,13 @@
data[key]['used'] = data[key]['total'] - data[key]['free'];
if (data[key]['used'] > data[key]['cache'])
data[key]['used'] -= data[key]['cache'];

var overcommit_display = 'overcommit' in data[key] ? 'inline' : 'none';
var els = ['tmpl', 'enabled'];
for (var n = 0; n < els.length; ++n) {
var el = document.getElementById(ns_key + '.overcommit_' + els[n]);
if (el != null) el.style.display = overcommit_display;
}
}
for (var n in timeSeries[cn])
timeSeries[cn][n].append(t, data[key][n]);
Expand Down Expand Up @@ -476,9 +486,9 @@
<span style="color: black">Total <b id="system_stats.memory.total"></b></span>
<span style="color: blue">Used <b id="system_stats.memory.used"></b></span>
<span style="color: green">Free <b id="system_stats.memory.free"></b></span>
<span style="color: orange">Buffers+Cache <b id="system_stats.memory.cache"></b></span><br>
<span style="color: orange">Buffers+Cache <b id="system_stats.memory.cache"></b></span><br id="system_stats.memory.overcommit_enabled">
<span style="color: black">Dirty <b id="system_stats.memory.dirty"></b></span>
<span style="color: black">Limit <b id="system_stats.memory.overcommit.commit_limit"></b> as <b id="system_stats.memory.overcommit.committed_as"></b></span>
<div id="system_stats.memory.overcommit_tmpl"><span style="color: black">Limit <b id="system_stats.memory.overcommit.commit_limit"></b> as <b id="system_stats.memory.overcommit.committed_as"></b></span></div>
</td>
<td>
<span style="color: black">data/<b id="io.data_dev"></b>:</span>
Expand All @@ -499,6 +509,20 @@
</div>
</td>
</tr>
<tr id="cgroup_enabled">
<td><b>cgroup Mem:</b>
<span>RSS: <b id="cgroup.memory.rss"></b></span>
<span>Cache: <b id="cgroup.memory.cache"></b></span>
<span>Usage: <b id="cgroup.memory.usage"></b></span>
<span>Limit: <b id="cgroup.memory.limit"></b></span>
</td>
<td><b>cgroup CPU:</b>
<span>User: <b id="cgroup.cpu.user"></b>%</span>
<span>System: <b id="cgroup.cpu.system"></b>%</span>
<span>Shares: <b id="cgroup.cpu.shares"></b></span>
<span>Quota: <b id="cgroup.cpu.quota"></b></span>
</td>
</tr>
</thead>
</table>
<h2>Partitions</h2>
Expand Down
10 changes: 10 additions & 0 deletions disk_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static bool run_du;
static unsigned long long data_du, wal_du;
static unsigned int du_counter;

char *cpu_cgroup_mount = NULL;
char *cpuacct_cgroup_mount = NULL;
char *memory_cgroup_mount = NULL;

/******************************************************
Expand Down Expand Up @@ -154,6 +156,14 @@ static List *read_mounts()
m->me_dummy = is_dummy(me);
m->me_remote = is_remote(me);
mounts = lappend(mounts, m);
if (cpu_cgroup_mount == NULL
&& strcmp(me->mnt_type, "cgroup") == 0
&& hasmntopt(me, "cpu"))
cpu_cgroup_mount = pstrdup(me->mnt_dir);
if (cpuacct_cgroup_mount == NULL
&& strcmp(me->mnt_type, "cgroup") == 0
&& hasmntopt(me, "cpuacct"))
cpuacct_cgroup_mount = pstrdup(me->mnt_dir);
if (memory_cgroup_mount == NULL
&& strcmp(me->mnt_type, "cgroup") == 0
&& hasmntopt(me, "memory"))
Expand Down
113 changes: 113 additions & 0 deletions system_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

#define PROC_OVERCOMMIT "/proc/sys/vm/overcommit_"

extern char *cpu_cgroup_mount;
static char *cpu_cgroup = NULL;
static int cpu_cgroup_len = 0;

extern char *cpuacct_cgroup_mount;
static char *cpuacct_cgroup = NULL;
static int cpuacct_cgroup_len = 0;

extern char *memory_cgroup_mount;
static char *memory_cgroup = NULL;
static int memory_cgroup_len = 0;
Expand Down Expand Up @@ -70,6 +78,17 @@ static unsigned long cgroup_read_ulong(const char *name)
return ret;
}

static unsigned long long read_ullong(const char *filename)
{
FILE *f;
unsigned long long ret = 0;
if ((f = fopen(filename, "r")) != NULL) {
if (fscanf(f, "%llu", &ret) != 1) {}
fclose(f);
}
return ret;
}

static cgroup_memory read_cgroup_memory_stats(void)
{
FILE *csfd;
Expand Down Expand Up @@ -166,6 +185,68 @@ static meminfo read_meminfo(void)
return mi;
}

static cgroup_cpu read_cgroup_cpu_stats(void)
{
FILE *csfd;
cgroup_cpu cc = {0,};

cc.available = true;

if (cpu_cgroup != NULL) {
strcpy(cpu_cgroup + cpu_cgroup_len, "cfs_quota_us");
if ((csfd = fopen(cpu_cgroup, "r")) != NULL) {
if (fscanf(csfd, "%lld", &cc.quota) != 2) {}
fclose(csfd);
}

strcpy(cpu_cgroup + cpu_cgroup_len, "shares");
cc.shares = read_ullong(cpu_cgroup);
}

if (cpuacct_cgroup != NULL) {
int i = 0, j = 0;
char name[7], buf[255];
unsigned long long value;
struct _cpu_tab {
const char *name;
unsigned long long *value;
} cpu_tab[] = {
{"user", &cc.user},
{"system", &cc.system},
{NULL, NULL}
};

strcpy(cpuacct_cgroup + cpuacct_cgroup_len, "usage");
/* nanosecondsInSecond = 1000000000.0; */
cc.total = read_ullong(cpuacct_cgroup) / 1000000000.0 * SC_CLK_TCK;

strcpy(cpuacct_cgroup + cpuacct_cgroup_len, "usage_percpu");
if ((csfd = fopen(cpuacct_cgroup, "r")) != NULL) {
while (fscanf(csfd, "%*u ") == 0)
++cc.online_cpus;
fclose(csfd);
}

strcpy(cpuacct_cgroup + cpuacct_cgroup_len, "stat");
if ((csfd = fopen(cpuacct_cgroup, "r")) == NULL)
return cc;

while (i < lengthof(cpu_tab) -1
&& fgets(buf, sizeof(buf), csfd)
&& sscanf(buf, "%6s %llu", name, &value) == 2)
for (j = 0; cpu_tab[j].name != NULL; ++j)
if (strcmp(cpu_tab[j].name, name) == 0) {
++i;
*cpu_tab[j].value = value;
break;
}

fclose(csfd);
}

return cc;
}

static system_stat read_proc_stat(void)
{
system_stat st = {0,};
Expand Down Expand Up @@ -199,6 +280,9 @@ static system_stat read_proc_stat(void)
st.procs_blocked = value;
}
fclose(stfd);

if (cpu_cgroup != NULL || cpuacct_cgroup != NULL)
st.cpu.cgroup = read_cgroup_cpu_stats();
return st;
}

Expand Down Expand Up @@ -231,6 +315,19 @@ static void diff_system_stats(system_stat *new_stats)
new_stats->cpu.idle_diff = new_stats->cpu.idle < system_stats_old.cpu.idle ? 0.0:
SP_VALUE(system_stats_old.cpu.idle, new_stats->cpu.idle, itv);

if (new_stats->cpu.cgroup.available) {
double total = new_stats->cpu.cgroup.online_cpus
* SP_VALUE_100(system_stats_old.cpu.cgroup.total, new_stats->cpu.cgroup.total, itv);
long long system_diff = new_stats->cpu.cgroup.system - system_stats_old.cpu.cgroup.system;
long long user_diff = new_stats->cpu.cgroup.user - system_stats_old.cpu.cgroup.user;
long long sum_diff = system_diff + user_diff;

if (total > 0 && sum_diff > 0) {
new_stats->cpu.cgroup.system_diff = system_diff > 0 ? total * system_diff / sum_diff : 0;
new_stats->cpu.cgroup.user_diff = user_diff > 0 ? total * user_diff / sum_diff : 0;
}
}

if (new_stats->cpu.cpu_count > 1) itv = new_stats->uptime - system_stats_old.uptime;
new_stats->ctxt_diff = S_VALUE(system_stats_old.ctxt, new_stats->ctxt, itv);
}
Expand Down Expand Up @@ -258,6 +355,22 @@ void system_stats_init(void)
if (uname(&un) == 0)
sysname = pstrdup(un.release);

if (cpu_cgroup_mount != NULL) {
const char prefix[] = "/cpu.";
cpu_cgroup_len = strlen(cpu_cgroup_mount);
cpu_cgroup = repalloc(cpu_cgroup_mount, cpu_cgroup_len + 18); /* strlen("/cpu.cfs_quota_us") + 1 */
strcpy(cpu_cgroup + cpu_cgroup_len, prefix);
cpu_cgroup_len += sizeof(prefix) - 1;
}

if (cpuacct_cgroup_mount != NULL) {
const char prefix[] = "/cpuacct.";
cpuacct_cgroup_len = strlen(cpuacct_cgroup_mount);
cpuacct_cgroup = repalloc(cpuacct_cgroup_mount, cpuacct_cgroup_len + 22); /* strlen("/cpuacct.usage_percpu") + 1 */
strcpy(cpuacct_cgroup + cpuacct_cgroup_len, prefix);
cpuacct_cgroup_len += sizeof(prefix) - 1;
}

if (memory_cgroup_mount != NULL) {
const char prefix[] = "/memory.";
memory_cgroup_len = strlen(memory_cgroup_mount);
Expand Down
13 changes: 13 additions & 0 deletions system_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ typedef struct {
overcommit overcommit;
} meminfo;

typedef struct {
bool available;
unsigned int online_cpus;
unsigned long long shares;
long long quota;
unsigned long long total;
unsigned long long system;
double system_diff;
unsigned long long user;
double user_diff;
} cgroup_cpu;

typedef struct {
int fields;
int cpu_count;
Expand All @@ -63,6 +75,7 @@ typedef struct {
double steal_diff;
unsigned long long uptime;
unsigned long long uptime0;
cgroup_cpu cgroup;
} cpu_stat;

typedef struct {
Expand Down

0 comments on commit 0eb5048

Please sign in to comment.