xref: /unit/src/test/nxt_mem_zone_test.c (revision 384:8f86d3ff3e29)
1*384Szelenkov@nginx.com 
2*384Szelenkov@nginx.com /*
3*384Szelenkov@nginx.com  * Copyright (C) Igor Sysoev
4*384Szelenkov@nginx.com  * Copyright (C) NGINX, Inc.
5*384Szelenkov@nginx.com  */
6*384Szelenkov@nginx.com 
7*384Szelenkov@nginx.com #include <nxt_main.h>
8*384Szelenkov@nginx.com #include "nxt_tests.h"
9*384Szelenkov@nginx.com 
10*384Szelenkov@nginx.com 
11*384Szelenkov@nginx.com nxt_int_t
nxt_mem_zone_test(nxt_thread_t * thr,nxt_uint_t runs,nxt_uint_t nblocks,size_t max_size)12*384Szelenkov@nginx.com nxt_mem_zone_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks,
13*384Szelenkov@nginx.com     size_t max_size)
14*384Szelenkov@nginx.com {
15*384Szelenkov@nginx.com     void            *start, **blocks;
16*384Szelenkov@nginx.com     size_t          total, zone_size;
17*384Szelenkov@nginx.com     uint32_t        size;
18*384Szelenkov@nginx.com     nxt_uint_t      i, n;
19*384Szelenkov@nginx.com     nxt_mem_zone_t  *zone;
20*384Szelenkov@nginx.com     const size_t    page_size = 4096;
21*384Szelenkov@nginx.com 
22*384Szelenkov@nginx.com     nxt_thread_time_update(thr);
23*384Szelenkov@nginx.com     nxt_log_error(NXT_LOG_NOTICE, thr->log,
24*384Szelenkov@nginx.com                   "mem zone test started, max:%uz", max_size);
25*384Szelenkov@nginx.com 
26*384Szelenkov@nginx.com     zone_size = (max_size + 1) * nblocks;
27*384Szelenkov@nginx.com 
28*384Szelenkov@nginx.com     start = nxt_memalign(page_size, zone_size);
29*384Szelenkov@nginx.com     if (start == NULL) {
30*384Szelenkov@nginx.com         return NXT_ERROR;
31*384Szelenkov@nginx.com     }
32*384Szelenkov@nginx.com 
33*384Szelenkov@nginx.com     zone = nxt_mem_zone_init(start, zone_size, page_size);
34*384Szelenkov@nginx.com     if (zone == NULL) {
35*384Szelenkov@nginx.com         return NXT_ERROR;
36*384Szelenkov@nginx.com     }
37*384Szelenkov@nginx.com 
38*384Szelenkov@nginx.com     blocks = nxt_malloc(nblocks * sizeof(void *));
39*384Szelenkov@nginx.com     if (blocks == NULL) {
40*384Szelenkov@nginx.com         return NXT_ERROR;
41*384Szelenkov@nginx.com     }
42*384Szelenkov@nginx.com 
43*384Szelenkov@nginx.com     size = 0;
44*384Szelenkov@nginx.com 
45*384Szelenkov@nginx.com     for (i = 0; i < runs; i++) {
46*384Szelenkov@nginx.com 
47*384Szelenkov@nginx.com         total = 0;
48*384Szelenkov@nginx.com 
49*384Szelenkov@nginx.com         for (n = 0; n < nblocks; n++) {
50*384Szelenkov@nginx.com             size = nxt_murmur_hash2(&size, sizeof(uint32_t));
51*384Szelenkov@nginx.com 
52*384Szelenkov@nginx.com             total += size & max_size;
53*384Szelenkov@nginx.com             blocks[n] = nxt_mem_zone_alloc(zone, size & max_size);
54*384Szelenkov@nginx.com 
55*384Szelenkov@nginx.com             if (blocks[n] == NULL) {
56*384Szelenkov@nginx.com                 nxt_log_error(NXT_LOG_NOTICE, thr->log,
57*384Szelenkov@nginx.com                               "mem zone test failed: %uz", total);
58*384Szelenkov@nginx.com                 return NXT_ERROR;
59*384Szelenkov@nginx.com             }
60*384Szelenkov@nginx.com         }
61*384Szelenkov@nginx.com 
62*384Szelenkov@nginx.com         for (n = 0; n < nblocks; n++) {
63*384Szelenkov@nginx.com             nxt_mem_zone_free(zone, blocks[n]);
64*384Szelenkov@nginx.com         }
65*384Szelenkov@nginx.com     }
66*384Szelenkov@nginx.com 
67*384Szelenkov@nginx.com     nxt_free(blocks);
68*384Szelenkov@nginx.com     nxt_free(zone);
69*384Szelenkov@nginx.com 
70*384Szelenkov@nginx.com     nxt_thread_time_update(thr);
71*384Szelenkov@nginx.com     nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem zone test passed");
72*384Szelenkov@nginx.com 
73*384Szelenkov@nginx.com     return NXT_OK;
74*384Szelenkov@nginx.com }
75