File: xahash-test.c

package info (click to toggle)
slurm-wlm-contrib 24.11.5-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 50,596 kB
  • sloc: ansic: 529,598; exp: 64,795; python: 17,051; sh: 9,411; javascript: 6,528; makefile: 4,030; perl: 3,762; pascal: 131
file content (327 lines) | stat: -rw-r--r-- 9,713 bytes parent folder | download | duplicates (5)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*****************************************************************************\
 *  Copyright (C) 2024 SchedMD LLC
 *
 *  This file is part of Slurm, a resource management program.
 *  For details, see <https://slurm.schedmd.com/>.
 *  Please also read the included file: DISCLAIMER.
 *
 *  Slurm is free software; you can redistribute it and/or modify it under
 *  the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  In addition, as a special exception, the copyright holders give permission
 *  to link the code of portions of this program with the OpenSSL library under
 *  certain conditions as described in each individual source file, and
 *  distribute linked combinations including the two. You must obey the GNU
 *  General Public License in all respects for all of the code used other than
 *  OpenSSL. If you modify file(s) with this exception, you may extend this
 *  exception to your version of the file(s), but you are not obligated to do
 *  so. If you do not wish to do so, delete this exception statement from your
 *  version.  If you delete this exception statement from all source files in
 *  the program, then also delete it here.
 *
 *  Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 *  details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with Slurm; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
\*****************************************************************************/

#include <stdint.h>
#include <check.h>

#include "src/common/log.h"
#include "src/common/macros.h"
#include "src/common/read_config.h"
#include "src/common/slurm_protocol_defs.h"
#include "src/common/xahash.h"

#define FIXED_STATE_ENTRIES 1024
#define FIXED_STATE_OVERCOMMIT_ENTRIES 512
#define KEY_SIZE sizeof(void *)

#define GLOBAL_STATE_MAGIC 0xeae0eef0
typedef struct {
	int magic; /* GLOBAL_STATE_MAGIC */
} global_state_t;

#define STATE_MAGIC 0xaa10e8f0
#define STATE_CALLER_MAGIC 0xba0088f0
typedef struct {
	int magic; /* STATE_MAGIC */
	void *key;
	int caller_magic;
} state_t;

static xahash_hash_t _hash(const void *key, const size_t key_bytes, void *state)
{
	global_state_t *gs = state;

	ck_assert(gs->magic == GLOBAL_STATE_MAGIC);
	ck_assert(key_bytes == KEY_SIZE);

	/* just use the pointer to make a hash */
	return (((uintptr_t) key) >> 32) ^ ((uintptr_t) key);
}

static bool _match(void *ptr, const void *key, const size_t key_bytes, void *state)
{
	global_state_t *gs = state;
	state_t *s = ptr;

	ck_assert(gs->magic == GLOBAL_STATE_MAGIC);
	ck_assert(s->magic == STATE_MAGIC);
	ck_assert(key_bytes == KEY_SIZE);

	if (s->key == key)
		return true;
	else
		return false;
}

static void _on_insert(void *ptr, const void *key, const size_t key_bytes, void *state)
{
	global_state_t *gs = state;
	state_t *s = ptr;

	ck_assert(gs->magic == GLOBAL_STATE_MAGIC);
	ck_assert(s != NULL);
	ck_assert(key_bytes == KEY_SIZE);

	*s = (state_t) {
		.magic = STATE_MAGIC,
		.key = (void *) key,
	};
}

static void _on_free(void *ptr, void *state)
{
	global_state_t *gs = state;
	state_t *s = ptr;

	ck_assert(gs->magic == GLOBAL_STATE_MAGIC);
	ck_assert(s != NULL);
	ck_assert(s->magic == STATE_MAGIC);

	*s = (state_t) {
		.magic = ~STATE_MAGIC,
		.key = NULL,
	};
}

static xahash_foreach_control_t _foreach(void *entry, void *state, void *arg)
{
	global_state_t *gs = state;
	state_t *s = entry;

	ck_assert(gs != NULL);
	ck_assert(gs->magic == GLOBAL_STATE_MAGIC);
	ck_assert(s != NULL);
	ck_assert(s->magic == STATE_MAGIC);

	return XAHASH_FOREACH_CONT;
}

