Merge branch 'qmemman' of git://qubes-os.org/rafal/core

This commit is contained in:
Joanna Rutkowska 2010-09-13 15:05:13 +02:00
commit 2a6356a6dd
4 changed files with 136 additions and 6 deletions

View File

@ -35,13 +35,8 @@ start()
(read a b c d ; xenstore-write device/qubes_used_mem $c)
# we're still running in DispVM template
echo "Waiting for save/restore..."
# WARNING: Nergalism!
# Apparently it has been determined that DomU kernel
# dmesg's "using vcpu" after restore
while ! dmesg -c | grep "using vcpu" ; do usleep 10 ; done
# we're now after restore in a new instance of a DispVM
# ... wait until qubes_restore.c (in Dom0) recreates VM-specific keys
while ! xenstore-read qubes_vm_type 2>/dev/null ; do
while ! xenstore-read qubes_restore_complete 2>/dev/null ; do
usleep 10
done
echo Back to life.
@ -87,6 +82,10 @@ start()
fi
fi
MEM_CHANGE_THRESHOLD_KB=30000
MEMINFO_DELAY_USEC=100000
/usr/lib/qubes/meminfo-writer $MEM_CHANGE_THRESHOLD_KB $MEMINFO_DELAY_USEC &
[ -x /rw/config/rc.local ] && /rw/config/rc.local
success
echo ""

7
common/Makefile Normal file
View File

@ -0,0 +1,7 @@
CC=gcc
CFLAGS=-Wall -g -O3
all: meminfo-writer
meminfo-writer: meminfo-writer.o
$(CC) -g -o meminfo-writer meminfo-writer.o -lxenstore
clean:
rm -f meminfo-writer *.o *~

121
common/meminfo-writer.c Normal file
View File

@ -0,0 +1,121 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <xs.h>
#include <syslog.h>
#include <string.h>
unsigned long prev_used_mem;
int used_mem_change_threshold;
int delay;
char *parse(char *buf)
{
char *ptr = buf;
char name[256];
static char outbuf[4096];
int val;
int len;
int MemTotal = 0, MemFree = 0, Buffers = 0, Cached = 0, SwapTotal =
0, SwapFree = 0;
unsigned long long key;
long used_mem, used_mem_diff;
int nitems = 0;
while (nitems != 6) {
sscanf(ptr, "%s %d kB\n%n", name, &val, &len);
key = *(unsigned long long *) ptr;
if (key == *(unsigned long long *) "MemTotal:") {
MemTotal = val;
nitems++;
} else if (key == *(unsigned long long *) "MemFree:") {
MemFree = val;
nitems++;
} else if (key == *(unsigned long long *) "Buffers:") {
Buffers = val;
nitems++;
} else if (key == *(unsigned long long *) "Cached: ") {
Cached = val;
nitems++;
} else if (key == *(unsigned long long *) "SwapTotal:") {
SwapTotal = val;
nitems++;
} else if (key == *(unsigned long long *) "SwapFree:") {
SwapFree = val;
nitems++;
}
ptr += len;
}
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
|| (used_mem > prev_used_mem && used_mem * 13 / 10 > MemTotal
&& used_mem_diff > used_mem_change_threshold/2)) {
prev_used_mem = used_mem;
sprintf(outbuf,
"MemTotal: %d kB\nMemFree: %d kB\nBuffers: %d kB\nCached: %d kB\n"
"SwapTotal: %d kB\nSwapFree: %d kB\n", MemTotal,
MemFree, Buffers, Cached, SwapTotal, SwapFree);
return outbuf;
}
return NULL;
}
void usage()
{
fprintf(stderr,
"usage: meminfo_writer threshold_in_kb delay_in_us\n");
exit(1);
}
void send_to_qmemman(struct xs_handle *xs, char *data)
{
if (!xs_write(xs, XBT_NULL, "memory/meminfo", data, strlen(data))) {
syslog(LOG_DAEMON | LOG_ERR, "error writing xenstore ?");
exit(1);
}
}
int main(int argc, char **argv)
{
char buf[4096];
int n;
char *meminfo_data;
int fd;
struct xs_handle *xs;
if (argc != 3)
usage();
used_mem_change_threshold = atoi(argv[1]);
delay = atoi(argv[2]);
if (!used_mem_change_threshold || !delay)
usage();
fd = open("/proc/meminfo", O_RDONLY);
if (fd < 0) {
perror("open meminfo");
exit(1);
}
xs = xs_domain_open();
if (!xs) {
perror("xs_domain_open");
exit(1);
}
for (;;) {
n = pread(fd, buf, sizeof(buf), 0);
buf[n] = 0;
meminfo_data = parse(buf);
if (meminfo_data)
send_to_qmemman(xs, meminfo_data);
usleep(delay);
}
}

View File

@ -52,6 +52,7 @@ fi
%build
make clean all
make -C ../common
%install
@ -65,6 +66,7 @@ cp qubes_timestamp qvm-copy-to-vm qvm-open-in-dvm $RPM_BUILD_ROOT/usr/bin
mkdir -p $RPM_BUILD_ROOT/usr/lib/qubes
cp qubes_add_pendrive_script qubes_penctl qvm-copy-to-vm.kde $RPM_BUILD_ROOT/usr/lib/qubes
ln -s /usr/bin/qvm-open-in-dvm $RPM_BUILD_ROOT/usr/lib/qubes/qvm-dvm-transfer
cp ../common/meminfo-writer $RPM_BUILD_ROOT/usr/lib/qubes
mkdir -p $RPM_BUILD_ROOT/%{kde_service_dir}
cp qvm-copy.desktop qvm-dvm.desktop $RPM_BUILD_ROOT/%{kde_service_dir}
mkdir -p $RPM_BUILD_ROOT/etc/udev/rules.d
@ -187,6 +189,7 @@ rm -rf $RPM_BUILD_ROOT
/usr/lib/qubes/qvm-copy-to-vm.kde
%attr(4755,root,root) /usr/bin/qvm-open-in-dvm
/usr/lib/qubes/qvm-dvm-transfer
/usr/lib/qubes/meminfo-writer
%{kde_service_dir}/qvm-copy.desktop
%{kde_service_dir}/qvm-dvm.desktop
%attr(4755,root,root) /usr/lib/qubes/qubes_penctl