test-write-threshold.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Test block device write threshold
  3. *
  4. * This work is licensed under the terms of the GNU LGPL, version 2 or later.
  5. * See the COPYING.LIB file in the top-level directory.
  6. *
  7. */
  8. #include "qemu/osdep.h"
  9. #include "block/block_int.h"
  10. #include "block/write-threshold.h"
  11. static void test_threshold_not_set_on_init(void)
  12. {
  13. uint64_t res;
  14. BlockDriverState bs;
  15. memset(&bs, 0, sizeof(bs));
  16. g_assert(!bdrv_write_threshold_is_set(&bs));
  17. res = bdrv_write_threshold_get(&bs);
  18. g_assert_cmpint(res, ==, 0);
  19. }
  20. static void test_threshold_set_get(void)
  21. {
  22. uint64_t threshold = 4 * 1024 * 1024;
  23. uint64_t res;
  24. BlockDriverState bs;
  25. memset(&bs, 0, sizeof(bs));
  26. bdrv_write_threshold_set(&bs, threshold);
  27. g_assert(bdrv_write_threshold_is_set(&bs));
  28. res = bdrv_write_threshold_get(&bs);
  29. g_assert_cmpint(res, ==, threshold);
  30. }
  31. static void test_threshold_multi_set_get(void)
  32. {
  33. uint64_t threshold1 = 4 * 1024 * 1024;
  34. uint64_t threshold2 = 15 * 1024 * 1024;
  35. uint64_t res;
  36. BlockDriverState bs;
  37. memset(&bs, 0, sizeof(bs));
  38. bdrv_write_threshold_set(&bs, threshold1);
  39. bdrv_write_threshold_set(&bs, threshold2);
  40. res = bdrv_write_threshold_get(&bs);
  41. g_assert_cmpint(res, ==, threshold2);
  42. }
  43. static void test_threshold_not_trigger(void)
  44. {
  45. uint64_t amount = 0;
  46. uint64_t threshold = 4 * 1024 * 1024;
  47. BlockDriverState bs;
  48. BdrvTrackedRequest req;
  49. memset(&bs, 0, sizeof(bs));
  50. memset(&req, 0, sizeof(req));
  51. req.offset = 1024;
  52. req.bytes = 1024;
  53. bdrv_write_threshold_set(&bs, threshold);
  54. amount = bdrv_write_threshold_exceeded(&bs, &req);
  55. g_assert_cmpuint(amount, ==, 0);
  56. }
  57. static void test_threshold_trigger(void)
  58. {
  59. uint64_t amount = 0;
  60. uint64_t threshold = 4 * 1024 * 1024;
  61. BlockDriverState bs;
  62. BdrvTrackedRequest req;
  63. memset(&bs, 0, sizeof(bs));
  64. memset(&req, 0, sizeof(req));
  65. req.offset = (4 * 1024 * 1024) - 1024;
  66. req.bytes = 2 * 1024;
  67. bdrv_write_threshold_set(&bs, threshold);
  68. amount = bdrv_write_threshold_exceeded(&bs, &req);
  69. g_assert_cmpuint(amount, >=, 1024);
  70. }
  71. typedef struct TestStruct {
  72. const char *name;
  73. void (*func)(void);
  74. } TestStruct;
  75. int main(int argc, char **argv)
  76. {
  77. size_t i;
  78. TestStruct tests[] = {
  79. { "/write-threshold/not-set-on-init",
  80. test_threshold_not_set_on_init },
  81. { "/write-threshold/set-get",
  82. test_threshold_set_get },
  83. { "/write-threshold/multi-set-get",
  84. test_threshold_multi_set_get },
  85. { "/write-threshold/not-trigger",
  86. test_threshold_not_trigger },
  87. { "/write-threshold/trigger",
  88. test_threshold_trigger },
  89. { NULL, NULL }
  90. };
  91. g_test_init(&argc, &argv, NULL);
  92. for (i = 0; tests[i].name != NULL; i++) {
  93. g_test_add_func(tests[i].name, tests[i].func);
  94. }
  95. return g_test_run();
  96. }