Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions qmemman/meminfo-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@ int used_mem_change_threshold;
int delay;
int usr1_received;

const char *parse(const char *meminfo_buf, const char* dom_current_buf)
typedef struct {
char *mem;
char *swap;
} UsedMem;

UsedMem parse(const char *meminfo_buf, const char* dom_current_buf)
{
const char *ptr = meminfo_buf;
static char outbuf[4096];
static char used_mem_buf[4096];
static char used_swap_buf[4096];
long long val;
int len;
int ret;
long long MemTotal = 0, MemFree = 0, Buffers = 0, Cached = 0, SwapTotal =
0, SwapFree = 0;
unsigned long long key;
long long used_mem, used_mem_diff;
long long used_swap;
int nitems = 0;

while (nitems != (1<<6)-1 || !*ptr) {
Expand Down Expand Up @@ -68,7 +75,8 @@ const char *parse(const char *meminfo_buf, const char* dom_current_buf)
used_mem =
MemTotal - Buffers - Cached - MemFree + SwapTotal - SwapFree;
if (used_mem < 0)
return NULL;
return (UsedMem){ .mem = NULL, .swap = NULL };
used_swap = SwapTotal - SwapFree;

used_mem_diff = used_mem - prev_used_mem;
if (used_mem_diff < 0)
Expand All @@ -78,10 +86,13 @@ const char *parse(const char *meminfo_buf, const char* dom_current_buf)
|| (used_mem > prev_used_mem && used_mem / 10 > (MemTotal+12) / 13
&& used_mem_diff > used_mem_change_threshold/2)) {
prev_used_mem = used_mem;
sprintf(outbuf, "%lld", used_mem);
return outbuf;
sprintf(used_mem_buf, "%lld", used_mem);
sprintf(used_swap_buf, "%lld", used_swap);
snprintf(used_mem_buf, sizeof used_mem_buf, "%lld", used_mem);
snprintf(used_swap_buf, sizeof used_swap_buf, "%lld", used_swap);
return (UsedMem){ .mem = used_mem_buf, .swap = used_swap_buf };
}
return NULL;
return (UsedMem){ .mem = NULL, .swap = NULL };
}

void usage(void)
Expand All @@ -94,10 +105,14 @@ void usage(void)
exit(1);
}

void send_to_qmemman(struct xs_handle *xs, const char *data)
void send_to_qmemman(struct xs_handle *xs, const char *mem, const char *swap)
{
if (!xs_write(xs, XBT_NULL, "memory/meminfo", data, strlen(data))) {
syslog(LOG_DAEMON | LOG_ERR, "error writing xenstore ?");
if (!xs_write(xs, XBT_NULL, "memory/meminfo", mem, strlen(mem))) {
syslog(LOG_DAEMON | LOG_ERR, "error writing meminfo to xenstore ?");
exit(1);
}
if (!xs_write(xs, XBT_NULL, "memory/swapinfo", swap, strlen(swap))) {
syslog(LOG_DAEMON | LOG_ERR, "error writing swapinfo to xenstore ?");
exit(1);
}
}
Expand All @@ -121,7 +136,6 @@ static void update(struct xs_handle *xs, int meminfo_fd, int dom_current_fd)
char dom_current_buf[32];
char dom_current_buf2[32];
char meminfo_buf[4096];
const char *meminfo_data;

pread0_string(dom_current_fd, dom_current_buf, sizeof(dom_current_buf));

Expand All @@ -140,9 +154,9 @@ static void update(struct xs_handle *xs, int meminfo_fd, int dom_current_fd)
break;
}

meminfo_data = parse(meminfo_buf, dom_current_buf);
if (meminfo_data)
send_to_qmemman(xs, meminfo_data);
UsedMem meminfo_data = parse(meminfo_buf, dom_current_buf);
if (meminfo_data.mem && meminfo_data.mem[0])
send_to_qmemman(xs, meminfo_data.mem, meminfo_data.swap);
}

int main(int argc, char **argv)
Expand Down