Add MADV_MERGEABLE advice (madvise) for KSM in the heaps and
read/write segments: mark these regions of memory as candidate for
KSM. As the stacks are stored on the heap, they are candidate for KSM
as well.
In order to work, KSM must be enabled on the system (by root):
# Enable:
echo 1 > /sys/kernel/mm/ksm/run
# Do some real work:
echo 10000 > /sys/kernel/mm/ksm/pages_to_scan
See Documentation/vm/ksm.txt:
https://www.kernel.org/doc/Documentation/vm/ksm.txt
We would like to only mark the .data+.bss and not the other sections
of the segment.
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
+#include <sys/mman.h>
#include <libgen.h>
#include "simgrid/sg_config.h"
mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
mc_object_infos[1] = mc_libsimgrid_info;
+#ifdef MADV_MERGEABLE
+ for(int i=0; i!=mc_object_infos_size; ++i) {
+ void* start = mc_object_infos[i]->start_rw;
+ void* end = mc_object_infos[i]->end_rw;
+ madvise(start, (char*)end - (char*)start, MADV_MERGEABLE);
+ }
+#endif
+
// Use information of the other objects:
MC_post_process_object_info(mc_binary_info);
MC_post_process_object_info(mc_libsimgrid_info);
abort();
}
+#ifdef MADV_MERGEABLE
+ madvise(mapto, mapbytes, MADV_MERGEABLE);
+#endif
+
if (mdp->top == 0)
mdp->base = mdp->breakval = mapto;