34 #include "mongo/base/static_assert.h" 35 #include "mongo/db/operation_context.h" 38 #include "mongo/stdx/mutex.h" 60 virtual void close(OperationContext* opCtx);
63 bool open(OperationContext* opCtx,
const std::string& fname);
82 bool create(OperationContext* opCtx,
const std::string& fname,
unsigned long long&
len);
104 DEV verify(!
_p.
_p.empty());
139 void setPath(
const std::string& pathAndFileName);
149 class MemoryMappedCOWBitset {
154 static const unsigned long long ChunkSize = 64 * 1024 * 1024;
157 static const unsigned long long NChunks = 64 * 1024;
160 static const unsigned long long MaxChunkMemory = ChunkSize * NChunks *
sizeof(
unsigned int) * 8;
163 static const unsigned long long MaxChunkBytes = NChunks *
sizeof(
unsigned int);
166 static const unsigned long long MaxWinMemory = 128ULL * 1024 * 1024 * 1024 * 1024;
169 MONGO_STATIC_ASSERT_MSG(MaxChunkMemory == MaxWinMemory,
170 "Need a larger bitset to cover max process VM space");
173 MemoryMappedCOWBitset() {
174 MONGO_STATIC_ASSERT_MSG(MemoryMappedCOWBitset::MaxChunkBytes ==
sizeof(bits),
175 "Validate our predicted bitset size is correct");
178 bool get(uintptr_t i)
const {
179 uintptr_t x = i / 32;
180 verify(x < MemoryMappedCOWBitset::NChunks);
181 return (bits[x].loadRelaxed() & (1 << (i % 32))) != 0;
185 void set(uintptr_t i) {
186 uintptr_t x = i / 32;
187 verify(x < MemoryMappedCOWBitset::NChunks);
188 bits[x].store(bits[x].loadRelaxed() | (1 << (i % 32)));
192 void clear(uintptr_t i) {
193 uintptr_t x = i / 32;
194 verify(x < MemoryMappedCOWBitset::NChunks);
195 bits[x].store(bits[x].loadRelaxed() & ~(1 << (i % 32)));
200 AtomicUInt32 bits[MemoryMappedCOWBitset::NChunks];
220 void remove(
void* view,
size_t length);
239 return _views.size();
243 void makeWritable(
void*,
unsigned len);
245 void clearWritableBits(
void* privateView,
size_t len);
248 void clearWritableBits_inlock(
void* privateView,
size_t len);
251 void makeChunkWritable(
size_t chunkno);
262 std::map<void*, DurableMappedFile*>
_views;
266 MemoryMappedCOWBitset writable;
272 size_t p =
reinterpret_cast<size_t>(privateView);
273 unsigned a = p / MemoryMappedCOWBitset::ChunkSize;
274 unsigned b = (p +
len) / MemoryMappedCOWBitset::ChunkSize;
276 for (
unsigned i = a; i <= b; i++) {
277 if (!writable.get(i)) {
278 makeChunkWritable(i);
unsigned long long len
Definition: mmap.h:289
unsigned long long length() const
Definition: mmap.h:262
std::map< void *, DurableMappedFile * > _views
Definition: durable_mapped_file.h:262
int ofs
Definition: btree_interface.cpp:337
bool _willNeedRemap
Definition: durable_mapped_file.h:135
bool willNeedRemap()
true if we have written.
Definition: durable_mapped_file.h:119
int fileSuffixNo() const
Definition: durable_mapped_file.h:108
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
std::string _p
Definition: paths.h:50
void * view_write() const
Definition: durable_mapped_file.h:94
void flush(bool sync)
Definition: mmap_posix.cpp:269
PointerToDurableMappedFile privateViews
Definition: durable_mapped_file.cpp:226
void * _view_write
Definition: durable_mapped_file.h:133
bool create(OperationContext *opCtx, const std::string &fname, unsigned long long &len)
Definition: durable_mapped_file.cpp:254
HANDLE getFd() const
Definition: mmap.h:265
void setWillNeedRemap()
Definition: durable_mapped_file.h:122
void setPath(const std::string &pathAndFileName)
Definition: durable_mapped_file.cpp:229
std::underlying_type< Options >::type OptionSet
Definition: mmap.h:127
int HANDLE
Definition: mmap.h:42
stdx::mutex & _mutex()
for doing many finds in a row with one lock operation
Definition: durable_mapped_file.h:230
void * _view_private
Definition: durable_mapped_file.h:134
virtual void * viewForFlushing()
Definition: durable_mapped_file.h:49
virtual bool isDurableMappedFile()
Definition: durable_mapped_file.h:128
stdx::mutex _m
Definition: durable_mapped_file.h:261
bool finishOpening()
Definition: durable_mapped_file.cpp:266
HANDLE getFd()
Definition: durable_mapped_file.h:111
int _fileSuffixNo
Definition: durable_mapped_file.h:137
unsigned numberOfViews_inlock() const
not-threadsafe, caller must hold _mutex()
Definition: durable_mapped_file.h:238
DurableMappedFile adds some layers atop memory mapped files - specifically our handling of private vi...
Definition: durable_mapped_file.h:47
DurableMappedFile(OperationContext *opCtx, OptionSet options=NONE)
Definition: durable_mapped_file.cpp:307
virtual ~DurableMappedFile()
Definition: durable_mapped_file.cpp:312
void * getView() const
Definition: durable_mapped_file.h:87
MONGO_DISALLOW_COPYING(MongoFile)
RelativePath _p
Definition: durable_mapped_file.h:136
std::string filename() const
Definition: durable_mapped_file.h:70
unsigned long long length() const
Definition: durable_mapped_file.h:66
OperationContext Database StringData BSONObj options
Definition: database_impl.cpp:949
std::string filename() const
Definition: mmap.h:151
bool open(OperationContext *opCtx, const std::string &fname)
Definition: durable_mapped_file.cpp:244
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
virtual void close(OperationContext *opCtx)
Callers must be holding a LockMongoFilesExclusive.
Definition: durable_mapped_file.cpp:288
const RelativePath & relativePath() const
for a filename a/b/c.3 filePath() is "a/b/c" fileSuffixNo() is 3 if the suffix is "ns"...
Definition: durable_mapped_file.h:103
void remapThePrivateView(OperationContext *opCtx)
Definition: durable_mapped_file.cpp:64
this is very much like a boost::path.
Definition: paths.h:49
void flush(bool sync)
Definition: durable_mapped_file.h:74
for durability support we want to be able to map pointers to specific DurableMappedFile objects...
Definition: durable_mapped_file.h:206
void makeWritable(void *, unsigned len)
make the private map range writable (necessary for our windows implementation)
Definition: durable_mapped_file.h:283