START_TEST(test_fixed_basic)
{
	xahash_table_t *ht;
	global_state_t *gs;
	state_t *s, *f;

	ht = xahash_new_table(_hash, _match, _on_insert, _on_free,
			      sizeof(global_state_t), sizeof(state_t),
			      FIXED_STATE_ENTRIES);

	ck_assert_msg(ht != NULL, "hashtable created");

	/* check global state table works */
	gs = xahash_get_state_ptr(ht);

	ck_assert_msg(ht != NULL, "hashtable state created");

	*gs = (global_state_t) {
		.magic = GLOBAL_STATE_MAGIC,
	};
	ck_assert(gs->magic == GLOBAL_STATE_MAGIC);

	/* verify we get the same pointer back */
	gs = xahash_get_state_ptr(ht);
	ck_assert(gs->magic == GLOBAL_STATE_MAGIC);

	/* verify we don't find anything in an empty table */
	ck_assert(!xahash_find_entry(ht, NULL, KEY_SIZE));
	ck_assert(!xahash_find_entry(ht, _match, KEY_SIZE));
	ck_assert(!xahash_free_entry(ht, _match, KEY_SIZE));
	ck_assert(!xahash_find_entry(ht, &ht, KEY_SIZE));
	ck_assert(!xahash_free_entry(ht, &ht, KEY_SIZE));

	/* try inserts */
	s = xahash_insert_entry(ht, &s, KEY_SIZE);
	ck_assert(s->magic == STATE_MAGIC);
	ck_assert(s->key == &s);
	ck_assert(s->caller_magic == 0);
	s->caller_magic = STATE_CALLER_MAGIC;

	/* verify we don't find invalid keys with entries */
	ck_assert(!xahash_find_entry(ht, NULL, KEY_SIZE));
	ck_assert(!xahash_find_entry(ht, _match, KEY_SIZE));
	ck_assert(!xahash_find_entry(ht, &ht, KEY_SIZE));

	/* verify we can find new entry */
	f = xahash_find_entry(ht, &s, KEY_SIZE);
	ck_assert(f != NULL);
	ck_assert(f == s);
	ck_assert(f->magic == STATE_MAGIC);
	ck_assert(f->key == &s);
	ck_assert(f->caller_magic == STATE_CALLER_MAGIC);
	ck_assert(s->magic == STATE_MAGIC);
	ck_assert(s->key == &s);
	ck_assert(s->caller_magic == STATE_CALLER_MAGIC);

	ck_assert(xahash_free_entry(ht, &s, KEY_SIZE));
	ck_assert(!xahash_free_entry(ht, &s, KEY_SIZE));
	ck_assert(!xahash_find_entry(ht, &s, KEY_SIZE));

	/* verify we get the same pointer and value back */
	gs = xahash_get_state_ptr(ht);
	ck_assert(gs->magic == GLOBAL_STATE_MAGIC);

	FREE_NULL_XAHASH_TABLE(ht);
}
END_TEST

