X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FMemoryMapping.cpp;h=35c8eda69db9f424f3b0e3ce247629055a7f7ed6;hb=0347a79f57ee6e1179c0482adacf3a7e6ff890ff;hp=43889257b5aa6c307a4cc9785264f4d1ab95e0f6;hpb=d2eda017c492baf468ea9cd51d5618d56b6ca900;p=folly.git diff --git a/folly/MemoryMapping.cpp b/folly/MemoryMapping.cpp index 43889257..35c8eda6 100644 --- a/folly/MemoryMapping.cpp +++ b/folly/MemoryMapping.cpp @@ -15,8 +15,12 @@ */ #include + +#include +#include +#include + #include -#include #ifdef __linux__ #include @@ -240,9 +244,8 @@ bool MemoryMapping::mlock(LockMode lock) { return true; } - auto msg(folly::format( - "mlock({}) failed at {}", - mapLength_, amountSucceeded).str()); + auto msg = + folly::sformat("mlock({}) failed at {}", mapLength_, amountSucceeded); if (lock == LockMode::TRY_LOCK && (errno == EPERM || errno == ENOMEM)) { PLOG(WARNING) << msg; @@ -283,17 +286,36 @@ MemoryMapping::~MemoryMapping() { size_t amountSucceeded = 0; if (!memOpInChunks(::munmap, mapStart_, mapLength_, options_.pageSize, amountSucceeded)) { - PLOG(FATAL) << folly::format( - "munmap({}) failed at {}", - mapLength_, amountSucceeded).str(); + PLOG(FATAL) << folly::format("munmap({}) failed at {}", + mapLength_, amountSucceeded); } } } -void MemoryMapping::advise(int advice) const { - if (mapLength_ && ::madvise(mapStart_, mapLength_, advice)) { - PLOG(WARNING) << "madvise()"; +void MemoryMapping::advise(int advice) const { advise(advice, 0, mapLength_); } + +void MemoryMapping::advise(int advice, size_t offset, size_t length) const { + CHECK_LE(offset + length, mapLength_) + << " offset: " << offset + << " length: " << length + << " mapLength_: " << mapLength_; + + // Include the entire start page: round down to page boundary. + const auto offMisalign = offset % options_.pageSize; + offset -= offMisalign; + length += offMisalign; + + // Round the last page down to page boundary. + if (offset + length != size_t(mapLength_)) { + length -= length % options_.pageSize; } + + if (length == 0) { + return; + } + + char* mapStart = static_cast(mapStart_) + offset; + PLOG_IF(WARNING, ::madvise(mapStart, length, advice)) << "madvise"; } MemoryMapping& MemoryMapping::operator=(MemoryMapping other) {