7a6ce569e70e71c4031a74e174bbe82fd73743b5
[librseq.git] / tests / mempool_test.c
1 // SPDX-License-Identifier: MIT
2 // SPDX-FileCopyrightText: 2024 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 /*
4 * rseq memory pool test.
5 */
6
7 #ifndef _GNU_SOURCE
8 #define _GNU_SOURCE
9 #endif
10 #include <assert.h>
11 #include <sched.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/time.h>
16 #include <inttypes.h>
17 #include <stdlib.h>
18
19 #include <rseq/mempool.h>
20
21 #include "list.h"
22 #include "tap.h"
23
24 struct test_data {
25 uintptr_t value;
26 struct test_data __rseq_percpu *backref;
27 struct list_head node;
28 };
29
30 static void test_mempool_fill(size_t len)
31 {
32 struct test_data __rseq_percpu *ptr;
33 struct test_data *iter, *tmp;
34 struct rseq_percpu_pool *mempool;
35 struct rseq_pool_attr *attr;
36 uint64_t count = 0;
37 LIST_HEAD(list);
38 int ret, i;
39
40 attr = rseq_pool_attr_create();
41 ret = rseq_pool_attr_set_robust(attr);
42 ok(ret == 0, "Setting mempool robust attribute");
43
44 mempool = rseq_percpu_pool_create("test_data",
45 sizeof(struct test_data),
46 len, CPU_SETSIZE, attr);
47 ok(mempool, "Create mempool of size %zu", len);
48 rseq_pool_attr_destroy(attr);
49
50 for (;;) {
51 struct test_data *cpuptr;
52
53 ptr = (struct test_data __rseq_percpu *) rseq_percpu_zmalloc(mempool);
54 if (!ptr)
55 break;
56 /* Link items in cpu 0. */
57 cpuptr = rseq_percpu_ptr(ptr, 0);
58 cpuptr->backref = ptr;
59 /* Randomize items in list. */
60 if (count & 1)
61 list_add(&cpuptr->node, &list);
62 else
63 list_add_tail(&cpuptr->node, &list);
64 count++;
65 }
66
67 ok(count * sizeof(struct test_data) == len, "Allocated %" PRIu64 " objects in pool", count);
68
69 list_for_each_entry(iter, &list, node) {
70 ptr = iter->backref;
71 for (i = 0; i < CPU_SETSIZE; i++) {
72 struct test_data *cpuptr = rseq_percpu_ptr(ptr, i);
73
74 if (cpuptr->value != 0)
75 abort();
76 cpuptr->value++;
77 }
78 }
79
80 ok(1, "Check for pool content corruption");
81
82 list_for_each_entry_safe(iter, tmp, &list, node) {
83 ptr = iter->backref;
84 rseq_percpu_free(ptr);
85 }
86 ret = rseq_percpu_pool_destroy(mempool);
87 ok(ret == 0, "Destroy mempool");
88 }
89
90 int main(void)
91 {
92 size_t len;
93
94 plan_no_plan();
95
96 /* From 4kB to 4MB */
97 for (len = 4096; len < 4096 * 1024; len <<= 1) {
98 test_mempool_fill(len);
99 }
100
101 exit(exit_status());
102 }
This page took 0.031188 seconds and 3 git commands to generate.