2016-02-03 12:27:54 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2017-04-20 04:39:33 +08:00
|
|
|
#include "jemalloc/internal/ticker.h"
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_ticker_tick) {
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NREPS 2
|
|
|
|
#define NTICKS 3
|
2016-02-03 12:27:54 +08:00
|
|
|
ticker_t ticker;
|
|
|
|
int32_t i, j;
|
|
|
|
|
|
|
|
ticker_init(&ticker, NTICKS);
|
|
|
|
for (i = 0; i < NREPS; i++) {
|
|
|
|
for (j = 0; j < NTICKS; j++) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(ticker_read(&ticker), NTICKS - j,
|
2016-02-03 12:27:54 +08:00
|
|
|
"Unexpected ticker value (i=%d, j=%d)", i, j);
|
2023-03-31 10:02:24 +08:00
|
|
|
expect_false(ticker_tick(&ticker, false),
|
2016-02-03 12:27:54 +08:00
|
|
|
"Unexpected ticker fire (i=%d, j=%d)", i, j);
|
|
|
|
}
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u32_eq(ticker_read(&ticker), 0,
|
2016-02-03 12:27:54 +08:00
|
|
|
"Expected ticker depletion");
|
2023-03-31 10:02:24 +08:00
|
|
|
expect_true(ticker_tick(&ticker, false),
|
2016-02-03 12:27:54 +08:00
|
|
|
"Expected ticker fire (i=%d)", i);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u32_eq(ticker_read(&ticker), NTICKS,
|
2016-02-03 12:27:54 +08:00
|
|
|
"Expected ticker reset");
|
|
|
|
}
|
|
|
|
#undef NTICKS
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_ticker_ticks) {
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NTICKS 3
|
2016-02-03 12:27:54 +08:00
|
|
|
ticker_t ticker;
|
|
|
|
|
|
|
|
ticker_init(&ticker, NTICKS);
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
|
2023-03-31 10:02:24 +08:00
|
|
|
expect_false(ticker_ticks(&ticker, NTICKS, false),
|
|
|
|
"Unexpected ticker fire");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(ticker_read(&ticker), 0, "Unexpected ticker value");
|
2023-03-31 10:02:24 +08:00
|
|
|
expect_true(ticker_ticks(&ticker, NTICKS, false),
|
|
|
|
"Expected ticker fire");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
|
2016-02-03 12:27:54 +08:00
|
|
|
|
2023-03-31 10:02:24 +08:00
|
|
|
expect_true(ticker_ticks(&ticker, NTICKS + 1, false),
|
|
|
|
"Expected ticker fire");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
|
2016-02-03 12:27:54 +08:00
|
|
|
#undef NTICKS
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_ticker_copy) {
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NTICKS 3
|
2016-02-03 12:27:54 +08:00
|
|
|
ticker_t ta, tb;
|
|
|
|
|
|
|
|
ticker_init(&ta, NTICKS);
|
|
|
|
ticker_copy(&tb, &ta);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
|
2023-03-31 10:02:24 +08:00
|
|
|
expect_true(ticker_ticks(&tb, NTICKS + 1, false),
|
|
|
|
"Expected ticker fire");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
|
2016-02-03 12:27:54 +08:00
|
|
|
|
2023-03-31 10:02:24 +08:00
|
|
|
ticker_tick(&ta, false);
|
2016-02-03 12:27:54 +08:00
|
|
|
ticker_copy(&tb, &ta);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(ticker_read(&tb), NTICKS - 1, "Unexpected ticker value");
|
2023-03-31 10:02:24 +08:00
|
|
|
expect_true(ticker_ticks(&tb, NTICKS, false), "Expected ticker fire");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
|
2016-02-03 12:27:54 +08:00
|
|
|
#undef NTICKS
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2021-02-01 03:55:45 +08:00
|
|
|
TEST_BEGIN(test_ticker_geom) {
|
|
|
|
const int32_t ticks = 100;
|
|
|
|
const uint64_t niters = 100 * 1000;
|
|
|
|
|
|
|
|
ticker_geom_t ticker;
|
|
|
|
ticker_geom_init(&ticker, ticks);
|
|
|
|
uint64_t total_ticks = 0;
|
|
|
|
/* Just some random constant. */
|
|
|
|
uint64_t prng_state = 0x343219f93496db9fULL;
|
|
|
|
for (uint64_t i = 0; i < niters; i++) {
|
2023-03-31 10:02:24 +08:00
|
|
|
while(!ticker_geom_tick(&ticker, &prng_state, false)) {
|
2021-02-01 03:55:45 +08:00
|
|
|
total_ticks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* In fact, with this choice of random seed and the PRNG implementation
|
|
|
|
* used at the time this was tested, total_ticks is 95.1% of the
|
|
|
|
* expected ticks.
|
|
|
|
*/
|
|
|
|
expect_u64_ge(total_ticks , niters * ticks * 9 / 10,
|
|
|
|
"Mean off by > 10%%");
|
|
|
|
expect_u64_le(total_ticks , niters * ticks * 11 / 10,
|
|
|
|
"Mean off by > 10%%");
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2023-03-31 10:02:24 +08:00
|
|
|
TEST_BEGIN(test_ticker_delay) {
|
|
|
|
const int32_t ticks = 1000;
|
|
|
|
const uint64_t niters = 10000;
|
|
|
|
|
|
|
|
ticker_t t1;
|
|
|
|
ticker_init(&t1, ticks);
|
|
|
|
|
|
|
|
ticker_geom_t t2;
|
|
|
|
/* Just some random constant. */
|
|
|
|
uint64_t prng_state = 0x43219f93496db9f3ULL;
|
|
|
|
ticker_geom_init(&t2, ticks);
|
|
|
|
|
|
|
|
bool delay = false;
|
|
|
|
expect_false(ticker_ticks(&t1, ticks, delay), "Unexpected ticker fire");
|
|
|
|
expect_false(ticker_geom_ticks(&t2, &prng_state, ticks, delay),
|
|
|
|
"Unexpected ticker fire");
|
|
|
|
expect_d_eq(ticker_read(&t1), 0, "Unexpected ticker value");
|
|
|
|
expect_d_eq(ticker_geom_read(&t2), 0, "Unexpected ticker value");
|
|
|
|
|
|
|
|
delay = true;
|
|
|
|
/* Not allowed to fire when delay is set to true. */
|
|
|
|
for (unsigned i = 0; i < niters; i++) {
|
|
|
|
expect_false(ticker_tick(&t1, delay), "Unexpected ticker fire");
|
|
|
|
expect_false(ticker_geom_tick(&t2, &prng_state, delay),
|
|
|
|
"Unexpected ticker fire");
|
|
|
|
expect_d_eq(ticker_read(&t1), 0, "Unexpected ticker value");
|
|
|
|
expect_d_eq(ticker_geom_read(&t2), 0, "Unexpected ticker value");
|
|
|
|
}
|
|
|
|
|
|
|
|
delay = false;
|
|
|
|
expect_true(ticker_tick(&t1, delay), "Expected ticker fire");
|
|
|
|
expect_true(ticker_geom_tick(&t2, &prng_state, delay),
|
|
|
|
"Expected ticker fire");
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2016-02-03 12:27:54 +08:00
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2016-02-03 12:27:54 +08:00
|
|
|
test_ticker_tick,
|
|
|
|
test_ticker_ticks,
|
2021-02-01 03:55:45 +08:00
|
|
|
test_ticker_copy,
|
2023-03-31 10:02:24 +08:00
|
|
|
test_ticker_geom,
|
|
|
|
test_ticker_delay);
|
2016-02-03 12:27:54 +08:00
|
|
|
}
|