summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/csuite/wt4333_handle_locks/main.c
blob: 9a263eb9ef6117b69f7ffb46a367e73ea966aa87 (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
/*-
 * Public Domain 2014-2020 MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
#include "test_util.h"

#include <signal.h>

#define MAXKEY 10000
#define PERIOD 60

static WT_CONNECTION *conn;
static uint64_t worker, worker_busy, verify, verify_busy;
static u_int workers, uris;
static bool done = false;
static bool verbose = false;
static char *uri_list[750];

static void
uri_init(void)
{
    WT_CURSOR *cursor;
    WT_SESSION *session;
    u_int i, key;
    char buf[128];

    for (i = 0; i < uris; ++i)
        if (uri_list[i] == NULL) {
            testutil_check(__wt_snprintf(buf, sizeof(buf), "table:%u", i));
            uri_list[i] = dstrdup(buf);
        }

    testutil_check(conn->open_session(conn, NULL, NULL, &session));

    /* Initialize the file contents. */
    for (i = 0; i < uris; ++i) {
        testutil_check(__wt_snprintf(buf, sizeof(buf),
          "key_format=S,value_format=S,"
          "allocation_size=4K,leaf_page_max=32KB,"));
        testutil_check(session->create(session, uri_list[i], buf));
        testutil_check(session->open_cursor(session, uri_list[i], NULL, NULL, &cursor));
        for (key = 1; key < MAXKEY; ++key) {
            testutil_check(__wt_snprintf(buf, sizeof(buf), "key:%020u", key));
            cursor->set_key(cursor, buf);
            cursor->set_value(cursor, buf);
            testutil_check(cursor->insert(cursor));
        }
        testutil_check(cursor->close(cursor));
    }

    /* Create a checkpoint we can use for readonly handles. */
    testutil_check(session->checkpoint(session, NULL));

    testutil_check(session->close(session, NULL));
}

static void
uri_teardown(void)
{
    u_int i;

    for (i = 0; i < WT_ELEMENTS(uri_list); ++i)
        free(uri_list[i]);
}

static void
op(WT_SESSION *session, WT_RAND_STATE *rnd, WT_CURSOR **cpp)
{
    WT_CURSOR *cursor;
    WT_DECL_RET;
    u_int i, key;
    char buf[128];
    bool readonly;

    /* Close any open cursor in the slot we're about to reuse. */
    if (*cpp != NULL) {
        testutil_check((*cpp)->close(*cpp));
        *cpp = NULL;
    }

    cursor = NULL;
    readonly = __wt_random(rnd) % 2 == 0;

    /* Loop to open an object handle. */
    for (i = __wt_random(rnd) % uris; !done; __wt_yield()) {
        /*
         * Use a checkpoint handle for 50% of reads.
         *
         * FIXME-WT-5927: Checkpoint cursors are known to have issues in durable history so we've
         * removing the use of checkpoint handles in this test. As part of WT-5927, we should either
         * re-enable the testing of checkpoint cursors or remove this comment.
         */
        ret = session->open_cursor(session, uri_list[i], NULL, NULL, &cursor);
        if (ret != EBUSY) {
            testutil_check(ret);
            break;
        }
        (void)__wt_atomic_add64(&worker_busy, 1);
    }
    if (cursor == NULL)
        return;

    /* Operate on some number of key/value pairs. */
    for (key = 1; !done && key < MAXKEY; key += __wt_random(rnd) % 37, __wt_yield()) {
        testutil_check(__wt_snprintf(buf, sizeof(buf), "key:%020u", key));
        cursor->set_key(cursor, buf);
        if (readonly)
            testutil_check(cursor->search(cursor));
        else {
            cursor->set_value(cursor, buf);
            testutil_check(cursor->insert(cursor));
        }
    }

    /* Close the cursor half the time, otherwise cache it. */
    if (__wt_random(rnd) % 2 == 0)
        testutil_check(cursor->close(cursor));
    else {
        testutil_check(cursor->reset(cursor));
        *cpp = cursor;
    }

    (void)__wt_atomic_add64(&worker, 1);
}

static void *
wthread(void *arg)
{
    WT_CURSOR *cursor_list[10];
    WT_RAND_STATE rnd;
    WT_SESSION *session;
    u_int next;

    (void)arg;

    memset(cursor_list, 0, sizeof(cursor_list));

    testutil_check(conn->open_session(conn, NULL, NULL, &session));
    __wt_random_init_seed((WT_SESSION_IMPL *)session, &rnd);

    for (next = 0; !done;) {
        if (++next == WT_ELEMENTS(cursor_list))
            next = 0;
        op(session, &rnd, &cursor_list[next]);
    }

    return (NULL);
}

static void *
vthread(void *arg)
{
    WT_CURSOR *cursor_list[10];
    WT_DECL_RET;
    WT_RAND_STATE rnd;
    WT_SESSION *session;
    u_int i, next;

    (void)arg;

    memset(cursor_list, 0, sizeof(cursor_list));

    testutil_check(conn->open_session(conn, NULL, NULL, &session));
    __wt_random_init_seed((WT_SESSION_IMPL *)session, &rnd);

    for (next = 0; !done;) {
        if (++next == WT_ELEMENTS(cursor_list))
            next = 0;
        op(session, &rnd, &cursor_list[next]);

        while (!done) {
            i = __wt_random(&rnd) % uris;
            ret = session->verify(session, uri_list[i], NULL);
            if (ret == EBUSY) {
                (void)__wt_atomic_add64(&verify_busy, 1);
                continue;
            }

            testutil_check(ret);
            (void)__wt_atomic_add64(&verify, 1);
            break;
        }
    }

    return (NULL);
}

