/src/cn/test/csched_test.c

https://github.com/hse-project/hse · C · 309 lines · 231 code · 62 blank · 16 comment · 16 complexity · 28327d31c37690b7ee6662372f4c6ec7 MD5 · raw file

  1. /* SPDX-License-Identifier: Apache-2.0 */
  2. /*
  3. * Copyright (C) 2015-2020 Micron Technology, Inc. All rights reserved.
  4. */
  5. #include <hse_ut/framework.h>
  6. #include <hse_test_support/mock_api.h>
  7. #include <hse_test_support/mapi_alloc_tester.h>
  8. #include <hse_util/hse_err.h>
  9. #include <hse_ikvdb/kvdb_rparams.h>
  10. #include <hse_ikvdb/kvdb_health.h>
  11. #include <hse_ikvdb/csched.h>
  12. #include <hse_ikvdb/ikvdb.h>
  13. #include "../csched_ops.h"
  14. #include "../csched_noop.h"
  15. #include "../csched_sp3.h"
  16. #include "../cn_metrics.h"
  17. struct kvdb_rparams *rp, rparams;
  18. const char * mp;
  19. struct kvdb_health mock_health;
  20. int set_debug_rparam;
  21. merr_t mocked_sp_create_rc;
  22. struct throttle_sensor;
  23. enum csched_policy policy_list[] = { csched_policy_old, csched_policy_noop, csched_policy_sp3 };
  24. static void
  25. mocked_sp_destroy(struct csched_ops *handle)
  26. {
  27. mapi_safe_free(handle);
  28. }
  29. static void
  30. mocked_sp_notify_ingest(struct csched_ops *handle, struct cn_tree *tree, size_t size, size_t dunno)
  31. {
  32. }
  33. static void
  34. mocked_sp_tree_add(struct csched_ops *handle, struct cn_tree *tree)
  35. {
  36. }
  37. static void
  38. mocked_sp_tree_remove(struct csched_ops *handle, struct cn_tree *tree, bool cancel)
  39. {
  40. }
  41. static struct tbkt *
  42. mocked_sp_tbkt_maint_get(struct csched_ops *handle)
  43. {
  44. return 0;
  45. }
  46. static void
  47. mocked_sp_throttle_sensor(struct csched_ops *handle, struct throttle_sensor *ts)
  48. {
  49. }
  50. static void
  51. mocked_sp_compact_request(struct csched_ops *handle, int flags)
  52. {
  53. }
  54. static void
  55. mocked_sp_compact_status(struct csched_ops *handle, struct hse_kvdb_compact_status *status)
  56. {
  57. }
  58. static merr_t
  59. mocked_sp_create2(
  60. struct kvdb_rparams *rp,
  61. const char * mp,
  62. struct kvdb_health * health,
  63. struct csched_ops ** handle)
  64. {
  65. struct csched_ops *self;
  66. if (mocked_sp_create_rc)
  67. return mocked_sp_create_rc;
  68. assert(handle);
  69. self = mapi_safe_malloc(sizeof(*self));
  70. if (!self)
  71. return merr(EBUG);
  72. memset(self, 0, sizeof(*self));
  73. self->cs_destroy = mocked_sp_destroy;
  74. *handle = self;
  75. return 0;
  76. }
  77. static merr_t
  78. mocked_sp_create(
  79. struct kvdb_rparams *rp,
  80. const char * mp,
  81. struct kvdb_health * health,
  82. struct csched_ops ** handle)
  83. {
  84. merr_t err;
  85. err = mocked_sp_create2(rp, mp, health, handle);
  86. if (!err) {
  87. (*handle)->cs_destroy = mocked_sp_destroy;
  88. (*handle)->cs_notify_ingest = mocked_sp_notify_ingest;
  89. (*handle)->cs_tree_add = mocked_sp_tree_add;
  90. (*handle)->cs_tree_remove = mocked_sp_tree_remove;
  91. (*handle)->cs_tbkt_maint_get = mocked_sp_tbkt_maint_get;
  92. (*handle)->cs_throttle_sensor = mocked_sp_throttle_sensor;
  93. (*handle)->cs_compact_request = mocked_sp_compact_request;
  94. (*handle)->cs_compact_status = mocked_sp_compact_status;
  95. }
  96. return err;
  97. }
  98. static merr_t
  99. mocked_sp3_create(
  100. struct mpool * ds,
  101. struct kvdb_rparams *rp,
  102. const char * mp,
  103. struct kvdb_health *health,
  104. struct csched_ops ** handle)
  105. {
  106. merr_t err;
  107. err = mocked_sp_create(rp, mp, health, handle);
  108. return err;
  109. }
  110. static int
  111. pre_collection(struct mtf_test_info *info)
  112. {
  113. struct mtf_test_coll_info *tci = info->ti_coll;
  114. int i;
  115. hse_log_set_verbose(true);
  116. hse_log_set_pri(HSE_DEBUG_VAL);
  117. /* To get max branch coverage, run once with
  118. * debug and once without.
  119. */
  120. for (i = 1; i < tci->tci_argc; i++) {
  121. if (!strcmp("debug", tci->tci_argv[i]))
  122. set_debug_rparam = 1;
  123. }
  124. return 0;
  125. }
  126. static void
  127. reset_rparams(void)
  128. {
  129. rparams = kvdb_rparams_defaults();
  130. rp = &rparams;
  131. if (set_debug_rparam)
  132. rp->csched_debug_mask = U64_MAX;
  133. }
  134. static int
  135. pre_test(struct mtf_test_info *ti)
  136. {
  137. mapi_inject(mapi_idx_sts_create, 0);
  138. mapi_inject(mapi_idx_sts_destroy, 0);
  139. mapi_inject(mapi_idx_sts_resume, 0);
  140. MOCK_SET_FN(csched_noop, sp_noop_create, mocked_sp_create);
  141. MOCK_SET_FN(csched_sp3, sp3_create, mocked_sp3_create);
  142. mp = "fred_flintstone";
  143. mocked_sp_create_rc = 0;
  144. reset_rparams();
  145. return 0;
  146. }
  147. MTF_BEGIN_UTEST_COLLECTION_PRE(test, pre_collection)
  148. MTF_DEFINE_UTEST_PRE(test, t_csched_create, pre_test)
  149. {
  150. struct csched *cs;
  151. merr_t err;
  152. uint i;
  153. for (i = 0; i < NELEM(policy_list); i++) {
  154. cs = (void *)0;
  155. err = csched_create(policy_list[i], NULL, rp, mp, &mock_health, &cs);
  156. ASSERT_EQ(err, 0);
  157. csched_destroy(cs);
  158. }
  159. /* invalid policy */
  160. cs = (void *)0;
  161. err = csched_create(12345, NULL, rp, mp, &mock_health, &cs);
  162. ASSERT_NE(err, 0);
  163. /* error paths */
  164. mocked_sp_create_rc = 1;
  165. for (i = 0; i < NELEM(policy_list); i++) {
  166. cs = (void *)0;
  167. err = csched_create(policy_list[i], NULL, rp, mp, &mock_health, &cs);
  168. /* mocked_sp_create_rc doesn't apply to csched_policy_old */
  169. if (policy_list[i] == csched_policy_old) {
  170. ASSERT_EQ(err, 0);
  171. csched_destroy(cs);
  172. } else {
  173. ASSERT_EQ(err, 1);
  174. }
  175. }
  176. }
  177. MTF_DEFINE_UTEST_PRE(test, t_csched_create_nomem, pre_test)
  178. {
  179. struct csched *cs;
  180. merr_t err;
  181. int rc;
  182. void run(struct mtf_test_info * lcl_ti, uint i, uint j)
  183. {
  184. err = csched_create(0, NULL, rp, mp, &mock_health, &cs);
  185. if (i == j)
  186. ASSERT_EQ(err, 0);
  187. else
  188. ASSERT_EQ(merr_errno(err), ENOMEM);
  189. }
  190. void clean(struct mtf_test_info * lcl_ti)
  191. {
  192. /* Note: parent function local vars are preserved from
  193. * previous call to run().
  194. */
  195. if (!err)
  196. csched_destroy(cs);
  197. }
  198. rc = mapi_alloc_tester(lcl_ti, run, clean);
  199. ASSERT_EQ(rc, 0);
  200. }
  201. MTF_DEFINE_UTEST_PRE(test, t_csched_methods, pre_test)
  202. {
  203. struct csched * cs;
  204. enum csched_policy pol;
  205. merr_t err;
  206. struct cn_tree * tree = (void *)1;
  207. /* we can mock any policy, choose csched_policy_noop */
  208. pol = csched_policy_noop;
  209. mapi_inject_unset(mapi_idx_sp_noop_create);
  210. MOCK_SET_FN(csched_noop, sp_noop_create, mocked_sp_create);
  211. cs = (void *)0;
  212. err = csched_create(pol, NULL, rp, mp, &mock_health, &cs);
  213. ASSERT_EQ(err, 0);
  214. ASSERT_TRUE(cs != NULL);
  215. int flags = 0;
  216. struct hse_kvdb_compact_status status;
  217. csched_tree_add(cs, tree);
  218. csched_throttle_sensor(cs, 0);
  219. csched_compact_request(cs, flags);
  220. csched_compact_status(cs, &status);
  221. csched_tbkt_maint_get(cs);
  222. csched_notify_ingest(cs, tree, 1234, 1234);
  223. csched_tree_remove(cs, tree, true);
  224. csched_destroy(cs);
  225. /* repeat with mock that has no ops */
  226. MOCK_SET_FN(csched_noop, sp_noop_create, mocked_sp_create2);
  227. cs = (void *)0;
  228. err = csched_create(pol, NULL, rp, mp, &mock_health, &cs);
  229. ASSERT_EQ(err, 0);
  230. ASSERT_TRUE(cs != NULL);
  231. csched_tree_add(cs, tree);
  232. csched_throttle_sensor(cs, 0);
  233. csched_compact_request(cs, flags);
  234. csched_compact_status(cs, &status);
  235. csched_tbkt_maint_get(cs);
  236. csched_notify_ingest(cs, tree, 1234, 1234);
  237. csched_tree_remove(cs, tree, true);
  238. csched_destroy(cs);
  239. /* check w/ null ptr */
  240. cs = 0;
  241. csched_tree_add(cs, tree);
  242. csched_throttle_sensor(cs, 0);
  243. csched_tbkt_maint_get(cs);
  244. csched_notify_ingest(cs, tree, 1234, 1234);
  245. csched_tree_remove(cs, tree, true);
  246. }
  247. MTF_END_UTEST_COLLECTION(test);