glusterfs

Форк
0
/
mem_pool_unittest.c 
489 строк · 12.9 Кб
1
/*
2
  Copyright (c) 2014 Red Hat, Inc. <http://www.redhat.com>
3
  This file is part of GlusterFS.
4

5
  This file is licensed to you under your choice of the GNU Lesser
6
  General Public License, version 3 or any later version (LGPLv3 or
7
  later), or the GNU General Public License, version 2 (GPLv2), in all
8
  cases as published by the Free Software Foundation.
9
*/
10

11
#include "glusterfs/mem-pool.h"
12
#include "glusterfs/logging.h"
13
#include "glusterfs/xlator.h"
14

15
#include <stdarg.h>
16
#include <stddef.h>
17
#include <setjmp.h>
18
#include <inttypes.h>
19
#include <string.h>
20
#include <cmocka_pbc.h>
21
#include <cmocka.h>
22

23
#ifndef assert_ptr_equal
24
#define assert_ptr_equal(a, b)                                                 \
25
    _assert_int_equal(cast_ptr_to_largest_integral_type(a),                    \
26
                      cast_ptr_to_largest_integral_type(b), __FILE__,          \
27
                      __LINE__)
28
#endif
29

30
/*
31
 * memory header for gf_mem_set_acct_info
32
 */
33
typedef struct {
34
    uint32_t type;
35
    size_t size;
36
    xlator_t *xl;
37
    gf_mem_magic_t header_magic;
38
    void *data[];
39
} mem_header_t;
40

41
/*
42
 * Prototypes to private functions
43
 */
44
int
45
gf_mem_set_acct_info(xlator_t *xl, char **alloc_ptr, size_t size, uint32_t type,
46
                     const char *typestr);
47
size_t
48
__gf_total_alloc_size(size_t req_size);
49
void
50
__gf_mem_trailer_write(uint8_t *trailer);
51
gf_mem_magic_t
52
__gf_mem_trailer_read(uint8_t *trailer);
53

54
/*
55
 * Helper functions
56
 */
57
static xlator_t *
58
helper_xlator_init(uint32_t num_types)
59
{
60
    xlator_t *xl;
61
    int i, ret;
62

63
    REQUIRE(num_types > 0);
64

65
    xl = test_calloc(1, sizeof(xlator_t));
66
    assert_non_null(xl);
67
    xl->mem_acct->num_types = num_types;
68
    xl->mem_acct = test_calloc(sizeof(struct mem_acct) +
69
                               sizeof(struct mem_acct_rec) * num_types);
70
    assert_non_null(xl->mem_acct);
71

72
    xl->ctx = test_calloc(1, sizeof(glusterfs_ctx_t));
73
    assert_non_null(xl->ctx);
74

75
    for (i = 0; i < num_types; i++) {
76
        ret = LOCK_INIT(&(xl->mem_acct->rec[i].lock));
77
        assert_int_equal(ret, 0);
78
    }
79

80
    ENSURE(num_types == xl->mem_acct->num_types);
81
    ENSURE(NULL != xl);
82

83
    return xl;
84
}
85

86
static int
87
helper_xlator_destroy(xlator_t *xl)
88
{
89
    int i, ret;
90

91
    for (i = 0; i < xl->mem_acct->num_types; i++) {
92
        ret = LOCK_DESTROY(&(xl->mem_acct->rec[i].lock));
93
        assert_int_equal(ret, 0);
94
    }
95

96
    free(xl->mem_acct->rec);
97
    free(xl->ctx);
98
    free(xl);
99
    return 0;
100
}
101

102
static void
103
helper_check_memory_headers(char *mem, xlator_t *xl, size_t size, uint32_t type)
104
{
105
    mem_header_t *p;
106

107
    p = (mem_header_t *)mem, assert_int_equal(p->type, type);
108
    assert_int_equal(p->size, size);
109
    assert_true(p->xl == xl);
110
    assert_int_equal(p->header_magic, GF_MEM_HEADER_MAGIC);
111
    assert_true(__gf_mem_trailer_read((uint8_t *)mem + sizeof(mem_header_t) +
112
                                      size) == GF_MEM_TRAILER_MAGIC);
113
}
114

115
/*
116
 * Tests
117
 */
118
static void
119
test_gf_mem_acct_enable_set(void **state)
120
{
121
    (void)state;
122
    glusterfs_ctx_t test_ctx;
123

124
    expect_assert_failure(gf_mem_acct_enable_set(NULL));
125

126
    memset(&test_ctx, 0, sizeof(test_ctx));
127
    assert_true(NULL == test_ctx.process_uuid);
128
    gf_mem_acct_enable_set((void *)&test_ctx);
129
    assert_true(1 == test_ctx.mem_acct_enable);
130
    assert_true(NULL == test_ctx.process_uuid);
131
}
132

