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