1 /* Start/stop KSM, for systemd.
2 * Copyright (C) 2009, 2011 Red Hat, Inc.
3 * Written by Paolo Bonzini <pbonzini@redhat.com>.
4 * Based on the original sysvinit script by Dan Kenigsberg <danken@redhat.com>
5 * This file is distributed under the GNU General Public License, version 2
15 #define KSM_MAX_KERNEL_PAGES_FILE "/sys/kernel/mm/ksm/max_kernel_pages"
16 #define KSM_RUN_FILE "/sys/kernel/mm/ksm/run"
22 fprintf(stderr, "Usage: %s {start|stop}\n", program_name);
26 int write_value(uint64_t value, char *filename)
29 if (!(fp = fopen(filename, "w")) ||
30 fprintf(fp, "%llu\n", (unsigned long long) value) == EOF ||
38 uint64_t ksm_max_kernel_pages()
40 char *var = getenv("KSM_MAX_KERNEL_PAGES");
44 value = strtoll(var, &endptr, 0);
45 if (value < LLONG_MAX && !*endptr)
48 /* Unless KSM_MAX_KERNEL_PAGES is set, let KSM munch up to half of
50 return sysconf(_SC_PHYS_PAGES) / 2;
55 if (access(KSM_MAX_KERNEL_PAGES_FILE, R_OK) >= 0)
56 write_value(ksm_max_kernel_pages(), KSM_MAX_KERNEL_PAGES_FILE);
57 return write_value(1, KSM_RUN_FILE);
62 return write_value(0, KSM_RUN_FILE);
65 int main(int argc, char **argv)
67 program_name = argv[0];
70 } else if (!strcmp(argv[1], "start")) {
72 } else if (!strcmp(argv[1], "stop")) {