Report Xen balloon current size instead of Linux total memory

This results in qmemman knowing about the memory map overhead and
properly sizing VMs.
This commit is contained in:
qubesuser 2015-10-10 00:47:10 +02:00
parent 8d5157a8ab
commit 2d871075cc

View File

@ -16,9 +16,9 @@ int used_mem_change_threshold;
int delay; int delay;
int usr1_received; int usr1_received;
const char *parse(const char *buf) const char *parse(const char *meminfo_buf, const char* dom_current_buf)
{ {
const char *ptr = buf; const char *ptr = meminfo_buf;
static char outbuf[4096]; static char outbuf[4096];
int val; int val;
int len; int len;
@ -59,6 +59,12 @@ const char *parse(const char *buf)
ptr += len; ptr += len;
} }
if(dom_current_buf) {
int DomTotal = strtol(dom_current_buf, 0, 10);
if(DomTotal)
MemTotal = DomTotal;
}
used_mem = used_mem =
MemTotal - Buffers - Cached - MemFree + SwapTotal - SwapFree; MemTotal - Buffers - Cached - MemFree + SwapTotal - SwapFree;
if (used_mem < 0) if (used_mem < 0)
@ -103,13 +109,50 @@ void usr1_handler(int sig __attribute__((__unused__))) {
usr1_received = 1; usr1_received = 1;
} }
static inline void pread0_string(int fd, char* buf, size_t buf_size)
{
int n = pread(fd, buf, buf_size - 1, 0);
if (n < 0) {
perror("pread");
exit(1);
}
buf[n] = 0;
}
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));
/* check until the dom current reading is stable to avoid races */
for(;;) {
pread0_string(meminfo_fd, meminfo_buf, sizeof(meminfo_buf));
pread0_string(dom_current_fd, dom_current_buf2, sizeof(dom_current_buf2));
if(!strcmp(dom_current_buf, dom_current_buf2))
break;
pread0_string(meminfo_fd, meminfo_buf, sizeof(meminfo_buf));
pread0_string(dom_current_fd, dom_current_buf, sizeof(dom_current_buf));
if(!strcmp(dom_current_buf, dom_current_buf2))
break;
}
meminfo_data = parse(meminfo_buf, dom_current_buf);
if (meminfo_data)
send_to_qmemman(xs, meminfo_data);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char buf[4096]; int meminfo_fd, dom_current_fd;
int n;
const char *meminfo_data;
int fd;
struct xs_handle *xs; struct xs_handle *xs;
int n;
if (argc != 3 && argc != 4) if (argc != 3 && argc != 4)
usage(); usage();
@ -121,6 +164,8 @@ int main(int argc, char **argv)
if (argc == 4) { if (argc == 4) {
pid_t pid; pid_t pid;
sigset_t mask, oldmask; sigset_t mask, oldmask;
int fd;
char buf[32];
switch (pid = fork()) { switch (pid = fork()) {
case -1: case -1:
@ -155,9 +200,14 @@ int main(int argc, char **argv)
} }
} }
fd = open("/proc/meminfo", O_RDONLY); meminfo_fd = open("/proc/meminfo", O_RDONLY);
if (fd < 0) { if (meminfo_fd < 0) {
perror("open meminfo"); perror("open /proc/meminfo");
exit(1);
}
dom_current_fd = open("/sys/devices/system/xen_memory/xen_memory0/info/current_kb", O_RDONLY);
if (dom_current_fd < 0) {
perror("open /sys/devices/system/xen_memory/xen_memory0/info/current_kb");
exit(1); exit(1);
} }
xs = xs_domain_open(); xs = xs_domain_open();
@ -167,15 +217,8 @@ int main(int argc, char **argv)
} }
if (argc == 3) { if (argc == 3) {
/* if not waiting for signal, fork after first info written to xenstore */ /* if not waiting for signal, fork after first info written to xenstore */
n = pread(fd, buf, sizeof(buf)-1, 0); update(xs, meminfo_fd, dom_current_fd);
if (n < 0) {
perror("pread");
exit(1);
}
buf[n] = 0;
meminfo_data = parse(buf);
if (meminfo_data)
send_to_qmemman(xs, meminfo_data);
n = fork(); n = fork();
if (n < 0) { if (n < 0) {
perror("fork"); perror("fork");
@ -187,15 +230,8 @@ int main(int argc, char **argv)
} }
for (;;) { for (;;) {
n = pread(fd, buf, sizeof(buf)-1, 0); update(xs, meminfo_fd, dom_current_fd);
if (n < 0) {
perror("pread");
exit(1);
}
buf[n] = 0;
meminfo_data = parse(buf);
if (meminfo_data)
send_to_qmemman(xs, meminfo_data);
usleep(delay); usleep(delay);
} }
} }