summaryrefslogtreecommitdiffhomepage
path: root/test/nxt_malloc_test.c
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2017-08-30 00:31:02 +0300
committerIgor Sysoev <igor@sysoev.ru>2017-08-30 00:31:02 +0300
commitcdc7e4479ff5aa08df0857a13a7acd7a00b79c89 (patch)
treea57bf9251fc79d9ef7bd3d33b1d05f3813dc3d4a /test/nxt_malloc_test.c
parent92f3b852981c739a2e6ff6d50dc646fe86031b27 (diff)
downloadunit-cdc7e4479ff5aa08df0857a13a7acd7a00b79c89.tar.gz
unit-cdc7e4479ff5aa08df0857a13a7acd7a00b79c89.tar.bz2
Lib unit tests have been renamed to tests.
Diffstat (limited to 'test/nxt_malloc_test.c')
-rw-r--r--test/nxt_malloc_test.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/test/nxt_malloc_test.c b/test/nxt_malloc_test.c
new file mode 100644
index 00000000..daa609b1
--- /dev/null
+++ b/test/nxt_malloc_test.c
@@ -0,0 +1,123 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) NGINX, Inc.
+ */
+
+#include <nxt_main.h>
+#include "nxt_tests.h"
+
+
+#define TIMES 1000
+
+
+typedef struct {
+ size_t size;
+ size_t alignment;
+ nxt_bool_t tight;
+} nxt_malloc_size_t;
+
+
+static nxt_malloc_size_t *
+nxt_malloc_run_test(nxt_thread_t *thr, nxt_malloc_size_t *last, size_t size,
+ nxt_uint_t times)
+{
+ size_t a, s, alignment;
+ uintptr_t n;
+ nxt_uint_t i, tight;
+ static u_char *p[TIMES + 1];
+
+ alignment = (size_t) -1;
+ tight = 0;
+
+ for (i = 1; i < times; i++) {
+
+ p[i] = nxt_malloc(size);
+ if (p[i] == NULL) {
+ return NULL;
+ }
+
+ n = (uintptr_t) p[i];
+ a = 0;
+
+ while ((n & 1) == 0) {
+ a++;
+ n >>= 1;
+ }
+
+ alignment = nxt_min(alignment, a);
+ }
+
+
+ for (i = 1; i < times; i++) {
+ s = size;
+ nxt_malloc_usable_size(p[i], s);
+
+ if (p[i - 1] + s == p[i] || p[i - 1] == p[i] + s) {
+ tight++;
+ }
+
+ nxt_free(p[i]);
+ }
+
+ alignment = 1 << alignment;
+
+#if 0
+ nxt_log_error(NXT_LOG_NOTICE, thr->log,
+ "malloc: %uz, %uz, %ui", size, alignment, tight);
+#endif
+
+ while (last->alignment >= alignment) {
+ last--;
+ }
+
+ last++;
+
+ last->size = size;
+ last->alignment = alignment;
+ last->tight = times * 9 / 10 < tight;
+
+ return last;
+}
+
+
+nxt_int_t
+nxt_malloc_test(nxt_thread_t *thr)
+{
+ size_t size;
+ nxt_malloc_size_t *last, *s;
+ static nxt_malloc_size_t sizes[100];
+
+ nxt_log_error(NXT_LOG_NOTICE, thr->log, "malloc test started");
+
+ last = &sizes[0];
+
+ for (size = 1; size < 64; size++) {
+ last = nxt_malloc_run_test(thr, last, size, TIMES);
+ if (last == NULL) {
+ return NXT_ERROR;
+ }
+ }
+
+ for (size = 64; size < 16384; size += 8) {
+ last = nxt_malloc_run_test(thr, last, size, TIMES / 4);
+ if (last == NULL) {
+ return NXT_ERROR;
+ }
+ }
+
+ for (size = 16384; size < 512 * 1024 + 129; size += 128) {
+ last = nxt_malloc_run_test(thr, last, size, TIMES / 16);
+ if (last == NULL) {
+ return NXT_ERROR;
+ }
+ }
+
+ for (s = &sizes[1]; s <= last; s++) {
+ nxt_log_error(NXT_LOG_NOTICE, thr->log,
+ "malloc sizes: %uz-%uz alignment:%uz tight:%ui",
+ s[-1].size + 1, s->size, s->alignment, s->tight);
+ }
+
+ return NXT_OK;
+}