summaryrefslogtreecommitdiff
path: root/src/mongo/db/storage/mmap_v1/mmap.h
blob: fc28d56e1d92ac3bb23ecdcd4028b7589c54b768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// mmap.h

/*    Copyright 2009 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects
 *    for all of the code used other than as permitted herein. If you modify
 *    file(s) with this exception, you may extend this exception to your
 *    version of the file(s), but you are not obligated to do so. If you do not
 *    wish to do so, delete this exception statement from your version. If you
 *    delete this exception statement from all source files in the program,
 *    then also delete it in the license file.
 */

#pragma once

#include <set>
#include <sstream>
#include <vector>

#include "mongo/base/disallow_copying.h"
#include "mongo/db/client.h"
#include "mongo/db/concurrency/d_concurrency.h"
#include "mongo/db/operation_context.h"

namespace mongo {

#if !defined(_WIN32)
typedef int HANDLE;
#endif

extern const size_t g_minOSPageSizeBytes;
void minOSPageSizeBytesTest(size_t minOSPageSizeBytes);  // lame-o

// call this if syncing data fails
void dataSyncFailedHandler();

class MAdvise {
    MONGO_DISALLOW_COPYING(MAdvise);

public:
    enum Advice { Sequential = 1, Random = 2 };
    MAdvise(void* p, unsigned len, Advice a);
    ~MAdvise();  // destructor resets the range to MADV_NORMAL
private:
    void* _p;
    unsigned _len;
};

// lock order: lock dbMutex before this if you lock both
class LockMongoFilesShared {
    friend class LockMongoFilesExclusive;
    static Lock::ResourceMutex mmmutex;
    static unsigned era;

    Lock::SharedLock lk;

public:
    explicit LockMongoFilesShared(OperationContext* txn) : lk(txn->lockState(), mmmutex) {
        // JS worker threads may not have cc() setup, as they work on behalf of other clients
        dassert(txn == cc().getOperationContext() || !cc().getOperationContext());
    }

    static void assertExclusivelyLocked(OperationContext* txn) {
        invariant(mmmutex.isExclusivelyLocked(txn->lockState()));
    }

    static void assertAtLeastReadLocked(OperationContext* txn) {
        invariant(mmmutex.isAtLeastReadLocked(txn->lockState()));
    }

    /** era changes anytime memory maps come and go.  thus you can use this as a cheap way to check
        if nothing has changed since the last time you locked.  Of course you must be shared locked
        at the time of this call, otherwise someone could be in progress.

        This is used for yielding; see PageFaultException::touch().
    */
    static unsigned getEra() {
        return era;
    }
};

class LockMongoFilesExclusive {
    Lock::ExclusiveLock lk;

public:
    explicit LockMongoFilesExclusive(OperationContext* txn)
        : lk(txn->lockState(), LockMongoFilesShared::mmmutex) {
        // JS worker threads may not have cc() setup, as they work on behalf of other clients
        dassert(txn == cc().getOperationContext() || !cc().getOperationContext());
        LockMongoFilesShared::era++;
    }
};

/* the administrative-ish stuff here */
class MongoFile {
    MONGO_DISALLOW_COPYING(MongoFile);

public:
    /** Flushable has to fail nicely if the underlying object gets killed */
    class Flushable {
    public:
        virtual ~Flushable() {}
        virtual void flush(OperationContext* txn) = 0;
    };

    enum Options {
        NONE = 0,
        SEQUENTIAL = 1 << 0,  // hint - e.g. FILE_FLAG_SEQUENTIAL_SCAN on windows.
        READONLY = 1 << 1     // if true, writing to the mapped file will crash the process.
    };

    // Integral type used as a BitSet of Options.
    using OptionSet = std::underlying_type<Options>::type;

    MongoFile(OptionSet options);
    virtual ~MongoFile() = default;

    /** @param fun is called for each MongoFile.
        called from within a mutex that MongoFile uses. so be careful not to deadlock.
    */
    template <class F>
    static void forEach(OperationContext* txn, F fun);

    /**
     * note: you need to be in mmmutex when using this. forEach (above) handles that for you
     * automatically.
     */
    static std::set<MongoFile*>& getAllFiles();

    static int flushAll(OperationContext* txn, bool sync);  // returns n flushed
    static void closeAllFiles(OperationContext* txn, std::stringstream& message);

    virtual bool isDurableMappedFile() {
        return false;
    }

    std::string filename() const {
        return _filename;
    }
    void setFilename(OperationContext* txn, const std::string& fn);

