Skip to content

Commit

Permalink
Fix affinity issue (and add -Werror)
Browse files Browse the repository at this point in the history
  • Loading branch information
DLochmelis33 committed Aug 3, 2024
1 parent 44d0abb commit 57e58da
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 3 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ kotlin {
create("barrier") {
defFile(project.file("src/nativeInterop/barrier.def"))
headers(project.file("src/nativeInterop/barrier.h"))
compilerOpts.addAll(listOf("-Wall", "-Werror"))
}
if (affinitySupported) {
create("affinity") {
defFile(project.file("src/nativeInterop/kaffinity.def"))
compilerOpts.add("-D_GNU_SOURCE")
compilerOpts.addAll(listOf("-Wall", "-Werror"))
}
}
create("kpthread") {
defFile(project.file("src/nativeInterop/kpthread.def"))
compilerOpts.addAll(listOf("-Wall", "-Werror"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ abstract class ThreadlikeRunner : LitmusRunner() {
): () -> LitmusResult {

val threads = List(test.threadCount) { threadlikeProducer() }
affinityMap?.let { map ->
affinityManager?.apply {
for ((i, t) in threads.withIndex()) {
setAffinityAndCheck(t, map.allowedCores(i))
}
}
}

val barrier = barrierProducer(test.threadCount)
val contexts = List(threads.size) { i ->
Expand All @@ -44,6 +37,15 @@ abstract class ThreadlikeRunner : LitmusRunner() {
thread.start(context, ::threadFunction)
}

// cannot set affinity before thread is started (because pthread_create has not been called yet)
affinityMap?.let { map ->
affinityManager?.apply {
for ((i, t) in threads.withIndex()) {
setAffinityAndCheck(t, map.allowedCores(i))
}
}
}

return {
futures.forEach { it.await() } // await all results
threads.forEach { it.dispose() } // stop all "threads"
Expand Down
8 changes: 5 additions & 3 deletions core/src/nativeInterop/kaffinity.def
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#include <pthread.h>

int set_affinity(void* thread, cpu_set_t* set) {
return pthread_setaffinity_np(*((pthread_t*) thread), sizeof(*set), set);
pthread_t pthread = *((pthread_t*) thread);
return pthread_setaffinity_np(pthread, sizeof(*set), set);
}
int get_affinity(void* thread, cpu_set_t* set) {
return pthread_getaffinity_np(*((pthread_t*) thread), sizeof(*set), set);
pthread_t pthread = *((pthread_t*) thread);
return pthread_getaffinity_np(pthread, sizeof(*set), set);
}

void cpu_zero(cpu_set_t* set) {
Expand All @@ -16,7 +18,7 @@ void cpu_set(int cpu, cpu_set_t* set) {
CPU_SET(cpu, set);
}
int cpu_isset(int cpu, cpu_set_t* set) {
CPU_ISSET(cpu, set);
return CPU_ISSET(cpu, set);
}
int cpu_setsize() {
return CPU_SETSIZE;
Expand Down

0 comments on commit 57e58da

Please sign in to comment.