133
static void
134
test_gf_mem_set_acct_info_asserts(void **state)
135
{
136
    xlator_t *xl;
137
    xlator_t xltest;
138
    char *alloc_ptr;
139
    size_t size;
140
    uint32_t type;
141

142
    memset(&xltest, 0, sizeof(xlator_t));
143
    xl = (xlator_t *)0xBADD;
144
    alloc_ptr = (char *)0xBADD;
145
    size = 8196;
146
    type = 0;
147

148
    // Check xl is NULL
149
    expect_assert_failure(
150
        gf_mem_set_acct_info(NULL, &alloc_ptr, size, type, ""));
151
    // Check xl->mem_acct = NULL
152
    expect_assert_failure(
153
        gf_mem_set_acct_info(&xltest, &alloc_ptr, 0, type, ""));
154
    // Check type <= xl->mem_acct->num_types
155
    type = 100;
156
    expect_assert_failure(
157
        gf_mem_set_acct_info(&xltest, &alloc_ptr, 0, type, ""));
158
    // Check alloc is NULL
159
    assert_int_equal(-1, gf_mem_set_acct_info(&xltest, NULL, size, type, ""));
160

161
    // Initialize xl
162
    xl = helper_xlator_init(10);
163

164
    // Test number of types
165
    type = 100;
166
    assert_true(NULL != xl->mem_acct);
167
    assert_true(type > xl->mem_acct->num_types);
168
    expect_assert_failure(gf_mem_set_acct_info(xl, &alloc_ptr, size, type, ""));
169

170
    helper_xlator_destroy(xl);
171
}
172

173
static void
174
test_gf_mem_set_acct_info_memory(void **state)
175
{
176
    xlator_t *xl;
177
    char *alloc_ptr;
178
    char *temp_ptr;
179
    size_t size;
180
    uint32_t type;
181
    const char *typestr = "TEST";
182

183
    size = 8196;
184
    type = 9;
185

186
    // Initialize xl
187
    xl = helper_xlator_init(10);
188
    assert_null(xl->mem_acct->rec[type].typestr);
189

190
    // Test allocation
191
    temp_ptr = test_calloc(1, size + GF_MEM_HEADER_SIZE + GF_MEM_TRAILER_SIZE);
192
    assert_non_null(temp_ptr);
193
    alloc_ptr = temp_ptr;
194
    gf_mem_set_acct_info(xl, &alloc_ptr, size, type, typestr);
195

196
    // Check values
197
    assert_ptr_equal(typestr, xl->mem_acct->rec[type].typestr);
198
    assert_int_equal(xl->mem_acct->rec[type].size, size);
199
    assert_int_equal(xl->mem_acct->rec[type].num_allocs, 1);
200
    assert_int_equal(xl->mem_acct->rec[type].total_allocs, 1);
201
    assert_int_equal(xl->mem_acct->rec[type].max_size, size);
202
    assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 1);
203

204
    // Check memory
205
    helper_check_memory_headers(temp_ptr, xl, size, type);
206

207
    // Check that alloc_ptr has been moved correctly
208
    // by gf_mem_set_acct_info
209
    {
210
        mem_header_t *p;
211

212
        p = (mem_header_t *)temp_ptr;
213
        p++;
214
        p->type = 1234;
215
        assert_int_equal(*(uint32_t *)alloc_ptr, p->type);
216
    }
217

218
    free(temp_ptr);
219
    helper_xlator_destroy(xl);
220
}
221

222
static void
223
test_gf_calloc_default_calloc(void **state)
224
{
225
    xlator_t *xl;
226
    void *mem;
227
    size_t size;
228
    uint32_t type;
229

230
    // Initialize xl
231
    xl = helper_xlator_init(10);
232
    assert_int_equal(xl->ctx->mem_acct_enable, 0);
233
    will_return(__glusterfs_this_location, &xl);
234

235
    // Call __gf_calloc
236
    size = 1024;
237
    type = 3;
238
    mem = __gf_calloc(1, size, type, "3");
239
    assert_non_null(mem);
240
    memset(mem, 0x5A, size);
241

242
    // Check xl did not change
243
    assert_int_equal(xl->mem_acct->rec[type].size, 0);
244
    assert_int_equal(xl->mem_acct->rec[type].num_allocs, 0);
245
    assert_int_equal(xl->mem_acct->rec[type].total_allocs, 0);
246
    assert_int_equal(xl->mem_acct->rec[type].max_size, 0);
247
    assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 0);
