Skip to content

Commit f30a872

Browse files
committed
runtime: fixup htlb invalidation and logging
1 parent 480cf35 commit f30a872

File tree

6 files changed

+116
-42
lines changed

6 files changed

+116
-42
lines changed

runtime/include/alaska/sim/HTLB.hpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ namespace alaska::sim {
8080
bool insert(const HTLBEntry &entry);
8181
virtual void invalidateAll();
8282

83+
void invalidate(alaska::Mapping &m);
84+
8385
std::vector<HTLBEntry> getAllEntriesSorted();
8486

8587
virtual std::vector<uint64_t> getHandles();
@@ -129,12 +131,17 @@ namespace alaska::sim {
129131
last_accessed = 0;
130132
l1_htlb.invalidateAll();
131133
l2_htlb.invalidateAll();
132-
l1_tlb.invalidateAll();
133-
l2_tlb.invalidateAll();
134+
// l1_tlb.invalidateAll();
135+
// l2_tlb.invalidateAll();
134136
full_access_trace_.clear();
135137
sm.reset();
136138
}
137139

140+
void invalidate_htlb(alaska::Mapping &m) {
141+
l1_htlb.invalidate(m);
142+
l2_htlb.invalidate(m);
143+
}
144+
138145
inline void access(alaska::Mapping &m) {
139146
// auto h = m.encode();
140147
// full_access_trace_.push_back(h);

runtime/include/alaska/sim/StatisticsManager.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace alaska::sim {
4949
~StatisticsManager();
5050

5151
void incrementStatistic(statistic metric, uint64_t value = 1);
52+
uint64_t getStatistic(statistic s);
5253

5354
void compute();
5455
void reset();

runtime/rt/halloc.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ void hfree(void *ptr) {
7474
// no-op if NULL is passed
7575
if (unlikely(ptr == NULL)) return;
7676

77+
#ifdef ALASKA_HTLB_SIM
78+
extern void alaska_htlb_sim_invalidate(uintptr_t handle);
79+
alaska_htlb_sim_invalidate((uintptr_t)ptr);
80+
#endif
81+
7782
// Simply ask the thread cache to free it!
7883
get_tc()->hfree(ptr);
7984
}

runtime/rt/sim.cpp

+85-40
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <alaska/Runtime.hpp>
99
#include <alaska/sim/HTLB.hpp>
1010
#include "alaska/config.h"
11+
#include "alaska/sim/StatisticsManager.hpp"
12+
#include <sys/time.h>
1113
#include <semaphore.h>
1214

1315

@@ -19,7 +21,7 @@
1921
#define L2_SETS 32
2022
#define TOTAL_ENTRIES (L1_WAYS * L1_SETS + L2_WAYS * L2_SETS)
2123

22-
#define RATE 1'000'000
24+
#define RATE 10'000'000UL
2325

2426
static long access_count = 0;
2527
static alaska::sim::HTLB *g_htlb = NULL;
@@ -41,43 +43,76 @@ static alaska::sim::HTLB &get_htlb(void) {
4143
return *g_htlb;
4244
}
4345

46+
47+
static uint64_t time_ms(void) {
48+
struct timeval tp;
49+
gettimeofday(&tp, NULL);
50+
uint64_t ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
51+
return ms;
52+
}
4453
static pthread_t sim_background_thread;
4554
static void *sim_background_thread_func(void *) {
4655
track_on_this_thread = false;
4756

4857
alaska::wait_for_initialization();
58+
auto &rt = alaska::Runtime::get();
59+
auto *tc = rt.new_threadcache();
60+
61+
FILE *log = fopen("out.csv", "w");
62+
fprintf(log, "trial,hitrate_baseline\n");
63+
4964

50-
while (true) {
65+
66+
for (long trial = 0; true; trial++) {
5167
pthread_mutex_lock(&dump_mutex);
5268
pthread_cond_wait(&dump_cond, &dump_mutex);
5369

70+
unsigned long moved_objects = 0;
71+
unsigned long unmoved_objects = 0;
72+
unsigned long bytes_in_dump = 0;
73+
// ck::set<uint64_t> pages;
74+
75+
// rt.with_barrier([&]() {
76+
// rt.heap.compact_locality_pages();
77+
// // unsigned long c = rt.heap.compact_sizedpages();
78+
// // printf("compacted %lu\n", c);
79+
80+
81+
// for (int i = 0; i < TOTAL_ENTRIES; i++) {
82+
// if (dump_buf[i] == 0) continue;
83+
// auto handle = (void *)(dump_buf[i] << ALASKA_SIZE_BITS);
84+
// auto *m = alaska::Mapping::from_handle_safe(handle);
5485

55-
// // Got a dump!
56-
auto &rt = alaska::Runtime::get();
57-
58-
rt.with_barrier([&]() {
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);
63-
return;
64-
65-
66-
unsigned long moved_objects = 0;
67-
for (int i = 0; i < TOTAL_ENTRIES; i++) {
68-
if (dump_buf[i] == 0) continue;
69-
uint64_t val = dump_buf[i] << ALASKA_SIZE_BITS;
70-
if (get_tc()->localize((void *)val, rt.localization_epoch)) {
71-
moved_objects++;
72-
}
73-
}
74-
auto sm = get_htlb().get_stats();
75-
sm.compute();
76-
// sm.dump();
77-
rt.localization_epoch++;
78-
printf("Objects moved: %lu\n", moved_objects);
79-
printf("TLB Hitrates: %f%% %f%%\n", sm.l1_tlb_hr, sm.l2_tlb_hr);
80-
});
86+
87+
// bool moved = false;
88+
// if (m == NULL or m->is_free() or m->is_pinned()) {
89+
// moved = false;
90+
// } else {
91+
// void *ptr = m->get_pointer();
92+
// // pages.add((uint64_t)ptr >> 12);
93+
// auto *source_page = rt.heap.pt.get_unaligned(m->get_pointer());
94+
// moved = tc->localize(*m, rt.localization_epoch);
95+
// }
96+
97+
// if (moved) {
98+
// moved_objects++;
99+
// bytes_in_dump += tc->get_size(handle);
100+
// } else {
101+
// unmoved_objects++;
102+
// }
103+
// }
104+
// });
105+
106+
rt.localization_epoch++;
107+
108+
auto &sm = get_htlb().get_stats();
109+
sm.compute();
110+
auto hr = sm.l1_tlb_hr;
111+
sm.reset();
112+
113+
fprintf(log, "%lu,%f\n", trial, hr);
114+
115+
fflush(log);
81116
pthread_mutex_unlock(&dump_mutex);
82117
}
83118

@@ -90,29 +125,39 @@ static void __attribute__((constructor)) sim_init(void) {
90125
}
91126

92127

128+
void alaska_htlb_sim_invalidate(uintptr_t maybe_handle) {
129+
if (not alaska::is_initialized()) return;
130+
ck::scoped_lock l(htlb_lock);
131+
auto m = alaska::Mapping::from_handle_safe((void *)maybe_handle);
132+
auto &htlb = get_htlb();
133+
if (m) {
134+
htlb.invalidate_htlb(*m);
135+
}
136+
}
137+
93138

94139
void alaska_htlb_sim_track(uintptr_t maybe_handle) {
95140
if (not alaska::is_initialized()) return;
96141
ck::scoped_lock l(htlb_lock);
97142
auto m = alaska::Mapping::from_handle_safe((void *)maybe_handle);
98143
auto &htlb = get_htlb();
144+
access_count++;
99145
if (m) {
100146
htlb.access(*m);
101-
access_count++;
147+
} else {
148+
htlb.access_non_handle((void *)maybe_handle);
149+
}
102150

103-
if (access_count > RATE) {
104-
access_count = 0;
151+
if (access_count > RATE) {
152+
access_count = 0;
105153

106-
// dump, and notify the movement thread!
107-
pthread_mutex_lock(&dump_mutex);
154+
// dump, and notify the movement thread!
155+
pthread_mutex_lock(&dump_mutex);
108156

109-
htlb.dump_entries(dump_buf);
110-
// htlb.reset();
157+
htlb.dump_entries(dump_buf);
158+
// htlb.reset();
111159

112-
pthread_cond_signal(&dump_cond);
113-
pthread_mutex_unlock(&dump_mutex);
114-
}
115-
} else {
116-
htlb.access_non_handle((void *)maybe_handle);
160+
pthread_cond_signal(&dump_cond);
161+
pthread_mutex_unlock(&dump_mutex);
117162
}
118163
}

runtime/sim/HTLB.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ void L2HTLB::invalidateAll() {
152152
}
153153
}
154154

155+
void L2HTLB::invalidate(alaska::Mapping &m) {
156+
printf("invalidate %x\b", m.handle_id());
157+
for (auto &set : sets) {
158+
for (auto &entry : set) {
159+
if (entry.hid == m.encode()) {
160+
entry.valid = false;
161+
}
162+
}
163+
}
164+
}
165+
155166
std::vector<uint64_t> L2HTLB::getHandles() {
156167
std::vector<uint64_t> handles;
157168
for (auto &set : sets) {

runtime/sim/StatisticsManager.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ void StatisticsManager::incrementStatistic(statistic metric, uint64_t value) {
1818
}
1919

2020

21+
22+
uint64_t StatisticsManager::getStatistic(statistic s) {
23+
return stats[s];
24+
}
25+
2126
//
2227
void StatisticsManager::compute() {
2328
l1_tlb_hr = 100 * (double)stats[L1_TLB_HITS] / stats[L1_TLB_ACCESSES];

0 commit comments

Comments
 (0)