File: test_mpool.cc

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (497 lines) | stat: -rw-r--r-- 14,644 bytes parent folder | download | duplicates (6)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/**
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2014. ALL RIGHTS RESERVED.
*
* See file LICENSE for terms.
*/

#include <common/test.h>
extern "C" {
#include <ucs/datastruct/mpool.h>
}

#include <limits.h>
#include <vector>
#include <queue>

class test_mpool : public ucs::test {
protected:
    static ucs_status_t test_alloc(ucs_mpool_t *mp, size_t *size_p, void **chunk_p) {
        *chunk_p = malloc(*size_p);
        return (*chunk_p == NULL) ? UCS_ERR_NO_MEMORY : UCS_OK;
    }

    static void test_free(ucs_mpool_t *mp, void *chunk) {
        free(chunk);
    }

    static void obj_str(ucs_mpool_t *mp, void *obj, ucs_string_buffer_t *strb)
    {
        ucs_string_buffer_appendf(strb, "test-obj-%p", obj);
    }

    static ucs_log_func_rc_t
    mpool_log_handler(const char *file, unsigned line, const char *function,
                      ucs_log_level_t level,
                      const ucs_log_component_config_t *comp_conf,
                      const char *message, va_list ap)
    {
        // Ignore errors that invalid input parameters as it is expected
        if (level == UCS_LOG_LEVEL_ERROR) {
            std::string err_str = format_message(message, ap);
            std::string exp_str = "Invalid memory pool parameter(s)";

            if (err_str == exp_str) {
                UCS_TEST_MESSAGE << err_str;
                return UCS_LOG_FUNC_RC_STOP;
            }
        }

        return UCS_LOG_FUNC_RC_CONTINUE;
    }

    static bool is_leak_str(const std::string &str)
    {
        return (str.find("not returned to mpool test") != std::string::npos) &&
               (str.find("{test-obj-") != std::string::npos);
    }

    static ucs_log_func_rc_t
    mpool_log_leak_handler(const char *file, unsigned line,
                           const char *function, ucs_log_level_t level,
                           const ucs_log_component_config_t *comp_conf,
                           const char *message, va_list ap)
    {
        if (level == UCS_LOG_LEVEL_WARN) {
            std::string msg = format_message(message, ap);
            if (is_leak_str(msg)) {
                UCS_TEST_MESSAGE << "< " << msg << " >";
                ++leak_count;
                return UCS_LOG_FUNC_RC_STOP;
            }
        }

        return UCS_LOG_FUNC_RC_CONTINUE;
    }

    static const size_t header_size = 30;
    static const size_t data_size = 152;
    static const size_t align = 128;
    static size_t leak_count;

    ucs_status_t setup_mpool(ucs_mpool_t *mp, size_t elem_size,
                             unsigned elems_per_chunk, unsigned max_elems = 0)
    {
        static ucs_mpool_ops_t mpool_ops = {ucs_mpool_chunk_malloc,
                                            ucs_mpool_chunk_free, NULL, NULL,
                                            NULL};
        if (max_elems == 0) {
            max_elems = elems_per_chunk;
        }

        ucs_mpool_params_t mp_params;

        ucs_mpool_params_reset(&mp_params);
        mp_params.elem_size       = header_size + elem_size;
        mp_params.align_offset    = header_size;
        mp_params.alignment       = align;
        mp_params.max_chunk_size  = 4 * UCS_GBYTE;
        mp_params.elems_per_chunk = elems_per_chunk;
        mp_params.max_elems       = max_elems;
        mp_params.ops             = &mpool_ops;
        mp_params.name            = "tests";
        return ucs_mpool_init(&mp_params, mp);
    }
};

size_t test_mpool::leak_count;

UCS_TEST_F(test_mpool, no_allocs) {
    ucs_mpool_t mp;
    ucs_status_t status;
    ucs_mpool_params_t mp_params;

    ucs_mpool_ops_t ops = {
       ucs_mpool_chunk_malloc,
       ucs_mpool_chunk_free,
       NULL,
       NULL,
       NULL
    };

    ucs_mpool_params_reset(&mp_params);
    mp_params.elem_size       = header_size + data_size;
    mp_params.align_offset    = header_size;
    mp_params.alignment       = align;
    mp_params.elems_per_chunk = 6;
    mp_params.max_elems       = 18;
    mp_params.ops             = &ops;
    mp_params.name            = "tests";
    status = ucs_mpool_init(&mp_params, &mp);
    ASSERT_UCS_OK(status);
    ucs_mpool_cleanup(&mp, 1);
}

