pthread_getattr_np.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <assert.h>
  16. #include <errno.h>
  17. #include <inttypes.h>
  18. #include <stdio.h>
  19. #include <stdio_ext.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/resource.h>
  23. #include "pthreadP.h"
  24. #include <lowlevellock.h>
  25. #include <ldsodefs.h>
  26. int
  27. pthread_getattr_np (
  28. pthread_t thread_id,
  29. pthread_attr_t *attr)
  30. {
  31. struct pthread *thread = (struct pthread *) thread_id;
  32. struct pthread_attr *iattr = (struct pthread_attr *) attr;
  33. int ret = 0;
  34. lll_lock (thread->lock, LLL_PRIVATE);
  35. /* The thread library is responsible for keeping the values in the
  36. thread desriptor up-to-date in case the user changes them. */
  37. memcpy (&iattr->schedparam, &thread->schedparam,
  38. sizeof (struct sched_param));
  39. iattr->schedpolicy = thread->schedpolicy;
  40. /* Clear the flags work. */
  41. iattr->flags = thread->flags;
  42. /* The thread might be detached by now. */
  43. if (IS_DETACHED (thread))
  44. iattr->flags |= ATTR_FLAG_DETACHSTATE;
  45. /* This is the guardsize after adjusting it. */
  46. iattr->guardsize = thread->reported_guardsize;
  47. /* The sizes are subject to alignment. */
  48. if (__builtin_expect (thread->stackblock != NULL, 1))
  49. {
  50. iattr->stacksize = thread->stackblock_size;
  51. iattr->stackaddr = (char *) thread->stackblock + iattr->stacksize;
  52. }
  53. else
  54. {
  55. /* No stack information available. This must be for the initial
  56. thread. Get the info in some magical way. */
  57. /* Stack size limit. */
  58. struct rlimit rl;
  59. /* The safest way to get the top of the stack is to read
  60. /proc/self/maps and locate the line into which
  61. __libc_stack_end falls. */
  62. FILE *fp = fopen ("/proc/self/maps", "rc");
  63. if (fp == NULL)
  64. ret = errno;
  65. /* We need the limit of the stack in any case. */
  66. else
  67. {
  68. if (getrlimit (RLIMIT_STACK, &rl) != 0)
  69. ret = errno;
  70. else
  71. {
  72. /* We need no locking. */
  73. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  74. /* Until we found an entry (which should always be the case)
  75. mark the result as a failure. */
  76. ret = ENOENT;
  77. char *line = NULL;
  78. size_t linelen = 0;
  79. uintptr_t last_to = 0;
  80. while (! feof_unlocked (fp))
  81. {
  82. if (getdelim (&line, &linelen, '\n', fp) <= 0)
  83. break;
  84. uintptr_t from;
  85. uintptr_t to;
  86. if (sscanf (line, "%" SCNxPTR "-%" SCNxPTR, &from, &to) != 2)
  87. continue;
  88. if (from <= (uintptr_t) __libc_stack_end
  89. && (uintptr_t) __libc_stack_end < to)
  90. {
  91. /* Found the entry. Now we have the info we need. */
  92. iattr->stacksize = rl.rlim_cur;
  93. iattr->stackaddr = (void *) to;
  94. /* The limit might be too high. */
  95. if ((size_t) iattr->stacksize
  96. > (size_t) iattr->stackaddr - last_to)
  97. iattr->stacksize = (size_t) iattr->stackaddr - last_to;
  98. /* We succeed and no need to look further. */
  99. ret = 0;
  100. break;
  101. }
  102. last_to = to;
  103. }
  104. free (line);
  105. }
  106. fclose (fp);
  107. }
  108. }
  109. iattr->flags |= ATTR_FLAG_STACKADDR;
  110. if (ret == 0)
  111. {
  112. size_t size = 16;
  113. cpu_set_t *cpuset = NULL;
  114. do
  115. {
  116. size <<= 1;
  117. void *newp = realloc (cpuset, size);
  118. if (newp == NULL)
  119. {
  120. ret = ENOMEM;
  121. break;
  122. }
  123. cpuset = (cpu_set_t *) newp;
  124. ret = __pthread_getaffinity_np (thread_id, size, cpuset);
  125. }
  126. /* Pick some ridiculous upper limit. Is 8 million CPUs enough? */
  127. while (ret == EINVAL && size < 1024 * 1024);
  128. if (ret == 0)
  129. {
  130. iattr->cpuset = cpuset;
  131. iattr->cpusetsize = size;
  132. }
  133. else
  134. {
  135. free (cpuset);
  136. if (ret == ENOSYS)
  137. {
  138. /* There is no such functionality. */
  139. ret = 0;
  140. iattr->cpuset = NULL;
  141. iattr->cpusetsize = 0;
  142. }
  143. }
  144. }
  145. lll_unlock (thread->lock, LLL_PRIVATE);
  146. return ret;
  147. }