pthread_getattr_np.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. assert (abs (thread->pid) == thread->tid);
  58. /* Stack size limit. */
  59. struct rlimit rl;
  60. /* The safest way to get the top of the stack is to read
  61. /proc/self/maps and locate the line into which
  62. __libc_stack_end falls. */
  63. FILE *fp = fopen ("/proc/self/maps", "rc");
  64. if (fp == NULL)
  65. ret = errno;
  66. /* We need the limit of the stack in any case. */
  67. else
  68. {
  69. if (getrlimit (RLIMIT_STACK, &rl) != 0)
  70. ret = errno;
  71. else
  72. {
  73. /* We need no locking. */
  74. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  75. /* Until we found an entry (which should always be the case)
  76. mark the result as a failure. */
  77. ret = ENOENT;
  78. char *line = NULL;
  79. size_t linelen = 0;
  80. uintptr_t last_to = 0;
  81. while (! feof_unlocked (fp))
  82. {
  83. if (getdelim (&line, &linelen, '\n', fp) <= 0)
  84. break;
  85. uintptr_t from;
  86. uintptr_t to;
  87. if (sscanf (line, "%" SCNxPTR "-%" SCNxPTR, &from, &to) != 2)
  88. continue;
  89. if (from <= (uintptr_t) __libc_stack_end
  90. && (uintptr_t) __libc_stack_end < to)
  91. {
  92. /* Found the entry. Now we have the info we need. */
  93. iattr->stacksize = rl.rlim_cur;
  94. iattr->stackaddr = (void *) to;
  95. /* The limit might be too high. */
  96. if ((size_t) iattr->stacksize
  97. > (size_t) iattr->stackaddr - last_to)
  98. iattr->stacksize = (size_t) iattr->stackaddr - last_to;
  99. /* We succeed and no need to look further. */
  100. ret = 0;
  101. break;
  102. }
  103. last_to = to;
  104. }
  105. free (line);
  106. }
  107. fclose (fp);
  108. }
  109. }
  110. iattr->flags |= ATTR_FLAG_STACKADDR;
  111. if (ret == 0)
  112. {
  113. size_t size = 16;
  114. cpu_set_t *cpuset = NULL;
  115. do
  116. {
  117. size <<= 1;
  118. void *newp = realloc (cpuset, size);
  119. if (newp == NULL)
  120. {
  121. ret = ENOMEM;
  122. break;
  123. }
  124. cpuset = (cpu_set_t *) newp;
  125. ret = __pthread_getaffinity_np (thread_id, size, cpuset);
  126. }
  127. /* Pick some ridiculous upper limit. Is 8 million CPUs enough? */
  128. while (ret == EINVAL && size < 1024 * 1024);
  129. if (ret == 0)
  130. {
  131. iattr->cpuset = cpuset;
  132. iattr->cpusetsize = size;
  133. }
  134. else
  135. {
  136. free (cpuset);
  137. if (ret == ENOSYS)
  138. {
  139. /* There is no such functionality. */
  140. ret = 0;
  141. iattr->cpuset = NULL;
  142. iattr->cpusetsize = 0;
  143. }
  144. }
  145. }
  146. lll_unlock (thread->lock, LLL_PRIVATE);
  147. return ret;
  148. }