UCS_TEST_F(test_mpool, wrong_ops) {
    ucs_mpool_t mp;
    ucs_status_t status;
    ucs_mpool_ops_t ops = { 0 };
    scoped_log_handler log_handler(mpool_log_handler);
    ucs_mpool_params_t mp_params;

    ucs_mpool_params_reset(&mp_params);
    mp_params.elem_size       = header_size + data_size;
    mp_params.align_offset    = header_size;
    mp_params.alignment       = align;
    mp_params.elems_per_chunk = 6;
    mp_params.max_elems       = 18;
    mp_params.ops             = &ops;
    mp_params.name            = "tests";
    status = ucs_mpool_init(&mp_params, &mp);
    EXPECT_TRUE(status == UCS_ERR_INVALID_PARAM);
}

UCS_TEST_F(test_mpool, wrong_mpool_chuk_size) {
    ucs_mpool_t mp;
    ucs_mpool_ops_t ops = { 0 };
    scoped_log_handler log_handler(mpool_log_handler);
    ucs_mpool_params_t mp_params;

    ucs_mpool_params_reset(&mp_params);
    mp_params.elem_size       = header_size + data_size;
    mp_params.align_offset    = header_size;
    mp_params.alignment       = align;
    mp_params.elems_per_chunk = 1;
    mp_params.max_chunk_size  = mp_params.elems_per_chunk * mp_params.elem_size;
    mp_params.ops             = &ops;
    mp_params.name            = "tests";
    ucs_status_t status       = ucs_mpool_init(&mp_params, &mp);
    EXPECT_EQ(UCS_ERR_INVALID_PARAM, status);
}

UCS_TEST_F(test_mpool, basic) {
    ucs_status_t status;
    ucs_mpool_t mp;

    ucs_mpool_ops_t ops = {
       ucs_mpool_chunk_malloc,
       ucs_mpool_chunk_free,
       NULL,
       NULL,
       NULL
    };
    ucs_mpool_params_t mp_params;

    ucs_mpool_params_reset(&mp_params);
    mp_params.elem_size       = header_size + data_size;
    mp_params.align_offset    = header_size;
    mp_params.alignment       = align;
    mp_params.elems_per_chunk = 6;
    mp_params.max_elems       = 18;
    mp_params.ops             = &ops;
    mp_params.name            = "tests";
    push_config();

    for (int mpool_fifo = 0; mpool_fifo <= 1; ++mpool_fifo) {
#if ENABLE_DEBUG_DATA
        modify_config("MPOOL_FIFO", ucs::to_string(mpool_fifo).c_str());
#else
        if (mpool_fifo == 1) {
            continue;
        }
#endif
        status = ucs_mpool_init(&mp_params, &mp);
        ASSERT_UCS_OK(status);

        for (unsigned loop = 0; loop < 10; ++loop) {
            std::vector<void*> objs;
            for (unsigned i = 0; i < 18; ++i) {
                void *ptr = ucs_mpool_get(&mp);
                ASSERT_TRUE(ptr != NULL);
                ASSERT_EQ(0ul, ((uintptr_t)ptr + header_size) % align) << ptr;
                memset(ptr, 0xAA, header_size + data_size);
                objs.push_back(ptr);
            }

            ASSERT_TRUE(NULL == ucs_mpool_get(&mp));

            for (std::vector<void*>::iterator iter = objs.begin(); iter != objs.end(); ++iter) {
                ucs_mpool_put(*iter);
            }
        }

        ucs_mpool_cleanup(&mp, 1);
    }

    pop_config();
}

UCS_TEST_F(test_mpool, custom_alloc) {
    ucs_status_t status;
    ucs_mpool_t mp;

    ucs_mpool_ops_t ops = {
       test_alloc,
       test_free,
       NULL,
       NULL,
       NULL
    };
    ucs_mpool_params_t mp_params;

    ucs_mpool_params_reset(&mp_params);
    mp_params.elem_size       = header_size + data_size;
    mp_params.align_offset    = header_size;
    mp_params.alignment       = align;
    mp_params.elems_per_chunk = 5;
    mp_params.max_elems       = 18;
    mp_params.ops             = &ops;
    mp_params.name            = "tests";
    status = ucs_mpool_init(&mp_params, &mp);
    ASSERT_UCS_OK(status);

    void *obj = ucs_mpool_get(&mp);
    EXPECT_TRUE(obj != NULL);

    ucs_mpool_put(obj);

    ucs_mpool_cleanup(&mp, 1);
}

UCS_TEST_F(test_mpool, grow) {
    ucs_status_t status;
    ucs_mpool_t mp;

    ucs_mpool_ops_t ops = {
       ucs_mpool_chunk_malloc,
       ucs_mpool_chunk_free,
       NULL,
       NULL,
       NULL
    };
    ucs_mpool_params_t mp_params;

    ucs_mpool_params_reset(&mp_params);
    mp_params.elem_size       = header_size + data_size;
    mp_params.align_offset    = header_size;
    mp_params.alignment       = align;
    mp_params.elems_per_chunk = 1000;
    mp_params.max_elems       = 2000;
    mp_params.ops             = &ops;
    mp_params.name            = "tests";
    status = ucs_mpool_init(&mp_params, &mp);
    ASSERT_UCS_OK(status);

    ucs_mpool_grow(&mp, 1);

    void *obj = ucs_mpool_get(&mp);
    EXPECT_TRUE(obj != NULL);

    ucs_mpool_put(obj);

    ucs_mpool_cleanup(&mp, 1);
}

UCS_TEST_F(test_mpool, infinite) {
    const unsigned NUM_ELEMS = 1000000 / ucs::test_time_multiplier();
    ucs_status_t status;
    ucs_mpool_t mp;

    ucs_mpool_ops_t ops = {
       ucs_mpool_chunk_malloc,
       ucs_mpool_chunk_free,
       NULL,
       NULL,
       NULL
    };
    ucs_mpool_params_t mp_params;

    ucs_mpool_params_reset(&mp_params);
    mp_params.elem_size       = header_size + data_size;
    mp_params.align_offset    = header_size;
    mp_params.alignment       = align;
    mp_params.elems_per_chunk = 10000;
    mp_params.ops             = &ops;
    mp_params.name            = "tests";
    status = ucs_mpool_init(&mp_params, &mp);
    ASSERT_UCS_OK(status);

    std::queue<void*> q;
    for (unsigned i = 0; i < NUM_ELEMS; ++i) {
        void *obj = ucs_mpool_get(&mp);
        ASSERT_TRUE(obj != NULL);
        q.push(obj);
    }

    while (!q.empty()) {
        ucs_mpool_put(q.front());
        q.pop();
    }

    ucs_mpool_cleanup(&mp, 1);
}

UCS_TEST_F(test_mpool, leak_check) {
    ucs_mpool_t mp;
    ucs_status_t status;

    ucs_mpool_ops_t ops = {
        ucs_mpool_chunk_malloc,
        ucs_mpool_chunk_free,
        NULL,
        NULL,
        obj_str
    };
    ucs_mpool_params_t mp_params;

    ucs_mpool_params_reset(&mp_params);
    mp_params.elem_size       = header_size + data_size;
    mp_params.align_offset    = header_size;
    mp_params.alignment       = align;
    mp_params.elems_per_chunk = 6;
    mp_params.max_elems       = 18;
    mp_params.ops             = &ops;
    mp_params.name            = "tests";
    status = ucs_mpool_init(&mp_params, &mp);
    ASSERT_UCS_OK(status);

    for (int i = 0; i < 5; ++i) {
        void *obj = ucs_mpool_get(&mp);
        EXPECT_TRUE(obj != NULL);
    }
    // Do not release allocated objects

    leak_count = 0;
    scoped_log_handler log_handler(mpool_log_leak_handler);
    ucs_mpool_cleanup(&mp, 1);

    EXPECT_EQ(5u, leak_count);
}

