aboutsummaryrefslogtreecommitdiff
path: root/test/unit/counter.c
diff options
context:
space:
mode:
authorWarner Losh <imp@FreeBSD.org>2025-02-05 23:20:13 +0000
committerWarner Losh <imp@FreeBSD.org>2025-02-05 23:20:13 +0000
commit48ec896efb0b78141df004eaa21288b84590c9da (patch)
tree33799792fd95c266d472ab1ae51d50ab4f942eb3 /test/unit/counter.c
parentd28d7fbede216494aa3942af042cc084fcd6098a (diff)
Import jemalloc 5.3.0. This import changes how manage the jemalloc vendor branch (which was just started anyway). Starting with 5.3.0, we import a clean tree from the upstream github, removing all the old files that are no longer upstream, or that we've kept around for some reason. We do this because we merge from this raw version of jemalloc into the FreeBSD contrib/jemalloc, then we run autogen stuff, generate all the generated .h files with gmake, then finally remove much of the generated files in contrib/jemalloc using an update script. Sponsored by: Netflix
Diffstat (limited to 'test/unit/counter.c')
-rw-r--r--test/unit/counter.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/test/unit/counter.c b/test/unit/counter.c
new file mode 100644
index 000000000000..277baac169eb
--- /dev/null
+++ b/test/unit/counter.c
@@ -0,0 +1,80 @@
+#include "test/jemalloc_test.h"
+
+static const uint64_t interval = 1 << 20;
+
+TEST_BEGIN(test_counter_accum) {
+ uint64_t increment = interval >> 4;
+ unsigned n = interval / increment;
+ uint64_t accum = 0;
+
+ counter_accum_t c;
+ counter_accum_init(&c, interval);
+
+ tsd_t *tsd = tsd_fetch();
+ bool trigger;
+ for (unsigned i = 0; i < n; i++) {
+ trigger = counter_accum(tsd_tsdn(tsd), &c, increment);
+ accum += increment;
+ if (accum < interval) {
+ expect_b_eq(trigger, false, "Should not trigger");
+ } else {
+ expect_b_eq(trigger, true, "Should have triggered");
+ }
+ }
+ expect_b_eq(trigger, true, "Should have triggered");
+}
+TEST_END
+
+void
+expect_counter_value(counter_accum_t *c, uint64_t v) {
+ uint64_t accum = locked_read_u64_unsynchronized(&c->accumbytes);
+ expect_u64_eq(accum, v, "Counter value mismatch");
+}
+
+#define N_THDS (16)
+#define N_ITER_THD (1 << 12)
+#define ITER_INCREMENT (interval >> 4)
+
+static void *
+thd_start(void *varg) {
+ counter_accum_t *c = (counter_accum_t *)varg;
+
+ tsd_t *tsd = tsd_fetch();
+ bool trigger;
+ uintptr_t n_triggered = 0;
+ for (unsigned i = 0; i < N_ITER_THD; i++) {
+ trigger = counter_accum(tsd_tsdn(tsd), c, ITER_INCREMENT);
+ n_triggered += trigger ? 1 : 0;
+ }
+
+ return (void *)n_triggered;
+}
+
+
+TEST_BEGIN(test_counter_mt) {
+ counter_accum_t shared_c;
+ counter_accum_init(&shared_c, interval);
+
+ thd_t thds[N_THDS];
+ unsigned i;
+ for (i = 0; i < N_THDS; i++) {
+ thd_create(&thds[i], thd_start, (void *)&shared_c);
+ }
+
+ uint64_t sum = 0;
+ for (i = 0; i < N_THDS; i++) {
+ void *ret;
+ thd_join(thds[i], &ret);
+ sum += (uintptr_t)ret;
+ }
+ expect_u64_eq(sum, N_THDS * N_ITER_THD / (interval / ITER_INCREMENT),
+ "Incorrect number of triggers");
+}
+TEST_END
+
+int
+main(void) {
+ return test(
+ test_counter_accum,
+ test_counter_mt);
+}