Commit ec17b702 authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Convert affinity control from the deprecated PLPA to HWLOC

parent d07b043e
/* -*- c -*-
*
* Copyright (c) 2004-2005 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2006-2008 Cisco, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
/*
* Some notes about the declarations and definitions in this file:
*
* This file is a mix of internal and public declarations.
* Applications are warned against using the internal types; they are
* subject to change with no warning.
*
* The PLPA_NAME() and PLPA_NAME_CAPS() macros are used for prefixing
* the PLPA type names, enum names, and symbol names when embedding
* PLPA. When not embedding, the default prefix is "plpa_" (or
* "PLPA_" when using PLPA_NAME_CAPS()). Hence, if you see a
* declaration like this:
*
* int PLPA_NAME(foo)(void);
*
* It's a function named plpa_foo() that returns an int and takes no
* arguments when building PLPA as a standalone library. It's a
* function with a different prefix than "plpa_" when the
* --enable-included-mode and --with-plpa-symbol-prefix options are
* supplied to PLPA's configure script.
*
* Note that this header file differentiates between a
* processor/socket/core ID and a processor/socket/core number. The
* "ID" is the integer that is used by Linux to identify that entity.
* These integers may or may not be contiguous. The "number" is a
* contiguous set of integers starting with 0 and going to (count-1),
* where (count) is the number of processors, sockets, and cores
* (where the count of cores is dependent upon the socket). Hence,
* "number" is a human convenience, and "ID" is the actual Linux
* identifier.
*/
#ifndef PLPA_H
#define PLPA_H
/* Absolutely must not include <sched.h> here or it will generate
conflicts. */
/* For memset() */
#include <string.h>
/* For pid_t and size_t */
#include <sys/types.h>
/***************************************************************************
* Internal types
***************************************************************************/
/* If we're building PLPA itself, <plpa_config.h> will have already
been included. But <plpa_config.h> is a private header file; it is
not installed into $includedir. Hence, applications including
<plpa.h> will not have included <plpa_config.h> (this is by
design). So include just enough information here to allow us to
continue. */
#ifndef PLPA_CONFIG_H
/* The PLPA symbol prefix */
#define PLPA_SYM_PREFIX plpa_
/* The PLPA symbol prefix in all caps */
#define PLPA_SYM_PREFIX_CAPS PLPA_
#endif
/* Preprocessors are fun -- the double inderection is unfortunately
necessary. */
#define PLPA_MUNGE_NAME(a, b) PLPA_MUNGE_NAME2(a, b)
#define PLPA_MUNGE_NAME2(a, b) a ## b
#define PLPA_NAME(name) PLPA_MUNGE_NAME(PLPA_SYM_PREFIX, name)
#define PLPA_NAME_CAPS(name) PLPA_MUNGE_NAME(PLPA_SYM_PREFIX_CAPS, name)
/***************************************************************************
* Public type
***************************************************************************/
/* Values that can be returned from plpa_api_probe() */
typedef enum {
/* Sentinel value */
PLPA_NAME_CAPS(PROBE_UNSET),
/* sched_setaffinity syscall available */
PLPA_NAME_CAPS(PROBE_OK),
/* syscall unavailable/unimplemented */
PLPA_NAME_CAPS(PROBE_NOT_SUPPORTED),
/* we experienced some strange failure that the user should report */
PLPA_NAME_CAPS(PROBE_UNKNOWN)
} PLPA_NAME(api_type_t);
/***************************************************************************
* Internal types
***************************************************************************/
/* Internal PLPA bitmask type. This type should not be used by
external applications! */
typedef unsigned long int PLPA_NAME(bitmask_t);
#define PLPA_BITMASK_T_NUM_BITS (sizeof(PLPA_NAME(bitmask_t)) * 8)
#define PLPA_BITMASK_CPU_MAX 1024
#define PLPA_BITMASK_NUM_ELEMENTS (PLPA_BITMASK_CPU_MAX / PLPA_BITMASK_T_NUM_BITS)
/***************************************************************************
* Public type
***************************************************************************/
/* Public type for the PLPA cpu set. */
typedef struct { PLPA_NAME(bitmask_t) bitmask[PLPA_BITMASK_NUM_ELEMENTS]; } PLPA_NAME(cpu_set_t);
/***************************************************************************
* Internal macros
***************************************************************************/
/* Internal macro for identifying the byte in a bitmask array. This
macro should not be used by external applications! */
#define PLPA_CPU_BYTE(num) ((num) / PLPA_BITMASK_T_NUM_BITS)
/* Internal macro for identifying the bit in a bitmask array. This
macro should not be used by external applications! */
#define PLPA_CPU_BIT(num) ((num) % PLPA_BITMASK_T_NUM_BITS)
/***************************************************************************
* Public macros
***************************************************************************/
/* Public macro to zero out a PLPA cpu set (analogous to the FD_ZERO()
macro; see select(2)). */
#define PLPA_CPU_ZERO(cpuset) \
memset((cpuset), 0, sizeof(PLPA_NAME(cpu_set_t)))
/* Public macro to set a bit in a PLPA cpu set (analogous to the
FD_SET() macro; see select(2)). */
#define PLPA_CPU_SET(num, cpuset) \
(cpuset)->bitmask[PLPA_CPU_BYTE(num)] |= ((PLPA_NAME(bitmask_t))1 << PLPA_CPU_BIT(num))
/* Public macro to clear a bit in a PLPA cpu set (analogous to the
FD_CLR() macro; see select(2)). */
#define PLPA_CPU_CLR(num, cpuset) \
(cpuset)->bitmask[PLPA_CPU_BYTE(num)] &= ~((PLPA_NAME(bitmask_t))1 << PLPA_CPU_BIT(num))
/* Public macro to test if a bit is set in a PLPA cpu set (analogous
to the FD_ISSET() macro; see select(2)). */
#define PLPA_CPU_ISSET(num, cpuset) \
(0 != (((cpuset)->bitmask[PLPA_CPU_BYTE(num)]) & ((PLPA_NAME(bitmask_t))1 << PLPA_CPU_BIT(num))))
/***************************************************************************
* Public functions
***************************************************************************/
/* Setup PLPA internals. This function is optional; it will be
automatically invoked by all the other API functions if you do not
invoke it explicitly. Returns 0 upon success. */
int PLPA_NAME(init)(void);
/* Check what API is on this machine. If api_type returns
PLPA_PROBE_OK, then PLPA can function properly on this machine.
Returns 0 upon success. */
int PLPA_NAME(api_probe)(PLPA_NAME(api_type_t) *api_type);
/* Set processor affinity. Use the PLPA_CPU_* macros to set the
cpuset value. The same rules and restrictions about pid apply as
they do for the sched_setaffinity(2) system call. Bits set in the
CPU mask correspond to Linux processor IDs. Returns 0 upon
success. */
int PLPA_NAME(sched_setaffinity)(pid_t pid, size_t cpusetsize,
const PLPA_NAME(cpu_set_t) *cpuset);
/* Get processor affinity. Use the PLPA_CPU_* macros to analyze the
returned value of cpuset. The same rules and restrictions about
pid apply as they do for the sched_getaffinity(2) system call.
Bits set in the CPU mask corresopnd to Linux processor IDs.
Returns 0 upon success. */
int PLPA_NAME(sched_getaffinity)(pid_t pid, size_t cpusetsize,
PLPA_NAME(cpu_set_t) *cpuset);
/* Return whether topology information is available (i.e.,
plpa_map_to_*, plpa_max_*). The topology functions will be
available if supported == 1 and the function returns 0. */
int PLPA_NAME(have_topology_information)(int *supported);
/* Map (socket_id,core_id) tuple to virtual processor ID. processor_id is
then suitable for use with the PLPA_CPU_* macros, probably leading
to a call to plpa_sched_setaffinity(). Returns 0 upon success. */
int PLPA_NAME(map_to_processor_id)(int socket_id, int core_id,
int *processor_id);
/* Map processor_id to (socket_id,core_id) tuple. The processor_id input is
usually obtained from the return from the plpa_sched_getaffinity()
call, using PLPA_CPU_ISSET to find individual bits in the map that
were set/unset. plpa_map_to_socket_core() can map the bit indexes
to a socket/core tuple. Returns 0 upon success. */
int PLPA_NAME(map_to_socket_core)(int processor_id,
int *socket_id, int *core_id);
/* This function is deprecated and will disappear in a future release.
It is exactly equivalent to calling
plpa_get_processor_data(PLPA_COUNT_ALL, num_processors,
max_processor_id). */
int PLPA_NAME(get_processor_info)(int *num_processors, int *max_processor_id);
/* Typedefs for specifying which processors / sockets / cores to count
in get_processor_data() and get_processor_id() */
typedef enum {
/* Only count online processors */
PLPA_NAME_CAPS(COUNT_ONLINE),
/* Only count offline processors */
PLPA_NAME_CAPS(COUNT_OFFLINE),
/* Count all processors (online and offline) */
PLPA_NAME_CAPS(COUNT_ALL)
} PLPA_NAME(count_specification_t);
/* Returns both the number of processors in a system and the maximum
Linux virtual processor ID (because it may be higher than the
number of processors if there are "holes" in the available Linux
virtual processor IDs). The count_spec argument specifies whether
to count all processors, only online processors, or only offline
processors. Returns 0 upon success. */
int PLPA_NAME(get_processor_data)(PLPA_NAME(count_specification_t) count_spec,
int *num_processors, int *max_processor_id);
/* Returns the Linux processor ID for the Nth processor. For example,
if the Linux processor IDs have "holes", use this function to say
"give me the Linux processor ID of the 4th processor." count_spec
specifies whether to count online, offline, or all processors when
looking for the processor_num'th processor. Returns 0 upon
success. */
int PLPA_NAME(get_processor_id)(int processor_num,
PLPA_NAME(count_specification_t) count_spec,
int *processor_id);
/* Check to see if a given Linux processor ID exists / is online.
Returns 0 on success. */
int PLPA_NAME(get_processor_flags)(int processor_id, int *exists, int *online);
/* Returns both the number of sockets in the system and the maximum
socket ID number (in case there are "holes" in the list of available
socket IDs). Returns 0 upon sucess. */
int PLPA_NAME(get_socket_info)(int *num_sockets, int *max_socket_id);
/* Returns the Linux socket ID for the Nth socket. For example, if
the socket IDs have "holes", use this function to say "give me the
Linux socket ID of the 2nd socket." Linux does not specify the
socket/core tuple information for offline processors, so a
plpa_count_specification_t parameter is not used here. Returns 0
upon success. */
int PLPA_NAME(get_socket_id)(int socket_num, int *socket_id);
/* Return both the number of cores and the max code ID for a given
socket (in case there are "holes" in the list of available core
IDs). Returns 0 upon success. */
int PLPA_NAME(get_core_info)(int socket_id, int *num_cores, int *max_core_id);
/* Given a specific socket, returns the Linux core ID for the Nth
core. For example, if the core IDs have "holes", use this function
to say "give me the Linux core ID of the 4th core on socket ID 7."
Linux does not specify the socket/core tuple information for
offline processors, so a plpa_count_specification_t parameter is
not used here. Returns 0 upon success. Returns 0 upon success. */
int PLPA_NAME(get_core_id)(int socket_id, int core_num, int *core_id);
/* Check to see if a given Linux (socket_id,core_id) tuple exists / is
online. Returns 0 on success. */
int PLPA_NAME(get_core_flags)(int socket_id, int core_id,
int *exists, int *online);
/* Typedefs for specifying the cache behavior via
plpa_set_cache_behavior() */
typedef enum {
/* Use the cache (default behavior); fills the cache right now if
it's not already full */
PLPA_NAME_CAPS(CACHE_USE),
/* Never use the cache; always look up the information in
the kernel */
PLPA_NAME_CAPS(CACHE_IGNORE),
/* Refresh the cache right now */
PLPA_NAME_CAPS(CACHE_REFRESH)
} PLPA_NAME(cache_behavior_t);
/* Set PLPA's cache behavior. Returns 0 upon success. */
int PLPA_NAME(set_cache_behavior)(PLPA_NAME(cache_behavior_t));
/* Shut down PLPA. This function releases resources used by the PLPA.
It should be the last PLPA function invoked, or can be used to
forcibly cause PLPA to dump its topology cache and re-analyze the
underlying system the next time another PLPA function is called.
Specifically: it is safe to call plpa_init() (or any other PLPA
function) again after plpa_finalized(). Returns 0 upon success. */
int PLPA_NAME(finalize)(void);
#endif /* PLPA_H */
/* -*- c -*-
*
* Copyright (c) 2004-2005 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2007-2008 Cisco, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "plpa_config.h"
#include "plpa.h"
#include "plpa_internal.h"
#include <errno.h>
#include <sys/syscall.h>
#include <unistd.h>
#if PLPA_WANT_VALGRIND_SUPPORT
#include "valgrind/memcheck.h"
#endif
/* Cache, just to make things a little more efficient */
static PLPA_NAME(api_type_t) cache = PLPA_NAME_CAPS(PROBE_UNSET);
/* The len value we find - not in public header, but used by the lib */
size_t PLPA_NAME(len) = 0;
int PLPA_NAME(api_probe_init)(void)
{
PLPA_NAME(cpu_set_t) mask;
int rc;
size_t len;
for (len = sizeof(mask); len != 0; len >>= 1) {
rc = syscall(__NR_sched_getaffinity, 0, len, &mask);
if (rc >= 0) {
/* OK, kernel is happy with a get(). Validate w/ a set(). */
/* Note that kernel may have told us the "proper" size */
size_t tmp = (0 != rc) ? ((size_t) rc) : len;
/* Pass mask=NULL, expect errno==EFAULT if tmp was OK
as a length */
#if PLPA_WANT_VALGRIND_SUPPORT
/* Lie to Valgrind and say that this memory is addressible
so that we don't get a false positive here -- we *know*
that 0 is unaddressible; that's the whole point of this
test (to see what error NR_sched_set_affinity will
return). So let's not see a warning from Valgrind from
something that we know is wrong. :-) */
VALGRIND_MAKE_MEM_DEFINED(0, len);
#endif
rc = syscall(__NR_sched_setaffinity, 0, tmp, NULL);
#if PLPA_WANT_VALGRIND_SUPPORT
VALGRIND_MAKE_MEM_UNDEFINED(0, len);
VALGRIND_MAKE_MEM_NOACCESS(0, len);
#endif
if ((rc < 0) && (errno == EFAULT)) {
cache = PLPA_NAME_CAPS(PROBE_OK);
PLPA_NAME(len) = tmp;
rc = 0;
break;
}
}
if (errno == ENOSYS) {
break; /* No point in looping */
}
}
if (rc >= 0) {
/* OK */
} else if (errno == ENOSYS) {
/* Kernel returns ENOSYS because there is no support for
processor affinity */
cache = PLPA_NAME_CAPS(PROBE_NOT_SUPPORTED);
} else {
/* Unknown! */
cache = PLPA_NAME_CAPS(PROBE_UNKNOWN);
}
return 0;
}
int PLPA_NAME(api_probe)(PLPA_NAME(api_type_t) *api_type)
{
int ret;
/* Check to see that we're initialized */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* Check for bozo arguments */
if (NULL == api_type) {
return EINVAL;
}
/* All done */
*api_type = cache;
return 0;
}
/* ./src/libplpa/plpa_config.h.in. Generated from configure.ac by autoheader. */
/* -*- c -*-
*
* Copyright (c) 2004-2005 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2006-2008 Cisco Systems, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#ifndef PLPA_CONFIG_H
#define PLPA_CONFIG_H
/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H
/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H
/* Define to 1 if you have the <strings.h> header file. */
#undef HAVE_STRINGS_H
/* Define to 1 if you have the <string.h> header file. */
#undef HAVE_STRING_H
/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
/* Define to 1 if you have the <sys/types.h> header file. */
#undef HAVE_SYS_TYPES_H
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H
/* Define to 1 if you have the <valgrind/valgrind.h> header file. */
#undef HAVE_VALGRIND_VALGRIND_H
/* Define to the sub-directory in which libtool stores uninstalled libraries.
*/
#undef LT_OBJDIR
/* Define to 1 if your C compiler doesn't accept -c and -o together. */
#undef NO_MINUS_C_MINUS_O
/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT
/* Define to the full name of this package. */
#undef PACKAGE_NAME
/* Define to the full name and version of this package. */
#undef PACKAGE_STRING
/* Define to the one symbol short name of this package. */
#undef PACKAGE_TARNAME
/* Define to the version of this package. */
#undef PACKAGE_VERSION
/* Whether we are in debugging more or not */
#undef PLPA_DEBUG
/* Major version of PLPA */
#undef PLPA_MAJOR_VERSION
/* Minor version of PLPA */
#undef PLPA_MINOR_VERSION
/* Release version of PLPA */
#undef PLPA_RELEASE_VERSION
/* The PLPA symbol prefix */
#undef PLPA_SYM_PREFIX
/* The PLPA symbol prefix in all caps */
#undef PLPA_SYM_PREFIX_CAPS
/* Whether we want Valgrind support or not */
#undef PLPA_WANT_VALGRIND_SUPPORT
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
/* Define to 1 if `lex' declares `yytext' as a `char *' by default, not a
`char[]'. */
#undef YYTEXT_POINTER
/* Emulated value */
#undef __NR_sched_getaffinity
/* Emulated value */
#undef __NR_sched_setaffinity
/* Define to `__inline__' or `__inline' if that's what the C compiler
calls it, or to nothing if 'inline' is not supported under any name. */
#ifndef __cplusplus
#undef inline
#endif
#endif /* PLPA_CONFIG_H */
/* -*- c -*-
*
* Copyright (c) 2004-2006 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2007-2008 Cisco Systems, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "plpa_config.h"
#include "plpa.h"
#include "plpa_internal.h"
#include <errno.h>
#include <sys/syscall.h>
#include <unistd.h>
/**
* Call the kernel's setaffinity, massaging the user's input
* parameters as necessary
*/
int PLPA_NAME(sched_setaffinity)(pid_t pid, size_t cpusetsize,
const PLPA_NAME(cpu_set_t) *cpuset)
{
int ret;
size_t i;
PLPA_NAME(cpu_set_t) tmp;
PLPA_NAME(api_type_t) api;
/* Check to see that we're initialized */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* Check for bozo arguments */
if (NULL == cpuset) {
return EINVAL;
}
/* Probe the API type */
if (0 != (ret = PLPA_NAME(api_probe)(&api))) {
return ret;
}
switch (api) {
case PLPA_NAME_CAPS(PROBE_OK):
/* This shouldn't happen, but check anyway */
if (cpusetsize > sizeof(*cpuset)) {
return EINVAL;
}
/* If the user-supplied bitmask is smaller than what the
kernel wants, zero out a temporary buffer of the size that
the kernel wants and copy the user-supplied bitmask to the
lower part of the temporary buffer. This could be done
more efficiently, but we're looking for clarity/simplicity
of code here -- this is not intended to be
performance-critical. */
if (cpusetsize < PLPA_NAME(len)) {
memset(&tmp, 0, sizeof(tmp));
for (i = 0; i < cpusetsize * 8; ++i) {
if (PLPA_CPU_ISSET(i, cpuset)) {
PLPA_CPU_SET(i, &tmp);
}
}
}
/* If the user-supplied bitmask is larger than what the kernel
will accept, scan it and see if there are any set bits in
the part larger than what the kernel will accept. If so,
return EINVAL. Otherwise, copy the part that the kernel
will accept into a temporary and use that. Again,
efficinency is not the issue of this code -- clarity is. */
else if (cpusetsize > PLPA_NAME(len)) {
for (i = PLPA_NAME(len) * 8; i < cpusetsize * 8; ++i) {
if (PLPA_CPU_ISSET(i, cpuset)) {
return EINVAL;
}
}
/* No upper-level bits are set, so now copy over the bits
that the kernel will look at */
memset(&tmp, 0, sizeof(tmp));
for (i = 0; i < PLPA_NAME(len) * 8; ++i) {
if (PLPA_CPU_ISSET(i, cpuset)) {
PLPA_CPU_SET(i, &tmp);
}
}
}
/* Otherwise, the user supplied a buffer that is exactly the
right size. Just for clarity of code, copy the user's
buffer into the temporary and use that. */
else {
memcpy(&tmp, cpuset, cpusetsize);
}
/* Now do the syscall */
ret = syscall(__NR_sched_setaffinity, pid, PLPA_NAME(len), &tmp);
/* Return 0 upon success. According to
http://www.open-mpi.org/community/lists/plpa-users/2006/02/0016.php,
all the kernel implementations return >= 0 upon success. */
return (ret >= 0) ? 0 : ret;
break;
case PLPA_NAME_CAPS(PROBE_NOT_SUPPORTED):
/* Process affinity not supported here */
return ENOSYS;
break;
default:
/* Something went wrong */
/* JMS: would be good to have something other than EINVAL here
-- suggestions? */
return EINVAL;
break;
}
}
/**
* Call the kernel's getaffinity, massaging the user's input
* parameters as necessary
*/
int PLPA_NAME(sched_getaffinity)(pid_t pid, size_t cpusetsize,
PLPA_NAME(cpu_set_t) *cpuset)
{
int ret;
PLPA_NAME(api_type_t) api;
/* Check to see that we're initialized */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* Check for bozo arguments */
if (NULL == cpuset) {
return EINVAL;
}
/* Probe the API type */
if (0 != (ret = PLPA_NAME(api_probe)(&api))) {
return ret;
}
switch (api) {
case PLPA_NAME_CAPS(PROBE_OK):
/* This shouldn't happen, but check anyway */
if (PLPA_NAME(len) > sizeof(*cpuset)) {
return EINVAL;
}
/* If the user supplied a buffer that is too small, then don't
even bother */
if (cpusetsize < PLPA_NAME(len)) {
return EINVAL;
}
/* Now we know that the user's buffer is >= the size required
by the kernel. If it's >, then zero it out so that the
bits at the top are cleared (since they won't be set by the
kernel) */
if (cpusetsize > PLPA_NAME(len)) {
memset(cpuset, 0, cpusetsize);
}
/* Now do the syscall */
ret = syscall(__NR_sched_getaffinity, pid, PLPA_NAME(len), cpuset);
/* Return 0 upon success. According to
http://www.open-mpi.org/community/lists/plpa-users/2006/02/0016.php,
all the kernel implementations return >= 0 upon success. */
return (ret >= 0) ? 0 : ret;
break;
case PLPA_NAME_CAPS(PROBE_NOT_SUPPORTED):
/* Process affinity not supported here */
return ENOSYS;
break;
default:
/* Something went wrong */
return EINVAL;
break;
}
}
/* -*- c -*-
*
* Copyright (c) 2004-2005 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2007-2008 Cisco Systems, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#ifndef PLPA_INTERNAL_H
#define PLPA_INTERNAL_H
#include <plpa.h>
/* Have we initialized yet? */
extern int PLPA_NAME(initialized);
/* Cached size of the affinity buffers that the kernel expects */
extern size_t PLPA_NAME(len);
/* Setup API type */
int PLPA_NAME(api_probe_init)(void);
#endif /* PLPA_INTERNAL_H */
/*
* Copyright (c) 2007-2008 Cisco Systems, Inc. All rights reserved.
*
* Portions of this file originally contributed by Advanced Micro
* Devices, Inc. See notice below.
*/
/* ============================================================
License Agreement
Copyright (c) 2006, 2007 Advanced Micro Devices, Inc.
All rights reserved.
Redistribution and use in any form of this material and any product
thereof including software in source or binary forms, along with any
related documentation, with or without modification ("this material"),
is permitted provided that the following conditions are met:
+ Redistributions of source code of any software must retain the above
copyright notice and all terms of this license as part of the code.
+ Redistributions in binary form of any software must reproduce the
above copyright notice and all terms of this license in any related
documentation and/or other materials.
+ Neither the names nor trademarks of Advanced Micro Devices, Inc. or
any copyright holders or contributors may be used to endorse or
promote products derived from this material without specific prior
written permission.
+ Notice about U.S. Government restricted rights: This material is
provided with "RESTRICTED RIGHTS." Use, duplication or disclosure by
the U.S. Government is subject to the full extent of restrictions set
forth in FAR52.227 and DFARS252.227 et seq., or any successor or
applicable regulations. Use of this material by the U.S. Government
constitutes acknowledgment of the proprietary rights of Advanced Micro
Devices, Inc.
and any copyright holders and contributors.
+ ANY BREACH OF ANY TERM OF THIS LICENSE SHALL RESULT IN THE IMMEDIATE
REVOCATION OF ALL RIGHTS TO REDISTRIBUTE, ACCESS OR USE THIS MATERIAL.
THIS MATERIAL IS PROVIDED BY ADVANCED MICRO DEVICES, INC. AND ANY
COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" IN ITS CURRENT CONDITION
AND WITHOUT ANY REPRESENTATIONS, GUARANTEE, OR WARRANTY OF ANY KIND OR
IN ANY WAY RELATED TO SUPPORT, INDEMNITY, ERROR FREE OR UNINTERRUPTED
OPERATION, OR THAT IT IS FREE FROM DEFECTS OR VIRUSES. ALL
OBLIGATIONS ARE HEREBY DISCLAIMED - WHETHER EXPRESS, IMPLIED, OR
STATUTORY - INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, ACCURACY,
COMPLETENESS, OPERABILITY, QUALITY OF SERVICE, OR NON-INFRINGEMENT. IN
NO EVENT SHALL ADVANCED MICRO DEVICES, INC. OR ANY COPYRIGHT HOLDERS
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, PUNITIVE, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, REVENUE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED OR BASED ON ANY THEORY OF LIABILITY ARISING IN ANY WAY RELATED
TO THIS MATERIAL, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE ENTIRE AND AGGREGATE LIABILITY OF ADVANCED MICRO DEVICES, INC. AND
ANY COPYRIGHT HOLDERS AND CONTRIBUTORS SHALL NOT EXCEED TEN DOLLARS
(US $10.00). ANYONE REDISTRIBUTING OR ACCESSING OR USING THIS MATERIAL
ACCEPTS THIS ALLOCATION OF RISK AND AGREES TO RELEASE ADVANCED MICRO
DEVICES, INC. AND ANY COPYRIGHT HOLDERS AND CONTRIBUTORS FROM ANY AND
ALL LIABILITIES, OBLIGATIONS, CLAIMS, OR DEMANDS IN EXCESS OF TEN
DOLLARS (US $10.00). THE FOREGOING ARE ESSENTIAL TERMS OF THIS LICENSE
AND, IF ANY OF THESE TERMS ARE CONSTRUED AS UNENFORCEABLE, FAIL IN
ESSENTIAL PURPOSE, OR BECOME VOID OR DETRIMENTAL TO ADVANCED MICRO
DEVICES, INC. OR ANY COPYRIGHT HOLDERS OR CONTRIBUTORS FOR ANY REASON,
THEN ALL RIGHTS TO REDISTRIBUTE, ACCESS OR USE THIS MATERIAL SHALL
TERMINATE IMMEDIATELY. MOREOVER, THE FOREGOING SHALL SURVIVE ANY
EXPIRATION OR TERMINATION OF THIS LICENSE OR ANY AGREEMENT OR ACCESS
OR USE RELATED TO THIS MATERIAL.
NOTICE IS HEREBY PROVIDED, AND BY REDISTRIBUTING OR ACCESSING OR USING
THIS MATERIAL SUCH NOTICE IS ACKNOWLEDGED, THAT THIS MATERIAL MAY BE
SUBJECT TO RESTRICTIONS UNDER THE LAWS AND REGULATIONS OF THE UNITED
STATES OR OTHER COUNTRIES, WHICH INCLUDE BUT ARE NOT LIMITED TO, U.S.
EXPORT CONTROL LAWS SUCH AS THE EXPORT ADMINISTRATION REGULATIONS AND
NATIONAL SECURITY CONTROLS AS DEFINED THEREUNDER, AS WELL AS STATE
DEPARTMENT CONTROLS UNDER THE U.S. MUNITIONS LIST. THIS MATERIAL MAY
NOT BE USED, RELEASED, TRANSFERRED, IMPORTED, EXPORTED AND/OR RE-
EXPORTED IN ANY MANNER PROHIBITED UNDER ANY APPLICABLE LAWS, INCLUDING
U.S. EXPORT CONTROL LAWS REGARDING SPECIFICALLY DESIGNATED PERSONS,
COUNTRIES AND NATIONALS OF COUNTRIES SUBJECT TO NATIONAL SECURITY
CONTROLS.
MOREOVER,
THE FOREGOING SHALL SURVIVE ANY EXPIRATION OR TERMINATION OF ANY
LICENSE OR AGREEMENT OR ACCESS OR USE RELATED TO THIS MATERIAL.
This license forms the entire agreement regarding the subject matter
hereof and supersedes all proposals and prior discussions and writings
between the parties with respect thereto. This license does not affect
any ownership, rights, title, or interest in, or relating to, this
material. No terms of this license can be modified or waived, and no
breach of this license can be excused, unless done so in a writing
signed by all affected parties. Each term of this license is
separately enforceable. If any term of this license is determined to
be or becomes unenforceable or illegal, such term shall be reformed to
the minimum extent necessary in order for this license to remain in
effect in accordance with its terms as modified by such reformation.
This license shall be governed by and construed in accordance with the
laws of the State of Texas without regard to rules on conflicts of law
of any state or jurisdiction or the United Nations Convention on the
International Sale of Goods. All disputes arising out of this license
shall be subject to the jurisdiction of the federal and state courts
in Austin, Texas, and all defenses are hereby waived concerning
personal jurisdiction and venue of these courts.
============================================================ */
#include "plpa_config.h"
#include "plpa.h"
#include "plpa_internal.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct tuple_t_ {
int processor_id, socket_id, core_id, online;
} tuple_t;
static const char *sysfs_mount = "/sys";
static int supported = 0;
static int num_processors = -1;
static int max_processor_id = -1;
static int num_sockets = -1;
static int max_socket_id = -1;
static int *max_core_id = NULL;
static int *num_cores = NULL;
static int max_core_id_overall = -1;
static tuple_t *map_processor_id_to_tuple = NULL;
static tuple_t **map_tuple_to_processor_id = NULL;
static PLPA_NAME(cache_behavior_t) cache_behavior = PLPA_NAME_CAPS(CACHE_IGNORE);
static void clear_cache(void)
{
if (NULL != max_core_id) {
free(max_core_id);
max_core_id = NULL;
}
if (NULL != num_cores) {
free(num_cores);
num_cores = NULL;
}
if (NULL != map_processor_id_to_tuple) {
free(map_processor_id_to_tuple);
map_processor_id_to_tuple = NULL;
}
if (NULL != map_tuple_to_processor_id) {
free(map_tuple_to_processor_id);
map_tuple_to_processor_id = NULL;
}
num_processors = max_processor_id = -1;
num_sockets = max_socket_id = -1;
max_core_id_overall = -1;
}
static void load_cache(void)
{
int i, j, k, invalid_entry, fd, found_online;
char path[PATH_MAX], buf[8];
PLPA_NAME(cpu_set_t) valid_processors;
PLPA_NAME(cpu_set_t) *cores_on_sockets;
int found;
DIR *dir;
struct dirent dentry, *dentryp = NULL;
#if PLPA_DEBUG
char *temp = getenv("PLPA_SYSFS_MOUNT");
if (temp) {
sysfs_mount = temp;
}
#endif
/* Check for the parent directory */
sprintf(path, "%s/devices/system/cpu", sysfs_mount);
if (access(path, R_OK|X_OK)) {
return;
}
dir = opendir(path);
if (NULL == dir) {
return;
}
/* Catch all entries of format "cpu%d", count them and maintain
max_processor_id */
num_processors = 0;
PLPA_CPU_ZERO(&valid_processors);
do {
int ret = readdir_r(dir, &dentry, &dentryp);
if (0 != ret) {
closedir(dir);
clear_cache();
return;
}
if (dentryp) {
int cpuid;
ret = sscanf(dentryp->d_name, "cpu%d", &cpuid);
if (1 == ret) {
++num_processors;
if (cpuid >= PLPA_BITMASK_CPU_MAX) {
closedir(dir);
clear_cache();
return;
} else if (cpuid > max_processor_id) {
max_processor_id = cpuid;
}
PLPA_CPU_SET(cpuid, &valid_processors);
}
}
} while (NULL != dentryp);
closedir(dir);
/* If we found no processors, then we have no topology info */
if (0 == num_processors) {
clear_cache();
return;
}
/* Malloc space for the first map (processor ID -> tuple).
Include enough space for one invalid entry. */
map_processor_id_to_tuple = malloc(sizeof(tuple_t) *
(max_processor_id + 2));
if (NULL == map_processor_id_to_tuple) {
clear_cache();
return;
}
for (i = 0; i <= max_processor_id; ++i) {
if (PLPA_CPU_ISSET(i, &valid_processors)) {
map_processor_id_to_tuple[i].processor_id = i;
} else {
map_processor_id_to_tuple[i].processor_id = -1;
}
map_processor_id_to_tuple[i].socket_id = -1;
map_processor_id_to_tuple[i].core_id = -1;
}
/* Set the invalid entry */
invalid_entry = i;
map_processor_id_to_tuple[invalid_entry].processor_id = -1;
map_processor_id_to_tuple[invalid_entry].socket_id = -1;
map_processor_id_to_tuple[invalid_entry].core_id = -1;
/* Build a cached map of (socket,core) tuples */
for (found = 0, i = 0; i <= max_processor_id; ++i) {
/* Check for invalid processor ID */
if (map_processor_id_to_tuple[i].processor_id < 0) {
continue;
}
/* Read the "online" state for this processor. If the online
file is not there, then the kernel likely doesn't have
hotplug support so just assume that it's online. Some notes:
- the perms on the "online" file are root/600, so only root
will see this info
- if online is 0, then all the topology files disappear (!)
-- so PLPA needs to compensate for that
*/
found_online = 0;
sprintf(path, "%s/devices/system/cpu/cpu%d/online",
sysfs_mount, i);
fd = open(path, O_RDONLY);
memset(buf, 0, sizeof(buf));
if (fd >= 0 && read(fd, buf, sizeof(buf) - 1) > 0) {
found_online = 1;
sscanf(buf, "%d", &(map_processor_id_to_tuple[i].online));
} else {
map_processor_id_to_tuple[i].online = 1;
}
if (fd >= 0) {
close(fd);
}
/* Core ID */
sprintf(path, "%s/devices/system/cpu/cpu%d/topology/core_id",
sysfs_mount, i);
fd = open(path, O_RDONLY);
if (fd >= 0) {
memset(buf, 0, sizeof(buf));
if (read(fd, buf, sizeof(buf) - 1) > 0) {
sscanf(buf, "%d", &(map_processor_id_to_tuple[i].core_id));
} else {
map_processor_id_to_tuple[i].core_id = -1;
}
close(fd);
}
/* Special case: we didn't find the core_id file, but we *did*
find the online file and the processor is offline -- then
just mark the core ID as "unknown" and keep going (because
if a processor is offline, the core_id file won't exist --
grumble) */
else if (found_online && 0 == map_processor_id_to_tuple[i].online) {
map_processor_id_to_tuple[i].core_id = -1;
}
/* Socket ID */
sprintf(path,
"%s/devices/system/cpu/cpu%d/topology/physical_package_id",
sysfs_mount, i);
fd = open(path, O_RDONLY);
if (fd >= 0) {
memset(buf, 0, sizeof(buf));
if (read(fd, buf, sizeof(buf) - 1) > 0) {
sscanf(buf, "%d", &(map_processor_id_to_tuple[i].socket_id));
}
close(fd);
found = 1;
}
/* Special case: we didn't find the socket_id file, but we
*did* find the online file and the processor is offline --
then just mark the socket ID as "unknown" and keep going
(because if a processor is offline, the socket_id file won't
exist -- grumble) */
else if (found_online && 0 == map_processor_id_to_tuple[i].online) {
map_processor_id_to_tuple[i].socket_id = -1;
}
/* Keep a running tab on the max socket number */
if (map_processor_id_to_tuple[i].socket_id > max_socket_id) {
max_socket_id = map_processor_id_to_tuple[i].socket_id;
}
}
/* If we didn't find any core_id/physical_package_id's, then we
don't have the topology info */
if (!found) {
clear_cache();
return;
}
/* Now that we know the max number of sockets, allocate some
arrays */
max_core_id = malloc(sizeof(int) * (max_socket_id + 1));
if (NULL == max_core_id) {
clear_cache();
return;
}
num_cores = malloc(sizeof(int) * (max_socket_id + 1));
if (NULL == num_cores) {
clear_cache();
return;
}
for (i = 0; i <= max_socket_id; ++i) {
num_cores[i] = -1;
max_core_id[i] = -1;
}
/* Find the max core number on each socket */
for (i = 0; i <= max_processor_id; ++i) {
if (map_processor_id_to_tuple[i].processor_id < 0 ||
map_processor_id_to_tuple[i].socket_id < 0) {
continue;
}
if (map_processor_id_to_tuple[i].core_id >
max_core_id[map_processor_id_to_tuple[i].socket_id]) {
max_core_id[map_processor_id_to_tuple[i].socket_id] =
map_processor_id_to_tuple[i].core_id;
}
if (max_core_id[map_processor_id_to_tuple[i].socket_id] >
max_core_id_overall) {
max_core_id_overall =
max_core_id[map_processor_id_to_tuple[i].socket_id];
}
}
/* Go through and count the number of unique sockets found. It
may not be the same as max_socket_id because there may be
"holes" -- e.g., sockets 0 and 3 are used, but sockets 1 and 2
are empty. */
for (j = i = 0; i <= max_socket_id; ++i) {
if (max_core_id[i] >= 0) {
++j;
}
}
if (j > 0) {
num_sockets = j;
}
/* Count how many cores are available on each socket. This may
not be the same as max_core_id[socket_num] if there are
"holes". I don't know if holes can happen (i.e., if specific
cores can be taken offline), but what the heck... */
cores_on_sockets = malloc(sizeof(PLPA_NAME(cpu_set_t)) *
(max_socket_id + 1));
if (NULL == cores_on_sockets) {
clear_cache();
return;
}
for (i = 0; i <= max_socket_id; ++i) {
PLPA_CPU_ZERO(&(cores_on_sockets[i]));
}
for (i = 0; i <= max_processor_id; ++i) {
if (map_processor_id_to_tuple[i].socket_id >= 0) {
PLPA_CPU_SET(map_processor_id_to_tuple[i].core_id,
&(cores_on_sockets[map_processor_id_to_tuple[i].socket_id]));
}
}
for (i = 0; i <= max_socket_id; ++i) {
int count = 0;
for (j = 0; j <= max_core_id[i]; ++j) {
if (PLPA_CPU_ISSET(j, &(cores_on_sockets[i]))) {
++count;
}
}
if (count > 0) {
num_cores[i] = count;
}
}
free(cores_on_sockets);
/* Now go through and build the map in the other direction:
(socket,core) => processor_id. This map simply points to
entries in the other map (i.e., it's by reference instead of by
value). */
map_tuple_to_processor_id = malloc(sizeof(tuple_t *) *
((max_socket_id + 1) *
(max_core_id_overall + 1)));
if (NULL == map_tuple_to_processor_id) {
clear_cache();
return;
}
/* Compute map */
for (i = 0; i <= max_socket_id; ++i) {
for (j = 0; j <= max_core_id_overall; ++j) {
tuple_t **tuple_ptr = &map_tuple_to_processor_id[
i * (max_core_id_overall + 1) + j];
/* Default to the invalid entry in the other map, meaning
that this (socket,core) combination doesn't exist
(e.g., the core number does not exist in this socket,
although it does exist in other sockets). */
*tuple_ptr = &map_processor_id_to_tuple[invalid_entry];
/* See if this (socket,core) tuple exists in the other
map. If so, set this entry to point to it (overriding
the invalid entry default). */
for (k = 0; k <= max_processor_id; ++k) {
if (map_processor_id_to_tuple[k].socket_id == i &&
map_processor_id_to_tuple[k].core_id == j) {
*tuple_ptr = &map_processor_id_to_tuple[k];
break;
}
}
}
}
supported = 1;
}
static int cache_action(void)
{
switch (cache_behavior) {
case PLPA_NAME_CAPS(CACHE_USE):
if (NULL == map_processor_id_to_tuple) {
load_cache();
}
break;
case PLPA_NAME_CAPS(CACHE_IGNORE):
clear_cache();
load_cache();
break;
default:
return EINVAL;
}
return 0;
}
/* Return whether this kernel supports topology information or not */
int PLPA_NAME(have_topology_information)(int *supported_arg)
{
int ret;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* Check for bozo arguments */
if (NULL == supported_arg) {
return EINVAL;
}
*supported_arg = supported;
return 0;
}
int PLPA_NAME(map_to_processor_id)(int socket_id, int core_id,
int *processor_id)
{
int ret;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check for bozo arguments */
if (NULL == processor_id) {
return EINVAL;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for some invalid entries */
if (socket_id < 0 || socket_id > max_socket_id ||
core_id < 0 || core_id > max_core_id[socket_id]) {
return ENOENT;
}
/* If the mapping returns -1, then this is a non-existent
socket/core combo (even though they fall within the max socket
/ max core overall values) */
ret = map_tuple_to_processor_id[socket_id * (max_core_id_overall + 1) +
core_id]->processor_id;
if (-1 == ret) {
return ENOENT;
}
/* Ok, all should be good -- return the mapping */
*processor_id = ret;
return 0;
}
int PLPA_NAME(map_to_socket_core)(int processor_id,
int *socket_id, int *core_id)
{
int ret;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check for bozo arguments */
if (NULL == socket_id || NULL == core_id) {
return EINVAL;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for some invalid entries */
if (processor_id < 0 || processor_id > max_processor_id ||
map_processor_id_to_tuple[processor_id].processor_id < 0) {
return ENOENT;
}
ret = map_processor_id_to_tuple[processor_id].socket_id;
if (-1 == ret) {
return ENOENT;
}
/* Ok, all should be good -- return the mapping */
*socket_id = ret;
*core_id = map_processor_id_to_tuple[processor_id].core_id;
return 0;
}
/* Deprecated function */
int PLPA_NAME(get_processor_info)(int *num_processors_arg,
int *max_processor_id_arg)
{
return PLPA_NAME(get_processor_data)(PLPA_NAME_CAPS(COUNT_ALL),
num_processors_arg,
max_processor_id_arg);
}
int PLPA_NAME(get_processor_data)(PLPA_NAME(count_specification_t) count_spec,
int *num_processors_arg,
int *max_processor_id_arg)
{
int i, ret;
bool match;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for bozo arguments */
if (NULL == max_processor_id_arg || NULL == num_processors_arg) {
return EINVAL;
}
/* If we wanted all processors, we're done */
if (PLPA_NAME_CAPS(COUNT_ALL) == count_spec) {
*num_processors_arg = num_processors;
*max_processor_id_arg = max_processor_id;
} else {
/* Otherwise, count the appropriate type */
*num_processors_arg = 0;
*max_processor_id_arg = 0;
for (i = 0; i <= max_processor_id; ++i) {
if (map_processor_id_to_tuple[i].processor_id >= 0) {
match = false;
switch (count_spec) {
case PLPA_NAME_CAPS(COUNT_ONLINE):
if (map_processor_id_to_tuple[i].online) {
match = true;
}
break;
case PLPA_NAME_CAPS(COUNT_OFFLINE):
if (!map_processor_id_to_tuple[i].online) {
match = true;
}
break;
default:
/* Just so that compilers don't complain */
break;
}
if (match) {
++(*num_processors_arg);
if (*max_processor_id_arg <
map_processor_id_to_tuple[i].processor_id) {
*max_processor_id_arg =
map_processor_id_to_tuple[i].processor_id;
}
}
}
}
}
return 0;
}
/* Returns the Linux processor ID for the Nth processor (starting with
0). */
int PLPA_NAME(get_processor_id)(int processor_num,
PLPA_NAME(count_specification_t) count_spec,
int *processor_id)
{
int ret, i, count;
bool match;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check for bozo arguments */
if (NULL == processor_id) {
return EINVAL;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for out of range params */
if (processor_num < 0 || processor_num > num_processors) {
return EINVAL;
}
/* Find the processor_num'th processor */
for (count = i = 0; i <= max_processor_id; ++i) {
if (map_processor_id_to_tuple[i].processor_id >= 0) {
match = false;
switch (count_spec) {
case PLPA_NAME_CAPS(COUNT_ONLINE):
if (map_processor_id_to_tuple[i].online) {
match = true;
}
break;
case PLPA_NAME_CAPS(COUNT_OFFLINE):
if (!map_processor_id_to_tuple[i].online) {
match = true;
}
break;
case PLPA_NAME_CAPS(COUNT_ALL):
match = true;
break;
}
if (match) {
if (count++ == processor_num) {
*processor_id = map_processor_id_to_tuple[i].processor_id;
return 0;
}
}
}
}
/* Didn't find it */
return ENODEV;
}
/* Check to see if a given Linux processor ID exists / is online.
Returns 0 on success. */
int PLPA_NAME(get_processor_flags)(int processor_id,
int *exists_arg, int *online_arg)
{
int ret, exists, online;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check for bozo arguments */
if (NULL == exists_arg && NULL == online_arg) {
return EINVAL;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for out of range params */
if (processor_id < 0 || processor_id > max_processor_id) {
return EINVAL;
}
exists = online = 0;
if (processor_id == map_processor_id_to_tuple[processor_id].processor_id) {
exists = 1;
if (map_processor_id_to_tuple[processor_id].online) {
online = 1;
}
}
if (NULL != exists_arg) {
*exists_arg = exists;
}
if (NULL != online_arg) {
*online_arg = online;
}
return 0;
}
/* Return the max socket number */
int PLPA_NAME(get_socket_info)(int *num_sockets_arg, int *max_socket_id_arg)
{
int ret;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for bozo arguments */
if (NULL == max_socket_id_arg || NULL == num_sockets_arg) {
return EINVAL;
}
/* All done */
*num_sockets_arg = num_sockets;
*max_socket_id_arg = max_socket_id;
return 0;
}
/* Returns the Linux socket ID for the Nth socket (starting with 0). */
int PLPA_NAME(get_socket_id)(int socket_num, int *socket_id)
{
int ret, i, j, k, count;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check for bozo arguments */
if (NULL == socket_id) {
return EINVAL;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for out of range params */
if (socket_num < 0 || socket_num > num_sockets) {
return EINVAL;
}
/* Find the socket_num'th socket */
for (count = i = 0; i <= max_socket_id; ++i) {
/* See if any core in this socket is active. If so, count
this socket */
for (j = 0; j <= max_core_id_overall; ++j) {
k = i * (max_core_id_overall + 1) + j;
if (map_tuple_to_processor_id[k]->processor_id >= 0) {
if (count++ == socket_num) {
*socket_id = map_tuple_to_processor_id[k]->socket_id;
return 0;
}
/* Ok, we found one -- skip to the end of this socket */
j = max_core_id_overall + 1;
}
}
}
/* Didn't find it */
return ENODEV;
}
/* Return the number of cores in a socket and the max core ID number */
int PLPA_NAME(get_core_info)(int socket_id, int *num_cores_arg,
int *max_core_id_arg)
{
int ret;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check for bozo arguments */
if (NULL == max_core_id_arg || NULL == num_cores_arg) {
return EINVAL;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for some invalid entries */
if (socket_id < 0 || socket_id > max_socket_id ||
-1 == max_core_id[socket_id]) {
return ENOENT;
}
ret = num_cores[socket_id];
if (-1 == ret) {
return ENOENT;
}
/* All done */
*num_cores_arg = ret;
*max_core_id_arg = max_core_id[socket_id];
return 0;
}
/* Given a specific socket, returns the Linux core ID for the Nth core
(starting with 0) */
int PLPA_NAME(get_core_id)(int socket_id, int core_num, int *core_id)
{
int ret, i, j, count;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check for bozo arguments */
if (NULL == core_id) {
return EINVAL;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for out of range params */
if (socket_id < 0 || socket_id > max_socket_id ||
core_num < 0 || core_num > max_core_id_overall) {
return EINVAL;
}
/* Find the core_num'th core */
for (count = i = 0, j = socket_id * (max_core_id_overall + 1);
i <= max_core_id_overall; ++i) {
if (map_tuple_to_processor_id[j + i]->processor_id >= 0) {
if (count++ == core_num) {
*core_id = map_tuple_to_processor_id[j + i]->core_id;
return 0;
}
}
}
/* Didn't find it */
return ENODEV;
}
/* Check to see if a given Linux (socket_id,core_id) tuple exists / is
online. Returns 0 on success. */
int PLPA_NAME(get_core_flags)(int socket_id, int core_id,
int *exists_arg, int *online_arg)
{
int ret, i, exists, online;
/* Initialize if not already done so */
if (!PLPA_NAME(initialized)) {
if (0 != (ret = PLPA_NAME(init)())) {
return ret;
}
}
/* If this system doesn't support mapping, sorry Charlie */
if (!supported) {
return ENOSYS;
}
/* Check for bozo arguments */
if (NULL == exists_arg && NULL == online_arg) {
return EINVAL;
}
/* Check cache behavior */
if (0 != (ret = cache_action())) {
return ret;
}
/* Check for out of range params */
if (socket_id < 0 || socket_id > max_socket_id ||
core_id < 0 || core_id > max_core_id_overall) {
return EINVAL;
}
exists = online = 0;
i = socket_id * (max_core_id_overall + 1) + core_id;
if (map_tuple_to_processor_id[i]->processor_id >= 0) {
exists = 1;
if (map_tuple_to_processor_id[i]->online) {
online = 1;
}
}
if (NULL != exists_arg) {
*exists_arg = exists;
}
if (NULL != online_arg) {
*online_arg = online;
}
return 0;
}
/* Set PLPA's caching behavior */
int PLPA_NAME(set_cache_behavior)(PLPA_NAME(cache_behavior_t) behavior)
{
switch (behavior) {
case PLPA_NAME_CAPS(CACHE_USE):
if (PLPA_NAME_CAPS(CACHE_USE) != cache_behavior) {
load_cache();
cache_behavior = PLPA_NAME_CAPS(CACHE_USE);
}
break;
case PLPA_NAME_CAPS(CACHE_IGNORE):
if (PLPA_NAME_CAPS(CACHE_IGNORE) != cache_behavior) {
clear_cache();
cache_behavior = PLPA_NAME_CAPS(CACHE_IGNORE);
}
break;
case PLPA_NAME_CAPS(CACHE_REFRESH):
if (PLPA_NAME_CAPS(CACHE_USE) != cache_behavior) {
return EINVAL;
}
clear_cache();
load_cache();
break;
default:
return EINVAL;
}
return 0;
}
/*
* Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
*/
#include "plpa_config.h"
#include "plpa.h"
#include "plpa_internal.h"
#include <errno.h>
#include <pthread.h>
/* Global variables */
int PLPA_NAME(initialized) = 0;
/* Local variables */
static int refcount = 0;
static pthread_mutex_t mutex;
/* Central clearing point for all parts of PLPA that need to be
initialized. It is erroneous to call this function by more than
one thread simultaneously. */
int PLPA_NAME(init)(void)
{
int ret;
/* If we're already initialized, simply increase the refcount */
if (PLPA_NAME(initialized)) {
pthread_mutex_lock(&mutex);
++refcount;
pthread_mutex_unlock(&mutex);
return 0;
}
/* Otherwise, initialize all the sybsystems */
if (0 != (ret = pthread_mutex_init(&mutex, NULL)) ||
0 != (ret = PLPA_NAME(api_probe_init)()) ||
0 != (ret = PLPA_NAME(set_cache_behavior)(PLPA_NAME_CAPS(CACHE_USE)))) {
return ret;
}
PLPA_NAME(initialized) = 1;
refcount = 1;
return 0;
}
/* Central clearing point for all parts of PLPA that need to be
shutdown. */
int PLPA_NAME(finalize)(void)
{
int val;
/* If we're not initialized, return an error */
if (!PLPA_NAME(initialized)) {
return ENOENT;
}
/* Decrement and check the refcount. If it's nonzero, then simply
return success. */
pthread_mutex_lock(&mutex);
val = --refcount;
pthread_mutex_unlock(&mutex);
if (0 != val) {
return 0;
}
/* Ok, we're the last one. Cleanup. */
PLPA_NAME(set_cache_behavior)(PLPA_NAME_CAPS(CACHE_IGNORE));
pthread_mutex_destroy(&mutex);
PLPA_NAME(initialized) = 0;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment