Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
241 lines (188 loc) · 7.07 KB
/
Copy pathlib.rs
File metadata and controls
241 lines (188 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#![no_std]
use core::{
sync::atomic::{AtomicU64, AtomicUsize, Ordering, fence},
time::Duration,
};
use alloc::{format, vec::Vec};
use array_macro::array;
use awkernel_lib::{
dvfs::DesiredPerformance,
sync::{mcs::MCSNode, mutex::Mutex},
};
extern crate alloc;
mod nbody;
const NUM_CPU: usize = 14;
const NUM_TRIALS_LATENCY: usize = 100;
const NUM_BUSY_LOOP: usize = 1000000000;
static LATENCY: [[[AtomicU64; NUM_TRIALS_LATENCY]; 11]; NUM_CPU] =
array![_ => array![_ => array![_ => AtomicU64::new(0); NUM_TRIALS_LATENCY]; 11]; NUM_CPU];
static COUNT: [[AtomicUsize; 11]; NUM_CPU] =
array![_ => array![_ => AtomicUsize::new(0); 11]; NUM_CPU];
static TOTAL_COUNT: AtomicUsize = AtomicUsize::new(0);
pub async fn run() {
let mut waiter = Vec::with_capacity(awkernel_lib::cpu::num_cpu() - 2);
for _ in 0..(awkernel_lib::cpu::num_cpu() - 2) {
let w = awkernel_async_lib::spawn(
"test_latency_diff".into(),
test_latency_diff(),
awkernel_async_lib::scheduler::SchedulerType::FIFO,
)
.await;
waiter.push(w);
}
for w in waiter {
let _ = w.join().await;
}
let mut waiter = Vec::with_capacity(awkernel_lib::cpu::num_cpu() - 2);
for _ in 0..(awkernel_lib::cpu::num_cpu() - 2) {
let w = awkernel_async_lib::spawn(
"test_latency".into(),
test_latency(),
awkernel_async_lib::scheduler::SchedulerType::FIFO,
)
.await;
waiter.push(w);
}
for w in waiter {
let _ = w.join().await;
}
}
async fn test_latency() {
let end_count = (awkernel_lib::cpu::num_cpu() - 1) * NUM_TRIALS_LATENCY * 11;
while TOTAL_COUNT.load(Ordering::Relaxed) + 1 < end_count {
let cpu_id = awkernel_lib::cpu::cpu_id();
for i in 0..=10 {
awkernel_lib::dvfs::set_min_max_performance(10 * i);
awkernel_lib::dvfs::set_energy_efficiency(0);
awkernel_lib::dvfs::set_desired_performance(DesiredPerformance::Auto);
warm_up();
let elapsed = workload();
log::debug!(
"CPU {cpu_id}: Performance {}: Elapsed: {} [us]",
i * 10,
elapsed.as_micros()
);
let count =
COUNT[cpu_id][i as usize].fetch_add(1, core::sync::atomic::Ordering::Relaxed);
if count < NUM_TRIALS_LATENCY {
LATENCY[cpu_id][i as usize][count].store(
elapsed.as_micros() as u64,
core::sync::atomic::Ordering::Relaxed,
);
let total_count = TOTAL_COUNT.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
log::debug!("progress: {total_count} / {end_count}");
if total_count + 1 == end_count {
print_latency();
}
}
}
awkernel_async_lib::r#yield().await;
}
}
fn warm_up() {
for _ in 0..(NUM_BUSY_LOOP) {
core::hint::black_box(());
}
}
fn workload() -> Duration {
let t = awkernel_async_lib::time::Time::now();
nbody::simulate();
t.elapsed()
}
fn print_latency() {
let mut result: [[Vec<u64>; 11]; NUM_CPU] =
array![_ => array![_ => Vec::with_capacity(NUM_TRIALS_LATENCY); 11]; NUM_CPU];
for (j, latency_cpu) in LATENCY.iter().enumerate() {
for (k, latency) in latency_cpu.iter().enumerate() {
let mut sum = 0;
let mut min = u64::MAX;
let mut max = 0;
for usec in latency.iter() {
let val = usec.load(core::sync::atomic::Ordering::Relaxed);
if min > val {
min = val;
}
if max < val {
max = val;
}
sum += val;
result[j][k].push(val);
}
let avg = sum / NUM_TRIALS_LATENCY as u64;
let msg = format!(
"CPU {j}: Performance {}: Average: {avg} us, Min: {min} us, Max: {max} us\r\n",
k * 10
);
awkernel_lib::console::print(&msg);
}
}
let result_json = serde_json::to_string(&result).unwrap();
let result_str = format!("{result_json}\r\n");
awkernel_lib::console::print(&result_str);
}
const NUM_TRIALS_LATENCY_DIFF: usize = 20;
static FREQ_LATENCY: [[Mutex<Vec<(u64, i64)>>; NUM_TRIALS_LATENCY_DIFF]; NUM_CPU] =
array![_ => array![_ => Mutex::new(Vec::new()); NUM_TRIALS_LATENCY_DIFF]; NUM_CPU];
static TOTAL_COUNT_LATENCY_DIFF: AtomicUsize = AtomicUsize::new(0);
static N: usize = 500;
async fn test_latency_diff() {
loop {
awkernel_lib::dvfs::set_min_max_performance(10);
awkernel_lib::dvfs::set_energy_efficiency(0);
awkernel_lib::dvfs::set_desired_performance(DesiredPerformance::Auto);
workload();
let mut diff = Vec::with_capacity(N);
awkernel_lib::dvfs::set_min_max_performance(100);
awkernel_lib::dvfs::set_energy_efficiency(0);
awkernel_lib::dvfs::set_desired_performance(DesiredPerformance::Auto);
let t = awkernel_async_lib::time::Time::now();
for _ in 0..N {
let start = unsafe { core::arch::x86_64::_rdtsc() };
fence(Ordering::AcqRel);
for _ in 0..1000 {
core::hint::black_box(());
}
fence(Ordering::AcqRel);
let end = unsafe { core::arch::x86_64::_rdtsc() };
diff.push((t.elapsed(), (end - start) as i64));
}
let mut result = Vec::with_capacity(diff.len());
for (t, d) in diff.iter() {
result.push((t.as_nanos() as u64, *d));
}
let cpu_id = awkernel_lib::cpu::cpu_id();
for (i, r) in FREQ_LATENCY[cpu_id].iter().enumerate() {
let mut node = MCSNode::new();
let mut guard = r.lock(&mut node);
if guard.is_empty() {
*guard = result;
drop(guard);
let old_total = TOTAL_COUNT_LATENCY_DIFF.fetch_add(1, Ordering::Relaxed);
log::debug!("{cpu_id}: {i}, {old_total}");
if old_total == (NUM_CPU - 1) * NUM_TRIALS_LATENCY_DIFF - 1 {
print_latency_diff();
}
break;
}
}
let total = TOTAL_COUNT_LATENCY_DIFF.load(Ordering::Relaxed);
if total == (NUM_CPU - 1) * NUM_TRIALS_LATENCY_DIFF {
break;
}
awkernel_async_lib::r#yield().await;
}
}
fn print_latency_diff() {
let mut result: [[Vec<(u64, i64)>; NUM_TRIALS_LATENCY_DIFF]; NUM_CPU] =
array![_ => array![_ => Vec::new(); NUM_TRIALS_LATENCY_DIFF]; NUM_CPU];
for (dst, src) in result.iter_mut().zip(FREQ_LATENCY.iter()) {
for (dst, src) in dst.iter_mut().zip(src.iter()) {
let mut node = MCSNode::new();
let guard = src.lock(&mut node);
*dst = guard.clone();
}
}
let result_json = serde_json::to_string(&result).unwrap();
let result_str = format!("{result_json}\r\n");
awkernel_lib::console::print(&result_str);
}