xref: /unit/src/test/nxt_mp_test.c (revision 384:8f86d3ff3e29)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 #include "nxt_tests.h"
9 
10 
11 nxt_int_t
nxt_mp_test(nxt_thread_t * thr,nxt_uint_t runs,nxt_uint_t nblocks,size_t max_size)12 nxt_mp_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks,
13     size_t max_size)
14 {
15     void          **blocks;
16     size_t        total;
17     uint32_t      value, size;
18     nxt_mp_t      *mp;
19     nxt_bool_t    valid;
20     nxt_uint_t    i, n;
21 
22     const size_t  min_chunk_size = 16;
23     const size_t  page_size = 128;
24     const size_t  page_alignment = 128;
25     const size_t  cluster_size = page_size * 8;
26 
27     nxt_thread_time_update(thr);
28     nxt_log_error(NXT_LOG_NOTICE, thr->log,
29                   "mem pool test started, max:%uz", max_size);
30 
31     blocks = nxt_malloc(nblocks * sizeof(void *));
32     if (blocks == NULL) {
33         return NXT_ERROR;
34     }
35 
36     valid = nxt_mp_test_sizes(cluster_size, page_alignment, page_size,
37                               min_chunk_size);
38     if (!valid) {
39         return NXT_ERROR;
40     }
41 
42     mp = nxt_mp_create(cluster_size, page_alignment, page_size, min_chunk_size);
43     if (mp == NULL) {
44         return NXT_ERROR;
45     }
46 
47     value = 0;
48 
49     for (i = 0; i < runs; i++) {
50 
51         total = 0;
52 
53         for (n = 0; n < nblocks; n++) {
54             value = nxt_murmur_hash2(&value, sizeof(uint32_t));
55 
56             size = value & max_size;
57 
58             if (size == 0) {
59                 size++;
60             }
61 
62             total += size;
63             blocks[n] = nxt_mp_alloc(mp, size);
64 
65             if (blocks[n] == NULL) {
66                 nxt_log_error(NXT_LOG_NOTICE, thr->log,
67                               "mem pool test failed: %uz", total);
68                 return NXT_ERROR;
69             }
70         }
71 
72         for (n = 0; n < nblocks; n++) {
73             nxt_mp_free(mp, blocks[n]);
74         }
75     }
76 
77     if (!nxt_mp_is_empty(mp)) {
78         nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem pool is not empty");
79         return NXT_ERROR;
80     }
81 
82     nxt_mp_destroy(mp);
83 
84     nxt_free(blocks);
85 
86     nxt_thread_time_update(thr);
87     nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem pool test passed");
88 
89     return NXT_OK;
90 }
91