1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2005 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
/* This component will only be compiled on Linux, where we are
guaranteed to have <unistd.h> and friends */
#include <stdio.h>
/* the affinity macros are only exposed if __USE_GNU is set (at least,
on Suse Linux for PPC). However, __USE_GNU causes problems for
some compilers (IBM XL) with stdio.h. So include stdio.h, then
set __USE_GNU. */
#ifndef __USE_GNU
#define __USE_GNU 1
#endif
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "opal/constants.h"
#include "opal/mca/base/mca_base_param.h"
#include "opal/mca/paffinity/paffinity.h"
#include "opal/mca/paffinity/base/base.h"
#include "paffinity_linux.h"
/*
* Local functions
*/
static int linux_module_init(void);
static int linux_module_get_num_procs(int *num_procs);
static int linux_module_set(int id);
static int linux_module_get(int *id);
/*
* Linux paffinity module
*/
static const opal_paffinity_base_module_1_0_0_t module = {
/* Initialization function */
linux_module_init,
/* Module function pointers */
linux_module_get_num_procs,
linux_module_set,
linux_module_get
};
const opal_paffinity_base_module_1_0_0_t *
opal_paffinity_linux_component_query(int *query)
{
int param;
param = mca_base_param_find("paffinity", "linux", "priority");
mca_base_param_lookup_int(param, query);
return &module;
}
static int linux_module_init(void)
{
/* Nothing to do */
return OPAL_SUCCESS;
}
static int linux_module_get_num_procs(int *num_procs)
{
*num_procs = sysconf(_SC_NPROCESSORS_ONLN);
return OPAL_SUCCESS;
}
/************************************************************************
See the note in paffinity_linux.h -- there are at least 3 different
ways that Linux's sched_setaffinity()/sched_getaffinity() are
implemented. Hence, rather than trying to pepper #if's all
throughout the code, just have multiple implementations of these
module functions.
************************************************************************/
#if !defined(HAVE_CPU_SET_T)
/************************************************************************
If we don't have cpu_set_t, then we have the "old style" Linux
sched_setaffinity():
int sched_setaffinity(pid_t pid, unsigned int len, unsigned long
*mask);
int sched_getaffinity(pid_t pid, unsigned int len, unsigned long
*mask);
We do not have the CPU_ZERO(), CPU_SET(), CPU_ISSET(), etc. macros.
************************************************************************/
static int make_mask(unsigned int *len, unsigned long **mask)
{
int num_procs;
linux_module_get_num_procs(&num_procs);
*len = num_procs / 8;
if (*len != (unsigned int)(num_procs * 8)) {
++*len;
}
/* Ensure *len is a multiple of sizeof(long) */
if (*len != (*len / sizeof(long)) * sizeof(long)) {
*len += sizeof(long) - (*len % sizeof(long));
}
/* Malloc */
*mask = malloc(*len);
if (NULL == *mask) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
memset(*mask, 0, *len);
return OPAL_SUCCESS;
}
static int linux_module_set(int id)
{
int ret, num_procs, byte, bit;
unsigned int len;
unsigned long *mask;
linux_module_get_num_procs(&num_procs);
if (id >= num_procs || id < 0) {
return OPAL_ERR_BAD_PARAM;
}
if (OPAL_SUCCESS != (ret = make_mask(&len, &mask))) {
return ret;
}
byte = id / 8;
bit = id % 8;
((char *) mask)[byte] = 1 << bit;
ret = sched_setaffinity(0, len, mask);
bit = errno;
free(mask);
errno = bit;
return (0 == ret) ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO;
}
static int linux_module_get(int *id)
{
int ret, save, byte, bit;
unsigned int i, len;
unsigned long *mask;
if (OPAL_SUCCESS != (ret = make_mask(&len, &mask))) {
return ret;
}
if (0 != sched_getaffinity(0, len, mask)) {
save = errno;
free(mask);
errno = save;
return OPAL_ERR_IN_ERRNO;
}
*id = -1;
for (i = 0; i < sizeof(mask); ++i) {
byte = i / 8;
bit = i % 8;
if (0 != (mask[byte] & (1 << bit))) {
*id = i;
break;
}
}
free(mask);
if (-1 == *id) {
return OPAL_ERR_NOT_FOUND;
}
return OPAL_SUCCESS;
}
#else
/************************************************************************
In this case, we're using the following prototypes for the affinity
functions (case 2)
int sched_setaffinity (pid_t __pid, size_t __cpusetsize, const
cpu_set_t *__cpuset);
int sched_getaffinity (pid_t __pid, size_t __cpusetsize, const
cpu_set_t *__cpuset);
or
int sched_setaffinity (pid_t __pid, const cpu_set_t *__cpuset);
int sched_getaffinity (pid_t __pid, const cpu_set_t *__cpuset);
The only difference is the missing size_t parameter, which is easy
to determine inline, below.
We also have the CPU_ZERO(), CPU_SET(), CPU_ISSET(), etc. macros
(but CPU_ZERO() may not work, so use the indirection
OMPI_CPU_ZERO()).
************************************************************************/
static int linux_module_set(int id)
{
int num_procs;
cpu_set_t mask;
linux_module_get_num_procs(&num_procs);
if (id >= num_procs || id < 0) {
return OPAL_ERR_BAD_PARAM;
}
OMPI_CPU_ZERO(&mask);
CPU_SET(id, &mask);
if (0 != sched_setaffinity(0,
#if OPAL_PAFFINITY_LINUX_SCHED_SETAFF_NUM_PARAMS == 3
sizeof(mask),
#endif
&mask)) {
return OPAL_ERR_IN_ERRNO;
}
return OPAL_SUCCESS;
}
static int linux_module_get(int *id)
{
unsigned int i;
cpu_set_t mask;
OMPI_CPU_ZERO(&mask);
if (0 != sched_getaffinity(0,
#if OPAL_PAFFINITY_LINUX_SCHED_SETAFF_NUM_PARAMS == 3
sizeof(mask),
#endif
&mask)) {
return OPAL_ERR_IN_ERRNO;
}
*id = -1;
for (i = 0; i < sizeof(mask); ++i) {
if (CPU_ISSET(i, &mask)) {
*id = i;
break;
}
}
if (-1 == *id) {
return OPAL_ERR_NOT_FOUND;
}
return OPAL_SUCCESS;
}
#endif
|