Home | History | Annotate | Download | only in ksm
      1 /*
      2  * Copyright (C) 2010-2017  Red Hat, Inc.
      3  *
      4  * This program is free software;  you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation; either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     12  * the GNU General Public License for more details.
     13  *
     14  * Kernel Samepage Merging (KSM) for Memory Resource Controller
     15  *
     16  * Basic tests were to start several programs with same and different
     17  * memory contents and ensure only to merge the ones with the same
     18  * contents. When changed the content of one of merged pages in a
     19  * process and to the mode "unmerging", it should discard all merged
     20  * pages there. Also tested it is possible to disable KSM. There are
     21  * also command-line options to specify the memory allocation size, and
     22  * number of processes have same memory contents so it is possible to
     23  * test more advanced things like KSM + OOM etc.
     24  *
     25  * Prerequisites:
     26  *
     27  * 1) ksm and ksmtuned daemons need to be disabled. Otherwise, it could
     28  *    distrub the testing as they also change some ksm tunables depends
     29  *    on current workloads.
     30  *
     31  * The test steps are:
     32  * - Check ksm feature and backup current run setting.
     33  * - Change run setting to 1 - merging.
     34  * - 3 memory allocation programs have the memory contents that 2 of
     35  *   them are all 'a' and one is all 'b'.
     36  * - Check ksm statistics and verify the content.
     37  * - 1 program changes the memory content from all 'a' to all 'b'.
     38  * - Check ksm statistics and verify the content.
     39  * - All programs change the memory content to all 'd'.
     40  * - Check ksm statistics and verify the content.
     41  * - Change one page of a process.
     42  * - Check ksm statistics and verify the content.
     43  * - Change run setting to 2 - unmerging.
     44  * - Check ksm statistics and verify the content.
     45  * - Change run setting to 0 - stop.
     46  */
     47 
     48 #include <sys/types.h>
     49 #include <sys/mman.h>
     50 #include <sys/stat.h>
     51 #include <sys/wait.h>
     52 #include <errno.h>
     53 #include <fcntl.h>
     54 #include <signal.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 #include "mem.h"
     60 #include "ksm_common.h"
     61 
     62 static int memcg_mounted;
     63 
     64 static void verify_ksm(void)
     65 {
     66 	write_memcg();
     67 	create_same_memory(size, num, unit);
     68 }
     69 
     70 static void setup(void)
     71 {
     72 	if (access(PATH_KSM, F_OK) == -1)
     73 		tst_brk(TCONF, "KSM configuration is not enabled");
     74 
     75 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) {
     76 		SAFE_FILE_SCANF(PATH_KSM "merge_across_nodes",
     77 				"%d", &merge_across_nodes);
     78 		SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
     79 	}
     80 
     81 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
     82 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
     83 	memcg_mounted = 1;
     84 }
     85 
     86 static void cleanup(void)
     87 {
     88 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0)
     89 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
     90 				 "%d", merge_across_nodes);
     91 
     92 	if (memcg_mounted)
     93 		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
     94 }
     95 
     96 static struct tst_test test = {
     97 	.needs_root = 1,
     98 	.forks_child = 1,
     99 	.options = ksm_options,
    100 	.setup = setup,
    101 	.cleanup = cleanup,
    102 	.save_restore = save_restore,
    103 	.test_all = verify_ksm,
    104 	.min_kver = "2.6.32",
    105 };
    106