Skip to content

Commit 36f9699

Browse files
committed
runtime: barriers take locks, and can fail to begin
1 parent 46a0be7 commit 36f9699

File tree

10 files changed

+108
-37
lines changed

10 files changed

+108
-37
lines changed

runtime/core/Runtime.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ namespace alaska {
2626
static volatile bool runtime_initialized = false;
2727

2828

29-
Runtime::Runtime(alaska::Configuration config) : handle_table(config) {
29+
Runtime::Runtime(alaska::Configuration config)
30+
: handle_table(config) {
3031
// Validate that there is not already a runtime (TODO: atomics?)
3132
ALASKA_ASSERT(g_runtime == nullptr, "Cannot create more than one runtime");
3233

@@ -77,6 +78,19 @@ namespace alaska {
7778
}
7879

7980

81+
void Runtime::lock_all_thread_caches(void) {
82+
tcs_lock.lock();
83+
84+
for (auto *tc : tcs)
85+
tc->lock.lock();
86+
}
87+
void Runtime::unlock_all_thread_caches(void) {
88+
for (auto *tc : tcs)
89+
tc->lock.unlock();
90+
tcs_lock.unlock();
91+
}
92+
93+
8094
void wait_for_initialization(void) {
8195
log_debug("waiting for initialization!\n");
8296
while (not is_initialized()) {
@@ -86,8 +100,6 @@ namespace alaska {
86100
}
87101

88102

89-
bool is_initialized(void) {
90-
return atomic_get(runtime_initialized);
91-
}
103+
bool is_initialized(void) { return atomic_get(runtime_initialized); }
92104

93105
} // namespace alaska

runtime/include/alaska/BarrierManager.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace alaska {
2020
// pin any handles, the barrier does not need to do anything.
2121
struct BarrierManager : public alaska::InternalHeapAllocated {
2222
virtual ~BarrierManager() = default;
23-
virtual void begin(void){};
23+
virtual bool begin(void) { return true; };
2424
virtual void end(void){};
2525

2626
unsigned long barrier_count = 0;

runtime/include/alaska/Runtime.hpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,25 @@ namespace alaska {
7676

7777
template <typename Fn>
7878
void with_barrier(Fn &&cb) {
79-
barrier_manager->begin();
80-
in_barrier = true;
81-
barrier_manager->barrier_count++;
82-
cb();
83-
in_barrier = false;
84-
barrier_manager->end();
79+
lock_all_thread_caches();
80+
if (barrier_manager->begin()) {
81+
in_barrier = true;
82+
barrier_manager->barrier_count++;
83+
cb();
84+
in_barrier = false;
85+
barrier_manager->end();
86+
} else {
87+
alaska::printf("Barrier failed\n");
88+
}
89+
unlock_all_thread_caches();
8590
}
8691

8792
private:
8893
int next_thread_cache_id = 0;
94+
95+
96+
void lock_all_thread_caches(void);
97+
void unlock_all_thread_caches(void);
8998
};
9099

91100

runtime/include/alaska/ThreadCache.hpp

+40
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <alaska/HandleTable.hpp>
1717
#include <alaska/LocalityPage.hpp>
1818
#include <alaska/alaska.hpp>
19+
#include "ck/lock.h"
1920

2021
namespace alaska {
2122

@@ -41,6 +42,13 @@ namespace alaska {
4142
bool localize(alaska::Mapping &m, uint64_t epoch);
4243
bool localize(void *handle, uint64_t epoch);
4344

45+
46+
protected:
47+
friend class LockedThreadCache;
48+
friend alaska::Runtime;
49+
50+
ck::mutex lock;
51+
4452
private:
4553
// Allocate backing data for a handle, but don't assign it yet.
4654
void *allocate_backing_data(const alaska::Mapping &m, size_t size);
@@ -79,4 +87,36 @@ namespace alaska {
7987

8088

8189

90+
class LockedThreadCache final {
91+
public:
92+
LockedThreadCache(ThreadCache &tc)
93+
: tc(tc) {
94+
tc.lock.lock();
95+
}
96+
97+
98+
~LockedThreadCache(void) {
99+
tc.lock.unlock();
100+
}
101+
102+
// Delete copy constructor and copy assignment operator
103+
LockedThreadCache(const LockedThreadCache &) = delete;
104+
LockedThreadCache &operator=(const LockedThreadCache &) = delete;
105+
106+
// Delete move constructor and move assignment operator
107+
LockedThreadCache(LockedThreadCache &&) = delete;
108+
LockedThreadCache &operator=(LockedThreadCache &&) = delete;
109+
110+
111+
112+
113+
ThreadCache &operator*(void) { return tc; }
114+
ThreadCache *operator->(void) { return &tc; }
115+
116+
private:
117+
ThreadCache &tc;
118+
};
119+
120+
121+
82122
} // namespace alaska

runtime/include/alaska/rt/barrier.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace alaska {
4444

4545
// Barrier operational lifetime. It is not recommended to use this interface, and
4646
// instead use `with_barrier` interface below:
47-
void begin();
47+
bool begin();
4848
void end();
4949

5050
// struct BarrierInfo {};

runtime/rt/barrier.cpp

+17-8
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ void dump_thread_states(void) {
328328

329329

330330

331-
void alaska::barrier::begin(void) {
331+
bool alaska::barrier::begin(void) {
332332
// Pseudocode:
333333
//
334334
// function begin():
@@ -369,9 +369,15 @@ void alaska::barrier::begin(void) {
369369
int retries = 0;
370370
int signals_sent = 0;
371371

372-
// printf("\n");
372+
bool success = true;
373+
373374
// Make sure the threads that are in unmanaged (library) code get signalled.
374375
while (true) {
376+
if (retries >= 1000) {
377+
success = false;
378+
break;
379+
}
380+
retries++;
375381
bool sent_signal = false;
376382
list_for_each_entry(pos, &all_threads, list_head) {
377383
if (pos->state->join_status == ALASKA_JOIN_REASON_NOT_JOINED) {
@@ -391,6 +397,9 @@ void alaska::barrier::begin(void) {
391397

392398
(void)retries;
393399
(void)signals_sent;
400+
401+
402+
return success;
394403
// printf("%10f ", (end - start) / 1000.0 / 1000.0 / 1000.0);
395404
// dump_thread_states();
396405
// printf(" retries = %d, signals = %d\n", retries, signals_sent);
@@ -437,14 +446,14 @@ static void alaska_barrier_signal_handler(int sig, siginfo_t* info, void* ptr) {
437446
// we need to return back to the thread so it can hit a poll.
438447

439448

440-
// // First, though, we need to wait for the patches to be done.
449+
// First, though, we need to wait for the patches to be done.
441450
// while (!patches_done) {
442451
// }
443-
//
444-
// for (auto [start, end] : managed_blob_text_regions) {
445-
// __builtin___clear_cache((char*)start, (char*)end);
446-
// }
447-
// printf("ManagedUntracked!\n");
452+
453+
for (auto [start, end] : managed_blob_text_regions) {
454+
__builtin___clear_cache((char*)start, (char*)end);
455+
}
456+
printf("ManagedUntracked!\n");
448457
// printf("EEP %p %d!\n", return_address, sig);
449458
return;
450459

runtime/rt/halloc.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
// TODO: don't have this be global!
2020
static __thread alaska::ThreadCache *g_tc = nullptr;
2121

22-
alaska::ThreadCache *get_tc(void) {
22+
alaska::LockedThreadCache get_tc(void) {
2323
if (unlikely(g_tc == nullptr)) {
2424
g_tc = alaska::Runtime::get().new_threadcache();
2525
}
26-
return g_tc;
26+
return *g_tc;
2727
}
2828

2929

@@ -44,11 +44,10 @@ void *hcalloc(size_t nmemb, size_t size) { return _halloc(nmemb * size, 1); }
4444

4545
// Reallocate a handle
4646
void *hrealloc(void *handle, size_t new_size) {
47-
auto *tc = get_tc();
4847
// If the handle is null, then this call is equivalent to malloc(size)
49-
if (handle == NULL) {
50-
return halloc(new_size);
51-
}
48+
if (handle == NULL) return halloc(new_size);
49+
50+
5251
auto *m = alaska::Mapping::from_handle_safe(handle);
5352
if (m == NULL) {
5453
if (!alaska::Runtime::get().heap.huge_allocator.owns(handle)) {
@@ -65,7 +64,7 @@ void *hrealloc(void *handle, size_t new_size) {
6564
return NULL;
6665
}
6766

68-
handle = tc->hrealloc(handle, new_size);
67+
handle = get_tc()->hrealloc(handle, new_size);
6968
return handle;
7069
}
7170

runtime/rt/init.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static alaska::Runtime *the_runtime = nullptr;
2828

2929
struct CompilerRuntimeBarrierManager : public alaska::BarrierManager {
3030
~CompilerRuntimeBarrierManager() override = default;
31-
void begin(void) override { alaska::barrier::begin(); }
31+
bool begin(void) override { return alaska::barrier::begin(); }
3232
void end(void) override { alaska::barrier::end(); }
3333
};
3434

@@ -44,8 +44,9 @@ static void *barrier_thread_func(void *) {
4444
usleep(50 * 1000);
4545

4646
alaska::Runtime::get().with_barrier([]() {
47-
long swapped = alaska::Runtime::get().heap.jumble();
48-
printf("Swapped %ld\n", swapped);
47+
alaska::Runtime::get().heap.compact_sizedpages();
48+
// long swapped = alaska::Runtime::get().heap.jumble();
49+
// printf("Swapped %ld\n", swapped);
4950
});
5051
// alaska::barrier::begin();
5152
// printf("Barrier.\n");

runtime/rt/sim.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#define L2_SETS 32
2020
#define TOTAL_ENTRIES (L1_WAYS * L1_SETS + L2_WAYS * L2_SETS)
2121

22-
#define RATE 10'000
22+
#define RATE 1'000'000
2323

2424
static long access_count = 0;
2525
static alaska::sim::HTLB *g_htlb = NULL;
@@ -56,9 +56,10 @@ static void *sim_background_thread_func(void *) {
5656
auto &rt = alaska::Runtime::get();
5757

5858
rt.with_barrier([&]() {
59-
unsigned long c = rt.heap.jumble();
60-
printf("jumbled %lu objects\n", c);
61-
// rt.heap.compact_sizedpages();
59+
// unsigned long c = rt.heap.jumble();
60+
// printf("jumbled %lu objects\n", c);
61+
unsigned long c = rt.heap.compact_sizedpages();
62+
printf("compacted %lu\n", c);
6263
return;
6364

6465

runtime/yukon/yukon.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
static alaska::ThreadCache *tc = NULL;
3636

3737
static void set_ht_addr(void *addr) {
38-
// printf("set htaddr to %p\n", addr);
39-
// write_csr(0xc2, addr);
38+
// alaska::printf("set htaddr to %p\n", addr);
39+
write_csr(0xc2, addr);
4040
}
4141

4242
static alaska::Runtime *the_runtime = NULL;
@@ -135,7 +135,7 @@ void __attribute__((destructor)) alaska_deinit(void) {
135135
// #define free hfree
136136

137137
static void *_halloc(size_t sz, int zero) {
138-
print_hex("_halloc", sz);
138+
// print_hex("_halloc", sz);
139139
if (dead) {
140140
return alaska_internal_malloc(sz);
141141
}

0 commit comments

Comments
 (0)