|
|
218e99 |
/* Start/stop KSM, for systemd.
|
|
|
218e99 |
* Copyright (C) 2009, 2011 Red Hat, Inc.
|
|
|
218e99 |
* Written by Paolo Bonzini <pbonzini@redhat.com>.
|
|
|
218e99 |
* Based on the original sysvinit script by Dan Kenigsberg <danken@redhat.com>
|
|
|
218e99 |
* This file is distributed under the GNU General Public License, version 2
|
|
|
218e99 |
* or later. */
|
|
|
218e99 |
|
|
|
218e99 |
#include <unistd.h>
|
|
|
218e99 |
#include <stdio.h>
|
|
|
218e99 |
#include <limits.h>
|
|
|
218e99 |
#include <stdint.h>
|
|
|
218e99 |
#include <stdlib.h>
|
|
|
218e99 |
#include <string.h>
|
|
|
218e99 |
|
|
|
218e99 |
#define KSM_MAX_KERNEL_PAGES_FILE "/sys/kernel/mm/ksm/max_kernel_pages"
|
|
|
218e99 |
#define KSM_RUN_FILE "/sys/kernel/mm/ksm/run"
|
|
|
218e99 |
|
|
|
218e99 |
char *program_name;
|
|
|
218e99 |
|
|
|
218e99 |
int usage(void)
|
|
|
218e99 |
{
|
|
|
218e99 |
fprintf(stderr, "Usage: %s {start|stop}\n", program_name);
|
|
|
218e99 |
return 1;
|
|
|
218e99 |
}
|
|
|
218e99 |
|
|
|
218e99 |
int write_value(uint64_t value, char *filename)
|
|
|
218e99 |
{
|
|
|
218e99 |
FILE *fp;
|
|
|
218e99 |
if (!(fp = fopen(filename, "w")) ||
|
|
|
218e99 |
fprintf(fp, "%llu\n", (unsigned long long) value) == EOF ||
|
|
|
218e99 |
fflush(fp) == EOF ||
|
|
|
218e99 |
fclose(fp) == EOF)
|
|
|
218e99 |
return 1;
|
|
|
218e99 |
|
|
|
218e99 |
return 0;
|
|
|
218e99 |
}
|
|
|
218e99 |
|
|
|
218e99 |
uint64_t ksm_max_kernel_pages()
|
|
|
218e99 |
{
|
|
|
218e99 |
char *var = getenv("KSM_MAX_KERNEL_PAGES");
|
|
|
218e99 |
char *endptr;
|
|
|
218e99 |
uint64_t value;
|
|
|
218e99 |
if (var && *var) {
|
|
|
218e99 |
value = strtoll(var, &endptr, 0);
|
|
|
218e99 |
if (value < LLONG_MAX && !*endptr)
|
|
|
218e99 |
return value;
|
|
|
218e99 |
}
|
|
|
218e99 |
/* Unless KSM_MAX_KERNEL_PAGES is set, let KSM munch up to half of
|
|
|
218e99 |
* total memory. */
|
|
|
218e99 |
return sysconf(_SC_PHYS_PAGES) / 2;
|
|
|
218e99 |
}
|
|
|
218e99 |
|
|
|
218e99 |
int start(void)
|
|
|
218e99 |
{
|
|
|
218e99 |
if (access(KSM_MAX_KERNEL_PAGES_FILE, R_OK) >= 0)
|
|
|
218e99 |
write_value(ksm_max_kernel_pages(), KSM_MAX_KERNEL_PAGES_FILE);
|
|
|
218e99 |
return write_value(1, KSM_RUN_FILE);
|
|
|
218e99 |
}
|
|
|
218e99 |
|
|
|
218e99 |
int stop(void)
|
|
|
218e99 |
{
|
|
|
218e99 |
return write_value(0, KSM_RUN_FILE);
|
|
|
218e99 |
}
|
|
|
218e99 |
|
|
|
218e99 |
int main(int argc, char **argv)
|
|
|
218e99 |
{
|
|
|
218e99 |
program_name = argv[0];
|
|
|
218e99 |
if (argc < 2) {
|
|
|
218e99 |
return usage();
|
|
|
218e99 |
} else if (!strcmp(argv[1], "start")) {
|
|
|
218e99 |
return start();
|
|
|
218e99 |
} else if (!strcmp(argv[1], "stop")) {
|
|
|
218e99 |
return stop();
|
|
|
218e99 |
} else {
|
|
|
218e99 |
return usage();
|
|
|
218e99 |
}
|
|
|
218e99 |
}
|