summaryrefslogtreecommitdiff
path: root/src/mongo/util/logfile.cpp
blob: b8d28e3cfa772654128bd8e0f57da6ea605e9a32 (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
// @file logfile.cpp simple file log writing / journaling

/**
*    Copyright (C) 2008 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/logfile.h"

#include "mongo/platform/posix_fadvise.h"
#include "mongo/util/log.h"
#include "mongo/util/mmap.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/util/startup_test.h"
#include "mongo/util/text.h"


using namespace mongoutils;

namespace mongo {
    struct LogfileTest : public StartupTest {
        LogfileTest() { }
        void run() {
            if( 0 && debug ) {
                try {
                    LogFile f("logfile_test");
                    void *p = malloc(16384);
                    char *buf = (char*) p;
                    buf += 4095;
                    buf = (char*) (((size_t)buf)&(~0xfff));
                    memset(buf, 'z', 8192);
                    buf[8190] = '\n';
                    buf[8191] = 'B';
                    buf[0] = 'A';
                    f.synchronousAppend(buf, 8192);
                    f.synchronousAppend(buf, 8192);
                    free(p);
                }
                catch(DBException& e ) {
                    log() << "logfile.cpp test failed : " << e.what() << endl;
                    throw;
                }
            }
        }
    } __test;
}

#if defined(_WIN32)

namespace mongo {

    LogFile::LogFile(const std::string& name, bool readwrite) : _name(name) {
        _fd = CreateFile(
                  toNativeString(name.c_str()).c_str(),
                  (readwrite?GENERIC_READ:0)|GENERIC_WRITE,
                  FILE_SHARE_READ,
                  NULL,
                  OPEN_ALWAYS,
                  FILE_FLAG_NO_BUFFERING,
                  NULL);
        if( _fd == INVALID_HANDLE_VALUE ) {
            DWORD e = GetLastError();
            uasserted(13518, str::stream() << "couldn't open file " << name << " for writing " << errnoWithDescription(e));
        }
        SetFilePointer(_fd, 0, 0, FILE_BEGIN);
    }

    LogFile::~LogFile() {
        if( _fd != INVALID_HANDLE_VALUE )
            CloseHandle(_fd);
    }

    void LogFile::truncate() {
        verify(_fd != INVALID_HANDLE_VALUE);

        if (!SetEndOfFile(_fd)){
            msgasserted(15871, "Couldn't truncate file: " + errnoWithDescription());
        }
    }

    void LogFile::writeAt(unsigned long long offset, const void *_buf, size_t _len) { 
// TODO 64 bit offsets
        OVERLAPPED o;
        memset(&o,0,sizeof(o));
        (unsigned long long&) o.Offset = offset; 
        BOOL ok= WriteFile(_fd, _buf, _len, 0, &o);
        verify(ok);
    }

    void LogFile::readAt(unsigned long long offset, void *_buf, size_t _len) { 
// TODO 64 bit offsets
        OVERLAPPED o;
        memset(&o,0,sizeof(o));
        (unsigned long long&) o.Offset = offset;
        DWORD nr;
        BOOL ok = ReadFile(_fd, _buf, _len, &nr, &o);
        if( !ok ) {
            string e = errnoWithDescription();
            //DWORD e = GetLastError();
            log() << "LogFile readAt(" << offset << ") len:" << _len << "errno:" << e << endl;
            verify(false);
        }
    }

    void LogFile::synchronousAppend(const void *_buf, size_t _len) {
        const size_t BlockSize = 8 * 1024 * 1024;
        verify(_fd);
        verify(_len % g_minOSPageSizeBytes == 0);
        const char *buf = (const char *) _buf;
        size_t left = _len;
        while( left ) {
            size_t toWrite = min(left, BlockSize);
            DWORD written;
            if( !WriteFile(_fd, buf, toWrite, &written, NULL) ) {
                DWORD e = GetLastError();
                if( e == 87 )
                    msgasserted(13519, "error 87 appending to file - invalid parameter");
                else
                    uasserted(13517, str::stream() << "error appending to file " << _name << ' ' << _len << ' ' << toWrite << ' ' << errnoWithDescription(e));
            }
            else {
                dassert( written == toWrite );
            }
            left -= written;
            buf += written;
        }
    }

}

#else

/// posix

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "paths.h"

namespace mongo {

    LogFile::LogFile(const std::string& name, bool readwrite) : _name(name) {
        int options = O_CREAT
                    | (readwrite?O_RDWR:O_WRONLY)
#if defined(O_DIRECT)
                    | O_DIRECT
#endif
#if defined(O_NOATIME)
                    | O_NOATIME
#endif
                    ;

        _fd = open(name.c_str(), options, S_IRUSR | S_IWUSR);

#if defined(O_DIRECT)
        _direct = true;
        if( _fd < 0 ) {
            _direct = false;
            options &= ~O_DIRECT;
            _fd = open(name.c_str(), options, S_IRUSR | S_IWUSR);
        }
#else
        _direct = false;
#endif

        if( _fd < 0 ) {
            uasserted(13516, str::stream() << "couldn't open file " << name << " for writing " << errnoWithDescription());
        }

        flushMyDirectory(name);
    }

    LogFile::~LogFile() {
        if( _fd >= 0 )
            close(_fd);
        _fd = -1;
    }

    void LogFile::truncate() {
        verify(_fd >= 0);

        BOOST_STATIC_ASSERT(sizeof(off_t) == 8); // we don't want overflow here
        const off_t pos = lseek(_fd, 0, SEEK_CUR); // doesn't actually seek
        if (ftruncate(_fd, pos) != 0){
            msgasserted(15873, "Couldn't truncate file: " + errnoWithDescription());
        }

        fsync(_fd);
    }

    void LogFile::writeAt(unsigned long long offset, const void *buf, size_t len) { 
        verify(((size_t)buf) % g_minOSPageSizeBytes == 0); // aligned
        ssize_t written = pwrite(_fd, buf, len, offset);
        if( written != (ssize_t) len ) {
            log() << "writeAt fails " << errnoWithDescription() << endl;
        }
#if defined(__linux__)
        fdatasync(_fd);
#else
        fsync(_fd);
#endif
    }

    void LogFile::readAt(unsigned long long offset, void *_buf, size_t _len) { 
        verify(((size_t)_buf) % g_minOSPageSizeBytes == 0); // aligned
        ssize_t rd = pread(_fd, _buf, _len, offset);
        verify( rd != -1 );
    }

    void LogFile::synchronousAppend(const void *b, size_t len) {

        const char *buf = static_cast<const char *>( b );
        ssize_t charsToWrite = static_cast<ssize_t>( len );

        fassert( 16144, charsToWrite >= 0 );
        fassert( 16142, _fd >= 0 );
        fassert( 16143, reinterpret_cast<ssize_t>( buf ) % g_minOSPageSizeBytes == 0 );  // aligned

#ifdef POSIX_FADV_DONTNEED
        const off_t pos = lseek(_fd, 0, SEEK_CUR); // doesn't actually seek, just get current position
#endif

        while ( charsToWrite > 0 ) {
            const ssize_t written = write( _fd, buf, static_cast<size_t>( charsToWrite ) );
            if ( -1 == written ) {
                log() << "LogFile::synchronousAppend failed with " << charsToWrite
                      << " bytes unwritten out of " << len << " bytes;  b=" << b << ' '
                      << errnoWithDescription() << std::endl;
                fassertFailed( 13515 );
            }
            buf += written;
            charsToWrite -= written;
        }

        if( 
#if defined(__linux__)
           fdatasync(_fd) < 0 
#else
           fsync(_fd)
#endif
            ) {
            log() << "error appending to file on fsync " << ' ' << errnoWithDescription();
            fassertFailed( 13514 );
        }

#ifdef POSIX_FADV_DONTNEED
        if (!_direct)
            posix_fadvise(_fd, pos, len, POSIX_FADV_DONTNEED);
#endif
    }

}

#endif