proc.c revision 1.1
1/* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 2 Free Software Foundation, Inc. 3 Contributed by Jakub Jelinek <jakub@redhat.com>. 4 5 This file is part of the GNU OpenMP Library (libgomp). 6 7 Libgomp is free software; you can redistribute it and/or modify it 8 under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3, or (at your option) 10 any later version. 11 12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 more details. 16 17 Under Section 7 of GPL version 3, you are granted additional 18 permissions described in the GCC Runtime Library Exception, version 19 3.1, as published by the Free Software Foundation. 20 21 You should have received a copy of the GNU General Public License and 22 a copy of the GCC Runtime Library Exception along with this program; 23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24 <http://www.gnu.org/licenses/>. */ 25 26/* This file contains system specific routines related to counting 27 online processors and dynamic load balancing. */ 28 29#ifndef _GNU_SOURCE 30#define _GNU_SOURCE 1 31#endif 32#include "libgomp.h" 33#include <sched.h> 34#include <stdlib.h> 35#include <unistd.h> 36#ifdef HAVE_GETLOADAVG 37# ifdef HAVE_SYS_LOADAVG_H 38# include <sys/loadavg.h> 39# endif 40#endif 41 42#ifdef HAVE_PTHREAD_AFFINITY_NP 43static unsigned long 44cpuset_popcount (cpu_set_t *cpusetp) 45{ 46#ifdef CPU_COUNT 47 /* glibc 2.6 and above provide a macro for this. */ 48 return CPU_COUNT (cpusetp); 49#else 50 size_t i; 51 unsigned long ret = 0; 52 extern int check[sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int)]; 53 54 (void) check; 55 for (i = 0; i < sizeof (*cpusetp) / sizeof (cpusetp->__bits[0]); i++) 56 { 57 unsigned long int mask = cpusetp->__bits[i]; 58 if (mask == 0) 59 continue; 60 ret += __builtin_popcountl (mask); 61 } 62 return ret; 63#endif 64} 65#endif 66 67/* At startup, determine the default number of threads. It would seem 68 this should be related to the number of cpus online. */ 69 70void 71gomp_init_num_threads (void) 72{ 73#ifdef HAVE_PTHREAD_AFFINITY_NP 74 cpu_set_t cpuset; 75 76 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset) == 0) 77 { 78 /* Count only the CPUs this process can use. */ 79 gomp_global_icv.nthreads_var = cpuset_popcount (&cpuset); 80 if (gomp_global_icv.nthreads_var == 0) 81 gomp_global_icv.nthreads_var = 1; 82 return; 83 } 84#endif 85#ifdef _SC_NPROCESSORS_ONLN 86 gomp_global_icv.nthreads_var = sysconf (_SC_NPROCESSORS_ONLN); 87#endif 88} 89 90static int 91get_num_procs (void) 92{ 93#ifdef HAVE_PTHREAD_AFFINITY_NP 94 cpu_set_t cpuset; 95 96 if (gomp_cpu_affinity == NULL) 97 { 98 /* Count only the CPUs this process can use. */ 99 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), 100 &cpuset) == 0) 101 { 102 int ret = cpuset_popcount (&cpuset); 103 return ret != 0 ? ret : 1; 104 } 105 } 106 else 107 { 108 /* We can't use pthread_getaffinity_np in this case 109 (we have changed it ourselves, it binds to just one CPU). 110 Count instead the number of different CPUs we are 111 using. gomp_init_affinity updated gomp_available_cpus to 112 the number of CPUs in the GOMP_AFFINITY mask that we are 113 allowed to use though. */ 114 return gomp_available_cpus; 115 } 116#endif 117#ifdef _SC_NPROCESSORS_ONLN 118 return sysconf (_SC_NPROCESSORS_ONLN); 119#else 120 return gomp_icv (false)->nthreads_var; 121#endif 122} 123 124/* When OMP_DYNAMIC is set, at thread launch determine the number of 125 threads we should spawn for this team. */ 126/* ??? I have no idea what best practice for this is. Surely some 127 function of the number of processors that are *still* online and 128 the load average. Here I use the number of processors online 129 minus the 15 minute load average. */ 130 131unsigned 132gomp_dynamic_max_threads (void) 133{ 134 unsigned n_onln, loadavg, nthreads_var = gomp_icv (false)->nthreads_var; 135 136 n_onln = get_num_procs (); 137 if (n_onln > nthreads_var) 138 n_onln = nthreads_var; 139 140 loadavg = 0; 141#ifdef HAVE_GETLOADAVG 142 { 143 double dloadavg[3]; 144 if (getloadavg (dloadavg, 3) == 3) 145 { 146 /* Add 0.1 to get a kind of biased rounding. */ 147 loadavg = dloadavg[2] + 0.1; 148 } 149 } 150#endif 151 152 if (loadavg >= n_onln) 153 return 1; 154 else 155 return n_onln - loadavg; 156} 157 158int 159omp_get_num_procs (void) 160{ 161 return get_num_procs (); 162} 163 164ialias (omp_get_num_procs) 165