pthread_getattr_np.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (C) 2002, 2003, 2004 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 (thread_id, attr)
  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. /* We have to handle cancellation in the following code since we are
  36. locking another threads desriptor. */
  37. pthread_cleanup_push ((void (*) (void *)) lll_unlock_wake_cb, &thread->lock);
  38. lll_lock (thread->lock);
  39. /* The thread library is responsible for keeping the values in the
  40. thread desriptor up-to-date in case the user changes them. */
  41. memcpy (&iattr->schedparam, &thread->schedparam,
  42. sizeof (struct sched_param));
  43. iattr->schedpolicy = thread->schedpolicy;
  44. /* Clear the flags work. */
  45. iattr->flags = thread->flags;
  46. /* The thread might be detached by now. */
  47. if (IS_DETACHED (thread))
  48. iattr->flags |= ATTR_FLAG_DETACHSTATE;
  49. /* This is the guardsize after adjusting it. */
  50. iattr->guardsize = thread->reported_guardsize;
  51. /* The sizes are subject to alignment. */
  52. if (__builtin_expect (thread->stackblock != NULL, 1))
  53. {
  54. iattr->stacksize = thread->stackblock_size;
  55. iattr->stackaddr = (char *) thread->stackblock + iattr->stacksize;
  56. }
  57. else
  58. {
  59. /* No stack information available. This must be for the initial
  60. thread. Get the info in some magical way. */
  61. assert (abs (thread->pid) == thread->tid);
  62. /* Stack size limit. */
  63. struct rlimit rl;
  64. /* The safest way to get the top of the stack is to read
  65. /proc/self/maps and locate the line into which
  66. __libc_stack_end falls. */
  67. FILE *fp = fopen ("/proc/self/maps", "rc");
  68. if (fp == NULL)
  69. ret = errno;
  70. /* We need the limit of the stack in any case. */
  71. else if (getrlimit (RLIMIT_STACK, &rl) != 0)
  72. ret = errno;
  73. else
  74. {
  75. /* We need no locking. */
  76. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  77. /* Until we found an entry (which should always be the case)
  78. mark the result as a failure. */
  79. ret = ENOENT;
  80. char *line = NULL;
  81. size_t linelen = 0;
  82. uintptr_t last_to = 0;
  83. while (! feof_unlocked (fp))
  84. {
  85. if (getdelim (&line, &linelen, '\n', fp) <= 0)
  86. break;
  87. uintptr_t from;
  88. uintptr_t to;
  89. if (sscanf (line, "%" SCNxPTR "-%" SCNxPTR, &from, &to) != 2)
  90. continue;
  91. if (from <= (uintptr_t) __libc_stack_end
  92. && (uintptr_t) __libc_stack_end < to)
  93. {
  94. /* Found the entry. Now we have the info we need. */
  95. iattr->stacksize = rl.rlim_cur;
  96. iattr->stackaddr = (void *) to;
  97. /* The limit might be too high. */
  98. if ((size_t) iattr->stacksize
  99. > (size_t) iattr->stackaddr - last_to)
  100. iattr->stacksize = (size_t) iattr->stackaddr - last_to;
  101. /* We succeed and no need to look further. */
  102. ret = 0;
  103. break;
  104. }
  105. last_to = to;
  106. }
  107. fclose (fp);
  108. free (line);
  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. /* There is no such functionality. */
  140. ret = 0;
  141. }
  142. }
  143. lll_unlock (thread->lock);
  144. pthread_cleanup_pop (0);
  145. return ret;
  146. }