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