{
switch(type_) {
case Type::Malloc:
- data_ = malloc(size_);
+ data_ = ::malloc(size_);
break;
case Type::Mmap:
- data_ = mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
+ data_ = ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
if (data_ == MAP_FAILED) {
data_ = nullptr;
size_ = 0;
RegionType region_type,
void *start_addr, void* permanent_addr, size_t size)
{
- simgrid::mc::Buffer::Type buffer_type;
+ // When KSM support is enables, we allocate memory using mmap:
+ // * we don't want to madvise bits of the heap;
+ // * mmap gives data aligned on page boundaries which is merge friendly.
+ simgrid::mc::Buffer data;
if (_sg_mc_ksm)
- // We use mmap to allocate the memory in order to madvise it.
- // We don't want to madvise the main heap.
- // Moreover we get aligned pgaes which is merge-friendly.
- buffer_type = simgrid::mc::Buffer::Type::Mmap;
+ data = Buffer::mmap(size);
else
- buffer_type = simgrid::mc::Buffer::Type::Malloc;
-
- simgrid::mc::Buffer data(size, buffer_type);
+ data = Buffer::malloc(size);
mc_model_checker->process().read_bytes(data.get(), size,
remote(permanent_addr),
if (_sg_mc_ksm)
// Mark the region as mergeable *after* we have written into it.
- // There no point to let KSM do the hard work before that.
+ // Trying to merge them before is useless/counterproductive.
madvise(data.get(), size, MADV_MERGEABLE);
simgrid::mc::RegionSnapshot region(
};
class Buffer {
-public:
+private:
enum class Type {
Malloc,
Mmap
};
-private:
void* data_ = nullptr;
std::size_t size_;
Type type_ = Type::Malloc;
-public:
- Buffer() {}
+private:
Buffer(std::size_t size, Type type = Type::Malloc);
Buffer(void* data, std::size_t size, Type type = Type::Malloc) :
data_(data), size_(size), type_(type) {}
+public:
+ Buffer() {}
void clear() noexcept;
~Buffer() noexcept { clear(); }
+ static Buffer malloc(std::size_t size)
+ {
+ return Buffer(size, Type::Malloc);
+ }
+ static Buffer mmap(std::size_t size)
+ {
+ return Buffer(size, Type::Mmap);
+ }
+
// No copy
Buffer(Buffer const& buffer) = delete;
Buffer& operator=(Buffer const& buffer) = delete;
void* get() { return data_; }
const void* get() const { return data_; }
std::size_t size() const { return size_; }
- Type type() const { return type_; }
};
/** A copy/snapshot of a given memory region