248

249
    free(mem);
250
    helper_xlator_destroy(xl);
251
}
252

253
static void
254
test_gf_calloc_mem_acct_enabled(void **state)
255
{
256
    xlator_t *xl;
257
    void *mem;
258
    size_t size;
259
    uint32_t type;
260

261
    // Initialize xl
262
    xl = helper_xlator_init(10);
263
    assert_int_equal(xl->ctx->mem_acct_enable, 0);
264
    xl->ctx->mem_acct_enable = 1;
265

266
    // For line mem-pool.c:115 and mem-pool:118
267
    will_return_always(__glusterfs_this_location, &xl);
268

269
    // Call __gf_calloc
270
    size = 1024;
271
    type = 3;
272
    mem = __gf_calloc(1, size, type, "3");
273
    assert_non_null(mem);
274
    memset(mem, 0x5A, size);
275

276
    // Check xl values
277
    assert_int_equal(xl->mem_acct->rec[type].size, size);
278
    assert_int_equal(xl->mem_acct->rec[type].num_allocs, 1);
279
    assert_int_equal(xl->mem_acct->rec[type].total_allocs, 1);
280
    assert_int_equal(xl->mem_acct->rec[type].max_size, size);
281
    assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 1);
282

283
    // Check memory
284
    helper_check_memory_headers(mem - sizeof(mem_header_t), xl, size, type);
285
    free(mem - sizeof(mem_header_t));
286
    helper_xlator_destroy(xl);
287
}
288

289
static void
290
test_gf_malloc_default_malloc(void **state)
291
{
292
    xlator_t *xl;
293
    void *mem;
294
    size_t size;
295
    uint32_t type;
296

297
    // Initialize xl
298
    xl = helper_xlator_init(10);
299
    assert_int_equal(xl->ctx->mem_acct_enable, 0);
300
    will_return(__glusterfs_this_location, &xl);
301

302
    // Call __gf_malloc
303
    size = 1024;
304
    type = 3;
305
    mem = __gf_malloc(size, type, "3");
306
    assert_non_null(mem);
307
    memset(mem, 0x5A, size);
308

309
    // Check xl did not change
310
    assert_int_equal(xl->mem_acct->rec[type].size, 0);
311
    assert_int_equal(xl->mem_acct->rec[type].num_allocs, 0);
312
    assert_int_equal(xl->mem_acct->rec[type].total_allocs, 0);
313
    assert_int_equal(xl->mem_acct->rec[type].max_size, 0);
314
    assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 0);
315

316
    free(mem);
317
    helper_xlator_destroy(xl);
318
}
319

320
static void
321
test_gf_malloc_mem_acct_enabled(void **state)
322
{
323
    xlator_t *xl;
324
    void *mem;
325
    size_t size;
326
    uint32_t type;
327

328
    // Initialize xl
329
    xl = helper_xlator_init(10);
330
    assert_int_equal(xl->ctx->mem_acct_enable, 0);
331
    xl->ctx->mem_acct_enable = 1;
332

333
    // For line mem-pool.c:115 and mem-pool:118
334
    will_return_always(__glusterfs_this_location, &xl);
335

336
    // Call __gf_malloc
337
    size = 1024;
338
    type = 3;
339
    mem = __gf_malloc(size, type, "3");
340
    assert_non_null(mem);
341
    memset(mem, 0x5A, size);
342

343
    // Check xl values
344
    assert_int_equal(xl->mem_acct->rec[type].size, size);
345
    assert_int_equal(xl->mem_acct->rec[type].num_allocs, 1);
346
    assert_int_equal(xl->mem_acct->rec[type].total_allocs, 1);
347
    assert_int_equal(xl->mem_acct->rec[type].max_size, size);
348
    assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 1);
349

350
    // Check memory
351
    helper_check_memory_headers(mem - sizeof(mem_header_t), xl, size, type);
352
    free(mem - sizeof(mem_header_t));
353
    helper_xlator_destroy(xl);
354
}
355

