ioq_unreg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "test.h"
  20. #if INCLUDE_IOQUEUE_UNREG_TEST
  21. /*
  22. * This tests the thread safety of ioqueue unregistration operation.
  23. */
  24. #include <pj/errno.h>
  25. #include <pj/ioqueue.h>
  26. #include <pj/log.h>
  27. #include <pj/os.h>
  28. #include <pj/pool.h>
  29. #include <pj/sock.h>
  30. #include <pj/compat/socket.h>
  31. #include <pj/string.h>
  32. #define THIS_FILE "ioq_unreg.c"
  33. //#define TRACE(expr) PJ_LOG(3,expr)
  34. #define TRACE(expr)
  35. enum test_method
  36. {
  37. UNREGISTER_IN_APP,
  38. UNREGISTER_IN_CALLBACK,
  39. };
  40. static int thread_quitting;
  41. static enum test_method test_method;
  42. static pj_time_val time_to_unregister;
  43. struct sock_data
  44. {
  45. pj_sock_t sock;
  46. pj_sock_t csock;
  47. pj_pool_t *pool;
  48. pj_ioqueue_key_t *key;
  49. pj_mutex_t *mutex;
  50. pj_ioqueue_op_key_t *op_key;
  51. char *buffer;
  52. pj_size_t bufsize;
  53. pj_bool_t unregistered;
  54. pj_ssize_t received;
  55. } sock_data;
  56. static void on_read_complete(pj_ioqueue_key_t *key,
  57. pj_ioqueue_op_key_t *op_key,
  58. pj_ssize_t bytes_read)
  59. {
  60. pj_ssize_t size;
  61. char *sendbuf = "Hello world";
  62. pj_status_t status;
  63. TRACE((THIS_FILE, "......on_read_complete(): unregistered=%d, bytes=%d",
  64. sock_data.unregistered, bytes_read));
  65. if (sock_data.unregistered) {
  66. TRACE((THIS_FILE, "........bailing out"));
  67. return;
  68. }
  69. pj_mutex_lock(sock_data.mutex);
  70. if (sock_data.unregistered) {
  71. TRACE((THIS_FILE, "........bailing out"));
  72. pj_mutex_unlock(sock_data.mutex);
  73. return;
  74. }
  75. if (bytes_read < 0) {
  76. if (-bytes_read != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL))
  77. app_perror("ioqueue reported recv error", (pj_status_t)-bytes_read);
  78. } else {
  79. sock_data.received += bytes_read;
  80. }
  81. if (test_method == UNREGISTER_IN_CALLBACK) {
  82. pj_time_val now;
  83. pj_gettimeofday(&now);
  84. if (PJ_TIME_VAL_GTE(now, time_to_unregister)) {
  85. sock_data.unregistered = 1;
  86. TRACE((THIS_FILE, "......on_read_complete(): unregistering"));
  87. pj_ioqueue_unregister(key);
  88. pj_mutex_unlock(sock_data.mutex);
  89. return;
  90. }
  91. }
  92. do {
  93. size = sock_data.bufsize;
  94. status = pj_ioqueue_recv(key, op_key, sock_data.buffer, &size, 0);
  95. TRACE((THIS_FILE, "........recv, status=%d", status));
  96. if (status != PJ_EPENDING && status != PJ_SUCCESS)
  97. app_perror("recv() error", status);
  98. } while (status == PJ_SUCCESS);
  99. pj_mutex_unlock(sock_data.mutex);
  100. size = pj_ansi_strlen(sendbuf);
  101. status = pj_sock_send(sock_data.csock, sendbuf, &size, 0);
  102. if (status != PJ_SUCCESS)
  103. app_perror("send() error", status);
  104. size = pj_ansi_strlen(sendbuf);
  105. status = pj_sock_send(sock_data.csock, sendbuf, &size, 0);
  106. if (status != PJ_SUCCESS)
  107. app_perror("send() error", status);
  108. TRACE((THIS_FILE, "........done"));
  109. }
  110. static int worker_thread(void *arg)
  111. {
  112. pj_ioqueue_t *ioqueue = (pj_ioqueue_t*) arg;
  113. while (!thread_quitting) {
  114. pj_time_val timeout = { 0, 200 };
  115. pj_ioqueue_poll(ioqueue, &timeout);
  116. }
  117. return 0;
  118. }
  119. /*
  120. * Perform unregistration test.
  121. *
  122. * This will create ioqueue and register a server socket. Depending
  123. * on the test method, either the callback or the main thread will
  124. * unregister and destroy the server socket after some period of time.
  125. */
  126. static int perform_unreg_test(pj_ioqueue_t *ioqueue,
  127. pj_pool_t *test_pool,
  128. const char *title,
  129. pj_bool_t other_socket)
  130. {
  131. enum { WORKER_CNT = 1, MSEC = 500, QUIT_MSEC = 500 };
  132. int i;
  133. pj_thread_t *thread[WORKER_CNT];
  134. struct sock_data osd;
  135. pj_ioqueue_callback callback;
  136. pj_time_val end_time;
  137. pj_status_t status;
  138. /* Sometimes its important to have other sockets registered to
  139. * the ioqueue, because when no sockets are registered, the ioqueue
  140. * will return from the poll early.
  141. */
  142. if (other_socket) {
  143. status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, 56127, &osd.sock);
  144. if (status != PJ_SUCCESS) {
  145. app_perror("Error creating other socket", status);
  146. return -12;
  147. }
  148. pj_bzero(&callback, sizeof(callback));
  149. status = pj_ioqueue_register_sock(test_pool, ioqueue, osd.sock,
  150. NULL, &callback, &osd.key);
  151. if (status != PJ_SUCCESS) {
  152. app_perror("Error registering other socket", status);
  153. return -13;
  154. }
  155. } else {
  156. osd.key = NULL;
  157. osd.sock = PJ_INVALID_SOCKET;
  158. }
  159. /* Init both time duration of testing */
  160. thread_quitting = 0;
  161. pj_gettimeofday(&time_to_unregister);
  162. time_to_unregister.msec += MSEC;
  163. pj_time_val_normalize(&time_to_unregister);
  164. end_time = time_to_unregister;
  165. end_time.msec += QUIT_MSEC;
  166. pj_time_val_normalize(&end_time);
  167. /* Create polling thread */
  168. for (i=0; i<WORKER_CNT; ++i) {
  169. status = pj_thread_create(test_pool, "unregtest", &worker_thread,
  170. ioqueue, 0, 0, &thread[i]);
  171. if (status != PJ_SUCCESS) {
  172. app_perror("Error creating thread", status);
  173. return -20;
  174. }
  175. }
  176. /* Create pair of client/server sockets */
  177. status = app_socketpair(pj_AF_INET(), pj_SOCK_DGRAM(), 0,
  178. &sock_data.sock, &sock_data.csock);
  179. if (status != PJ_SUCCESS) {
  180. app_perror("app_socketpair error", status);
  181. return -30;
  182. }
  183. /* Initialize test data */
  184. sock_data.pool = pj_pool_create(mem, "sd", 1000, 1000, NULL);
  185. sock_data.buffer = (char*) pj_pool_alloc(sock_data.pool, 128);
  186. sock_data.bufsize = 128;
  187. sock_data.op_key = (pj_ioqueue_op_key_t*)
  188. pj_pool_alloc(sock_data.pool,
  189. sizeof(*sock_data.op_key));
  190. sock_data.received = 0;
  191. sock_data.unregistered = 0;
  192. pj_ioqueue_op_key_init(sock_data.op_key, sizeof(*sock_data.op_key));
  193. status = pj_mutex_create_simple(sock_data.pool, "sd", &sock_data.mutex);
  194. if (status != PJ_SUCCESS) {
  195. app_perror("create_mutex() error", status);
  196. return -35;
  197. }
  198. /* Register socket to ioqueue */
  199. pj_bzero(&callback, sizeof(callback));
  200. callback.on_read_complete = &on_read_complete;
  201. status = pj_ioqueue_register_sock(sock_data.pool, ioqueue, sock_data.sock,
  202. NULL, &callback, &sock_data.key);
  203. if (status != PJ_SUCCESS) {
  204. app_perror("pj_ioqueue_register error", status);
  205. return -40;
  206. }
  207. /* Bootstrap the first send/receive */
  208. on_read_complete(sock_data.key, sock_data.op_key, 0);
  209. /* Loop until test time ends */
  210. for (;;) {
  211. pj_time_val now, timeout;
  212. int n;
  213. pj_gettimeofday(&now);
  214. if (test_method == UNREGISTER_IN_APP &&
  215. PJ_TIME_VAL_GTE(now, time_to_unregister) &&
  216. !sock_data.unregistered)
  217. {
  218. sock_data.unregistered = 1;
  219. TRACE((THIS_FILE, "......main: unregistering"));
  220. /* Wait (as much as possible) for callback to complete */
  221. pj_mutex_lock(sock_data.mutex);
  222. pj_mutex_unlock(sock_data.mutex);
  223. pj_ioqueue_unregister(sock_data.key);
  224. }
  225. if (PJ_TIME_VAL_GT(now, end_time) && sock_data.unregistered)
  226. break;
  227. timeout.sec = 0; timeout.msec = 200;
  228. n = pj_ioqueue_poll(ioqueue, &timeout);
  229. if (n < 0) {
  230. app_perror("pj_ioqueue_poll error", -n);
  231. pj_thread_sleep(1);
  232. }
  233. }
  234. thread_quitting = 1;
  235. for (i=0; i<WORKER_CNT; ++i) {
  236. pj_thread_join(thread[i]);
  237. pj_thread_destroy(thread[i]);
  238. }
  239. /* Destroy data */
  240. pj_mutex_destroy(sock_data.mutex);
  241. pj_pool_release(sock_data.pool);
  242. sock_data.pool = NULL;
  243. if (other_socket) {
  244. pj_ioqueue_unregister(osd.key);
  245. }
  246. pj_sock_close(sock_data.csock);
  247. PJ_LOG(3,(THIS_FILE, "....%s: done (%ld KB/s)",
  248. title, sock_data.received * 1000 / MSEC / 1000));
  249. return 0;
  250. }
  251. static int udp_ioqueue_unreg_test_imp(pj_bool_t allow_concur)
  252. {
  253. enum { LOOP = 10 };
  254. int i, rc;
  255. char title[30];
  256. pj_ioqueue_t *ioqueue;
  257. pj_pool_t *test_pool;
  258. PJ_LOG(3,(THIS_FILE, "..testing with concurency=%d", allow_concur));
  259. test_method = UNREGISTER_IN_APP;
  260. test_pool = pj_pool_create(mem, "unregtest", 4000, 4000, NULL);
  261. rc = pj_ioqueue_create(test_pool, 16, &ioqueue);
  262. if (rc != PJ_SUCCESS) {
  263. app_perror("Error creating ioqueue", rc);
  264. return -10;
  265. }
  266. rc = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur);
  267. if (rc != PJ_SUCCESS) {
  268. app_perror("Error in pj_ioqueue_set_default_concurrency()", rc);
  269. return -12;
  270. }
  271. PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 0/3, unregister in app (%s)",
  272. pj_ioqueue_name()));
  273. for (i=0; i<LOOP; ++i) {
  274. pj_ansi_snprintf(title, sizeof(title), "repeat %d/%d", i, LOOP);
  275. rc = perform_unreg_test(ioqueue, test_pool, title, 0);
  276. if (rc != 0)
  277. return rc;
  278. }
  279. PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 1/3, unregister in app (%s)",
  280. pj_ioqueue_name()));
  281. for (i=0; i<LOOP; ++i) {
  282. pj_ansi_snprintf(title, sizeof(title), "repeat %d/%d", i, LOOP);
  283. rc = perform_unreg_test(ioqueue, test_pool, title, 1);
  284. if (rc != 0)
  285. return rc;
  286. }
  287. test_method = UNREGISTER_IN_CALLBACK;
  288. PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 2/3, unregister in cb (%s)",
  289. pj_ioqueue_name()));
  290. for (i=0; i<LOOP; ++i) {
  291. pj_ansi_snprintf(title, sizeof(title), "repeat %d/%d", i, LOOP);
  292. rc = perform_unreg_test(ioqueue, test_pool, title, 0);
  293. if (rc != 0)
  294. return rc;
  295. }
  296. PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 3/3, unregister in cb (%s)",
  297. pj_ioqueue_name()));
  298. for (i=0; i<LOOP; ++i) {
  299. pj_ansi_snprintf(title, sizeof(title), "repeat %d/%d", i, LOOP);
  300. rc = perform_unreg_test(ioqueue, test_pool, title, 1);
  301. if (rc != 0)
  302. return rc;
  303. }
  304. pj_ioqueue_destroy(ioqueue);
  305. pj_pool_release(test_pool);
  306. return 0;
  307. }
  308. int udp_ioqueue_unreg_test(void)
  309. {
  310. int rc;
  311. rc = udp_ioqueue_unreg_test_imp(PJ_TRUE);
  312. if (rc != 0)
  313. return rc;
  314. rc = udp_ioqueue_unreg_test_imp(PJ_FALSE);
  315. if (rc != 0)
  316. return rc;
  317. return 0;
  318. }
  319. #else
  320. /* To prevent warning about "translation unit is empty"
  321. * when this test is disabled.
  322. */
  323. int dummy_uiq_unreg;
  324. #endif /* INCLUDE_IOQUEUE_UNREG_TEST */