Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ jobs:
- name: install packages
run: |
./alpine.sh apk update
./alpine.sh apk add build-base cmake git python3 py3-pip clang ninja
./alpine.sh apk add build-base cmake git python3 py3-pip clang ninja util-linux
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need this here then we likely need it in the release too:

./alpine.sh apk add build-base cmake git python3 clang ninja py3-pip


- name: avoid d8 tests (jsvu is not compatible with alpine)
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ jobs:
- name: install packages
run: |
./alpine.sh apk update
./alpine.sh apk add build-base cmake git python3 clang ninja py3-pip
./alpine.sh apk add build-base cmake git python3 py3-pip clang ninja util-linux

- name: avoid d8 tests (jsvu is not compatible with alpine)
run: |
Expand Down
24 changes: 24 additions & 0 deletions src/support/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@
#include <iostream>
#include <string>

#ifdef __linux__
#include <sched.h> // For sched_getaffinity
#endif

#include "compiler-support.h"
#include "support/debug.h"
#include "threads.h"
#include "utilities.h"

// debugging tools

// DEBUG_TYPE is for BYN_TRACE macro. This tracing can be enabled at runtime
#define DEBUG_TYPE "threads"

// BINARYEN_THREAD_DEBUG is a build-time setting for detailed thread tracing
#ifdef BINARYEN_THREAD_DEBUG
static std::mutex debug;
#define DEBUG_THREAD(x) \
Expand Down Expand Up @@ -145,9 +154,24 @@ size_t ThreadPool::getNumCores() {
return 1;
#else
size_t num = std::max(1U, std::thread::hardware_concurrency());
#ifdef __linux__
// On linux we can do better since we can get the number of CPU that are
// actually usable by the current process.
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
if (sched_getaffinity(0, sizeof(cpu_set), &cpu_set) == 0) {
num = 0;
for (int i = 0; i < CPU_SETSIZE; i++) {
if (CPU_ISSET(i, &cpu_set)) {
num++;
}
}
}
#endif
if (getenv("BINARYEN_CORES")) {
num = std::stoi(getenv("BINARYEN_CORES"));
}
BYN_TRACE("getNumCores: " << num << "\n");
return num;
#endif
}
Expand Down
4 changes: 4 additions & 0 deletions test/lit/lit.cfg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import lit.formats

config.name = "Binaryen lit tests"
Expand Down Expand Up @@ -26,6 +27,9 @@
python = sys.executable.replace('\\', '/')
config.substitutions.append((tool, python + ' ' + tool_file))

if 'linux' in sys.platform:
config.available_features.add('linux')

# Finds the given executable 'program' in PATH.
# Operates like the Unix tool 'which'.
# This is similar to script/test/shared.py, but does not use binaryen_root, and
Expand Down
13 changes: 13 additions & 0 deletions test/lit/num_cores.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;; REQUIRES: linux
;; Test that getNumCores honors thread affinity on linux

(module
(func $a)
)

;; RUN: taskset -c 0 wasm-opt -O1 --debug=threads %s 2>&1 | filecheck %s
;; RUN: taskset -c 0,2 wasm-opt -O1 --debug=threads %s 2>&1 | filecheck %s --check-prefix=TWO

;; CHECK: getNumCores: 1

;; TWO: getNumCores: 2
Loading