2014-06-16 10:48:01 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Nicira, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* On non-Linux, these functions are defined inline in ovs-numa.h. */
|
|
|
|
#ifdef __linux__
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include "ovs-numa.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "hash.h"
|
|
|
|
#include "hmap.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "ovs-thread.h"
|
|
|
|
#include "vlog.h"
|
|
|
|
|
|
|
|
VLOG_DEFINE_THIS_MODULE(ovs_numa);
|
|
|
|
|
2014-09-05 06:17:34 +00:00
|
|
|
/* ovs-numa module
|
|
|
|
* ===============
|
|
|
|
*
|
|
|
|
* This module stores the affinity information of numa nodes and cpu cores.
|
|
|
|
* It also provides functions to bookkeep the pin of threads on cpu cores.
|
|
|
|
*
|
|
|
|
* It is assumed that the numa node ids and cpu core ids all start from 0 and
|
|
|
|
* range continuously. So, for example, if 'ovs_numa_get_n_cores()' returns N,
|
|
|
|
* user can assume core ids from 0 to N-1 are all valid and there is a
|
|
|
|
* 'struct cpu_core' for each id.
|
|
|
|
*
|
|
|
|
* NOTE, the assumption above will fail when cpu hotplug is used. In that
|
|
|
|
* case ovs-numa will not function correctly. For now, add a TODO entry
|
|
|
|
* for addressing it in the future.
|
|
|
|
*
|
|
|
|
* TODO: Fix ovs-numa when cpu hotplug is used.
|
|
|
|
*/
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
#define MAX_NUMA_NODES 128
|
2014-06-16 10:48:01 -07:00
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
/* numa node. */
|
|
|
|
struct numa_node {
|
|
|
|
struct hmap_node hmap_node; /* In the 'all_numa_nodes'. */
|
2014-12-15 14:10:38 +01:00
|
|
|
struct ovs_list cores; /* List of cpu cores on the numa node. */
|
2014-09-05 06:17:31 +00:00
|
|
|
int numa_id; /* numa node id. */
|
2014-06-16 10:48:01 -07:00
|
|
|
};
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
/* Cpu core on a numa node. */
|
2014-06-16 10:48:01 -07:00
|
|
|
struct cpu_core {
|
|
|
|
struct hmap_node hmap_node;/* In the 'all_cpu_cores'. */
|
2014-12-15 14:10:38 +01:00
|
|
|
struct ovs_list list_node; /* In 'numa_node->cores' list. */
|
2014-09-05 06:17:31 +00:00
|
|
|
struct numa_node *numa; /* numa node containing the core. */
|
2014-06-16 10:48:01 -07:00
|
|
|
int core_id; /* Core id. */
|
2014-06-22 18:08:15 -07:00
|
|
|
bool available; /* If the core can be pinned. */
|
2014-06-16 10:48:01 -07:00
|
|
|
bool pinned; /* If a thread has been pinned to the core. */
|
|
|
|
};
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
/* Contains all 'struct numa_node's. */
|
|
|
|
static struct hmap all_numa_nodes = HMAP_INITIALIZER(&all_numa_nodes);
|
2014-06-16 10:48:01 -07:00
|
|
|
/* Contains all 'struct cpu_core's. */
|
|
|
|
static struct hmap all_cpu_cores = HMAP_INITIALIZER(&all_cpu_cores);
|
2014-09-05 06:17:31 +00:00
|
|
|
/* True if numa node and core info are correctly extracted. */
|
|
|
|
static bool found_numa_and_core;
|
2014-06-16 10:48:01 -07:00
|
|
|
|
|
|
|
/* Returns true if 'str' contains all digits. Returns false otherwise. */
|
|
|
|
static bool
|
|
|
|
contain_all_digits(const char *str)
|
|
|
|
{
|
|
|
|
return str[strspn(str, "0123456789")] == '\0';
|
|
|
|
}
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
/* Discovers all numa nodes and the corresponding cpu cores.
|
|
|
|
* Constructs the 'struct numa_node' and 'struct cpu_core'. */
|
2014-06-16 10:48:01 -07:00
|
|
|
static void
|
2014-09-05 06:17:31 +00:00
|
|
|
discover_numa_and_core(void)
|
2014-06-16 10:48:01 -07:00
|
|
|
{
|
|
|
|
int n_cpus = 0;
|
|
|
|
int i;
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
for (i = 0; i < MAX_NUMA_NODES; i++) {
|
2014-06-16 10:48:01 -07:00
|
|
|
DIR *dir;
|
|
|
|
char* path;
|
|
|
|
|
|
|
|
/* Constructs the path to node /sys/devices/system/nodeX. */
|
|
|
|
path = xasprintf("/sys/devices/system/node/node%d", i);
|
|
|
|
dir = opendir(path);
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
/* Creates 'struct numa_node' if the 'dir' is non-null. */
|
2014-06-16 10:48:01 -07:00
|
|
|
if (dir) {
|
2014-09-05 06:17:31 +00:00
|
|
|
struct numa_node *n = xzalloc(sizeof *n);
|
2014-06-16 10:48:01 -07:00
|
|
|
struct dirent *subdir;
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
hmap_insert(&all_numa_nodes, &n->hmap_node, hash_int(i, 0));
|
|
|
|
list_init(&n->cores);
|
|
|
|
n->numa_id = i;
|
2014-06-16 10:48:01 -07:00
|
|
|
|
|
|
|
while ((subdir = readdir(dir)) != NULL) {
|
|
|
|
if (!strncmp(subdir->d_name, "cpu", 3)
|
|
|
|
&& contain_all_digits(subdir->d_name + 3)){
|
|
|
|
struct cpu_core *c = xzalloc(sizeof *c);
|
|
|
|
uint32_t core_id;
|
|
|
|
|
|
|
|
core_id = strtoul(subdir->d_name + 3, NULL, 10);
|
|
|
|
hmap_insert(&all_cpu_cores, &c->hmap_node,
|
|
|
|
hash_int(core_id, 0));
|
2014-09-05 06:17:31 +00:00
|
|
|
list_insert(&n->cores, &c->list_node);
|
2014-06-16 10:48:01 -07:00
|
|
|
c->core_id = core_id;
|
2014-09-08 08:24:15 -07:00
|
|
|
c->numa = n;
|
2014-06-22 18:08:15 -07:00
|
|
|
c->available = true;
|
2014-06-16 10:48:01 -07:00
|
|
|
n_cpus++;
|
|
|
|
}
|
|
|
|
}
|
2014-09-05 06:17:31 +00:00
|
|
|
VLOG_INFO("Discovered %"PRIuSIZE" CPU cores on NUMA node %d",
|
|
|
|
list_size(&n->cores), n->numa_id);
|
2014-06-16 10:48:01 -07:00
|
|
|
free(path);
|
|
|
|
closedir(dir);
|
|
|
|
} else {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
VLOG_WARN("opendir(%s) failed (%s)", path,
|
|
|
|
ovs_strerror(errno));
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
VLOG_INFO("Discovered %"PRIuSIZE" NUMA nodes and %d CPU cores",
|
|
|
|
hmap_count(&all_numa_nodes), n_cpus);
|
|
|
|
if (hmap_count(&all_numa_nodes) && hmap_count(&all_cpu_cores)) {
|
|
|
|
found_numa_and_core = true;
|
2014-06-16 10:48:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extracts the numa node and core info from the 'sysfs'. */
|
|
|
|
void
|
|
|
|
ovs_numa_init(void)
|
|
|
|
{
|
|
|
|
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
|
|
|
|
|
|
|
|
if (ovsthread_once_start(&once)) {
|
2014-09-05 06:17:31 +00:00
|
|
|
discover_numa_and_core();
|
2014-06-16 10:48:01 -07:00
|
|
|
ovsthread_once_done(&once);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-09-05 06:17:31 +00:00
|
|
|
ovs_numa_numa_id_is_valid(int numa_id)
|
2014-06-16 10:48:01 -07:00
|
|
|
{
|
2014-09-05 06:17:32 +00:00
|
|
|
return found_numa_and_core && numa_id < ovs_numa_get_n_numas();
|
2014-06-16 10:48:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-09-05 06:17:31 +00:00
|
|
|
ovs_numa_core_id_is_valid(int core_id)
|
2014-06-16 10:48:01 -07:00
|
|
|
{
|
2014-09-05 06:17:32 +00:00
|
|
|
return found_numa_and_core && core_id < ovs_numa_get_n_cores();
|
2014-06-16 10:48:01 -07:00
|
|
|
}
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
/* Returns the number of numa nodes. */
|
2014-06-16 10:48:01 -07:00
|
|
|
int
|
2014-09-05 06:17:31 +00:00
|
|
|
ovs_numa_get_n_numas(void)
|
2014-06-16 10:48:01 -07:00
|
|
|
{
|
2014-09-05 06:17:31 +00:00
|
|
|
return found_numa_and_core ? hmap_count(&all_numa_nodes)
|
|
|
|
: OVS_NUMA_UNSPEC;
|
2014-06-16 10:48:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the number of cpu cores. */
|
|
|
|
int
|
|
|
|
ovs_numa_get_n_cores(void)
|
|
|
|
{
|
2014-09-05 06:17:31 +00:00
|
|
|
return found_numa_and_core ? hmap_count(&all_cpu_cores)
|
|
|
|
: OVS_CORE_UNSPEC;
|
2014-06-16 10:48:01 -07:00
|
|
|
}
|
|
|
|
|
2014-09-05 06:17:33 +00:00
|
|
|
/* Given 'core_id', returns the corresponding numa node id. Returns
|
|
|
|
* OVS_NUMA_UNSPEC if 'core_id' is invalid. */
|
|
|
|
int
|
|
|
|
ovs_numa_get_numa_id(int core_id)
|
|
|
|
{
|
|
|
|
if (ovs_numa_core_id_is_valid(core_id)) {
|
|
|
|
struct cpu_core *core;
|
|
|
|
|
|
|
|
core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
|
|
|
|
hash_int(core_id, 0)),
|
|
|
|
struct cpu_core, hmap_node);
|
|
|
|
|
|
|
|
return core->numa->numa_id;
|
|
|
|
}
|
|
|
|
return OVS_NUMA_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2014-09-05 06:17:32 +00:00
|
|
|
/* Returns the number of cpu cores on numa node. Returns OVS_CORE_UNSPEC
|
|
|
|
* if 'numa_id' is invalid. */
|
2014-06-16 10:48:01 -07:00
|
|
|
int
|
2014-09-05 06:17:31 +00:00
|
|
|
ovs_numa_get_n_cores_on_numa(int numa_id)
|
2014-06-16 10:48:01 -07:00
|
|
|
{
|
2014-09-05 06:17:32 +00:00
|
|
|
if (ovs_numa_numa_id_is_valid(numa_id)) {
|
2014-09-05 06:17:31 +00:00
|
|
|
struct numa_node *numa;
|
2014-06-16 10:48:01 -07:00
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
|
|
|
|
hash_int(numa_id, 0)),
|
|
|
|
struct numa_node, hmap_node);
|
2014-06-16 10:48:01 -07:00
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
return list_size(&numa->cores);
|
2014-06-16 10:48:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return OVS_CORE_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2014-06-22 18:08:15 -07:00
|
|
|
/* Returns the number of cpu cores that are available and unpinned
|
|
|
|
* on numa node. Returns OVS_CORE_UNSPEC if 'numa_id' is invalid. */
|
2014-06-16 10:48:01 -07:00
|
|
|
int
|
2014-09-05 06:17:31 +00:00
|
|
|
ovs_numa_get_n_unpinned_cores_on_numa(int numa_id)
|
2014-06-16 10:48:01 -07:00
|
|
|
{
|
2014-09-05 06:17:32 +00:00
|
|
|
if (ovs_numa_numa_id_is_valid(numa_id)) {
|
2014-09-05 06:17:31 +00:00
|
|
|
struct numa_node *numa;
|
2014-06-16 10:48:01 -07:00
|
|
|
struct cpu_core *core;
|
|
|
|
int count = 0;
|
|
|
|
|
2014-09-05 06:17:31 +00:00
|
|
|
numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
|
|
|
|
hash_int(numa_id, 0)),
|
|
|
|
struct numa_node, hmap_node);
|
|
|
|
LIST_FOR_EACH(core, list_node, &numa->cores) {
|
2014-06-22 18:08:15 -07:00
|
|
|
if (core->available && !core->pinned) {
|
2014-06-16 10:48:01 -07:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OVS_CORE_UNSPEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given 'core_id', tries to pin that core. Returns true, if succeeds.
|
2014-06-22 18:08:15 -07:00
|
|
|
* False, if the core has already been pinned, or if it is invalid or
|
|
|
|
* not available. */
|
2014-06-16 10:48:01 -07:00
|
|
|
bool
|
|
|
|
ovs_numa_try_pin_core_specific(int core_id)
|
|
|
|
{
|
2014-09-05 06:17:32 +00:00
|
|
|
if (ovs_numa_core_id_is_valid(core_id)) {
|
|
|
|
struct cpu_core *core;
|
2014-06-16 10:48:01 -07:00
|
|
|
|
2014-09-05 06:17:32 +00:00
|
|
|
core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
|
|
|
|
hash_int(core_id, 0)),
|
|
|
|
struct cpu_core, hmap_node);
|
2014-06-22 18:08:15 -07:00
|
|
|
if (core->available && !core->pinned) {
|
2014-09-05 06:17:32 +00:00
|
|
|
core->pinned = true;
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-16 10:48:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-22 18:08:15 -07:00
|
|
|
/* Searches through all cores for an unpinned and available core. Returns
|
|
|
|
* the 'core_id' if found and sets the 'core->pinned' to true. Otherwise,
|
|
|
|
* returns OVS_CORE_UNSPEC. */
|
2014-06-16 10:48:01 -07:00
|
|
|
int
|
|
|
|
ovs_numa_get_unpinned_core_any(void)
|
|
|
|
{
|
|
|
|
struct cpu_core *core;
|
|
|
|
|
|
|
|
HMAP_FOR_EACH(core, hmap_node, &all_cpu_cores) {
|
2014-06-22 18:08:15 -07:00
|
|
|
if (core->available && !core->pinned) {
|
2014-06-16 10:48:01 -07:00
|
|
|
core->pinned = true;
|
|
|
|
return core->core_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OVS_CORE_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2014-06-22 18:08:15 -07:00
|
|
|
/* Searches through all cores on numa node with 'numa_id' for an
|
|
|
|
* unpinned and available core. Returns the core_id if found and
|
|
|
|
* sets the 'core->pinned' to true. Otherwise, returns OVS_CORE_UNSPEC. */
|
2014-06-16 10:48:01 -07:00
|
|
|
int
|
2014-09-05 06:17:31 +00:00
|
|
|
ovs_numa_get_unpinned_core_on_numa(int numa_id)
|
2014-06-16 10:48:01 -07:00
|
|
|
{
|
2014-09-05 06:17:32 +00:00
|
|
|
if (ovs_numa_numa_id_is_valid(numa_id)) {
|
|
|
|
struct numa_node *numa;
|
|
|
|
struct cpu_core *core;
|
2014-06-16 10:48:01 -07:00
|
|
|
|
2014-09-05 06:17:32 +00:00
|
|
|
numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
|
|
|
|
hash_int(numa_id, 0)),
|
|
|
|
struct numa_node, hmap_node);
|
|
|
|
LIST_FOR_EACH(core, list_node, &numa->cores) {
|
2014-06-22 18:08:15 -07:00
|
|
|
if (core->available && !core->pinned) {
|
2014-09-05 06:17:32 +00:00
|
|
|
core->pinned = true;
|
|
|
|
return core->core_id;
|
|
|
|
}
|
2014-06-16 10:48:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OVS_CORE_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2014-06-22 18:08:15 -07:00
|
|
|
/* Unpins the core with 'core_id'. */
|
2014-06-16 10:48:01 -07:00
|
|
|
void
|
|
|
|
ovs_numa_unpin_core(int core_id)
|
|
|
|
{
|
2014-09-05 06:17:32 +00:00
|
|
|
if (ovs_numa_core_id_is_valid(core_id)) {
|
|
|
|
struct cpu_core *core;
|
2014-06-16 10:48:01 -07:00
|
|
|
|
2014-09-05 06:17:32 +00:00
|
|
|
core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
|
|
|
|
hash_int(core_id, 0)),
|
|
|
|
struct cpu_core, hmap_node);
|
|
|
|
core->pinned = false;
|
|
|
|
}
|
2014-06-16 10:48:01 -07:00
|
|
|
}
|
|
|
|
|
2014-06-22 18:08:15 -07:00
|
|
|
/* Reads the cpu mask configuration from 'cmask' and sets the
|
|
|
|
* 'available' of corresponding cores. For unspecified cores,
|
|
|
|
* sets 'available' to false. */
|
|
|
|
void
|
|
|
|
ovs_numa_set_cpu_mask(const char *cmask)
|
|
|
|
{
|
|
|
|
int core_id = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!found_numa_and_core) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no mask specified, resets the 'available' to true for all cores. */
|
|
|
|
if (!cmask) {
|
|
|
|
struct cpu_core *core;
|
|
|
|
|
|
|
|
HMAP_FOR_EACH(core, hmap_node, &all_cpu_cores) {
|
|
|
|
core->available = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = strlen(cmask) - 1; i >= 0; i--) {
|
|
|
|
char hex = toupper(cmask[i]);
|
|
|
|
int bin, j;
|
|
|
|
|
|
|
|
if (hex >= '0' && hex <= '9') {
|
|
|
|
bin = hex - '0';
|
|
|
|
} else if (hex >= 'A' && hex <= 'F') {
|
|
|
|
bin = hex - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
bin = 0;
|
|
|
|
VLOG_WARN("Invalid cpu mask: %c", cmask[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < 4; j++) {
|
|
|
|
struct cpu_core *core;
|
|
|
|
|
|
|
|
core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
|
|
|
|
hash_int(core_id++, 0)),
|
|
|
|
struct cpu_core, hmap_node);
|
|
|
|
core->available = (bin >> j) & 0x1;
|
|
|
|
|
|
|
|
if (core_id >= hmap_count(&all_cpu_cores)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For unspecified cores, sets 'available' to false. */
|
|
|
|
while (core_id < hmap_count(&all_cpu_cores)) {
|
|
|
|
struct cpu_core *core;
|
|
|
|
|
|
|
|
core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
|
|
|
|
hash_int(core_id++, 0)),
|
|
|
|
struct cpu_core, hmap_node);
|
|
|
|
core->available = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 10:48:01 -07:00
|
|
|
#endif /* __linux__ */
|