START_TEST(test_fixed_mass)
{
	global_state_t *gs;
	xahash_table_t *ht;
	state_t *s[FIXED_STATE_ENTRIES + FIXED_STATE_OVERCOMMIT_ENTRIES] = {0};

	ht = xahash_new_table(_hash, _match, _on_insert, _on_free,
			      sizeof(global_state_t), sizeof(state_t),
			      FIXED_STATE_ENTRIES);
	gs = xahash_get_state_ptr(ht);
	*gs = (global_state_t) {
		.magic = GLOBAL_STATE_MAGIC,
	};

	/* insert all entries */
	for (int i = 0; i < ARRAY_SIZE(s); i++) {
		state_t *f;
		int caller_magic = STATE_CALLER_MAGIC * ((uint64_t) &s[i]);

		/* try inserts */
		f = s[i] = xahash_insert_entry(ht, &s[i], KEY_SIZE);

		/* verify on_insert changes */
		ck_assert(f->magic == STATE_MAGIC);
		ck_assert(f->key == &s[i]);
		ck_assert(f->caller_magic == 0);
		f->caller_magic = caller_magic;

		/* verify we can still find via same key */
		ck_assert(f == xahash_find_entry(ht, (s + i), KEY_SIZE));
		ck_assert(f == xahash_find_entry(ht, &s[i], KEY_SIZE));
		ck_assert(s[i]->caller_magic == caller_magic);
		ck_assert(f == s[i]);
	}

	/* verify all entries and blobs */
	for (int i = 0; i < ARRAY_SIZE(s); i++) {
		int caller_magic = STATE_CALLER_MAGIC * ((uint64_t) &s[i]);

		/* verify we can find every entry */
		state_t *f = xahash_find_entry(ht, &s[i], KEY_SIZE);
		ck_assert(f != NULL);
		ck_assert(f == s[i]);
		ck_assert(f->magic == STATE_MAGIC);
		ck_assert(f->key == &s[i]);
		ck_assert(f->caller_magic == s[i]->caller_magic);
		ck_assert(f->caller_magic == caller_magic);
	}

	/* verify all entries and blobs with foreach */
	ck_assert(xahash_foreach_entry(ht, _foreach, NULL) == ARRAY_SIZE(s));

	/* verify all entries and blobs (in reverse) */
	for (int i = (ARRAY_SIZE(s) - 1); i >= 0; i--) {
		int caller_magic = STATE_CALLER_MAGIC * ((uint64_t) &s[i]);

		/* verify we can find every entry */
		state_t *f = xahash_find_entry(ht, &s[i], KEY_SIZE);
		ck_assert(f != NULL);
		ck_assert(f == s[i]);
		ck_assert(f->magic == STATE_MAGIC);
		ck_assert(f->key == &s[i]);
		ck_assert(f->caller_magic == s[i]->caller_magic);
		ck_assert(f->caller_magic == caller_magic);
	}

	/* remove and verify all entries removed */
	for (int i = 0; i < ARRAY_SIZE(s); i++) {
		ck_assert(xahash_find_entry(ht, &s[i], KEY_SIZE) == s[i]);
		s[i] = NULL;
		ck_assert(xahash_free_entry(ht, &s[i], KEY_SIZE));
		ck_assert(!xahash_find_entry(ht, &s[i], KEY_SIZE));
	}

	/* verify all removed (again) */
	for (int i = 0; i < ARRAY_SIZE(s); i++)
		ck_assert(!xahash_find_entry(ht, &s[i], KEY_SIZE));

	/* verify we get the same pointer and value back */
	ck_assert(gs == xahash_get_state_ptr(ht));
	gs = xahash_get_state_ptr(ht);
	ck_assert(gs->magic == GLOBAL_STATE_MAGIC);

	FREE_NULL_XAHASH_TABLE(ht);
}
END_TEST

Suite *suite_xahash(void)
{
	Suite *s = suite_create("xahash");
	TCase *tc_core = tcase_create("xahash");

	tcase_add_test(tc_core, test_fixed_basic);
	tcase_add_test(tc_core, test_fixed_mass);

	suite_add_tcase(s, tc_core);
	return s;
}

int main(void)
{
	int number_failed;
	enum print_output po = CK_ENV;
	enum fork_status fs = CK_FORK_GETENV;
	SRunner *sr = NULL;
	const char *debug_env = getenv("SLURM_DEBUG");
	const char *debug_flags_env = getenv("SLURM_DEBUG_FLAGS");

	log_options_t log_opts = LOG_OPTS_INITIALIZER;

	if (debug_env)
		log_opts.stderr_level = log_string2num(debug_env);
	if (debug_flags_env)
		debug_str2flags(debug_flags_env, &slurm_conf.debug_flags);

	log_init("xahash-test", log_opts, 0, NULL);

	if (log_opts.stderr_level >= LOG_LEVEL_DEBUG) {
		/* automatically be gdb friendly when debug logging */
		po = CK_VERBOSE;
		fs = CK_NOFORK;
	}

	sr = srunner_create(suite_xahash());
	srunner_set_fork_status(sr, fs);
	srunner_run_all(sr, po);
	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}