1/* Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
2   Contributed by Jakub Jelinek <jakub@redhat.com>.
3
4   This file is part of the GNU OpenMP Library (libgomp).
5
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.
10
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.
15
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.  */
20
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.  */
27
28/* This file contains system specific routines related to counting
29   online processors and dynamic load balancing.  */
30
31#ifndef _GNU_SOURCE
32#define _GNU_SOURCE 1
33#endif
34#include "libgomp.h"
35#include <sched.h>
36#include <stdlib.h>
37#include <unistd.h>
38#ifdef HAVE_GETLOADAVG
39# ifdef HAVE_SYS_LOADAVG_H
40#  include <sys/loadavg.h>
41# endif
42#endif
43
44#ifdef HAVE_PTHREAD_AFFINITY_NP
45static unsigned long
46cpuset_popcount (cpu_set_t *cpusetp)
47{
48#ifdef CPU_COUNT
49  /* glibc 2.6 and above provide a macro for this.  */
50  return CPU_COUNT (cpusetp);
51#else
52  size_t i;
53  unsigned long ret = 0;
54  extern int check[sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int)];
55
56  (void) check;
57  for (i = 0; i < sizeof (*cpusetp) / sizeof (cpusetp->__bits[0]); i++)
58    {
59      unsigned long int mask = cpusetp->__bits[i];
60      if (mask == 0)
61	continue;
62      ret += __builtin_popcountl (mask);
63    }
64  return ret;
65#endif
66}
67#endif
68
69/* At startup, determine the default number of threads.  It would seem
70   this should be related to the number of cpus online.  */
71
72void
73gomp_init_num_threads (void)
74{
75#ifdef HAVE_PTHREAD_AFFINITY_NP
76  cpu_set_t cpuset;
77
78  if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset) == 0)
79    {
80      /* Count only the CPUs this process can use.  */
81      gomp_nthreads_var = cpuset_popcount (&cpuset);
82      if (gomp_nthreads_var == 0)
83	gomp_nthreads_var = 1;
84      return;
85    }
86#endif
87#ifdef _SC_NPROCESSORS_ONLN
88  gomp_nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
89#endif
90}
91
92static int
93get_num_procs (void)
94{
95#ifdef HAVE_PTHREAD_AFFINITY_NP
96  cpu_set_t cpuset;
97
98  if (gomp_cpu_affinity == NULL)
99    {
100      /* Count only the CPUs this process can use.  */
101      if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset),
102				  &cpuset) == 0)
103	{
104	  int ret = cpuset_popcount (&cpuset);
105	  return ret != 0 ? ret : 1;
106	}
107    }
108  else
109    {
110      size_t idx;
111      static int affinity_cpus;
112
113      /* We can't use pthread_getaffinity_np in this case
114	 (we have changed it ourselves, it binds to just one CPU).
115	 Count instead the number of different CPUs we are
116	 using.  */
117      CPU_ZERO (&cpuset);
118      if (affinity_cpus == 0)
119	{
120	  int cpus = 0;
121	  for (idx = 0; idx < gomp_cpu_affinity_len; idx++)
122	    if (! CPU_ISSET (gomp_cpu_affinity[idx], &cpuset))
123	      {
124		cpus++;
125		CPU_SET (gomp_cpu_affinity[idx], &cpuset);
126	      }
127	  affinity_cpus = cpus;
128	}
129      return affinity_cpus;
130    }
131#endif
132#ifdef _SC_NPROCESSORS_ONLN
133  return sysconf (_SC_NPROCESSORS_ONLN);
134#else
135  return gomp_nthreads_var;
136#endif
137}
138
139/* When OMP_DYNAMIC is set, at thread launch determine the number of
140   threads we should spawn for this team.  */
141/* ??? I have no idea what best practice for this is.  Surely some
142   function of the number of processors that are *still* online and
143   the load average.  Here I use the number of processors online
144   minus the 15 minute load average.  */
145
146unsigned
147gomp_dynamic_max_threads (void)
148{
149  unsigned n_onln, loadavg;
150
151  n_onln = get_num_procs ();
152  if (n_onln > gomp_nthreads_var)
153    n_onln = gomp_nthreads_var;
154
155  loadavg = 0;
156#ifdef HAVE_GETLOADAVG
157  {
158    double dloadavg[3];
159    if (getloadavg (dloadavg, 3) == 3)
160      {
161	/* Add 0.1 to get a kind of biased rounding.  */
162	loadavg = dloadavg[2] + 0.1;
163      }
164  }
165#endif
166
167  if (loadavg >= n_onln)
168    return 1;
169  else
170    return n_onln - loadavg;
171}
172
173int
174omp_get_num_procs (void)
175{
176  return get_num_procs ();
177}
178
179ialias (omp_get_num_procs)
180