pthread_getattr_np.c 4.7 KB

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