summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/support/thread_group.c
blob: 0f3e3084b54119f4a2ddefb4c5b6837d12aa9098 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*-
 * Copyright (c) 2014-present MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __thread_run --
 *     General wrapper for any thread.
 */
static WT_THREAD_RET
__thread_run(void *arg)
{
    WT_DECL_RET;
    WT_SESSION_IMPL *session;
    WT_THREAD *thread;

    thread = (WT_THREAD *)arg;
    session = thread->session;

    for (;;) {
        if (!F_ISSET(thread, WT_THREAD_RUN))
            break;
        if (!F_ISSET(thread, WT_THREAD_ACTIVE))
            __wt_cond_wait(
              session, thread->pause_cond, WT_THREAD_PAUSE * WT_MILLION, thread->chk_func);
        WT_ERR(thread->run_func(session, thread));
    }

/*
 * If a thread is stopping it may have subsystem cleanup to do.
 */
err:
    if (thread->stop_func != NULL)
        ret = thread->stop_func(session, thread);

    if (ret != 0 && F_ISSET(thread, WT_THREAD_PANIC_FAIL))
        WT_IGNORE_RET(__wt_panic(session, ret, "Unrecoverable utility thread error"));

    /*
     * The cases when threads are expected to stop are:
     * 1.  When recovery is done.
     * 2.  When the connection is closing.
     * 3.  When a shutdown has been requested via clearing the run flag.
     * 4.  When an error has occurred and the connection panic flag is set.
     */
    WT_ASSERT(session,
      !F_ISSET(thread, WT_THREAD_RUN) ||
        F_ISSET(S2C(session), WT_CONN_CLOSING | WT_CONN_PANIC | WT_CONN_RECOVERING));

    return (WT_THREAD_RET_VALUE);
}

/*
 * __thread_group_shrink --
 *     Decrease the number of threads in the group and free memory associated with slots larger than
 *     the new count.
 */
static int
__thread_group_shrink(WT_SESSION_IMPL *session, WT_THREAD_GROUP *group, uint32_t new_count)
{
    WT_DECL_RET;
    WT_SESSION *wt_session;
    WT_THREAD *thread;
    uint32_t current_slot;

    WT_ASSERT(session, __wt_rwlock_islocked(session, &group->lock));

    for (current_slot = group->alloc; current_slot > new_count;) {
        /*
         * The offset value is a counter not an array index, so adjust it before finding the last
         * thread in the group.
         */
        thread = group->threads[--current_slot];

        if (thread == NULL)
            continue;

        WT_ASSERT(session, thread->tid.created);
        __wt_verbose(session, WT_VERB_THREAD_GROUP, "Stopping utility thread: %s:%" PRIu32,
          group->name, thread->id);
        if (F_ISSET(thread, WT_THREAD_ACTIVE))
            --group->current_threads;
        F_CLR(thread, WT_THREAD_ACTIVE | WT_THREAD_RUN);
        /*
         * Signal the thread in case it is in a long timeout.
         */
        __wt_cond_signal(session, thread->pause_cond);
        __wt_cond_signal(session, group->wait_cond);
    }

    /*
     * We have to perform the join without holding the lock because the threads themselves may be
     * waiting on the lock.
     */
    __wt_writeunlock(session, &group->lock);
    for (current_slot = group->alloc; current_slot > new_count;) {
        thread = group->threads[--current_slot];

        if (thread == NULL)
            continue;
        WT_TRET(__wt_thread_join(session, &thread->tid));
        __wt_cond_destroy(session, &thread->pause_cond);
    }
    __wt_writelock(session, &group->lock);
    for (current_slot = group->alloc; current_slot > new_count;) {
        thread = group->threads[--current_slot];

        if (thread == NULL)
            continue;
        WT_ASSERT(session, thread->session != NULL);
        wt_session = (WT_SESSION *)thread->session;
        WT_TRET(wt_session->close(wt_session, NULL));
        thread->session = NULL;
        __wt_free(session, thread);
        group->threads[current_slot] = NULL;
    }

    return (ret);
}

/*
 * __thread_group_resize --
 *     Resize an array of utility threads already holding the lock.
 */
