ticker.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/ticker.h"
  3. TEST_BEGIN(test_ticker_tick) {
  4. #define NREPS 2
  5. #define NTICKS 3
  6. ticker_t ticker;
  7. int32_t i, j;
  8. ticker_init(&ticker, NTICKS);
  9. for (i = 0; i < NREPS; i++) {
  10. for (j = 0; j < NTICKS; j++) {
  11. expect_u_eq(ticker_read(&ticker), NTICKS - j,
  12. "Unexpected ticker value (i=%d, j=%d)", i, j);
  13. expect_false(ticker_tick(&ticker),
  14. "Unexpected ticker fire (i=%d, j=%d)", i, j);
  15. }
  16. expect_u32_eq(ticker_read(&ticker), 0,
  17. "Expected ticker depletion");
  18. expect_true(ticker_tick(&ticker),
  19. "Expected ticker fire (i=%d)", i);
  20. expect_u32_eq(ticker_read(&ticker), NTICKS,
  21. "Expected ticker reset");
  22. }
  23. #undef NTICKS
  24. }
  25. TEST_END
  26. TEST_BEGIN(test_ticker_ticks) {
  27. #define NTICKS 3
  28. ticker_t ticker;
  29. ticker_init(&ticker, NTICKS);
  30. expect_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
  31. expect_false(ticker_ticks(&ticker, NTICKS), "Unexpected ticker fire");
  32. expect_u_eq(ticker_read(&ticker), 0, "Unexpected ticker value");
  33. expect_true(ticker_ticks(&ticker, NTICKS), "Expected ticker fire");
  34. expect_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
  35. expect_true(ticker_ticks(&ticker, NTICKS + 1), "Expected ticker fire");
  36. expect_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
  37. #undef NTICKS
  38. }
  39. TEST_END
  40. TEST_BEGIN(test_ticker_copy) {
  41. #define NTICKS 3
  42. ticker_t ta, tb;
  43. ticker_init(&ta, NTICKS);
  44. ticker_copy(&tb, &ta);
  45. expect_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
  46. expect_true(ticker_ticks(&tb, NTICKS + 1), "Expected ticker fire");
  47. expect_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
  48. ticker_tick(&ta);
  49. ticker_copy(&tb, &ta);
  50. expect_u_eq(ticker_read(&tb), NTICKS - 1, "Unexpected ticker value");
  51. expect_true(ticker_ticks(&tb, NTICKS), "Expected ticker fire");
  52. expect_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
  53. #undef NTICKS
  54. }
  55. TEST_END
  56. TEST_BEGIN(test_ticker_geom) {
  57. const int32_t ticks = 100;
  58. const uint64_t niters = 100 * 1000;
  59. ticker_geom_t ticker;
  60. ticker_geom_init(&ticker, ticks);
  61. uint64_t total_ticks = 0;
  62. /* Just some random constant. */
  63. uint64_t prng_state = 0x343219f93496db9fULL;
  64. for (uint64_t i = 0; i < niters; i++) {
  65. while(!ticker_geom_tick(&ticker, &prng_state)) {
  66. total_ticks++;
  67. }
  68. }
  69. /*
  70. * In fact, with this choice of random seed and the PRNG implementation
  71. * used at the time this was tested, total_ticks is 95.1% of the
  72. * expected ticks.
  73. */
  74. expect_u64_ge(total_ticks , niters * ticks * 9 / 10,
  75. "Mean off by > 10%%");
  76. expect_u64_le(total_ticks , niters * ticks * 11 / 10,
  77. "Mean off by > 10%%");
  78. }
  79. TEST_END
  80. int
  81. main(void) {
  82. return test(
  83. test_ticker_tick,
  84. test_ticker_ticks,
  85. test_ticker_copy,
  86. test_ticker_geom);
  87. }