1217309Snwhitehorn/* Copyright (C) 2005-2020 Free Software Foundation, Inc. 2217309Snwhitehorn Contributed by Richard Henderson <rth@redhat.com>. 3217309Snwhitehorn 4217309Snwhitehorn This file is part of the GNU Offloading and Multi Processing Library 5217309Snwhitehorn (libgomp). 6217309Snwhitehorn 7217309Snwhitehorn Libgomp is free software; you can redistribute it and/or modify it 8217309Snwhitehorn under the terms of the GNU General Public License as published by 9217309Snwhitehorn the Free Software Foundation; either version 3, or (at your option) 10217309Snwhitehorn any later version. 11217309Snwhitehorn 12217309Snwhitehorn Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 13217309Snwhitehorn WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14217309Snwhitehorn FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15217309Snwhitehorn more details. 16217309Snwhitehorn 17217309Snwhitehorn Under Section 7 of GPL version 3, you are granted additional 18217309Snwhitehorn permissions described in the GCC Runtime Library Exception, version 19217309Snwhitehorn 3.1, as published by the Free Software Foundation. 20217309Snwhitehorn 21217309Snwhitehorn You should have received a copy of the GNU General Public License and 22217309Snwhitehorn a copy of the GCC Runtime Library Exception along with this program; 23217309Snwhitehorn see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24217309Snwhitehorn <http://www.gnu.org/licenses/>. */ 25217309Snwhitehorn 26217309Snwhitehorn/* This file contains system specific routines related to counting 27217309Snwhitehorn online processors and dynamic load balancing. It is expected that 28217309Snwhitehorn a system may well want to write special versions of each of these. 29217309Snwhitehorn 30 The following implementation uses a mix of POSIX and BSD routines. */ 31 32#include "libgomp.h" 33#include <unistd.h> 34#include <stdlib.h> 35#ifdef HAVE_GETLOADAVG 36# ifdef HAVE_SYS_LOADAVG_H 37# include <sys/loadavg.h> 38# endif 39#endif 40 41 42/* At startup, determine the default number of threads. It would seem 43 this should be related to the number of cpus online. */ 44 45void 46gomp_init_num_threads (void) 47{ 48#ifdef _SC_NPROCESSORS_ONLN 49 gomp_global_icv.nthreads_var = sysconf (_SC_NPROCESSORS_ONLN); 50#endif 51} 52 53/* When OMP_DYNAMIC is set, at thread launch determine the number of 54 threads we should spawn for this team. */ 55/* ??? I have no idea what best practice for this is. Surely some 56 function of the number of processors that are *still* online and 57 the load average. Here I use the number of processors online 58 minus the 15 minute load average. */ 59 60unsigned 61gomp_dynamic_max_threads (void) 62{ 63 unsigned n_onln, loadavg; 64 unsigned nthreads_var = gomp_icv (false)->nthreads_var; 65 66#ifdef _SC_NPROCESSORS_ONLN 67 n_onln = sysconf (_SC_NPROCESSORS_ONLN); 68 if (n_onln > nthreads_var) 69 n_onln = nthreads_var; 70#else 71 n_onln = nthreads_var; 72#endif 73 74 loadavg = 0; 75#ifdef HAVE_GETLOADAVG 76 { 77 double dloadavg[3]; 78 if (getloadavg (dloadavg, 3) == 3) 79 { 80 /* Add 0.1 to get a kind of biased rounding. */ 81 loadavg = dloadavg[2] + 0.1; 82 } 83 } 84#endif 85 86 if (loadavg >= n_onln) 87 return 1; 88 else 89 return n_onln - loadavg; 90} 91 92int 93omp_get_num_procs (void) 94{ 95#ifdef _SC_NPROCESSORS_ONLN 96 return sysconf (_SC_NPROCESSORS_ONLN); 97#else 98 return gomp_icv (false)->nthreads_var; 99#endif 100} 101 102ialias (omp_get_num_procs) 103