class test_mpool_grow : public test_mpool {
public:
    void run_grow_test(double grow_factor,
                       std::vector<unsigned> &num_elems_per_chunk)
    {
        ucs_mpool_ops_t ops = {ucs_mpool_chunk_malloc,
                               ucs_mpool_chunk_free,
                               NULL, NULL, NULL};
        unsigned chunk_num  = num_elems_per_chunk.size();
        ucs_mpool_chunk_t *chunk;
        ucs_mpool_params_t mp_params;
        ucs_mpool_t mp;
        unsigned i;

        ucs_mpool_params_reset(&mp_params);
        mp_params.elem_size       = header_size + elem_size;
        mp_params.align_offset    = header_size;
        mp_params.alignment       = align;
        mp_params.elems_per_chunk = num_elems_first_chunk;
        mp_params.max_elems       = total_num_elems;
        mp_params.max_chunk_size  = max_chunk_size;
        mp_params.grow_factor     = grow_factor;
        mp_params.ops             = &ops;
        mp_params.name            = "tests";
        ASSERT_UCS_OK(ucs_mpool_init(&mp_params, &mp));
        for (i = 0; i < total_num_elems; ++i) {
            EXPECT_NE(ucs_mpool_get(&mp), nullptr);
        }

        chunk = mp.data->chunks;
        EXPECT_NE(chunk, nullptr);

        for (; (chunk != NULL) && (chunk_num > 0);
             chunk_num--, chunk = chunk->next) {
            EXPECT_EQ(chunk->num_elems,
                      num_elems_per_chunk[chunk_num - 1]);
        }

        EXPECT_EQ(chunk_num, 0);
        EXPECT_EQ(chunk, nullptr);
        ucs_mpool_cleanup(&mp, 0);
    }

private:
    static const size_t elem_size = 1 * UCS_MBYTE;
    static const unsigned num_elems_first_chunk = 8;
    static const unsigned total_num_elems = 25;
    static const unsigned max_chunk_size = 32 * UCS_MBYTE;
};

UCS_TEST_F(test_mpool_grow, grow_factor1) {
    std::vector<unsigned> num_elems_per_chunk = {8, 8, 8, 1};
    run_grow_test(1.0, num_elems_per_chunk);
}

UCS_TEST_F(test_mpool_grow, grow_factor2) {
    std::vector<unsigned> num_elems_per_chunk = {8, 16, 1};
    run_grow_test(2.0, num_elems_per_chunk);
}

UCS_TEST_SKIP_COND_F(test_mpool, alloc_4g, RUNNING_ON_VALGRIND) {
    const size_t elem_size         = 32 * UCS_MBYTE;
    const unsigned elems_per_chunk = ucs::limit_buffer_size(4 * UCS_GBYTE) /
                                     elem_size;

    ucs_mpool_t mp;
    ucs_status_t status = setup_mpool(&mp, elem_size, elems_per_chunk);
    ASSERT_UCS_OK(status);

    // Allocate objects per one chunk size
    std::vector<void*> objs;
    for (unsigned i = 0; i < elems_per_chunk; ++i) {
        void *obj = ucs_mpool_get(&mp);
        if (obj == NULL) {
            ADD_FAILURE() << "Could not allocate object [" << i
                          << "] from mpool";
            break;
        }
        objs.push_back(obj);
    }

    // Release allocated objects
    while (!objs.empty()) {
        ucs_mpool_put(objs.back());
        objs.pop_back();
    }

    ucs_mpool_cleanup(&mp, 1);
}

class test_mpool_fifo : public test_mpool {
public:
    void init()
    {
        modify_config("MPOOL_FIFO", ucs::to_string(1).c_str());
        test_mpool::init();
    }
};

UCS_TEST_SKIP_COND_F(test_mpool_fifo, alloc_release, !ENABLE_DEBUG_DATA)
{
    const size_t elem_size         = 16;
    const unsigned elems_per_chunk = 32;
    const unsigned max_elems       = 128;
    ucs_mpool_t mp;
    std::vector<void*> objs;

    ucs_status_t status = setup_mpool(&mp, elem_size, elems_per_chunk,
                                      max_elems);
    ASSERT_UCS_OK(status);

    for (unsigned i = 0; i < 2 * elems_per_chunk; ++i) {
        objs.push_back(ucs_mpool_get(&mp));
    }

    void *obj = ucs_mpool_get(&mp);
    EXPECT_NE(nullptr, obj);
    ucs_mpool_put(obj);

    for (auto obj : objs) {
        ASSERT_NE(nullptr, obj);
        ucs_mpool_elem_t *elem = (ucs_mpool_elem_t*)obj - 1;
        VALGRIND_MAKE_MEM_DEFINED(elem, sizeof *elem);
        ASSERT_EQ(&mp, elem->mpool);
    }

    ucs_mpool_cleanup(&mp, 0); // skip individual put as obj could be corrupted
}