Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Processor: Intel Xeon E5450
- // Cache Level: L2
- // Cache Size: 12MB
- // Theoretically, on average there should be no difference in access times
- // in our two retrievals. However, running this script
- // will demonstrate that there is a marked difference between the
- // the two retrievals. The reason is because before the first retrieval,
- // we make sure the cache on the processor is full of the evictionBuffer.
- // The first retrieval causes the value to be cached, so that
- // on the second retrieval, the processor does not have to go to RAM
- // to retrieve the value. Instead, it can retrieve it from cache.
- // The third and the fourth tests are provided as controls to validate that we
- // can reproduce the results by performing the same actions in a different order.
- // This is the size in MB we need to iterate over in order to
- // evict the cache. Change this value and check the amount of misses to verify
- // the correct eviction size.
- var megabytes = 7;
- // we use this buffer to evict everything from cache
- var evictionBuffer = new ArrayBuffer((1024 * 1024 * megabytes) + 3);
- var evictionView = new DataView(evictionBuffer);
- // we use this buffer to test retrieval
- var probeBuffer = new ArrayBuffer((1024 * 1024 * megabytes) + 3);
- var probeView = new DataView(probeBuffer);
- // cache line size
- var offset = 64;
- // bogus retrieval variable
- var current;
- // running counts
- var flushedTotal = 0;
- var unflushedTotal = 0;
- var flushedControlTotal = 0;
- var unflushedControlTotal = 0;
- // This is a counter for how many times the flushed cache lookup is
- // longer than the unflushed cache lookup. if the cache is properly
- // evicted, this should always equal zero.
- var misses = 0;
- // run test 'rounds' number of times
- var rounds = 100;
- for (var c = 0; c < rounds; c++) {
- // purge cache to prime eviction set
- for (var i = 0; i < (1024 * 1024 * megabytes); i++) {
- current = evictionView.getUint32(i);
- }
- // first retrieval - comes from RAM
- var beginFirst = window.performance.now();
- current = probeView.getUint32(0);
- var endFirst = window.performance.now();
- var diffFirst = endFirst - beginFirst;
- // second retrieval - comes from cache
- var beginSecond = window.performance.now();
- current = probeView.getUint32(0);
- var endSecond = window.performance.now();
- var diffSecond = endSecond - beginSecond;
- // Control tests:
- // this test should be comparable to the second
- var beginThird = window.performance.now();
- current = probeView.getUint32(0);
- var endThird = window.performance.now();
- var diffThird = endThird - beginThird;
- // purge cache to prime eviction set
- for (var i = 0; i < (1024 * 1024 * megabytes); i++) {
- current = evictionView.getUint32(i);
- }
- // this test should be comparable to the first
- var beginFourth = window.performance.now();
- current = probeView.getUint32(0);
- var endFourth = window.performance.now();
- var diffFourth = endFourth - beginFourth;
- // measure differences
- flushedTotal += diffFirst;
- unflushedTotal += diffSecond;
- flushedControlTotal += diffFourth;
- unflushedControlTotal += diffThird;
- // A 'miss' is when the unflushed access tries take longer than
- // the flushed access tries. Those should not happen if we have
- // the entire cache evicted.
- if (diffFirst < diffSecond || diffFirst < diffThird || diffSecond > diffFourth || diffThird > diffFourth) misses++;
- }
- console.log({
- flushedAverage: flushedTotal / rounds,
- unflushedAverage: unflushedTotal / rounds,
- flushedControlAverage: flushedControlTotal / rounds,
- unflushedControlAverage: unflushedControlTotal / rounds,
- misses: misses
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement