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