356
static void
357
test_gf_realloc_default_realloc(void **state)
358
{
359
    xlator_t *xl;
360
    void *mem;
361
    size_t size;
362
    uint32_t type;
363

364
    // Initialize xl
365
    xl = helper_xlator_init(10);
366
    assert_int_equal(xl->ctx->mem_acct_enable, 0);
367
    will_return_always(__glusterfs_this_location, &xl);
368

369
    // Call __gf_malloc then realloc
370
    size = 10;
371
    type = 3;
372
    mem = __gf_malloc(size, type, "3");
373
    assert_non_null(mem);
374
    memset(mem, 0xA5, size);
375

376
    size = 1024;
377
    mem = __gf_realloc(mem, size);
378
    assert_non_null(mem);
379
    memset(mem, 0x5A, size);
380

381
    // Check xl did not change
382
    assert_int_equal(xl->mem_acct->rec[type].size, 0);
383
    assert_int_equal(xl->mem_acct->rec[type].num_allocs, 0);
384
    assert_int_equal(xl->mem_acct->rec[type].total_allocs, 0);
385
    assert_int_equal(xl->mem_acct->rec[type].max_size, 0);
386
    assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 0);
387

388
    free(mem);
389
    helper_xlator_destroy(xl);
390
}
391

392
static void
393
test_gf_realloc_mem_acct_enabled(void **state)
394
{
395
    xlator_t *xl;
396
    void *mem;
397
    size_t size;
398
    uint32_t type;
399

400
    // Initialize xl
401
    xl = helper_xlator_init(10);
402
    assert_int_equal(xl->ctx->mem_acct_enable, 0);
403
    xl->ctx->mem_acct_enable = 1;
404

405
    // For line mem-pool.c:115 and mem-pool:118
406
    will_return_always(__glusterfs_this_location, &xl);
407

408
    // Call __gf_malloc then realloc
409
    size = 1024;
410
    type = 3;
411
    mem = __gf_malloc(size, type, "3");
412
    assert_non_null(mem);
413
    memset(mem, 0xA5, size);
414

415
    size = 2048;
416
    mem = __gf_realloc(mem, size);
417
    assert_non_null(mem);
418
    memset(mem, 0x5A, size);
419

420
    // Check xl values
421
    //
422
    // :TODO: This is really weird.  I would have expected
423
    // xl to only have a size equal to that of the realloc
424
    // not to the realloc + the malloc.
425
    // Is this a bug?
426
    //
427
    assert_int_equal(xl->mem_acct->rec[type].size, size + 1024);
428
    assert_int_equal(xl->mem_acct->rec[type].num_allocs, 2);
429
    assert_int_equal(xl->mem_acct->rec[type].total_allocs, 2);
430
    assert_int_equal(xl->mem_acct->rec[type].max_size, size + 1024);
431
    assert_int_equal(xl->mem_acct->rec[type].max_num_allocs, 2);
432

433
    // Check memory
434
    helper_check_memory_headers(mem - sizeof(mem_header_t), xl, size, type);
435
    free(mem - sizeof(mem_header_t));
436
    helper_xlator_destroy(xl);
437
}
438

439
static void
440
test_gf_realloc_ptr(void **state)
441
{
442
    xlator_t *xl;
443
    void *mem;
444
    size_t size;
445

446
    // Initialize xl
447
    xl = helper_xlator_init(10);
448
    assert_int_equal(xl->ctx->mem_acct_enable, 0);
449

450
    // For line mem-pool.c:115 and mem-pool:118
451
    will_return_always(__glusterfs_this_location, &xl);
452

453
    // Tests according to the manpage for realloc
454

455
    // Like a malloc
456
    size = 1024;
457
    mem = __gf_realloc(NULL, size);
458
    assert_non_null(mem);
459
    memset(mem, 0xA5, size);
460

461
    // Like a free
462
    mem = __gf_realloc(mem, 0);
463
    assert_null(mem);
464

465
    // Now enable xl context
466
    xl->ctx->mem_acct_enable = 1;
467
    expect_assert_failure(__gf_realloc(NULL, size));
468

469
    helper_xlator_destroy(xl);
470
}
471

472
int
473
main(void)
474
{
475
    const struct CMUnitTest libglusterfs_mem_pool_tests[] = {
476
        cmocka_unit_test(test_gf_mem_acct_enable_set),
477
        cmocka_unit_test(test_gf_mem_set_acct_info_asserts),
478
        cmocka_unit_test(test_gf_mem_set_acct_info_memory),
479
        cmocka_unit_test(test_gf_calloc_default_calloc),
480
        cmocka_unit_test(test_gf_calloc_mem_acct_enabled),
481
        cmocka_unit_test(test_gf_malloc_default_malloc),
482
        cmocka_unit_test(test_gf_malloc_mem_acct_enabled),
483
        cmocka_unit_test(test_gf_realloc_default_realloc),
484
        cmocka_unit_test(test_gf_realloc_mem_acct_enabled),
485
        cmocka_unit_test(test_gf_realloc_ptr),
486
    };
487

488
    return cmocka_run_group_tests(libglusterfs_mem_pool_tests, NULL, NULL);
489
}
490

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.