Skip to content

Commit

Permalink
thingies
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomShaper committed Oct 1, 2024
1 parent d21a975 commit 13aa42c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ concurrency:
cancel-in-progress: true

jobs:
linux-build:
name: 🐧 Linux
uses: ./.github/workflows/linux_builds.yml
# linux-build:
# name: 🐧 Linux
# uses: ./.github/workflows/linux_builds.yml

macos-build:
name: 🍎 macOS
Expand Down
26 changes: 23 additions & 3 deletions tests/core/templates/test_rid.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

#include "tests/test_macros.h"

#include <mach/mach.h>
#include <pthread.h>
#include <unistd.h>

namespace TestRID {
TEST_CASE("[RID] Default Constructor") {
RID rid;
Expand Down Expand Up @@ -114,6 +118,7 @@ TEST_CASE("[RID_Owner] Thread safety") {
struct RID_OwnerTester {
RID_Owner<DataHolder, true> rid_owner;
TightLocalVector<Thread> threads;
long cpu_cores = 0;
SafeNumeric<uint32_t> next_thread_idx;
// Using std::atomic directly since SafeNumeric doesn't support relaxed ordering.
TightLocalVector<std::atomic<uint64_t>> rids; // Atomic here to prevent false data race warnings.
Expand All @@ -136,7 +141,11 @@ TEST_CASE("[RID_Owner] Thread safety") {

explicit RID_OwnerTester(bool p_chunk_for_all, bool p_chunks_preallocated) :
rid_owner(sizeof(DataHolder) * (p_chunk_for_all ? OS::get_singleton()->get_processor_count() : 1)) {
threads.resize(OS::get_singleton()->get_processor_count() * 2);
cpu_cores = sysconf(_SC_NPROCESSORS_ONLN);
printf("############################# %ld cores\n", cpu_cores);

threads.resize(OS::get_singleton()->get_processor_count());
printf("############################# %u threads\n", threads.size());
rids.resize(threads.size());
if (p_chunks_preallocated) {
LocalVector<RID> prealloc_rids;
Expand Down Expand Up @@ -164,6 +173,15 @@ TEST_CASE("[RID_Owner] Thread safety") {
// 1. Each thread gets a zero-based index.
uint32_t self_th_idx = rot->next_thread_idx.postincrement();

// Use pthread to set affinity to the core self_th_idx modulo cpu cores.
// This is to avoid threads running on the same core.
thread_port_t mach_thread;
int core = self_th_idx % rot->cpu_cores;
thread_affinity_policy_data_t policy = { core };
mach_thread = pthread_mach_thread_np(pthread_self());
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1);
printf("############################# thread %d - %d\n", self_th_idx, core);

rot->lockstep(0);

// 2. Each thread makes a RID holding unique data.
Expand Down Expand Up @@ -211,8 +229,10 @@ TEST_CASE("[RID_Owner] Thread safety") {
};

SUBCASE("All items in one chunk, pre-allocated") {
RID_OwnerTester tester(true, true);
tester.test();
for (int i = 0; i < 1000; i++) {
RID_OwnerTester tester(true, true);
tester.test();
}
}
#if 0
SUBCASE("All items in one chunk, NOT pre-allocated") {
Expand Down

0 comments on commit 13aa42c

Please sign in to comment.