2014-01-05 04:32:57 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-04-21 01:14:32 +00:00
|
|
|
#ifdef USE_XENSTORE_H /* Xen >= 4.2 */
|
|
|
|
#include <xenstore.h>
|
|
|
|
#else
|
2014-01-05 04:32:57 +00:00
|
|
|
#include <xs.h>
|
2014-04-21 01:14:32 +00:00
|
|
|
#endif
|
2014-01-05 04:32:57 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2014-02-16 09:43:01 +00:00
|
|
|
long prev_used_mem;
|
2014-01-05 04:32:57 +00:00
|
|
|
int used_mem_change_threshold;
|
|
|
|
int delay;
|
|
|
|
int usr1_received;
|
|
|
|
|
2015-10-09 22:47:10 +00:00
|
|
|
const char *parse(const char *meminfo_buf, const char* dom_current_buf)
|
2014-01-05 04:32:57 +00:00
|
|
|
{
|
2015-10-09 22:47:10 +00:00
|
|
|
const char *ptr = meminfo_buf;
|
2014-01-05 04:32:57 +00:00
|
|
|
static char outbuf[4096];
|
2015-10-09 23:37:30 +00:00
|
|
|
long long val;
|
2014-01-05 04:32:57 +00:00
|
|
|
int len;
|
2013-12-26 15:34:16 +00:00
|
|
|
int ret;
|
2015-10-09 23:37:30 +00:00
|
|
|
long long MemTotal = 0, MemFree = 0, Buffers = 0, Cached = 0, SwapTotal =
|
2014-01-05 04:32:57 +00:00
|
|
|
0, SwapFree = 0;
|
|
|
|
unsigned long long key;
|
2015-10-09 23:37:30 +00:00
|
|
|
long long used_mem, used_mem_diff;
|
2014-01-05 04:32:57 +00:00
|
|
|
int nitems = 0;
|
|
|
|
|
2013-12-26 15:34:16 +00:00
|
|
|
while (nitems != (1<<6)-1 || !*ptr) {
|
2015-10-09 23:37:30 +00:00
|
|
|
ret = sscanf(ptr, "%*s %lld kB\n%n", &val, &len);
|
2014-02-16 09:43:01 +00:00
|
|
|
if (ret < 1 || len < (int)sizeof (unsigned long long)) {
|
2013-12-26 15:34:16 +00:00
|
|
|
ptr += len;
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-05 04:32:57 +00:00
|
|
|
key = *(unsigned long long *) ptr;
|
|
|
|
if (key == *(unsigned long long *) "MemTotal:") {
|
|
|
|
MemTotal = val;
|
2013-12-26 15:34:16 +00:00
|
|
|
nitems |= 1;
|
2014-01-05 04:32:57 +00:00
|
|
|
} else if (key == *(unsigned long long *) "MemFree:") {
|
|
|
|
MemFree = val;
|
2013-12-26 15:34:16 +00:00
|
|
|
nitems |= 2;
|
2014-01-05 04:32:57 +00:00
|
|
|
} else if (key == *(unsigned long long *) "Buffers:") {
|
|
|
|
Buffers = val;
|
2013-12-26 15:34:16 +00:00
|
|
|
nitems |= 4;
|
2014-01-05 04:32:57 +00:00
|
|
|
} else if (key == *(unsigned long long *) "Cached: ") {
|
|
|
|
Cached = val;
|
2013-12-26 15:34:16 +00:00
|
|
|
nitems |= 8;
|
2014-01-05 04:32:57 +00:00
|
|
|
} else if (key == *(unsigned long long *) "SwapTotal:") {
|
|
|
|
SwapTotal = val;
|
2013-12-26 15:34:16 +00:00
|
|
|
nitems |= 16;
|
2014-01-05 04:32:57 +00:00
|
|
|
} else if (key == *(unsigned long long *) "SwapFree:") {
|
|
|
|
SwapFree = val;
|
2013-12-26 15:34:16 +00:00
|
|
|
nitems |= 32;
|
2014-01-05 04:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ptr += len;
|
|
|
|
}
|
|
|
|
|
2015-10-09 22:47:10 +00:00
|
|
|
if(dom_current_buf) {
|
2015-10-09 23:37:30 +00:00
|
|
|
long long DomTotal = strtoll(dom_current_buf, 0, 10);
|
2015-10-09 22:47:10 +00:00
|
|
|
if(DomTotal)
|
|
|
|
MemTotal = DomTotal;
|
|
|
|
}
|
|
|
|
|
2014-01-05 04:32:57 +00:00
|
|
|
used_mem =
|
|
|
|
MemTotal - Buffers - Cached - MemFree + SwapTotal - SwapFree;
|
|
|
|
if (used_mem < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
used_mem_diff = used_mem - prev_used_mem;
|
|
|
|
if (used_mem_diff < 0)
|
|
|
|
used_mem_diff = -used_mem_diff;
|
|
|
|
if (used_mem_diff > used_mem_change_threshold
|
2013-12-26 15:37:21 +00:00
|
|
|
|| prev_used_mem == 0
|
2013-12-26 15:46:24 +00:00
|
|
|
|| (used_mem > prev_used_mem && used_mem / 10 > (MemTotal+12) / 13
|
2014-01-05 04:32:57 +00:00
|
|
|
&& used_mem_diff > used_mem_change_threshold/2)) {
|
|
|
|
prev_used_mem = used_mem;
|
|
|
|
sprintf(outbuf,
|
2015-10-09 23:37:30 +00:00
|
|
|
"MemTotal: %lld kB\nMemFree: %lld kB\nBuffers: %lld kB\nCached: %lld kB\n"
|
|
|
|
"SwapTotal: %lld kB\nSwapFree: %lld kB\n", MemTotal,
|
2014-01-05 04:32:57 +00:00
|
|
|
MemFree, Buffers, Cached, SwapTotal, SwapFree);
|
|
|
|
return outbuf;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-26 15:38:26 +00:00
|
|
|
void usage(void)
|
2014-01-05 04:32:57 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: meminfo_writer threshold_in_kb delay_in_us [pidfile]\n");
|
|
|
|
fprintf(stderr, " When pidfile set, meminfo-writer will:\n");
|
|
|
|
fprintf(stderr, " - fork into background\n");
|
2013-12-26 15:38:26 +00:00
|
|
|
fprintf(stderr, " - wait for SIGUSR1 (in background) before starting main work\n");
|
2014-01-05 04:32:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-12-26 15:33:13 +00:00
|
|
|
void send_to_qmemman(struct xs_handle *xs, const char *data)
|
2014-01-05 04:32:57 +00:00
|
|
|
{
|
|
|
|
if (!xs_write(xs, XBT_NULL, "memory/meminfo", data, strlen(data))) {
|
|
|
|
syslog(LOG_DAEMON | LOG_ERR, "error writing xenstore ?");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 09:43:01 +00:00
|
|
|
void usr1_handler(int sig __attribute__((__unused__))) {
|
2014-01-05 04:32:57 +00:00
|
|
|
usr1_received = 1;
|
|
|
|
}
|
|
|
|
|
2015-10-09 22:47:10 +00:00
|
|
|
static inline void pread0_string(int fd, char* buf, size_t buf_size)
|
2014-01-05 04:32:57 +00:00
|
|
|
{
|
2015-10-09 22:47:10 +00:00
|
|
|
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];
|
2013-12-26 15:33:13 +00:00
|
|
|
const char *meminfo_data;
|
2015-10-09 22:47:10 +00:00
|
|
|
|
|
|
|
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 meminfo_fd, dom_current_fd;
|
2014-01-05 04:32:57 +00:00
|
|
|
struct xs_handle *xs;
|
2015-10-09 22:47:10 +00:00
|
|
|
int n;
|
2014-01-05 04:32:57 +00:00
|
|
|
|
|
|
|
if (argc != 3 && argc != 4)
|
|
|
|
usage();
|
|
|
|
used_mem_change_threshold = atoi(argv[1]);
|
|
|
|
delay = atoi(argv[2]);
|
2013-12-26 15:38:42 +00:00
|
|
|
if (used_mem_change_threshold <= 0 || delay <= 0)
|
2014-01-05 04:32:57 +00:00
|
|
|
usage();
|
|
|
|
|
|
|
|
if (argc == 4) {
|
|
|
|
pid_t pid;
|
|
|
|
sigset_t mask, oldmask;
|
2015-10-09 22:47:10 +00:00
|
|
|
int fd;
|
|
|
|
char buf[32];
|
2014-01-05 04:32:57 +00:00
|
|
|
|
|
|
|
switch (pid = fork()) {
|
|
|
|
case -1:
|
|
|
|
perror("fork");
|
|
|
|
exit(1);
|
|
|
|
case 0:
|
|
|
|
sigemptyset (&mask);
|
|
|
|
sigaddset (&mask, SIGUSR1);
|
|
|
|
/* Wait for a signal to arrive. */
|
|
|
|
sigprocmask (SIG_BLOCK, &mask, &oldmask);
|
|
|
|
usr1_received = 0;
|
|
|
|
signal(SIGUSR1, usr1_handler);
|
|
|
|
while (!usr1_received)
|
|
|
|
sigsuspend (&oldmask);
|
|
|
|
sigprocmask (SIG_UNBLOCK, &mask, NULL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fd = open(argv[3], O_CREAT | O_TRUNC | O_WRONLY, 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open pidfile");
|
2013-12-26 15:39:09 +00:00
|
|
|
kill(pid,9);
|
2014-01-05 04:32:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
n = sprintf(buf, "%d\n", pid);
|
|
|
|
if (write(fd, buf, n) != n) {
|
|
|
|
perror("write pid");
|
2013-12-26 15:39:09 +00:00
|
|
|
kill(pid,9);
|
2014-01-05 04:32:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 22:47:10 +00:00
|
|
|
meminfo_fd = open("/proc/meminfo", O_RDONLY);
|
|
|
|
if (meminfo_fd < 0) {
|
|
|
|
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");
|
2014-01-05 04:32:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
xs = xs_domain_open();
|
|
|
|
if (!xs) {
|
|
|
|
perror("xs_domain_open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (argc == 3) {
|
|
|
|
/* if not waiting for signal, fork after first info written to xenstore */
|
2015-10-09 22:47:10 +00:00
|
|
|
update(xs, meminfo_fd, dom_current_fd);
|
|
|
|
|
2013-12-26 15:40:12 +00:00
|
|
|
n = fork();
|
|
|
|
if (n < 0) {
|
|
|
|
perror("fork");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (n > 0)
|
2014-01-05 04:32:57 +00:00
|
|
|
exit(0);
|
2013-12-26 15:40:12 +00:00
|
|
|
usleep(delay);
|
2014-01-05 04:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2015-10-09 22:47:10 +00:00
|
|
|
update(xs, meminfo_fd, dom_current_fd);
|
2014-01-05 04:32:57 +00:00
|
|
|
usleep(delay);
|
|
|
|
}
|
|
|
|
}
|
2015-10-09 22:47:10 +00:00
|
|
|
|