pthread_getattr_np.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 (
  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);
  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 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. fclose (fp);
  105. free (line);
  106. }
  107. }
  108. iattr->flags |= ATTR_FLAG_STACKADDR;
  109. if (ret == 0)
  110. {
  111. size_t size = 16;
  112. cpu_set_t *cpuset = NULL;
  113. do
  114. {
  115. size <<= 1;
  116. void *newp = realloc (cpuset, size);
  117. if (newp == NULL)
  118. {
  119. ret = ENOMEM;
  120. break;
  121. }
  122. cpuset = (cpu_set_t *) newp;
  123. ret = __pthread_getaffinity_np (thread_id, size, cpuset);
  124. }
  125. /* Pick some ridiculous upper limit. Is 8 million CPUs enough? */
  126. while (ret == EINVAL && size < 1024 * 1024);
  127. if (ret == 0)
  128. {
  129. iattr->cpuset = cpuset;
  130. iattr->cpusetsize = size;
  131. }
  132. else
  133. {
  134. free (cpuset);
  135. if (ret == ENOSYS)
  136. /* There is no such functionality. */
  137. ret = 0;
  138. }
  139. }
  140. lll_unlock (thread->lock);
  141. return ret;
  142. }