xref: /unit/src/test/nxt_mem_zone_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_mem_zone_test(nxt_thread_t * thr,nxt_uint_t runs,nxt_uint_t nblocks,size_t max_size)12 nxt_mem_zone_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks,
13     size_t max_size)
14 {
15     void            *start, **blocks;
16     size_t          total, zone_size;
17     uint32_t        size;
18     nxt_uint_t      i, n;
19     nxt_mem_zone_t  *zone;
20     const size_t    page_size = 4096;
21 
22     nxt_thread_time_update(thr);
23     nxt_log_error(NXT_LOG_NOTICE, thr->log,
24                   "mem zone test started, max:%uz", max_size);
25 
26     zone_size = (max_size + 1) * nblocks;
27 
28     start = nxt_memalign(page_size, zone_size);
29     if (start == NULL) {
30         return NXT_ERROR;
31     }
32 
33     zone = nxt_mem_zone_init(start, zone_size, page_size);
34     if (zone == NULL) {
35         return NXT_ERROR;
36     }
37 
38     blocks = nxt_malloc(nblocks * sizeof(void *));
39     if (blocks == NULL) {
40         return NXT_ERROR;
41     }
42 
43     size = 0;
44 
45     for (i = 0; i < runs; i++) {
46 
47         total = 0;
48 
49         for (n = 0; n < nblocks; n++) {
50             size = nxt_murmur_hash2(&size, sizeof(uint32_t));
51 
52             total += size & max_size;
53             blocks[n] = nxt_mem_zone_alloc(zone, size & max_size);
54 
55             if (blocks[n] == NULL) {
56                 nxt_log_error(NXT_LOG_NOTICE, thr->log,
57                               "mem zone test failed: %uz", total);
58                 return NXT_ERROR;
59             }
60         }
61 
62         for (n = 0; n < nblocks; n++) {
63             nxt_mem_zone_free(zone, blocks[n]);
64         }
65     }
66 
67     nxt_free(blocks);
68     nxt_free(zone);
69 
70     nxt_thread_time_update(thr);
71     nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem zone test passed");
72 
73     return NXT_OK;
74 }
75