summaryrefslogtreecommitdiff
path: root/memory/jemalloc/src/test/unit/witness.c
blob: ed172753c7c4d75577868541c74a33b3fc64af83 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "test/jemalloc_test.h"

static witness_lock_error_t *witness_lock_error_orig;
static witness_owner_error_t *witness_owner_error_orig;
static witness_not_owner_error_t *witness_not_owner_error_orig;
static witness_lockless_error_t *witness_lockless_error_orig;

static bool saw_lock_error;
static bool saw_owner_error;
static bool saw_not_owner_error;
static bool saw_lockless_error;

static void
witness_lock_error_intercept(const witness_list_t *witnesses,
    const witness_t *witness)
{

	saw_lock_error = true;
}

static void
witness_owner_error_intercept(const witness_t *witness)
{

	saw_owner_error = true;
}

static void
witness_not_owner_error_intercept(const witness_t *witness)
{

	saw_not_owner_error = true;
}

static void
witness_lockless_error_intercept(const witness_list_t *witnesses)
{

	saw_lockless_error = true;
}

static int
witness_comp(const witness_t *a, const witness_t *b)
{

	assert_u_eq(a->rank, b->rank, "Witnesses should have equal rank");

	return (strcmp(a->name, b->name));
}

static int
witness_comp_reverse(const witness_t *a, const witness_t *b)
{

	assert_u_eq(a->rank, b->rank, "Witnesses should have equal rank");

	return (-strcmp(a->name, b->name));
}

TEST_BEGIN(test_witness)
{
	witness_t a, b;
	tsdn_t *tsdn;

	test_skip_if(!config_debug);

	tsdn = tsdn_fetch();

	witness_assert_lockless(tsdn);

	witness_init(&a, "a", 1, NULL);
	witness_assert_not_owner(tsdn, &a);
	witness_lock(tsdn, &a);
	witness_assert_owner(tsdn, &a);

	witness_init(&b, "b", 2, NULL);
	witness_assert_not_owner(tsdn, &b);
	witness_lock(tsdn, &b);
	witness_assert_owner(tsdn, &b);

	witness_unlock(tsdn, &a);
	witness_unlock(tsdn, &b);

	witness_assert_lockless(tsdn);
}
TEST_END

TEST_BEGIN(test_witness_comp)
{
	witness_t a, b, c, d;
	tsdn_t *tsdn;

	test_skip_if(!config_debug);

	tsdn = tsdn_fetch();

	witness_assert_lockless(tsdn);

	witness_init(&a, "a", 1, witness_comp);
	witness_assert_not_owner(tsdn, &a);
	witness_lock(tsdn, &a);
	witness_assert_owner(tsdn, &a);

	witness_init(&b, "b", 1, witness_comp);
	witness_assert_not_owner(tsdn, &b);
	witness_lock(tsdn, &b);
	witness_assert_owner(tsdn, &b);
	witness_unlock(tsdn, &b);

	witness_lock_error_orig = witness_lock_error;
	witness_lock_error = witness_lock_error_intercept;
	saw_lock_error = false;

	witness_init(&c, "c", 1, witness_comp_reverse);
	witness_assert_not_owner(tsdn, &c);
	assert_false(saw_lock_error, "Unexpected witness lock error");
	witness_lock(tsdn, &c);
	assert_true(saw_lock_error, "Expected witness lock error");
	witness_unlock(tsdn, &c);

	saw_lock_error = false;

	witness_init(&d, "d", 1, NULL);
	witness_assert_not_owner(tsdn, &d);
	assert_false(saw_lock_error, "Unexpected witness lock error");
	witness_lock(tsdn, &d);
	assert_true(saw_lock_error, "Expected witness lock error");
	witness_unlock(tsdn, &d);

	witness_unlock(tsdn, &a);

	witness_assert_lockless(tsdn);

	witness_lock_error = witness_lock_error_orig;
}
TEST_END

TEST_BEGIN(test_witness_reversal)
{
	witness_t a, b;
	tsdn_t *tsdn;

	test_skip_if(!config_debug);

	witness_lock_error_orig = witness_lock_error;
	witness_lock_error = witness_lock_error_intercept;
	saw_lock_error = false;

	tsdn = tsdn_fetch();

	witness_assert_lockless(tsdn);

	witness_init(&a, "a", 1, NULL);
	witness_init(&b, "b", 2, NULL);

	witness_lock(tsdn, &b);
	assert_false(saw_lock_error, "Unexpected witness lock error");
	witness_lock(tsdn, &a);
	assert_true(saw_lock_error, "Expected witness lock error");

	witness_unlock(tsdn, &a);
	witness_unlock(tsdn, &b);

	witness_assert_lockless(tsdn);

	witness_lock_error = witness_lock_error_orig;
}
TEST_END

TEST_BEGIN(test_witness_recursive)
{
	witness_t a;
	tsdn_t *tsdn;

	test_skip_if(!config_debug);

	witness_not_owner_error_orig = witness_not_owner_error;
	witness_not_owner_error = witness_not_owner_error_intercept;
	saw_not_owner_error = false;

	witness_lock_error_orig = witness_lock_error;
	witness_lock_error = witness_lock_error_intercept;
	saw_lock_error = false;

	tsdn = tsdn_fetch();

	witness_assert_lockless(tsdn);

	witness_init(&a, "a", 1, NULL);

	witness_lock(tsdn, &a);
	assert_false(saw_lock_error, "Unexpected witness lock error");
	assert_false(saw_not_owner_error, "Unexpected witness not owner error");
	witness_lock(tsdn, &a);
	assert_true(saw_lock_error, "Expected witness lock error");
	assert_true(saw_not_owner_error, "Expected witness not owner error");

	witness_unlock(tsdn, &a);

	witness_assert_lockless(tsdn);

	witness_owner_error = witness_owner_error_orig;
	witness_lock_error = witness_lock_error_orig;

}
TEST_END

TEST_BEGIN(test_witness_unlock_not_owned)
{
	witness_t a;
	tsdn_t *tsdn;

	test_skip_if(!config_debug);

	witness_owner_error_orig = witness_owner_error;
	witness_owner_error = witness_owner_error_intercept;
	saw_owner_error = false;

	tsdn = tsdn_fetch();

	witness_assert_lockless(tsdn);

	witness_init(&a, "a", 1, NULL);

	assert_false(saw_owner_error, "Unexpected owner error");
	witness_unlock(tsdn, &a);
	assert_true(saw_owner_error, "Expected owner error");

	witness_assert_lockless(tsdn);

	witness_owner_error = witness_owner_error_orig;
}
TEST_END

TEST_BEGIN(test_witness_lockful)
{
	witness_t a;
	tsdn_t *tsdn;

	test_skip_if(!config_debug);

	witness_lockless_error_orig = witness_lockless_error;
	witness_lockless_error = witness_lockless_error_intercept;
	saw_lockless_error = false;

	tsdn = tsdn_fetch();

	witness_assert_lockless(tsdn);

	witness_init(&a, "a", 1, NULL);

	assert_false(saw_lockless_error, "Unexpected lockless error");
	witness_assert_lockless(tsdn);

	witness_lock(tsdn, &a);
	witness_assert_lockless(tsdn);
	assert_true(saw_lockless_error, "Expected lockless error");

	witness_unlock(tsdn, &a);

	witness_assert_lockless(tsdn);

	witness_lockless_error = witness_lockless_error_orig;
}
TEST_END

int
main(void)
{

	return (test(
	    test_witness,
	    test_witness_comp,
	    test_witness_reversal,
	    test_witness_recursive,
	    test_witness_unlock_not_owned,
	    test_witness_lockful));
}