summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/include/os_fhandle_inline.h
blob: f2a74ea0ef0359d176018bea8bcb14555d99d55d (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
/*-
 * Copyright (c) 2014-present MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

/*
 * Define functions that increment histogram statistics for filesystem operations latency.
 */
WT_STAT_MSECS_HIST_INCR_FUNC(fsread, perf_hist_fsread_latency, 10)
WT_STAT_MSECS_HIST_INCR_FUNC(fswrite, perf_hist_fswrite_latency, 10)

/*
 * __wt_fsync --
 *     POSIX fsync.
 */
static inline int
__wt_fsync(WT_SESSION_IMPL *session, WT_FH *fh, bool block)
{
    WT_DECL_RET;
    WT_FILE_HANDLE *handle;

    WT_ASSERT(session, !F_ISSET(S2C(session), WT_CONN_READONLY));

    __wt_verbose(session, WT_VERB_HANDLEOPS, "%s: handle-sync", fh->handle->name);

    handle = fh->handle;
    /*
     * There is no way to check when the non-blocking sync-file-range is complete, but we track the
     * time taken in the call for completeness.
     */
    WT_STAT_CONN_INCR_ATOMIC(session, thread_fsync_active);
    WT_STAT_CONN_INCR(session, fsync_io);
    if (block)
        ret = (handle->fh_sync == NULL ? 0 : handle->fh_sync(handle, (WT_SESSION *)session));
    else
        ret =
          (handle->fh_sync_nowait == NULL ? 0 :
                                            handle->fh_sync_nowait(handle, (WT_SESSION *)session));
    WT_STAT_CONN_DECR_ATOMIC(session, thread_fsync_active);
    return (ret);
}

/*
 * __wt_fextend --
 *     Extend a file.
 */
static inline int
__wt_fextend(WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t offset)
{
    WT_FILE_HANDLE *handle;
#ifdef HAVE_DIAGNOSTIC
    wt_off_t cur_size;
#endif

    WT_ASSERT(session, !F_ISSET(S2C(session), WT_CONN_READONLY));
    WT_ASSERT(session, !F_ISSET(S2C(session), WT_CONN_IN_MEMORY));

    __wt_verbose(session, WT_VERB_HANDLEOPS, "%s: handle-extend: to %" PRIuMAX, fh->handle->name,
      (uintmax_t)offset);

    /*
     * Our caller is responsible for handling any locking issues, all we have to do is find a
     * function to call.
     */
    handle = fh->handle;
#ifdef HAVE_DIAGNOSTIC
    /* Make sure we don't try to shrink the file during backup. */
    if (handle->fh_size != NULL) {
        WT_RET(handle->fh_size(handle, (WT_SESSION *)session, &cur_size));
        WT_ASSERT(session, cur_size <= offset || S2C(session)->hot_backup_start == 0);
    }
#endif
    if (handle->fh_extend_nolock != NULL)
        return (handle->fh_extend_nolock(handle, (WT_SESSION *)session, offset));
    if (handle->fh_extend != NULL)
        return (handle->fh_extend(handle, (WT_SESSION *)session, offset));
    return (__wt_set_return(session, ENOTSUP));
}

/*
 * __wt_file_lock --
 *     Lock/unlock a file.
 */
static inline int
__wt_file_lock(WT_SESSION_IMPL *session, WT_FH *fh, bool lock)
{
    WT_FILE_HANDLE *handle;

    __wt_verbose(session, WT_VERB_HANDLEOPS, "%s: handle-lock: %s", fh->handle->name,
      lock ? "lock" : "unlock");

    handle = fh->handle;
    return (handle->fh_lock == NULL ? 0 : handle->fh_lock(handle, (WT_SESSION *)session, lock));
}

/*
 * __wt_read --
 *     POSIX pread.
 */
static inline int
__wt_read(WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t offset, size_t len, void *buf)
{
    WT_DECL_RET;
    uint64_t time_start, time_stop;

    __wt_verbose_debug2(session, WT_VERB_HANDLEOPS,
      "%s: handle-read: %" WT_SIZET_FMT " at %" PRIuMAX, fh->handle->name, len, (uintmax_t)offset);

    WT_STAT_CONN_INCR_ATOMIC(session, thread_read_active);
    WT_STAT_CONN_INCR(session, read_io);
    time_start = __wt_clock(session);

    ret = fh->handle->fh_read(fh->handle, (WT_SESSION *)session, offset, len, buf);

    /* Flag any failed read: if we're in startup, it may be fatal. */
    if (ret != 0)
        F_SET(S2C(session), WT_CONN_DATA_CORRUPTION);

    time_stop = __wt_clock(session);
    __wt_stat_msecs_hist_incr_fsread(session, WT_CLOCKDIFF_MS(time_stop, time_start));
    WT_STAT_CONN_DECR_ATOMIC(session, thread_read_active);
    return (ret);
}

/*
 * __wt_filesize --
 *     Get the size of a file in bytes, by file handle.
 */
static inline int
__wt_filesize(WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t *sizep)
{
    __wt_verbose(session, WT_VERB_HANDLEOPS, "%s: handle-size", fh->handle->name);

    return (fh->handle->fh_size(fh->handle, (WT_SESSION *)session, sizep));
}

/*
 * __wt_ftruncate --
 *     Truncate a file.
 */
static inline int
__wt_ftruncate(WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t offset)
{
    WT_FILE_HANDLE *handle;
#ifdef HAVE_DIAGNOSTIC
    wt_off_t cur_size;
#endif

    WT_ASSERT(session, !F_ISSET(S2C(session), WT_CONN_READONLY));

    __wt_verbose(session, WT_VERB_HANDLEOPS, "%s: handle-truncate: to %" PRIuMAX, fh->handle->name,
      (uintmax_t)offset);

    /*
     * Our caller is responsible for handling any locking issues, all we have to do is find a
     * function to call.
     */
    handle = fh->handle;
#ifdef HAVE_DIAGNOSTIC
    /* Make sure we don't try to shrink the file during backup. */
    if (handle->fh_size != NULL) {
        WT_RET(handle->fh_size(handle, (WT_SESSION *)session, &cur_size));
        WT_ASSERT(session, cur_size <= offset || S2C(session)->hot_backup_start == 0);
    }
#endif
    if (handle->fh_truncate != NULL)
        return (handle->fh_truncate(handle, (WT_SESSION *)session, offset));
    return (__wt_set_return(session, ENOTSUP));
}

/*
 * __wt_write --
 *     POSIX pwrite.
 */
static inline int
__wt_write(WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t offset, size_t len, const void *buf)
{
    WT_DECL_RET;
    uint64_t time_start, time_stop;

    WT_ASSERT(session,
      !F_ISSET(S2C(session), WT_CONN_READONLY) ||
        WT_STRING_MATCH(fh->name, WT_SINGLETHREAD, strlen(WT_SINGLETHREAD)));

    __wt_verbose_debug2(session, WT_VERB_HANDLEOPS,
      "%s: handle-write: %" WT_SIZET_FMT " at %" PRIuMAX, fh->handle->name, len, (uintmax_t)offset);

    /*
     * Do a final panic check before I/O, so we stop writing as quickly as possible if there's an
     * unanticipated error. We aren't handling the error correctly by definition, and writing won't
     * make things better.
     */
    WT_RET(WT_SESSION_CHECK_PANIC(session));

    WT_STAT_CONN_INCR(session, write_io);
    WT_STAT_CONN_INCR_ATOMIC(session, thread_write_active);
    time_start = __wt_clock(session);

    ret = fh->handle->fh_write(fh->handle, (WT_SESSION *)session, offset, len, buf);

    time_stop = __wt_clock(session);
    __wt_stat_msecs_hist_incr_fswrite(session, WT_CLOCKDIFF_MS(time_stop, time_start));
    (void)__wt_atomic_addv64(&fh->written, len);
    WT_STAT_CONN_DECR_ATOMIC(session, thread_write_active);
    return (ret);
}