proc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (C) 2005-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This file contains system specific routines related to counting
  21. online processors and dynamic load balancing. It is expected that
  22. a system may well want to write special versions of each of these.
  23. The following implementation uses a mix of POSIX and BSD routines. */
  24. #include "libgomp.h"
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #ifdef HAVE_GETLOADAVG
  28. # ifdef HAVE_SYS_LOADAVG_H
  29. # include <sys/loadavg.h>
  30. # endif
  31. #endif
  32. #ifdef HAVE_SYS_SYSCTL_H
  33. # include <sys/sysctl.h>
  34. #endif
  35. static int
  36. get_num_procs (void)
  37. {
  38. #ifdef _SC_NPROCESSORS_ONLN
  39. return sysconf (_SC_NPROCESSORS_ONLN);
  40. #elif defined HW_NCPU
  41. int ncpus = 1;
  42. size_t len = sizeof(ncpus);
  43. sysctl((int[2]) {CTL_HW, HW_NCPU}, 2, &ncpus, &len, NULL, 0);
  44. return ncpus;
  45. #else
  46. return 0;
  47. #endif
  48. }
  49. /* At startup, determine the default number of threads. It would seem
  50. this should be related to the number of cpus online. */
  51. void
  52. gomp_init_num_threads (void)
  53. {
  54. int ncpus = get_num_procs ();
  55. if (ncpus > 0)
  56. gomp_global_icv.nthreads_var = ncpus;
  57. }
  58. /* When OMP_DYNAMIC is set, at thread launch determine the number of
  59. threads we should spawn for this team. */
  60. /* ??? I have no idea what best practice for this is. Surely some
  61. function of the number of processors that are *still* online and
  62. the load average. Here I use the number of processors online
  63. minus the 15 minute load average. */
  64. unsigned
  65. gomp_dynamic_max_threads (void)
  66. {
  67. unsigned n_onln, loadavg;
  68. unsigned nthreads_var = gomp_icv (false)->nthreads_var;
  69. n_onln = get_num_procs ();
  70. if (!n_onln || n_onln > nthreads_var)
  71. n_onln = nthreads_var;
  72. loadavg = 0;
  73. #ifdef HAVE_GETLOADAVG
  74. {
  75. double dloadavg[3];
  76. if (getloadavg (dloadavg, 3) == 3)
  77. {
  78. /* Add 0.1 to get a kind of biased rounding. */
  79. loadavg = dloadavg[2] + 0.1;
  80. }
  81. }
  82. #endif
  83. if (loadavg >= n_onln)
  84. return 1;
  85. else
  86. return n_onln - loadavg;
  87. }
  88. int
  89. omp_get_num_procs (void)
  90. {
  91. int ncpus = get_num_procs ();
  92. if (ncpus <= 0)
  93. ncpus = gomp_icv (false)->nthreads_var;
  94. return ncpus;
  95. }
  96. ialias (omp_get_num_procs)