check_nodestore.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include <open62541/plugin/nodestore_default.h>
  5. #include <open62541/types.h>
  6. #include <open62541/util.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include "check.h"
  11. #include "ziptree.h"
  12. /* container_of */
  13. #define container_of(ptr, type, member) \
  14. (type *)((uintptr_t)ptr - offsetof(type,member))
  15. #ifdef UA_ENABLE_MULTITHREADING
  16. #include <pthread.h>
  17. #endif
  18. /* Dirty redifinition from ua_nodestore_default.c to check that all nodes were
  19. * released */
  20. struct NodeEntry;
  21. typedef struct NodeEntry NodeEntry;
  22. struct NodeEntry {
  23. ZIP_ENTRY(NodeEntry) zipfields;
  24. UA_UInt32 nodeIdHash;
  25. UA_UInt16 refCount; /* How many consumers have a reference to the node? */
  26. UA_Boolean deleted; /* Node was marked as deleted and can be deleted when refCount == 0 */
  27. NodeEntry *orig; /* If a copy is made to replace a node, track that we
  28. * replace only the node from which the copy was made.
  29. * Important for concurrent operations. */
  30. UA_NodeId nodeId; /* This is actually a UA_Node that also starts with a NodeId */
  31. };
  32. static void checkAllReleased(void *context, const UA_Node* node) {
  33. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  34. ck_assert_int_eq(entry->refCount, 0); /* The count is increased when the visited node is checked out */
  35. }
  36. UA_Nodestore ns;
  37. static void setup(void) {
  38. UA_Nodestore_default_new(&ns);
  39. }
  40. static void teardown(void) {
  41. ns.iterate(ns.context, NULL, checkAllReleased);
  42. ns.deleteNodestore(ns.context);
  43. }
  44. static int zeroCnt = 0;
  45. static int visitCnt = 0;
  46. static void checkZeroVisitor(void *context, const UA_Node* node) {
  47. visitCnt++;
  48. if (node == NULL) zeroCnt++;
  49. }
  50. static UA_Node* createNode(UA_Int16 nsid, UA_Int32 id) {
  51. UA_Node *p = ns.newNode(ns.context, UA_NODECLASS_VARIABLE);
  52. p->nodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  53. p->nodeId.namespaceIndex = nsid;
  54. p->nodeId.identifier.numeric = id;
  55. p->nodeClass = UA_NODECLASS_VARIABLE;
  56. return p;
  57. }
  58. START_TEST(replaceExistingNode) {
  59. UA_Node* n1 = createNode(0,2253);
  60. ns.insertNode(ns.context, n1, NULL);
  61. UA_NodeId in1 = UA_NODEID_NUMERIC(0, 2253);
  62. UA_Node* n2;
  63. ns.getNodeCopy(ns.context, &in1, &n2);
  64. UA_StatusCode retval = ns.replaceNode(ns.context, n2);
  65. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  66. }
  67. END_TEST
  68. START_TEST(replaceOldNode) {
  69. UA_Node* n1 = createNode(0,2253);
  70. ns.insertNode(ns.context, n1, NULL);
  71. UA_NodeId in1 = UA_NODEID_NUMERIC(0,2253);
  72. UA_Node* n2;
  73. UA_Node* n3;
  74. ns.getNodeCopy(ns.context, &in1, &n2);
  75. ns.getNodeCopy(ns.context, &in1, &n3);
  76. /* shall succeed */
  77. UA_StatusCode retval = ns.replaceNode(ns.context, n2);
  78. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  79. /* shall fail */
  80. retval = ns.replaceNode(ns.context, n3);
  81. ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
  82. }
  83. END_TEST
  84. START_TEST(findNodeInUA_NodeStoreWithSingleEntry) {
  85. UA_Node* n1 = createNode(0,2253);
  86. ns.insertNode(ns.context, n1, NULL);
  87. UA_NodeId in1 = UA_NODEID_NUMERIC(0,2253);
  88. const UA_Node* nr = ns.getNode(ns.context, &in1);
  89. ck_assert_int_eq((uintptr_t)n1, (uintptr_t)nr);
  90. ns.releaseNode(ns.context, nr);
  91. }
  92. END_TEST
  93. START_TEST(failToFindNodeInOtherUA_NodeStore) {
  94. UA_Node* n1 = createNode(0,2255);
  95. ns.insertNode(ns.context, n1, NULL);
  96. UA_NodeId in1 = UA_NODEID_NUMERIC(1, 2255);
  97. const UA_Node* nr = ns.getNode(ns.context, &in1);
  98. ck_assert_int_eq((uintptr_t)nr, 0);
  99. }
  100. END_TEST
  101. START_TEST(findNodeInUA_NodeStoreWithSeveralEntries) {
  102. UA_Node* n1 = createNode(0,2253);
  103. ns.insertNode(ns.context, n1, NULL);
  104. UA_Node* n2 = createNode(0,2255);
  105. ns.insertNode(ns.context, n2, NULL);
  106. UA_Node* n3 = createNode(0,2257);
  107. ns.insertNode(ns.context, n3, NULL);
  108. UA_Node* n4 = createNode(0,2200);
  109. ns.insertNode(ns.context, n4, NULL);
  110. UA_Node* n5 = createNode(0,1);
  111. ns.insertNode(ns.context, n5, NULL);
  112. UA_Node* n6 = createNode(0,12);
  113. ns.insertNode(ns.context, n6, NULL);
  114. UA_NodeId in3 = UA_NODEID_NUMERIC(0, 2257);
  115. const UA_Node* nr = ns.getNode(ns.context, &in3);
  116. ck_assert_int_eq((uintptr_t)nr, (uintptr_t)n3);
  117. ns.releaseNode(ns.context, nr);
  118. }
  119. END_TEST
  120. START_TEST(iterateOverUA_NodeStoreShallNotVisitEmptyNodes) {
  121. UA_Node* n1 = createNode(0,2253);
  122. ns.insertNode(ns.context, n1, NULL);
  123. UA_Node* n2 = createNode(0,2255);
  124. ns.insertNode(ns.context, n2, NULL);
  125. UA_Node* n3 = createNode(0,2257);
  126. ns.insertNode(ns.context, n3, NULL);
  127. UA_Node* n4 = createNode(0,2200);
  128. ns.insertNode(ns.context, n4, NULL);
  129. UA_Node* n5 = createNode(0,1);
  130. ns.insertNode(ns.context, n5, NULL);
  131. UA_Node* n6 = createNode(0,12);
  132. ns.insertNode(ns.context, n6, NULL);
  133. zeroCnt = 0;
  134. visitCnt = 0;
  135. ns.iterate(ns.context, NULL, checkZeroVisitor);
  136. ck_assert_int_eq(zeroCnt, 0);
  137. ck_assert_int_eq(visitCnt, 6);
  138. }
  139. END_TEST
  140. START_TEST(findNodeInExpandedNamespace) {
  141. for(UA_UInt32 i = 0; i < 200; i++) {
  142. UA_Node* n = createNode(0,i);
  143. ns.insertNode(ns.context, n, NULL);
  144. }
  145. // when
  146. UA_Node *n2 = createNode(0,25);
  147. const UA_Node* nr = ns.getNode(ns.context, &n2->nodeId);
  148. ck_assert_int_eq(nr->nodeId.identifier.numeric, n2->nodeId.identifier.numeric);
  149. ns.releaseNode(ns.context, nr);
  150. ns.deleteNode(ns.context, n2);
  151. }
  152. END_TEST
  153. START_TEST(iterateOverExpandedNamespaceShallNotVisitEmptyNodes) {
  154. for(UA_UInt32 i = 0; i < 200; i++) {
  155. UA_Node* n = createNode(0,i+1);
  156. ns.insertNode(ns.context, n, NULL);
  157. }
  158. // when
  159. zeroCnt = 0;
  160. visitCnt = 0;
  161. ns.iterate(ns.context, NULL, checkZeroVisitor);
  162. // then
  163. ck_assert_int_eq(zeroCnt, 0);
  164. ck_assert_int_eq(visitCnt, 200);
  165. }
  166. END_TEST
  167. START_TEST(failToFindNonExistentNodeInUA_NodeStoreWithSeveralEntries) {
  168. UA_Node* n1 = createNode(0,2253);
  169. ns.insertNode(ns.context, n1, NULL);
  170. UA_Node* n2 = createNode(0,2255);
  171. ns.insertNode(ns.context, n2, NULL);
  172. UA_Node* n3 = createNode(0,2257);
  173. ns.insertNode(ns.context, n3, NULL);
  174. UA_Node* n4 = createNode(0,2200);
  175. ns.insertNode(ns.context, n4, NULL);
  176. UA_Node* n5 = createNode(0,1);
  177. ns.insertNode(ns.context, n5, NULL);
  178. UA_NodeId id = UA_NODEID_NUMERIC(0, 12);
  179. const UA_Node* nr = ns.getNode(ns.context, &id);
  180. ck_assert_int_eq((uintptr_t)nr, 0);
  181. }
  182. END_TEST
  183. /************************************/
  184. /* Performance Profiling Test Cases */
  185. /************************************/
  186. #ifdef UA_ENABLE_MULTITHREADING
  187. struct UA_NodeStoreProfileTest {
  188. UA_Int32 min_val;
  189. UA_Int32 max_val;
  190. UA_Int32 rounds;
  191. };
  192. static void *profileGetThread(void *arg) {
  193. struct UA_NodeStoreProfileTest *test = (struct UA_NodeStoreProfileTest*) arg;
  194. UA_NodeId id;
  195. UA_NodeId_init(&id);
  196. UA_Int32 max_val = test->max_val;
  197. for(UA_Int32 x = 0; x<test->rounds; x++) {
  198. for(UA_Int32 i=test->min_val; i<max_val; i++) {
  199. id.identifier.numeric = i+1;
  200. const UA_Node *n = ns.getNode(ns.context, &id);
  201. ns.releaseNode(ns.context, n);
  202. }
  203. }
  204. return NULL;
  205. }
  206. #endif
  207. #define N 1000 /* make bigger to test */
  208. START_TEST(profileGetDelete) {
  209. clock_t begin, end;
  210. begin = clock();
  211. for(UA_UInt32 i = 0; i < N; i++) {
  212. UA_Node *n = createNode(0,i+1);
  213. ns.insertNode(ns.context, n, NULL);
  214. }
  215. #ifdef UA_ENABLE_MULTITHREADING
  216. #define THREADS 4
  217. pthread_t t[THREADS];
  218. struct UA_NodeStoreProfileTest p[THREADS];
  219. for (int i = 0; i < THREADS; i++) {
  220. p[i] = (struct UA_NodeStoreProfileTest){i*(N/THREADS), (i+1)*(N/THREADS), 50};
  221. pthread_create(&t[i], NULL, profileGetThread, &p[i]);
  222. }
  223. for (int i = 0; i < THREADS; i++)
  224. pthread_join(t[i], NULL);
  225. end = clock();
  226. printf("Time for %d create/get/delete on %d threads in a namespace: %fs.\n", N, THREADS, (double)(end - begin) / CLOCKS_PER_SEC);
  227. #else
  228. UA_NodeId id = UA_NODEID_NULL;
  229. for(size_t i = 0; i < N; i++) {
  230. id.identifier.numeric = (UA_UInt32)i+1;
  231. const UA_Node *node = ns.getNode(ns.context, &id);
  232. ns.releaseNode(ns.context, node);
  233. }
  234. end = clock();
  235. printf("Time for single-threaded %d create/get/delete in a namespace: %fs.\n", N,
  236. (double)(end - begin) / CLOCKS_PER_SEC);
  237. #endif
  238. }
  239. END_TEST
  240. static Suite * namespace_suite (void) {
  241. Suite *s = suite_create ("UA_NodeStore");
  242. TCase* tc_find = tcase_create ("Find");
  243. tcase_add_checked_fixture(tc_find, setup, teardown);
  244. tcase_add_test (tc_find, findNodeInUA_NodeStoreWithSingleEntry);
  245. tcase_add_test (tc_find, findNodeInUA_NodeStoreWithSeveralEntries);
  246. tcase_add_test (tc_find, findNodeInExpandedNamespace);
  247. tcase_add_test (tc_find, failToFindNonExistentNodeInUA_NodeStoreWithSeveralEntries);
  248. tcase_add_test (tc_find, failToFindNodeInOtherUA_NodeStore);
  249. suite_add_tcase (s, tc_find);
  250. TCase *tc_replace = tcase_create("Replace");
  251. tcase_add_checked_fixture(tc_replace, setup, teardown);
  252. tcase_add_test (tc_replace, replaceExistingNode);
  253. tcase_add_test (tc_replace, replaceOldNode);
  254. suite_add_tcase (s, tc_replace);
  255. TCase* tc_iterate = tcase_create ("Iterate");
  256. tcase_add_checked_fixture(tc_iterate, setup, teardown);
  257. tcase_add_test (tc_iterate, iterateOverUA_NodeStoreShallNotVisitEmptyNodes);
  258. tcase_add_test (tc_iterate, iterateOverExpandedNamespaceShallNotVisitEmptyNodes);
  259. suite_add_tcase (s, tc_iterate);
  260. TCase* tc_profile = tcase_create ("Profile");
  261. tcase_add_checked_fixture(tc_profile, setup, teardown);
  262. tcase_add_test (tc_profile, profileGetDelete);
  263. suite_add_tcase (s, tc_profile);
  264. return s;
  265. }
  266. int main (void) {
  267. int number_failed = 0;
  268. Suite *s = namespace_suite();
  269. SRunner *sr = srunner_create(s);
  270. srunner_set_fork_status(sr,CK_NOFORK);
  271. srunner_run_all(sr, CK_NORMAL);
  272. number_failed += srunner_ntests_failed (sr);
  273. srunner_free(sr);
  274. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  275. }