    virtual uint64_t getUniqueId() const = 0;

private:
    std::string _filename;
    static int _flushAll(OperationContext* txn, bool sync);  // returns n flushed
    const OptionSet _options;

protected:
    /**
     * Implementations may assume this is called from within `LockMongoFilesExclusive`.
     */
    virtual void close(OperationContext* txn) = 0;
    virtual void flush(bool sync) = 0;
    /**
     * returns a thread safe object that you can call flush on
     * Flushable has to fail nicely if the underlying object gets killed
     */
    virtual Flushable* prepareFlush() = 0;

    /**
     * Returns true iff the file is closed.
     */
    virtual bool isClosed() = 0;

    void created(OperationContext* txn); /* subclass must call after create */

    /**
     * Implementations may assume this is called from within `LockMongoFilesExclusive`.
     *
     * subclass must call in destructor (or at close).
     *  removes this from pathToFile and other maps
     *  safe to call more than once, albeit might be wasted work
     *  ideal to call close to the close, if the close is well before object destruction
     */
    void destroyed(OperationContext* txn);

    virtual unsigned long long length() const = 0;

    bool isOptionSet(Options option) const {
        return _options & option;
    }
};

/** look up a MMF by filename. scoped mutex locking convention.
    example:
      MMFFinderByName finder;
      DurableMappedFile *a = finder.find("file_name_a");
      DurableMappedFile *b = finder.find("file_name_b");
*/
class MongoFileFinder {
    MONGO_DISALLOW_COPYING(MongoFileFinder);

public:
    MongoFileFinder(OperationContext* txn) : _lk(txn) {}

    /** @return The MongoFile object associated with the specified file name.  If no file is open
                with the specified name, returns null.
    */
    MongoFile* findByPath(const std::string& path) const;

private:
    LockMongoFilesShared _lk;
};

class MemoryMappedFile : public MongoFile {
protected:
    virtual void* viewForFlushing() {
        if (views.size() == 0)
            return 0;
        verify(views.size() == 1);
        return views[0];
    }

public:
    MemoryMappedFile(OperationContext* txn, OptionSet options = NONE);

    virtual ~MemoryMappedFile();

    /**
     * Callers must be holding a `LockMongoFilesExclusive`.
     */
    virtual void close(OperationContext* txn);

    /**
     * uasserts if file doesn't exist. fasserts on mmap error.
     */
    void* map(OperationContext* txn, const char* filename);

    /**
     * uasserts if file exists. fasserts on mmap error.
     * @param zero fill file with zeros when true
     */
    void* create(OperationContext* txn,
                 const std::string& filename,
                 unsigned long long len,
                 bool zero);

    void flush(bool sync);

    virtual bool isClosed();

    virtual Flushable* prepareFlush();

    long shortLength() const {
        return (long)len;
    }
    unsigned long long length() const {
        return len;
    }
    HANDLE getFd() const {
        return fd;
    }

    /**
     * Creates a new view with the specified properties. Automatically cleaned up upon
     * close/destruction of the MemoryMappedFile object. Returns nullptr on mmap error.
     */
    void* createPrivateMap();

    virtual uint64_t getUniqueId() const {
        return _uniqueId;
    }

    static int totalMappedLengthInMB() {
        return static_cast<int>(totalMappedLength.load() / 1024 / 1024);
    }

private:
    static void updateLength(const char* filename, unsigned long long& length);

    HANDLE fd = 0;
    HANDLE maphandle = 0;
    std::vector<void*> views;
    unsigned long long len = 0u;
    static AtomicUInt64 totalMappedLength;
    const uint64_t _uniqueId;
#ifdef _WIN32
    // flush Mutex
    //
    // Protects:
    //  Prevent flush() and close() from concurrently running.
    //  It ensures close() cannot complete while flush() is running
    // Lock Ordering:
    //  LockMongoFilesShared must be taken before _flushMutex if both are taken
    stdx::mutex _flushMutex;
#endif

protected:
    /**
     * Creates with length if DNE, otherwise validates input length. Returns nullptr on mmap
     * error.
     */
    void* map(OperationContext* txn, const char* filename, unsigned long long& length);

    /**
     * Close the current private view and open a new replacement. Returns nullptr on mmap error.
     */
    void* remapPrivateView(OperationContext* txn, void* oldPrivateAddr);
};

/** p is called from within a mutex that MongoFile uses.  so be careful not to deadlock. */
template <class F>
inline void MongoFile::forEach(OperationContext* txn, F p) {
    LockMongoFilesShared lklk(txn);
    const std::set<MongoFile*>& mmfiles = MongoFile::getAllFiles();
    for (std::set<MongoFile*>::const_iterator i = mmfiles.begin(); i != mmfiles.end(); i++)
        p(*i);
}

}  // namespace mongo