test_ctx_options.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <limits>
  25. #include "testutil.hpp"
  26. #include "testutil_unity.hpp"
  27. SETUP_TEARDOWN_TESTCONTEXT
  28. #define WAIT_FOR_BACKGROUND_THREAD_INSPECTION (0)
  29. #ifdef ZMQ_HAVE_LINUX
  30. #include <sys/time.h>
  31. #include <sys/resource.h>
  32. #include <unistd.h> // for sleep()
  33. #include <sched.h>
  34. #define TEST_POLICY \
  35. (SCHED_OTHER) // NOTE: SCHED_OTHER is the default Linux scheduler
  36. bool is_allowed_to_raise_priority ()
  37. {
  38. // NOTE1: if setrlimit() fails with EPERM, this means that current user has not enough permissions.
  39. // NOTE2: even for privileged users (e.g., root) getrlimit() would usually return 0 as nice limit; the only way to
  40. // discover if the user is able to increase the nice value is to actually try to change the rlimit:
  41. struct rlimit rlim;
  42. rlim.rlim_cur = 40;
  43. rlim.rlim_max = 40;
  44. if (setrlimit (RLIMIT_NICE, &rlim) == 0) {
  45. // rlim_cur == 40 means that this process is allowed to set a nice value of -20
  46. if (WAIT_FOR_BACKGROUND_THREAD_INSPECTION)
  47. printf ("This process has enough permissions to raise ZMQ "
  48. "background thread priority!\n");
  49. return true;
  50. }
  51. if (WAIT_FOR_BACKGROUND_THREAD_INSPECTION)
  52. printf ("This process has NOT enough permissions to raise ZMQ "
  53. "background thread priority.\n");
  54. return false;
  55. }
  56. #else
  57. #define TEST_POLICY (0)
  58. bool is_allowed_to_raise_priority ()
  59. {
  60. return false;
  61. }
  62. #endif
  63. void test_ctx_thread_opts ()
  64. {
  65. // verify that setting negative values (e.g., default values) fail:
  66. TEST_ASSERT_FAILURE_ERRNO (
  67. EINVAL, zmq_ctx_set (get_test_context (), ZMQ_THREAD_SCHED_POLICY,
  68. ZMQ_THREAD_SCHED_POLICY_DFLT));
  69. TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_ctx_set (get_test_context (),
  70. ZMQ_THREAD_PRIORITY,
  71. ZMQ_THREAD_PRIORITY_DFLT));
  72. // test scheduling policy:
  73. // set context options that alter the background thread CPU scheduling/affinity settings;
  74. // as of ZMQ 4.2.3 this has an effect only on POSIX systems (nothing happens on Windows, but still it should return success):
  75. TEST_ASSERT_SUCCESS_ERRNO (
  76. zmq_ctx_set (get_test_context (), ZMQ_THREAD_SCHED_POLICY, TEST_POLICY));
  77. TEST_ASSERT_EQUAL_INT (
  78. TEST_POLICY, zmq_ctx_get (get_test_context (), ZMQ_THREAD_SCHED_POLICY));
  79. // test priority:
  80. // in theory SCHED_OTHER supports only the static priority 0 but quoting the docs
  81. // http://man7.org/linux/man-pages/man7/sched.7.html
  82. // "The thread to run is chosen from the static priority 0 list based on
  83. // a dynamic priority that is determined only inside this list. The
  84. // dynamic priority is based on the nice value [...]
  85. // The nice value can be modified using nice(2), setpriority(2), or sched_setattr(2)."
  86. // ZMQ will internally use nice(2) to set the nice value when using SCHED_OTHER.
  87. // However changing the nice value of a process requires appropriate permissions...
  88. // check that the current effective user is able to do that:
  89. if (is_allowed_to_raise_priority ()) {
  90. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_set (
  91. get_test_context (), ZMQ_THREAD_PRIORITY,
  92. 1 /* any positive value different than the default will be ok */));
  93. }
  94. // test affinity:
  95. // this should result in background threads being placed only on the
  96. // first CPU available on this system; try experimenting with other values
  97. // (e.g., 5 to use CPU index 5) and use "top -H" or "taskset -pc" to see the result
  98. int cpus_add[] = {0, 1};
  99. for (unsigned int idx = 0; idx < sizeof (cpus_add) / sizeof (cpus_add[0]);
  100. idx++) {
  101. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_set (
  102. get_test_context (), ZMQ_THREAD_AFFINITY_CPU_ADD, cpus_add[idx]));
  103. }
  104. // you can also remove CPUs from list of affinities:
  105. int cpus_remove[] = {1};
  106. for (unsigned int idx = 0;
  107. idx < sizeof (cpus_remove) / sizeof (cpus_remove[0]); idx++) {
  108. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_set (get_test_context (),
  109. ZMQ_THREAD_AFFINITY_CPU_REMOVE,
  110. cpus_remove[idx]));
  111. }
  112. // test INTEGER thread name prefix:
  113. TEST_ASSERT_SUCCESS_ERRNO (
  114. zmq_ctx_set (get_test_context (), ZMQ_THREAD_NAME_PREFIX, 1234));
  115. TEST_ASSERT_EQUAL_INT (
  116. 1234, zmq_ctx_get (get_test_context (), ZMQ_THREAD_NAME_PREFIX));
  117. #ifdef ZMQ_BUILD_DRAFT_API
  118. // test STRING thread name prefix:
  119. const char prefix[] = "MyPrefix9012345"; // max len is 16 chars
  120. TEST_ASSERT_SUCCESS_ERRNO (
  121. zmq_ctx_set_ext (get_test_context (), ZMQ_THREAD_NAME_PREFIX, prefix,
  122. sizeof (prefix) / sizeof (char)));
  123. char buf[16];
  124. size_t buflen = sizeof (buf) / sizeof (char);
  125. zmq_ctx_get_ext (get_test_context (), ZMQ_THREAD_NAME_PREFIX, buf, &buflen);
  126. TEST_ASSERT_EQUAL_STRING (prefix, buf);
  127. #endif
  128. }
  129. void test_ctx_zero_copy ()
  130. {
  131. #ifdef ZMQ_ZERO_COPY_RECV
  132. int zero_copy;
  133. // Default value is 1.
  134. zero_copy = zmq_ctx_get (get_test_context (), ZMQ_ZERO_COPY_RECV);
  135. TEST_ASSERT_EQUAL_INT (1, zero_copy);
  136. // Test we can set it to 0.
  137. TEST_ASSERT_SUCCESS_ERRNO (
  138. zmq_ctx_set (get_test_context (), ZMQ_ZERO_COPY_RECV, 0));
  139. zero_copy = zmq_ctx_get (get_test_context (), ZMQ_ZERO_COPY_RECV);
  140. TEST_ASSERT_EQUAL_INT (0, zero_copy);
  141. // Create a TCP socket pair using the context and test that messages can be
  142. // received. Note that inproc sockets cannot be used for this test.
  143. void *pull = zmq_socket (get_test_context (), ZMQ_PULL);
  144. char endpoint[MAX_SOCKET_STRING];
  145. bind_loopback_ipv4 (pull, endpoint, sizeof endpoint);
  146. void *push = zmq_socket (get_test_context (), ZMQ_PUSH);
  147. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (push, endpoint));
  148. const char *small_str = "abcd";
  149. const char *large_str =
  150. "01234567890123456789012345678901234567890123456789";
  151. send_string_expect_success (push, small_str, 0);
  152. send_string_expect_success (push, large_str, 0);
  153. recv_string_expect_success (pull, small_str, 0);
  154. recv_string_expect_success (pull, large_str, 0);
  155. // Clean up.
  156. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (push));
  157. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (pull));
  158. TEST_ASSERT_SUCCESS_ERRNO (
  159. zmq_ctx_set (get_test_context (), ZMQ_ZERO_COPY_RECV, 1));
  160. TEST_ASSERT_EQUAL_INT (
  161. 1, zmq_ctx_get (get_test_context (), ZMQ_ZERO_COPY_RECV));
  162. #endif
  163. }
  164. void test_ctx_option_max_sockets ()
  165. {
  166. TEST_ASSERT_EQUAL_INT (ZMQ_MAX_SOCKETS_DFLT,
  167. zmq_ctx_get (get_test_context (), ZMQ_MAX_SOCKETS));
  168. }
  169. void test_ctx_option_socket_limit ()
  170. {
  171. #if defined(ZMQ_USE_SELECT)
  172. TEST_ASSERT_EQUAL_INT (FD_SETSIZE - 1, zmq_ctx_get (ctx, ZMQ_SOCKET_LIMIT));
  173. #elif defined(ZMQ_USE_POLL) || defined(ZMQ_USE_EPOLL) \
  174. || defined(ZMQ_USE_DEVPOLL) || defined(ZMQ_USE_KQUEUE)
  175. TEST_ASSERT_EQUAL_INT (65535, zmq_ctx_get (ctx, ZMQ_SOCKET_LIMIT));
  176. #endif
  177. }
  178. void test_ctx_option_io_threads ()
  179. {
  180. TEST_ASSERT_EQUAL_INT (ZMQ_IO_THREADS_DFLT,
  181. zmq_ctx_get (get_test_context (), ZMQ_IO_THREADS));
  182. }
  183. void test_ctx_option_ipv6 ()
  184. {
  185. TEST_ASSERT_EQUAL_INT (0, zmq_ctx_get (get_test_context (), ZMQ_IPV6));
  186. }
  187. void test_ctx_option_msg_t_size ()
  188. {
  189. #if defined(ZMQ_MSG_T_SIZE)
  190. TEST_ASSERT_EQUAL_INT (sizeof (zmq_msg_t),
  191. zmq_ctx_get (get_test_context (), ZMQ_MSG_T_SIZE));
  192. #endif
  193. }
  194. void test_ctx_option_ipv6_set ()
  195. {
  196. TEST_ASSERT_SUCCESS_ERRNO (
  197. zmq_ctx_set (get_test_context (), ZMQ_IPV6, true));
  198. TEST_ASSERT_EQUAL_INT (1, zmq_ctx_get (get_test_context (), ZMQ_IPV6));
  199. }
  200. void test_ctx_option_blocky ()
  201. {
  202. TEST_ASSERT_SUCCESS_ERRNO (
  203. zmq_ctx_set (get_test_context (), ZMQ_IPV6, true));
  204. void *router = test_context_socket (ZMQ_ROUTER);
  205. int value;
  206. size_t optsize = sizeof (int);
  207. TEST_ASSERT_SUCCESS_ERRNO (
  208. zmq_getsockopt (router, ZMQ_IPV6, &value, &optsize));
  209. TEST_ASSERT_EQUAL_INT (1, value);
  210. TEST_ASSERT_SUCCESS_ERRNO (
  211. zmq_getsockopt (router, ZMQ_LINGER, &value, &optsize));
  212. TEST_ASSERT_EQUAL_INT (-1, value);
  213. test_context_socket_close (router);
  214. #if WAIT_FOR_BACKGROUND_THREAD_INSPECTION
  215. // this is useful when you want to use an external tool (like top or taskset) to view
  216. // properties of the background threads
  217. printf ("Sleeping for 100sec. You can now use 'top -H -p $(pgrep -f "
  218. "test_ctx_options)' and 'taskset -pc <ZMQ background thread PID>' "
  219. "to view ZMQ background thread properties.\n");
  220. sleep (100);
  221. #endif
  222. TEST_ASSERT_SUCCESS_ERRNO (
  223. zmq_ctx_set (get_test_context (), ZMQ_BLOCKY, false));
  224. TEST_ASSERT_EQUAL_INT (0, TEST_ASSERT_SUCCESS_ERRNO ((zmq_ctx_get (
  225. get_test_context (), ZMQ_BLOCKY))));
  226. router = test_context_socket (ZMQ_ROUTER);
  227. TEST_ASSERT_SUCCESS_ERRNO (
  228. zmq_getsockopt (router, ZMQ_LINGER, &value, &optsize));
  229. TEST_ASSERT_EQUAL_INT (0, value);
  230. test_context_socket_close (router);
  231. }
  232. int main (void)
  233. {
  234. setup_test_environment ();
  235. UNITY_BEGIN ();
  236. RUN_TEST (test_ctx_option_max_sockets);
  237. RUN_TEST (test_ctx_option_socket_limit);
  238. RUN_TEST (test_ctx_option_io_threads);
  239. RUN_TEST (test_ctx_option_ipv6);
  240. RUN_TEST (test_ctx_option_msg_t_size);
  241. RUN_TEST (test_ctx_option_ipv6_set);
  242. RUN_TEST (test_ctx_thread_opts);
  243. RUN_TEST (test_ctx_zero_copy);
  244. RUN_TEST (test_ctx_option_blocky);
  245. return UNITY_END ();
  246. }