xref: /openbsd-src/gnu/gcc/libgomp/config/posix/proc.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Copyright (C) 2005, 2006 Free Software Foundation, Inc.
2*404b540aSrobert    Contributed by Richard Henderson <rth@redhat.com>.
3*404b540aSrobert 
4*404b540aSrobert    This file is part of the GNU OpenMP Library (libgomp).
5*404b540aSrobert 
6*404b540aSrobert    Libgomp is free software; you can redistribute it and/or modify it
7*404b540aSrobert    under the terms of the GNU Lesser General Public License as published by
8*404b540aSrobert    the Free Software Foundation; either version 2.1 of the License, or
9*404b540aSrobert    (at your option) any later version.
10*404b540aSrobert 
11*404b540aSrobert    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12*404b540aSrobert    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13*404b540aSrobert    FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
14*404b540aSrobert    more details.
15*404b540aSrobert 
16*404b540aSrobert    You should have received a copy of the GNU Lesser General Public License
17*404b540aSrobert    along with libgomp; see the file COPYING.LIB.  If not, write to the
18*404b540aSrobert    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19*404b540aSrobert    MA 02110-1301, USA.  */
20*404b540aSrobert 
21*404b540aSrobert /* As a special exception, if you link this library with other files, some
22*404b540aSrobert    of which are compiled with GCC, to produce an executable, this library
23*404b540aSrobert    does not by itself cause the resulting executable to be covered by the
24*404b540aSrobert    GNU General Public License.  This exception does not however invalidate
25*404b540aSrobert    any other reasons why the executable file might be covered by the GNU
26*404b540aSrobert    General Public License.  */
27*404b540aSrobert 
28*404b540aSrobert /* This file contains system specific routines related to counting
29*404b540aSrobert    online processors and dynamic load balancing.  It is expected that
30*404b540aSrobert    a system may well want to write special versions of each of these.
31*404b540aSrobert 
32*404b540aSrobert    The following implementation uses a mix of POSIX and BSD routines.  */
33*404b540aSrobert 
34*404b540aSrobert #include "libgomp.h"
35*404b540aSrobert #include <unistd.h>
36*404b540aSrobert #include <stdlib.h>
37*404b540aSrobert #ifdef HAVE_GETLOADAVG
38*404b540aSrobert # ifdef HAVE_SYS_LOADAVG_H
39*404b540aSrobert #  include <sys/loadavg.h>
40*404b540aSrobert # endif
41*404b540aSrobert #endif
42*404b540aSrobert 
43*404b540aSrobert 
44*404b540aSrobert /* At startup, determine the default number of threads.  It would seem
45*404b540aSrobert    this should be related to the number of cpus online.  */
46*404b540aSrobert 
47*404b540aSrobert void
gomp_init_num_threads(void)48*404b540aSrobert gomp_init_num_threads (void)
49*404b540aSrobert {
50*404b540aSrobert #ifdef _SC_NPROCESSORS_ONLN
51*404b540aSrobert   gomp_nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
52*404b540aSrobert #endif
53*404b540aSrobert }
54*404b540aSrobert 
55*404b540aSrobert /* When OMP_DYNAMIC is set, at thread launch determine the number of
56*404b540aSrobert    threads we should spawn for this team.  */
57*404b540aSrobert /* ??? I have no idea what best practice for this is.  Surely some
58*404b540aSrobert    function of the number of processors that are *still* online and
59*404b540aSrobert    the load average.  Here I use the number of processors online
60*404b540aSrobert    minus the 15 minute load average.  */
61*404b540aSrobert 
62*404b540aSrobert unsigned
gomp_dynamic_max_threads(void)63*404b540aSrobert gomp_dynamic_max_threads (void)
64*404b540aSrobert {
65*404b540aSrobert   unsigned n_onln, loadavg;
66*404b540aSrobert 
67*404b540aSrobert #ifdef _SC_NPROCESSORS_ONLN
68*404b540aSrobert   n_onln = sysconf (_SC_NPROCESSORS_ONLN);
69*404b540aSrobert   if (n_onln > gomp_nthreads_var)
70*404b540aSrobert     n_onln = gomp_nthreads_var;
71*404b540aSrobert #else
72*404b540aSrobert   n_onln = gomp_nthreads_var;
73*404b540aSrobert #endif
74*404b540aSrobert 
75*404b540aSrobert   loadavg = 0;
76*404b540aSrobert #ifdef HAVE_GETLOADAVG
77*404b540aSrobert   {
78*404b540aSrobert     double dloadavg[3];
79*404b540aSrobert     if (getloadavg (dloadavg, 3) == 3)
80*404b540aSrobert       {
81*404b540aSrobert 	/* Add 0.1 to get a kind of biased rounding.  */
82*404b540aSrobert 	loadavg = dloadavg[2] + 0.1;
83*404b540aSrobert       }
84*404b540aSrobert   }
85*404b540aSrobert #endif
86*404b540aSrobert 
87*404b540aSrobert   if (loadavg >= n_onln)
88*404b540aSrobert     return 1;
89*404b540aSrobert   else
90*404b540aSrobert     return n_onln - loadavg;
91*404b540aSrobert }
92*404b540aSrobert 
93*404b540aSrobert int
omp_get_num_procs(void)94*404b540aSrobert omp_get_num_procs (void)
95*404b540aSrobert {
96*404b540aSrobert #ifdef _SC_NPROCESSORS_ONLN
97*404b540aSrobert   return sysconf (_SC_NPROCESSORS_ONLN);
98*404b540aSrobert #else
99*404b540aSrobert   return gomp_nthreads_var;
100*404b540aSrobert #endif
101*404b540aSrobert }
102*404b540aSrobert 
103*404b540aSrobert ialias (omp_get_num_procs)
104