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
|
// mmap.cpp
/* 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.
*/
#include "mongo/pch.h"
#include "mongo/util/mmap.h"
#include <boost/filesystem/operations.hpp>
#include "mongo/base/owned_pointer_vector.h"
#include "mongo/util/concurrency/rwlock.h"
#include "mongo/util/map_util.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/util/processinfo.h"
#include "mongo/util/progress_meter.h"
#include "mongo/util/startup_test.h"
namespace mongo {
void minOSPageSizeBytesTest(size_t minOSPageSizeBytes) {
fassert( 16325, minOSPageSizeBytes > 0 );
fassert( 16326, minOSPageSizeBytes < 1000000 );
// check to see if the page size is a power of 2
fassert( 16327, (minOSPageSizeBytes & (minOSPageSizeBytes - 1)) == 0);
}
namespace {
set<MongoFile*> mmfiles;
map<string,MongoFile*> pathToFile;
} // namespace
/* Create. Must not exist.
@param zero fill file with zeros when true
*/
void* MemoryMappedFile::create(const std::string& filename, unsigned long long len, bool zero) {
uassert( 13468, string("can't create file already exists ") + filename, ! boost::filesystem::exists(filename) );
void *p = map(filename.c_str(), len);
if( p && zero ) {
size_t sz = (size_t) len;
verify( len == sz );
memset(p, 0, sz);
}
return p;
}
/*static*/ void MemoryMappedFile::updateLength( const char *filename, unsigned long long &length ) {
if ( !boost::filesystem::exists( filename ) )
return;
// make sure we map full length if preexisting file.
boost::uintmax_t l = boost::filesystem::file_size( filename );
length = l;
}
void* MemoryMappedFile::map(const char *filename) {
unsigned long long l;
try {
l = boost::filesystem::file_size( filename );
}
catch(boost::filesystem::filesystem_error& e) {
uasserted(15922, mongoutils::str::stream() << "couldn't get file length when opening mapping " << filename << ' ' << e.what() );
}
return map( filename , l );
}
void* MemoryMappedFile::mapWithOptions(const char *filename, int options) {
unsigned long long l;
try {
l = boost::filesystem::file_size( filename );
}
catch(boost::filesystem::filesystem_error& e) {
uasserted(15923, mongoutils::str::stream() << "couldn't get file length when opening mapping " << filename << ' ' << e.what() );
}
return map( filename , l, options );
}
/* --- MongoFile -------------------------------------------------
this is the administrative stuff
*/
RWLockRecursiveNongreedy LockMongoFilesShared::mmmutex("mmmutex",10*60*1000 /* 10 minutes */);
unsigned LockMongoFilesShared::era = 99; // note this rolls over
set<MongoFile*>& MongoFile::getAllFiles() { return mmfiles; }
/* 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 MongoFile::destroyed() {
LockMongoFilesShared::assertExclusivelyLocked();
mmfiles.erase(this);
pathToFile.erase( filename() );
}
/*static*/
void MongoFile::closeAllFiles( stringstream &message ) {
static int closingAllFiles = 0;
if ( closingAllFiles ) {
message << "warning closingAllFiles=" << closingAllFiles << endl;
return;
}
++closingAllFiles;
LockMongoFilesExclusive lk;
ProgressMeter pm(mmfiles.size(), 2, 1, "files", "File Closing Progress");
set<MongoFile*> temp = mmfiles;
for ( set<MongoFile*>::iterator i = temp.begin(); i != temp.end(); i++ ) {
(*i)->close(); // close() now removes from mmfiles
pm.hit();
}
message << "closeAllFiles() finished";
--closingAllFiles;
}
/*static*/ long long MongoFile::totalMappedLength() {
unsigned long long total = 0;
LockMongoFilesShared lk;
for ( set<MongoFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ )
total += (*i)->length();
return total;
}
void nullFunc() { }
// callback notifications
void (*MongoFile::notifyPreFlush)() = nullFunc;
void (*MongoFile::notifyPostFlush)() = nullFunc;
/*static*/ int MongoFile::flushAll( bool sync ) {
if ( sync ) notifyPreFlush();
int x = _flushAll(sync);
if ( sync ) notifyPostFlush();
return x;
}
/*static*/ int MongoFile::_flushAll( bool sync ) {
if ( ! sync ) {
int num = 0;
LockMongoFilesShared lk;
for ( set<MongoFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ ) {
num++;
MongoFile * mmf = *i;
if ( ! mmf )
continue;
mmf->flush( sync );
}
return num;
}
// want to do it sync
// get a thread-safe Flushable object for each file first in a single lock
// so that we can iterate and flush without doing any locking here
OwnedPointerVector<Flushable> thingsToFlushWrapper;
vector<Flushable*>& thingsToFlush = thingsToFlushWrapper.mutableVector();
{
LockMongoFilesShared lk;
for ( set<MongoFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ ) {
MongoFile* mmf = *i;
if ( !mmf )
continue;
thingsToFlush.push_back( mmf->prepareFlush() );
}
}
for ( size_t i = 0; i < thingsToFlush.size(); i++ ) {
thingsToFlush[i]->flush();
}
return thingsToFlush.size();
}
void MongoFile::created() {
LockMongoFilesExclusive lk;
mmfiles.insert(this);
}
void MongoFile::setFilename(const std::string& fn) {
LockMongoFilesExclusive lk;
verify( _filename.empty() );
_filename = boost::filesystem::absolute(fn).generic_string();
MongoFile *&ptf = pathToFile[_filename];
massert(13617, "MongoFile : multiple opens of same filename", ptf == 0);
ptf = this;
}
MongoFile* MongoFileFinder::findByPath(const std::string& path) const {
return mapFindWithDefault(pathToFile,
boost::filesystem::absolute(path).generic_string(),
static_cast<MongoFile*>(NULL));
}
void printMemInfo( const char * where ) {
LogstreamBuilder out = log();
out << "mem info: ";
if ( where )
out << where << " ";
ProcessInfo pi;
if ( ! pi.supported() ) {
out << " not supported";
return;
}
out << "vsize: " << pi.getVirtualMemorySize()
<< " resident: " << pi.getResidentSize()
<< " mapped: " << ( MemoryMappedFile::totalMappedLength() / ( 1024 * 1024 ) );
}
void dataSyncFailedHandler() {
problem() << "error syncing data to disk, probably a disk error";
problem() << " shutting down immediately to avoid corruption";
fassertFailed( 17346 );
}
} // namespace mongo
|