static void
on_alarm(int signo)
{
    (void)signo; /* Unused parameter */

    done = true;
}

static void
sweep_stats(void)
{
    static const int list[] = {WT_STAT_CONN_CURSOR_SWEEP_BUCKETS, WT_STAT_CONN_CURSOR_SWEEP_CLOSED,
      WT_STAT_CONN_CURSOR_SWEEP_EXAMINED, WT_STAT_CONN_CURSOR_SWEEP, WT_STAT_CONN_DH_SWEEP_REF,
      WT_STAT_CONN_DH_SWEEP_CLOSE, WT_STAT_CONN_DH_SWEEP_REMOVE, WT_STAT_CONN_DH_SWEEP_TOD,
      WT_STAT_CONN_DH_SWEEPS, WT_STAT_CONN_DH_SESSION_SWEEPS, -1};
    WT_SESSION *session;
    WT_CURSOR *cursor;
    uint64_t value;
    int i;
    const char *desc, *pvalue;

    testutil_check(conn->open_session(conn, NULL, NULL, &session));
    testutil_check(session->open_cursor(session, "statistics:", NULL, NULL, &cursor));
    for (i = 0;; ++i) {
        if (list[i] == -1)
            break;
        cursor->set_key(cursor, list[i]);
        testutil_check(cursor->search(cursor));
        testutil_check(cursor->get_value(cursor, &desc, &pvalue, &value));
        printf(
          "\t"
          "%s=%s\n",
          desc, pvalue);
    }
}

static void
runone(bool config_cache)
{
    pthread_t idlist[1000];
    u_int i, j;
    char buf[256], home[256];

    done = false;

    testutil_work_dir_from_path(home, sizeof(home), "WT_TEST.wt4333_handle_locks");
    testutil_make_work_dir(home);

    testutil_check(__wt_snprintf(buf, sizeof(buf),
      "create"
      ", cache_cursors=%s"
      ", cache_size=5GB"
      ", checkpoint_sync=true"
      ", eviction=(threads_max=5)"
      ", file_manager=("
      "close_handle_minimum=1,close_idle_time=1,close_scan_interval=1)"
      ", mmap=true"
      ", session_max=%u"
      ", statistics=(all)",
      config_cache ? "true" : "false", workers + 100));
    testutil_check(wiredtiger_open(home, NULL, buf, &conn));

    printf("%s: %d seconds, cache_cursors=%s, %u workers, %u files\n", progname, PERIOD,
      config_cache ? "true" : "false", workers, uris);

    uri_init();

    /* 75% readers, 25% writers. */
    for (i = 0; i < workers; ++i)
        testutil_check(pthread_create(&idlist[i], NULL, wthread, NULL));
    testutil_check(pthread_create(&idlist[i], NULL, vthread, NULL));
    ++i;

    (void)alarm(PERIOD);

    for (j = 0; j < i; ++j)
        testutil_check(pthread_join(idlist[j], NULL));

    printf(
      "\t"
      "worker %" PRIu64 ", worker_busy %" PRIu64 ", verify %" PRIu64 ", verify_busy %" PRIu64 "\n",
      worker, worker_busy, verify, verify_busy);

    if (verbose)
        sweep_stats();

    testutil_check(conn->close(conn, NULL));
}

static int
run(int argc, char *argv[])
{
    static const struct {
        u_int workers;
        u_int uris;
        bool cache_cursors;
    } runs[] = {
      {1, 1, false}, {1, 1, true}, {8, 1, false}, {8, 1, true}, {16, 1, false}, {16, 1, true},
      {16, WT_ELEMENTS(uri_list), false}, {16, WT_ELEMENTS(uri_list), true}, {200, 100, false},
      {200, 100, true}, {200, WT_ELEMENTS(uri_list), false}, {200, WT_ELEMENTS(uri_list), true},
      {300, 100, false}, {300, 100, true}, {600, WT_ELEMENTS(uri_list), false},
      {600, WT_ELEMENTS(uri_list), true},
    };
    WT_RAND_STATE rnd;
    u_int i, n;
    int ch;

    (void)testutil_set_progname(argv);
    __wt_random_init_seed(NULL, &rnd);

    while ((ch = __wt_getopt(argv[0], argc, argv, "v")) != EOF) {
        switch (ch) {
        case 'v':
            verbose = true;
            break;
        default:
            fprintf(stderr, "usage: %s [-v]\n", argv[0]);
            return (EXIT_FAILURE);
        }
    }

    (void)signal(SIGALRM, on_alarm);

    /* Each test in the table runs for a minute, run 5 tests at random. */
    for (i = 0; i < 5; ++i) {
        n = __wt_random(&rnd) % WT_ELEMENTS(runs);
        workers = runs[n].workers;
        uris = runs[n].uris;
        runone(runs[n].cache_cursors);
    }

    uri_teardown();

    return (EXIT_SUCCESS);
}

int
main(int argc, char *argv[])
{
    bool skip;

    skip = false;

    /*
     * Bypass this test for valgrind. It has a fairly low thread limit.
     */
    if (testutil_is_flag_set("TESTUTIL_BYPASS_VALGRIND"))
        skip = true;

/*
 * Bypass this test for OS X. We periodically see it hang without error, leaving a zombie process
 * that never exits (WT-4613, BUILD-7616).
 */
#if defined(__APPLE__)
    skip = true;
#endif

    return (skip ? EXIT_SUCCESS : run(argc, argv));
}