summaryrefslogtreecommitdiff
path: root/erts/emulator/test/alloc_SUITE_data/cpool.c
blob: 73026cc758a35b3bb76a86b7d934e4af994ed628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2013. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %CopyrightEnd%
 */


#ifndef __WIN32__
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "testcase_driver.h"
#include "allocator_test.h"

#define FATAL_ASSERT(A)						\
    ((void) ((A)						\
	     ? 1						\
	     : (fatal_assert_failed(#A,				\
				    (char *) __FILE__,		\
				    __LINE__),			\
		0)))

static void
fatal_assert_failed(char* expr, char* file, int line)
{
    fflush(stdout);
    fprintf(stderr, "%s:%d: Assertion failed: %s\n",
	    file, line, expr);
    fflush(stderr);
    abort();
}

#define TEST_NO_THREADS 10
#define TEST_NO_CARRIERS_PER_THREAD 100000
#define TEST_CARRIERS_OFFSET 5

static Allctr_t *alloc = NULL;

static void stop_allocator(void)
{
    if (alloc) {
	STOP_ALC(alloc);
	alloc = NULL;
    }
}


void *thread_func(void *arg);

char *
testcase_name(void)
{
    return "cpool";
}

void
testcase_cleanup(TestCaseState_t *tcs)
{
    stop_allocator();
}

void *
thread_func(void *arg)
{
    Carrier_t *crr = (Carrier_t *) arg;
    int i;

    for (i = 0; i < (TEST_NO_CARRIERS_PER_THREAD+TEST_CARRIERS_OFFSET); i++) {
	int d;
	if (i < TEST_NO_CARRIERS_PER_THREAD) {
	    (void) CPOOL_INSERT(alloc, crr[i]);
	    if ((i & 0x7) == 0)
		FATAL_ASSERT(CPOOL_IS_IN_POOL(alloc, crr[i]));
	}
	d = i-TEST_CARRIERS_OFFSET;
	if (d >= 0) {
	    (void) CPOOL_DELETE(alloc, crr[d]);
	    if ((d & 0x7) == 0)
		FATAL_ASSERT(!CPOOL_IS_IN_POOL(alloc, crr[d]));
	}
    }
    for (i = 0; i < TEST_NO_CARRIERS_PER_THREAD; i++)
	FATAL_ASSERT(!CPOOL_IS_IN_POOL(alloc, crr[i]));
    return NULL;
}

static struct {
    erts_thread tid;
    Carrier_t *crr[TEST_NO_CARRIERS_PER_THREAD];
} threads[TEST_NO_THREADS] = {{0}};

void
testcase_run(TestCaseState_t *tcs)
{
    int no_threads, t, c;
    char *block, *p;
    Ulong zcrr_sz;

    if (!IS_SMP_ENABLED)
	testcase_skipped(tcs, "No SMP support");

    alloc = START_ALC("Zero carrier allocator", 1, NULL);

    zcrr_sz = ZERO_CRR_SIZE;

    block = p = ALLOC(alloc, zcrr_sz*TEST_NO_THREADS*TEST_NO_CARRIERS_PER_THREAD);

    ASSERT(tcs, block != NULL);    

    for (t = 0; t < TEST_NO_THREADS; t++) {
	for (c = 0; c < TEST_NO_CARRIERS_PER_THREAD; c++) {
	    Carrier_t *crr = (Carrier_t *) p;
	    p += zcrr_sz;
	    (void) ZERO_CRR_INIT(alloc, crr);
	    threads[t].crr[c] = crr;
	}
    }

    no_threads = 0;
    for (t = 0; t < TEST_NO_THREADS; t++) {
    	threads[t].tid = THR_CREATE(thread_func, (void *) threads[t].crr);
	if (threads[t].tid) {
	    testcase_printf(tcs, "Successfully created thread %d\n", t);
	    no_threads++;
	}
	else {
	    testcase_printf(tcs, "Failed to create thread %d\n", t);
	    break;
	}
    }

    for (t = 0; t < no_threads; t++)
	THR_JOIN(threads[t].tid);

    FATAL_ASSERT(CPOOL_IS_EMPTY(alloc));

    FREE(alloc, block);

    ASSERT(tcs, no_threads == TEST_NO_THREADS);
}

ERL_NIF_INIT(cpool, testcase_nif_funcs, testcase_nif_init,
	     NULL, NULL, NULL);