Skip to content

Commit

Permalink
Simplify numasupport (dotnet#84207)
Browse files Browse the repository at this point in the history
* Simplify numasupport

* short-circuit

* Cleanup from QUIC readme

* Address CR feedback: early bail for < 2 NUMA nodes

* Fix node numbering, which is 0-based
  • Loading branch information
am11 committed Apr 6, 2023
1 parent aca3700 commit de5a04b
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 252 deletions.
1 change: 0 additions & 1 deletion .devcontainer/libraries/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
libicu-dev \
liblttng-ust-dev \
libssl-dev \
libnuma-dev \
libkrb5-dev \
zlib1g-dev \
ninja-build
1 change: 0 additions & 1 deletion .devcontainer/wasm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
libicu-dev \
liblttng-ust-dev \
libssl-dev \
libnuma-dev \
libkrb5-dev \
zlib1g-dev \
ninja-build
Expand Down
3 changes: 1 addition & 2 deletions docs/workflow/requirements/linux-requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ Install the following packages for the toolchain:
* liblttng-ust-dev
* libssl-dev
* libkrb5-dev
* libnuma-dev (optional, enables numa support)
* zlib1g-dev
* ninja-build (optional, enables building native code with ninja instead of make)

```bash
sudo apt install -y cmake llvm lld clang build-essential \
python-is-python3 curl git lldb libicu-dev liblttng-ust-dev \
libssl-dev libnuma-dev libkrb5-dev zlib1g-dev ninja-build
libssl-dev libkrb5-dev zlib1g-dev ninja-build
```

You now have all the required components.
Expand Down
2 changes: 1 addition & 1 deletion eng/install-native-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ case "$os" in
apt update

apt install -y build-essential gettext locales cmake llvm clang lldb liblldb-dev libunwind8-dev libicu-dev liblttng-ust-dev \
libssl-dev libkrb5-dev libnuma-dev zlib1g-dev
libssl-dev libkrb5-dev zlib1g-dev

localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
;;
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/gc/unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include(configure.cmake)

set(GC_PAL_SOURCES
gcenv.unix.cpp
numasupport.dynamic.cpp
numasupport.cpp
events.cpp
cgroup.cpp)

Expand Down
1 change: 0 additions & 1 deletion src/coreclr/gc/unix/config.gc.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#cmakedefine01 HAVE_VM_FLAGS_SUPERPAGE_SIZE_ANY
#cmakedefine01 HAVE_MAP_HUGETLB
#cmakedefine01 HAVE_SCHED_GETCPU
#cmakedefine01 HAVE_NUMA_H
#cmakedefine01 HAVE_VM_ALLOCATE
#cmakedefine01 HAVE_SWAPCTL
#cmakedefine01 HAVE_SYSCTLBYNAME
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/gc/unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ include(CheckLibraryExists)

check_include_files(sys/time.h HAVE_SYS_TIME_H)
check_include_files(sys/mman.h HAVE_SYS_MMAN_H)
check_include_files(numa.h HAVE_NUMA_H)
check_include_files(pthread_np.h HAVE_PTHREAD_NP_H)

check_function_exists(vm_allocate HAVE_VM_ALLOCATE)
Expand Down
13 changes: 6 additions & 7 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ void GCToOSInterface::Shutdown()
munmap(g_helperPage, OS_PAGE_SIZE);

CleanupCGroup();
NUMASupportCleanup();
}

// Get numeric id of the current thread if possible on the
Expand Down Expand Up @@ -615,7 +614,7 @@ bool GCToOSInterface::VirtualCommit(void* address, size_t size, uint16_t node)
}
#endif

#if HAVE_NUMA_H
#ifdef TARGET_LINUX
if (success && g_numaAvailable && (node != NUMA_NODE_UNDEFINED))
{
if ((int)node <= g_highestNumaNode)
Expand All @@ -628,12 +627,12 @@ bool GCToOSInterface::VirtualCommit(void* address, size_t size, uint16_t node)
int index = node / sizeof(unsigned long);
nodeMask[index] = ((unsigned long)1) << (node & (sizeof(unsigned long) - 1));

int st = mbind(address, size, MPOL_PREFERRED, nodeMask, usedNodeMaskBits, 0);
int st = BindMemoryPolicy(address, size, nodeMask, usedNodeMaskBits);
assert(st == 0);
// If the mbind fails, we still return the allocated memory since the node is just a hint
}
}
#endif // HAVE_NUMA_H
#endif // TARGET_LINUX