static int
__thread_group_resize(WT_SESSION_IMPL *session, WT_THREAD_GROUP *group, uint32_t new_min,
  uint32_t new_max, uint32_t flags)
{
    WT_CONNECTION_IMPL *conn;
    WT_DECL_RET;
    WT_SESSION *wt_session;
    WT_THREAD *thread;
    size_t alloc;
    uint32_t i, session_flags;

    conn = S2C(session);
    thread = NULL;

    __wt_verbose(session, WT_VERB_THREAD_GROUP,
      "Resize thread group: %s, from min: %" PRIu32 " -> %" PRIu32 " from max: %" PRIu32
      " -> %" PRIu32,
      group->name, group->min, new_min, group->max, new_max);

    WT_ASSERT(session,
      group->current_threads <= group->alloc && __wt_rwlock_islocked(session, &group->lock));

    if (new_min == group->min && new_max == group->max)
        return (0);

    if (new_min > new_max)
        WT_ERR_MSG(session, EINVAL,
          "Illegal thread group resize: %s, from min: %" PRIu32 " -> %" PRIu32 " from max: %" PRIu32
          " -> %" PRIu32,
          group->name, group->min, new_min, group->max, new_max);

    /*
     * Call shrink to reduce the number of thread structures and running threads if required by the
     * change in group size.
     */
    WT_ERR(__thread_group_shrink(session, group, new_max));

    /*
     * Only reallocate the thread array if it is the largest ever, since our realloc doesn't support
     * shrinking the allocated size.
     */
    if (group->alloc < new_max) {
        alloc = group->alloc * sizeof(*group->threads);
        WT_ERR(__wt_realloc(session, &alloc, new_max * sizeof(*group->threads), &group->threads));
        group->alloc = new_max;
    }

    /*
     * Initialize the structures based on the previous group size, not the previous allocated size.
     */
    for (i = group->max; i < new_max; i++) {
        WT_ERR(__wt_calloc_one(session, &thread));
        /* Threads get their own session. */
        session_flags = LF_ISSET(WT_THREAD_CAN_WAIT) ? WT_SESSION_CAN_WAIT : 0;
        WT_ERR(
          __wt_open_internal_session(conn, group->name, false, session_flags, 0, &thread->session));
        if (LF_ISSET(WT_THREAD_PANIC_FAIL))
            F_SET(thread, WT_THREAD_PANIC_FAIL);
        thread->id = i;
        thread->chk_func = group->chk_func;
        thread->run_func = group->run_func;
        thread->stop_func = group->stop_func;
        WT_ERR(__wt_cond_alloc(session, "Thread cond", &thread->pause_cond));

        /*
         * Start thread as inactive. We'll activate the needed number later.
         */
        __wt_verbose(session, WT_VERB_THREAD_GROUP, "Starting utility thread: %s:%" PRIu32,
          group->name, thread->id);
        F_SET(thread, WT_THREAD_RUN);
        WT_ERR(__wt_thread_create(thread->session, &thread->tid, __thread_run, thread));

        WT_ASSERT(session, group->threads[i] == NULL);
        group->threads[i] = thread;
        thread = NULL;
    }

    group->max = new_max;
    group->min = new_min;
    while (group->current_threads < new_min)
        __wt_thread_group_start_one(session, group, true);
    return (0);

err:
    /*
     * An error resizing a thread array is currently fatal, it should only happen in an out of
     * memory situation. Do real cleanup just in case that changes in the future.
     */
    if (thread != NULL) {
        if (thread->session != NULL) {
            wt_session = (WT_SESSION *)thread->session;
            WT_TRET(wt_session->close(wt_session, NULL));
        }
        __wt_cond_destroy(session, &thread->pause_cond);
        __wt_free(session, thread);
    }

    /*
     * Update the thread group information even on failure to improve our chances of cleaning up
     * properly.
     */
    group->max = new_max;
    group->min = new_min;
    WT_TRET(__wt_thread_group_destroy(session, group));

    WT_RET_PANIC(session, ret, "Error while resizing thread group");
}

/*
 * __wt_thread_group_resize --
 *     Resize an array of utility threads taking the lock.
 */
int
__wt_thread_group_resize(WT_SESSION_IMPL *session, WT_THREAD_GROUP *group, uint32_t new_min,
  uint32_t new_max, uint32_t flags)
{
    WT_DECL_RET;

    __wt_writelock(session, &group->lock);
    WT_TRET(__thread_group_resize(session, group, new_min, new_max, flags));
    /* If the resize fails, the thread group is destroyed, including the lock. */
    if (ret == 0)
        __wt_writeunlock(session, &group->lock);
    return (ret);
}

/*
 * __wt_thread_group_create --
 *     Create a new thread group, assumes incoming group structure is zero initialized.
 */
