tst-default-attr.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* Verify that pthread_[gs]etattr_default_np work correctly.
  2. Copyright (C) 2013-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <pthread.h>
  16. #include <stdio.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <stdbool.h>
  22. #define RETURN_IF_FAIL(f, ...) \
  23. ({ \
  24. int ret = f (__VA_ARGS__); \
  25. if (ret != 0) \
  26. { \
  27. printf ("%s:%d: %s returned %d (errno = %d)\n", __FILE__, __LINE__, \
  28. #f, ret, errno); \
  29. return ret; \
  30. } \
  31. })
  32. static int (*verify_result) (pthread_attr_t *);
  33. static size_t stacksize = 1024 * 1024;
  34. static size_t guardsize;
  35. static bool do_join = true;
  36. static int running = 0;
  37. static int detach_failed = 0;
  38. static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  39. static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
  40. static void *
  41. thr (void *unused __attribute__ ((unused)))
  42. {
  43. pthread_attr_t attr;
  44. int ret;
  45. memset (&attr, 0xab, sizeof attr);
  46. /* To verify that the pthread_setattr_default_np worked. */
  47. if ((ret = pthread_getattr_default_np (&attr)) != 0)
  48. {
  49. printf ("pthread_getattr_default_np failed: %s\n", strerror (ret));
  50. goto out;
  51. }
  52. if ((ret = (*verify_result) (&attr)) != 0)
  53. goto out;
  54. memset (&attr, 0xab, sizeof attr);
  55. /* To verify that the attributes actually got applied. */
  56. if ((ret = pthread_getattr_np (pthread_self (), &attr)) != 0)
  57. {
  58. printf ("pthread_getattr_default_np failed: %s\n", strerror (ret));
  59. goto out;
  60. }
  61. ret = (*verify_result) (&attr);
  62. out:
  63. if (!do_join)
  64. {
  65. pthread_mutex_lock (&m);
  66. running--;
  67. pthread_cond_signal (&c);
  68. pthread_mutex_unlock (&m);
  69. detach_failed |= ret;
  70. }
  71. return (void *) (uintptr_t) ret;
  72. }
  73. static int
  74. run_threads (const pthread_attr_t *attr)
  75. {
  76. pthread_t t;
  77. void *tret = NULL;
  78. RETURN_IF_FAIL (pthread_setattr_default_np, attr);
  79. /* Run twice to ensure that the attributes do not get overwritten in the
  80. first run somehow. */
  81. for (int i = 0; i < 2; i++)
  82. {
  83. RETURN_IF_FAIL (pthread_create, &t, NULL, thr, NULL);
  84. if (do_join)
  85. RETURN_IF_FAIL (pthread_join, t, &tret);
  86. else
  87. {
  88. pthread_mutex_lock (&m);
  89. running++;
  90. pthread_mutex_unlock (&m);
  91. }
  92. if (tret != NULL)
  93. {
  94. puts ("Thread failed");
  95. return 1;
  96. }
  97. }
  98. /* Stay in sync for detached threads and get their status. */
  99. while (!do_join)
  100. {
  101. pthread_mutex_lock (&m);
  102. if (running == 0)
  103. {
  104. pthread_mutex_unlock (&m);
  105. break;
  106. }
  107. pthread_cond_wait (&c, &m);
  108. pthread_mutex_unlock (&m);
  109. }
  110. return 0;
  111. }
  112. static int
  113. verify_detach_result (pthread_attr_t *attr)
  114. {
  115. int state;
  116. RETURN_IF_FAIL (pthread_attr_getdetachstate, attr, &state);
  117. if (state != PTHREAD_CREATE_DETACHED)
  118. {
  119. puts ("failed to set detach state");
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. static int
  125. do_detach_test (void)
  126. {
  127. pthread_attr_t attr;
  128. do_join = false;
  129. RETURN_IF_FAIL (pthread_attr_init, &attr);
  130. RETURN_IF_FAIL (pthread_attr_setdetachstate, &attr, PTHREAD_CREATE_DETACHED);
  131. RETURN_IF_FAIL (run_threads, &attr);
  132. return detach_failed;
  133. }
  134. static int
  135. verify_affinity_result (pthread_attr_t *attr)
  136. {
  137. cpu_set_t cpuset;
  138. RETURN_IF_FAIL (pthread_attr_getaffinity_np, attr, sizeof (cpuset), &cpuset);
  139. if (!CPU_ISSET (0, &cpuset))
  140. {
  141. puts ("failed to set cpu affinity");
  142. return 1;
  143. }
  144. return 0;
  145. }
  146. static int
  147. do_affinity_test (void)
  148. {
  149. pthread_attr_t attr;
  150. RETURN_IF_FAIL (pthread_attr_init, &attr);
  151. /* Processor affinity. Like scheduling policy, this could fail if the user
  152. does not have the necessary privileges. So we only spew a warning if
  153. pthread_create fails with EPERM. A computer has at least one CPU. */
  154. cpu_set_t cpuset;
  155. CPU_ZERO (&cpuset);
  156. CPU_SET (0, &cpuset);
  157. RETURN_IF_FAIL (pthread_attr_setaffinity_np, &attr, sizeof (cpuset), &cpuset);
  158. int ret = run_threads (&attr);
  159. if (ret == EPERM)
  160. {
  161. printf ("Skipping CPU Affinity test: %s\n", strerror (ret));
  162. return 0;
  163. }
  164. else if (ret != 0)
  165. return ret;
  166. return 0;
  167. }
  168. static int
  169. verify_sched_result (pthread_attr_t *attr)
  170. {
  171. int inherited, policy;
  172. struct sched_param param;
  173. RETURN_IF_FAIL (pthread_attr_getinheritsched, attr, &inherited);
  174. if (inherited != PTHREAD_EXPLICIT_SCHED)
  175. {
  176. puts ("failed to set EXPLICIT_SCHED (%d != %d)");
  177. return 1;
  178. }
  179. RETURN_IF_FAIL (pthread_attr_getschedpolicy, attr, &policy);
  180. if (policy != SCHED_RR)
  181. {
  182. printf ("failed to set SCHED_RR (%d != %d)\n", policy, SCHED_RR);
  183. return 1;
  184. }
  185. RETURN_IF_FAIL (pthread_attr_getschedparam, attr, &param);
  186. if (param.sched_priority != 42)
  187. {
  188. printf ("failed to set sched_priority (%d != %d)\n",
  189. param.sched_priority, 42);
  190. return 1;
  191. }
  192. return 0;
  193. }
  194. static int
  195. do_sched_test (void)
  196. {
  197. pthread_attr_t attr;
  198. RETURN_IF_FAIL (pthread_attr_init, &attr);
  199. /* Scheduling policy. Note that we don't always test these since it's
  200. possible that the user the tests run as don't have the appropriate
  201. privileges. */
  202. RETURN_IF_FAIL (pthread_attr_setinheritsched, &attr, PTHREAD_EXPLICIT_SCHED);
  203. RETURN_IF_FAIL (pthread_attr_setschedpolicy, &attr, SCHED_RR);
  204. struct sched_param param;
  205. param.sched_priority = 42;
  206. RETURN_IF_FAIL (pthread_attr_setschedparam, &attr, &param);
  207. int ret = run_threads (&attr);
  208. if (ret == EPERM)
  209. {
  210. printf ("Skipping Scheduler Attributes test: %s\n", strerror (ret));
  211. return 0;
  212. }
  213. else if (ret != 0)
  214. return ret;
  215. return 0;
  216. }
  217. static int
  218. verify_guardsize_result (pthread_attr_t *attr)
  219. {
  220. size_t guard;
  221. RETURN_IF_FAIL (pthread_attr_getguardsize, attr, &guard);
  222. if (guardsize != guard)
  223. {
  224. printf ("failed to set guardsize (%zu, %zu)\n", guardsize, guard);
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. static int
  230. do_guardsize_test (void)
  231. {
  232. long int pagesize = sysconf (_SC_PAGESIZE);
  233. pthread_attr_t attr;
  234. if (pagesize < 0)
  235. {
  236. printf ("sysconf failed: %s\n", strerror (errno));
  237. return 1;
  238. }
  239. RETURN_IF_FAIL (pthread_getattr_default_np, &attr);
  240. /* Increase default guardsize by a page. */
  241. RETURN_IF_FAIL (pthread_attr_getguardsize, &attr, &guardsize);
  242. guardsize += pagesize;
  243. RETURN_IF_FAIL (pthread_attr_setguardsize, &attr, guardsize);
  244. RETURN_IF_FAIL (run_threads, &attr);
  245. return 0;
  246. }
  247. static int
  248. verify_stacksize_result (pthread_attr_t *attr)
  249. {
  250. size_t stack;
  251. RETURN_IF_FAIL (pthread_attr_getstacksize, attr, &stack);
  252. if (stacksize != stack)
  253. {
  254. printf ("failed to set default stacksize (%zu, %zu)\n", stacksize, stack);
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. static int
  260. do_stacksize_test (void)
  261. {
  262. long int pagesize = sysconf (_SC_PAGESIZE);
  263. pthread_attr_t attr;
  264. if (pagesize < 0)
  265. {
  266. printf ("sysconf failed: %s\n", strerror (errno));
  267. return 1;
  268. }
  269. /* Perturb the size by a page so that we're not aligned on the 64K boundary.
  270. pthread_create does this perturbation on x86 to avoid causing the 64k
  271. aliasing conflict. We want to prevent pthread_create from doing that
  272. since it is not consistent for all architectures. */
  273. stacksize += pagesize;
  274. RETURN_IF_FAIL (pthread_attr_init, &attr);
  275. /* Run twice to ensure that we don't give a false positive. */
  276. RETURN_IF_FAIL (pthread_attr_setstacksize, &attr, stacksize);
  277. RETURN_IF_FAIL (run_threads, &attr);
  278. stacksize *= 2;
  279. RETURN_IF_FAIL (pthread_attr_setstacksize, &attr, stacksize);
  280. RETURN_IF_FAIL (run_threads, &attr);
  281. return 0;
  282. }
  283. /* We test each attribute separately because sched and affinity tests may need
  284. additional user privileges that may not be available during the test run.
  285. Each attribute test is a set of two functions, viz. a function to set the
  286. default attribute (do_foo_test) and another to verify its result
  287. (verify_foo_result). Each test spawns a thread and checks (1) if the
  288. attribute values were applied correctly and (2) if the change in the default
  289. value reflected. */
  290. static int
  291. do_test (void)
  292. {
  293. puts ("stacksize test");
  294. verify_result = verify_stacksize_result;
  295. RETURN_IF_FAIL (do_stacksize_test);
  296. puts ("guardsize test");
  297. verify_result = verify_guardsize_result;
  298. RETURN_IF_FAIL (do_guardsize_test);
  299. puts ("sched test");
  300. verify_result = verify_sched_result;
  301. RETURN_IF_FAIL (do_sched_test);
  302. puts ("affinity test");
  303. verify_result = verify_affinity_result;
  304. RETURN_IF_FAIL (do_affinity_test);
  305. puts ("detach test");
  306. verify_result = verify_detach_result;
  307. RETURN_IF_FAIL (do_detach_test);
  308. return 0;
  309. }
  310. #define TEST_FUNCTION do_test ()
  311. #include "../test-skeleton.c"