51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
const N= 393216; // Number of cache lines 24MB/64B
|
|
// Number of sweep counts
|
|
// TODO (Exercise 2-1): Choose an appropriate value!
|
|
let P = 10;
|
|
|
|
// Number of elements in your trace
|
|
let K = 5 * 1000 / P;
|
|
|
|
// Array of length K with your trace's values
|
|
let T;
|
|
|
|
// Value of performance.now() when you started recording your trace
|
|
let start;
|
|
|
|
function record() {
|
|
|
|
// Create empty array for saving trace values
|
|
T = new Array(K);
|
|
|
|
// Fill array with -1 so we can be sure memory is allocated
|
|
T.fill(-1, 0, T.length);
|
|
const LINE_SIZE = 8; // 64/sizeof(double) Note that js treats all numbers as double
|
|
const M = new Array(N * LINE_SIZE).fill(-1);
|
|
// Save start timestamp
|
|
start = performance.now();
|
|
for (let i = 0; i < K; i++) {
|
|
let count=0;
|
|
while (performance.now() - start < P*(i+1)) {
|
|
// sweep the cache
|
|
for (let j = 0; j < N; j++) {
|
|
let val = M[j * LINE_SIZE];
|
|
}
|
|
count++;
|
|
}
|
|
T[i]=count;
|
|
}
|
|
|
|
// TODO (Exercise 2-1): Record data for 5 seconds and save values to T.
|
|
|
|
// Once done recording, send result to main thread
|
|
postMessage(JSON.stringify(T));
|
|
console.log(T);
|
|
}
|
|
|
|
// DO NOT MODIFY BELOW THIS LINE -- PROVIDED BY COURSE STAFF
|
|
self.onmessage = (e) => {
|
|
if (e.data.type === "start") {
|
|
setTimeout(record, 0);
|
|
}
|
|
};
|