summaryrefslogtreecommitdiff
path: root/src/lib/ssm/tests/pool_sharding_test.c
blob: 72ae1cb736c9d92cf305d0d5ba23d67fe3d7d1ea (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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 * Ouroboros - Copyright (C) 2016 - 2026
 *
 * Test of the SSM pool sharding with fallback
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#if defined(__linux__) || defined(__CYGWIN__)
#define _DEFAULT_SOURCE
#else
#define _POSIX_C_SOURCE 200112L
#endif

#include "config.h"
#include "ssm.h"

#include <test/test.h>
#include <ouroboros/ssm_pool.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>

#define TEST_SIZE 256

/* Helper to get pool header for inspection */
static struct _ssm_pool_hdr * get_pool_hdr(struct ssm_pool * pool)
{
        /* ssm_pool is opaque, but we know its layout:
         * uint8_t * shm_base
         * struct _ssm_pool_hdr * hdr
         * void * pool_base
         */
        struct _ssm_pool_hdr ** hdr_ptr =
                (struct _ssm_pool_hdr **)((uint8_t *)pool + sizeof(void *));
        return *hdr_ptr;
}

static int test_lazy_distribution(void)
{
        struct ssm_pool *        pool;
        struct _ssm_pool_hdr *   hdr;
        struct _ssm_size_class * sc;
        int                  i;
        int                  sc_idx;

        TEST_START();

        ssm_pool_purge();

        pool = ssm_pool_create();
        if (pool == NULL) {
                printf("Failed to create pool.\n");
                goto fail;
        }

        hdr = get_pool_hdr(pool);
        if (hdr == NULL) {
                printf("Failed to get pool header.\n");
                goto fail_pool;
        }

        /* Find the first size class with blocks */
        sc_idx = -1;
        for (i = 0; i < SSM_POOL_MAX_CLASSES; i++) {
                if (hdr->size_classes[i].object_count > 0) {
                        sc_idx = i;
                        break;
                }
        }

        if (sc_idx < 0) {
                printf("No size classes configured.\n");
                for (i = 0; i < SSM_POOL_MAX_CLASSES; i++) {
                        printf("  Class %d: count=%zu\n", i,
                               hdr->size_classes[i].object_count);
                }
                goto fail_pool;
        }

        sc = &hdr->size_classes[sc_idx];

        /* Verify all blocks start in shard 0 */
        if (sc->shards[0].free_count == 0) {
                printf("Shard 0 should have all blocks initially.\n");
                goto fail_pool;
        }

        /* Verify other shards are empty */
        for (i = 1; i < SSM_POOL_SHARDS; i++) {
                if (sc->shards[i].free_count != 0) {
                        printf("Shard %d should be empty, has %zu.\n",
                               i, sc->shards[i].free_count);
                        goto fail_pool;
                }
        }

        ssm_pool_destroy(pool);

        TEST_SUCCESS();
        return TEST_RC_SUCCESS;

 fail_pool:
        ssm_pool_destroy(pool);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_shard_migration(void)
{
        struct ssm_pool *        pool;
        struct _ssm_pool_hdr *   hdr;
        struct _ssm_size_class * sc;
        struct ssm_pk_buff *  spb;
        uint8_t *             ptr;
        ssize_t               off;
        int                   shard_idx;
        int                   sc_idx;
        int                   i;

        TEST_START();

        ssm_pool_purge();

        pool = ssm_pool_create();
        if (pool == NULL) {
                printf("Failed to create pool.\n");
                goto fail;
        }

        hdr = get_pool_hdr(pool);

        /* Find the first size class with blocks */
        sc_idx = -1;
        for (i = 0; i < SSM_POOL_MAX_CLASSES; i++) {
                if (hdr->size_classes[i].object_count > 0) {
                        sc_idx = i;
                        break;
                }
        }

        if (sc_idx < 0) {
                printf("No size classes configured.\n");
                goto fail;
        }

        sc = &hdr->size_classes[sc_idx];

        /* Allocate from this process */
        off = ssm_pool_alloc(pool, TEST_SIZE, &ptr, &spb);
        if (off < 0) {
                printf("Allocation failed: %zd.\n", off);
                goto fail_pool;
        }

        /* Free it - should go to this process's shard */
        shard_idx = getpid() % SSM_POOL_SHARDS;
        if (ssm_pool_remove(pool, off) != 0) {
                printf("Remove failed.\n");
                goto fail_pool;
        }

        /* Verify block migrated away from shard 0 or in allocator's shard */
        if (sc->shards[shard_idx].free_count == 0 &&
            sc->shards[0].free_count == 0) {
                printf("Block should have been freed to a shard.\n");
                goto fail_pool;
        }

        ssm_pool_destroy(pool);

        TEST_SUCCESS();
        return TEST_RC_SUCCESS;

 fail_pool:
        ssm_pool_destroy(pool);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_fallback_stealing(void)
{
        struct ssm_pool *        pool;
        struct _ssm_pool_hdr *   hdr;
        struct _ssm_size_class * sc;
        struct ssm_pk_buff ** spbs;
        uint8_t **            ptrs;
        size_t                total_blocks;
        size_t                total_free;
        size_t                i;
        int                   sc_idx;
        int                   c;

        TEST_START();

        ssm_pool_purge();

        pool = ssm_pool_create();
        if (pool == NULL) {
                printf("Failed to create pool.\n");
                goto fail;
        }

        hdr = get_pool_hdr(pool);

        /* Find the first size class with blocks */
        sc_idx = -1;
        for (c = 0; c < SSM_POOL_MAX_CLASSES; c++) {
                if (hdr->size_classes[c].object_count > 0) {
                        sc_idx = c;
                        break;
                }
        }

        if (sc_idx < 0) {
                printf("No size classes configured.\n");
                goto fail;
        }

        sc = &hdr->size_classes[sc_idx];
        total_blocks = sc->object_count;

        spbs = malloc(total_blocks * sizeof(struct ssm_pk_buff *));
        ptrs = malloc(total_blocks * sizeof(uint8_t *));
        if (spbs == NULL || ptrs == NULL) {
                printf("Failed to allocate test arrays.\n");
                goto fail_pool;
        }

        /* Allocate half the blocks from single process */
        for (i = 0; i < total_blocks / 2; i++) {
                ssize_t off = ssm_pool_alloc(pool, TEST_SIZE,
                                             &ptrs[i], &spbs[i]);
                if (off < 0) {
                        printf("Allocation %zu failed: %zd.\n", i, off);
                        free(spbs);
                        free(ptrs);
                        goto fail_pool;
                }
        }

        /* Free them all - they go to local_shard */
        for (i = 0; i < total_blocks / 2; i++) {
                size_t off = ssm_pk_buff_get_idx(spbs[i]);
                if (ssm_pool_remove(pool, off) != 0) {
                        printf("Remove %zu failed.\n", i);
                        free(spbs);
                        free(ptrs);
                        goto fail_pool;
                }
        }

        /* Freed blocks should be in shards (all blocks free again) */
        total_free = 0;
        for (i = 0; i < SSM_POOL_SHARDS; i++) {
                total_free += sc->shards[i].free_count;
        }

        if (total_free != total_blocks) {
                printf("Expected %zu free blocks total, got %zu.\n",
                       total_blocks, total_free);
                free(spbs);
                free(ptrs);
                goto fail_pool;
        }

        /* Allocate again - should succeed by taking from shards */
        for (i = 0; i < total_blocks / 2; i++) {
                ssize_t off = ssm_pool_alloc(pool, TEST_SIZE,
                                             &ptrs[i], &spbs[i]);
                if (off < 0) {
                        printf("Fallback alloc %zu failed: %zd.\n", i, off);
                        free(spbs);
                        free(ptrs);
                        goto fail_pool;
                }
        }

        /* Now all allocated blocks are in use again */
        /* Cleanup - free all allocated blocks */
        for (i = 0; i < total_blocks / 2; i++) {
                size_t off = ssm_pk_buff_get_idx(spbs[i]);
                ssm_pool_remove(pool, off);
        }

        free(spbs);
        free(ptrs);
        ssm_pool_destroy(pool);

        TEST_SUCCESS();
        return TEST_RC_SUCCESS;

 fail_pool:
        ssm_pool_destroy(pool);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_multiprocess_sharding(void)
{
        struct ssm_pool *        pool;
        struct _ssm_pool_hdr *   hdr;
        struct _ssm_size_class * sc;
        pid_t                 children[SSM_POOL_SHARDS];
        int                   i;
        int                   status;

        TEST_START();

        ssm_pool_purge();

        pool = ssm_pool_create();
        if (pool == NULL) {
                printf("Failed to create pool.\n");
                goto fail;
        }

        /* Fork processes to test different shards */
        for (i = 0; i < SSM_POOL_SHARDS; i++) {
                children[i] = fork();
                if (children[i] == -1) {
                        printf("Fork %d failed.\n", i);
                        goto fail_children;
                }

                if (children[i] == 0) {
                        /* Child process */
                        struct ssm_pool *    child_pool;
                        struct ssm_pk_buff * spb;
                        uint8_t *            ptr;
                        ssize_t              off;
                        int                  my_shard;

                        child_pool = ssm_pool_open();
                        if (child_pool == NULL)
                                exit(EXIT_FAILURE);

                        my_shard = getpid() % SSM_POOL_SHARDS;
                        (void) my_shard; /* Reserved for future use */

                        /* Each child allocates and frees a block */
                        off = ssm_pool_alloc(child_pool, TEST_SIZE,
                                            &ptr, &spb);
                        if (off < 0) {
                                ssm_pool_close(child_pool);
                                exit(EXIT_FAILURE);
                        }

                        /* Small delay to ensure allocation visible */
                        usleep(10000);

                        if (ssm_pool_remove(child_pool, off) != 0) {
                                ssm_pool_close(child_pool);
                                exit(EXIT_FAILURE);
                        }

                        ssm_pool_close(child_pool);
                        exit(EXIT_SUCCESS);
                }
        }

        /* Wait for all children */
        for (i = 0; i < SSM_POOL_SHARDS; i++) {
                if (waitpid(children[i], &status, 0) == -1) {
                        printf("Waitpid %d failed.\n", i);
                        goto fail_children;
                }
                if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
                        printf("Child %d failed.\n", i);
                        goto fail_pool;
                }
        }

        /* Verify blocks distributed across shards */
        hdr = get_pool_hdr(pool);

        /* Find the first size class with blocks */
        sc = NULL;
        for (i = 0; i < SSM_POOL_MAX_CLASSES; i++) {
                if (hdr->size_classes[i].object_count > 0) {
                        sc = &hdr->size_classes[i];
                        break;
                }
        }

        if (sc == NULL) {
                printf("No size classes configured.\n");
                goto fail_pool;
        }

        /* After children allocate and free, blocks should be in shards
         * (though exact distribution depends on PID values)
         */
        for (i = 0; i < SSM_POOL_SHARDS; i++) {
                /* At least some shards should have blocks */
                if (sc->shards[i].free_count > 0) {
                        break;
                }
        }

        ssm_pool_destroy(pool);

        TEST_SUCCESS();
        return TEST_RC_SUCCESS;

 fail_children:
        /* Kill any remaining children */
        for (i = 0; i < SSM_POOL_SHARDS; i++) {
                if (children[i] > 0)
                        kill(children[i], SIGKILL);
        }
 fail_pool:
        ssm_pool_destroy(pool);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_exhaustion_with_fallback(void)
{
        struct ssm_pool *    pool;
        struct ssm_pk_buff * spb;
        uint8_t *            ptr;
        ssize_t              off;

        TEST_START();

        ssm_pool_purge();

        pool = ssm_pool_create();
        if (pool == NULL) {
                printf("Failed to create pool.\n");
                goto fail;
        }

        /* Allocate until exhausted across all shards */
        while (true) {
                off = ssm_pool_alloc(pool, TEST_SIZE, &ptr, &spb);
                if (off < 0) {
                        if (off == -EAGAIN)
                                break;
                        printf("Unexpected error: %zd.\n", off);
                        goto fail_pool;
                }
        }

        /* Should fail with -EAGAIN when truly exhausted */
        off = ssm_pool_alloc(pool, TEST_SIZE, &ptr, &spb);
        if (off != -EAGAIN) {
                printf("Expected -EAGAIN, got %zd.\n", off);
                goto fail_pool;
        }

        ssm_pool_destroy(pool);

        TEST_SUCCESS();
        return TEST_RC_SUCCESS;

 fail_pool:
        ssm_pool_destroy(pool);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

int pool_sharding_test(int     argc,
                       char ** argv)
{
        int ret = 0;

        (void) argc;
        (void) argv;

        ret |= test_lazy_distribution();
        ret |= test_shard_migration();
        ret |= test_fallback_stealing();
        ret |= test_multiprocess_sharding();
        ret |= test_exhaustion_with_fallback();

        return ret;
}