int
__wt_thread_group_create(WT_SESSION_IMPL *session, WT_THREAD_GROUP *group, const char *name,
  uint32_t min, uint32_t max, uint32_t flags, bool (*chk_func)(WT_SESSION_IMPL *session),
  int (*run_func)(WT_SESSION_IMPL *session, WT_THREAD *context),
  int (*stop_func)(WT_SESSION_IMPL *session, WT_THREAD *context))
{
    WT_DECL_RET;
    bool cond_alloced;

    /* Check that the structure is initialized as expected */
    WT_ASSERT(session, group->alloc == 0);

    cond_alloced = false;

    __wt_verbose(session, WT_VERB_THREAD_GROUP, "Creating thread group: %s", name);

    WT_RET(__wt_rwlock_init(session, &group->lock));
    WT_ERR(__wt_cond_alloc(session, "thread group cond", &group->wait_cond));
    cond_alloced = true;

    __wt_writelock(session, &group->lock);
    group->chk_func = chk_func;
    group->run_func = run_func;
    group->stop_func = stop_func;
    group->name = name;

    WT_TRET(__thread_group_resize(session, group, min, max, flags));
    /* If the resize fails, the thread group is destroyed, including the lock. */
    if (ret == 0)
        __wt_writeunlock(session, &group->lock);

/* Cleanup on error to avoid leaking resources */
err:
    if (ret != 0) {
        if (cond_alloced)
            __wt_cond_destroy(session, &group->wait_cond);
        __wt_rwlock_destroy(session, &group->lock);
    }
    return (ret);
}

/*
 * __wt_thread_group_destroy --
 *     Shut down a thread group. Our caller must hold the lock.
 */
int
__wt_thread_group_destroy(WT_SESSION_IMPL *session, WT_THREAD_GROUP *group)
{
    WT_DECL_RET;

    __wt_verbose(session, WT_VERB_THREAD_GROUP, "Destroying thread group: %s", group->name);

    WT_ASSERT(session, __wt_rwlock_islocked(session, &group->lock));

    /* Shut down all threads and free associated resources. */
    WT_TRET(__thread_group_shrink(session, group, 0));

    __wt_free(session, group->threads);

    __wt_cond_destroy(session, &group->wait_cond);
    __wt_rwlock_destroy(session, &group->lock);

    /*
     * Clear out any settings from the group, some structures are reused for different thread groups
     * - in particular the eviction thread group for recovery and then normal runtime.
     */
    memset(group, 0, sizeof(*group));

    return (ret);
}

/*
 * __wt_thread_group_start_one --
 *     Start a new thread if possible.
 */
void
__wt_thread_group_start_one(WT_SESSION_IMPL *session, WT_THREAD_GROUP *group, bool is_locked)
{
    WT_THREAD *thread;

    if (group->current_threads >= group->max)
        return;

    if (!is_locked)
        __wt_writelock(session, &group->lock);

    /* Recheck the bounds now that we hold the lock */
    if (group->current_threads < group->max) {
        thread = group->threads[group->current_threads++];
        WT_ASSERT(session, thread != NULL);
        __wt_verbose(session, WT_VERB_THREAD_GROUP, "Activating utility thread: %s:%" PRIu32,
          group->name, thread->id);
        WT_ASSERT(session, !F_ISSET(thread, WT_THREAD_ACTIVE));
        F_SET(thread, WT_THREAD_ACTIVE);
        __wt_cond_signal(session, thread->pause_cond);
    }
    if (!is_locked)
        __wt_writeunlock(session, &group->lock);
}

/*
 * __wt_thread_group_stop_one --
 *     Pause one thread if possible.
 */
void
__wt_thread_group_stop_one(WT_SESSION_IMPL *session, WT_THREAD_GROUP *group)
{
    WT_THREAD *thread;

    if (group->current_threads <= group->min)
        return;

    __wt_writelock(session, &group->lock);
    /* Recheck the bounds now that we hold the lock */
    if (group->current_threads > group->min) {
        thread = group->threads[--group->current_threads];
        __wt_verbose(session, WT_VERB_THREAD_GROUP, "Pausing utility thread: %s:%" PRIu32,
          group->name, thread->id);
        WT_ASSERT(session, F_ISSET(thread, WT_THREAD_ACTIVE));
        F_CLR(thread, WT_THREAD_ACTIVE);
        __wt_cond_signal(session, thread->pause_cond);
    }
    __wt_writeunlock(session, &group->lock);
}