return success;
}
Expand Down Expand Up @@ -1430,14 +1429,14 @@ bool GCToOSInterface::GetProcessorForHeap(uint16_t heap_number, uint16_t* proc_n
if (availableProcNumber == heap_number)
{
*proc_no = procNumber;
#if HAVE_NUMA_H
#ifdef TARGET_LINUX
if (GCToOSInterface::CanEnableGCNumaAware())
{
int result = numa_node_of_cpu(procNumber);
int result = GetNumaNodeNumByCpu(procNumber);
*node_no = (result >= 0) ? (uint16_t)result : NUMA_NODE_UNDEFINED;
}
else
#endif // HAVE_NUMA_H
#endif // TARGET_LINUX
{
*node_no = NUMA_NODE_UNDEFINED;
}
Expand Down
86 changes: 86 additions & 0 deletions src/coreclr/gc/unix/numasupport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "numasupport.h"

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/syscall.h>
#include <minipal/utils.h>

// The highest NUMA node available
int g_highestNumaNode = 0;
// Is numa available
bool g_numaAvailable = false;

#ifdef TARGET_LINUX
static int GetNodeNum(const char* path, bool firstOnly)
{
DIR *dir;
struct dirent *entry;
int result = -1;

dir = opendir(path);
if (dir)
{
while ((entry = readdir(dir)) != NULL)
{
if (strncmp(entry->d_name, "node", STRING_LENGTH("node")))
continue;

int nodeNum = strtoul(entry->d_name + STRING_LENGTH("node"), NULL, 0);
if (result < nodeNum)
result = nodeNum;

if (firstOnly)
break;
}

closedir(dir);
}

return result;
}
#endif

void NUMASupportInitialize()
{
#ifdef TARGET_LINUX
if (syscall(__NR_get_mempolicy, NULL, NULL, 0, 0, 0) < 0 && errno == ENOSYS)
return;

int highestNumaNode = GetNodeNum("/sys/devices/system/node", false);
// we only use this implementation when there are two or more NUMA nodes available
if (highestNumaNode < 1)
return;

g_numaAvailable = true;
g_highestNumaNode = highestNumaNode;
#endif
}

int GetNumaNodeNumByCpu(int cpu)
{
#ifdef TARGET_LINUX
char path[64];
if (snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d", cpu) < 0)
return -1;

return GetNodeNum(path, true);
#else
return -1;
#endif
}

long BindMemoryPolicy(void* start, unsigned long len, const unsigned long* nodemask, unsigned long maxnode)
{
#ifdef TARGET_LINUX
return syscall(__NR_mbind, (long)start, len, 1, (long)nodemask, maxnode, 0);
#else
return -1;
#endif
}
124 changes: 0 additions & 124 deletions src/coreclr/gc/unix/numasupport.dynamic.cpp

This file was deleted.

35 changes: 2 additions & 33 deletions src/coreclr/gc/unix/numasupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,8 @@
#ifndef __NUMASUPPORT_H__
#define __NUMASUPPORT_H__

#include "config.gc.h"

#if HAVE_NUMA_H

#include <numa.h>
#include <numaif.h>

#endif // HAVE_NUMA_H

void NUMASupportInitialize();
void NUMASupportCleanup();

#if HAVE_NUMA_H

// List of all functions from the numa library that are used
#define FOR_ALL_NUMA_FUNCTIONS \
PER_FUNCTION_BLOCK(mbind) \
PER_FUNCTION_BLOCK(numa_available) \
PER_FUNCTION_BLOCK(numa_max_node) \
PER_FUNCTION_BLOCK(numa_node_of_cpu)

// Declare pointers to all the used numa functions
#define PER_FUNCTION_BLOCK(fn) extern decltype(fn)* fn##_ptr;
FOR_ALL_NUMA_FUNCTIONS
#undef PER_FUNCTION_BLOCK

// Redefine all calls to numa functions as calls through pointers that are set
// to the functions of libnuma in the initialization.
#define mbind(...) mbind_ptr(__VA_ARGS__)
#define numa_available() numa_available_ptr()
#define numa_max_node() numa_max_node_ptr()
#define numa_node_of_cpu(...) numa_node_of_cpu_ptr(__VA_ARGS__)

#endif // HAVE_NUMA_H
int GetNumaNodeNumByCpu(int cpu);
long BindMemoryPolicy(void* start, unsigned long len, const unsigned long* nodemask, unsigned long maxnode);

#endif // __NUMASUPPORT_H__
Loading

0 comments on commit de5a04b

Please sign in to comment.