Advertisement
Guest User

Untitled

a guest
May 6th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. // Processor: Intel Xeon E5450
  2. // Cache Level: L2
  3. // Cache Size: 12MB
  4.  
  5. // Theoretically, on average there should be no difference in access times
  6. // in our two retrievals. However, running this script
  7. // will demonstrate that there is a marked difference between the
  8. // the two retrievals. The reason is because before the first retrieval,
  9. // we make sure the cache on the processor is full of the evictionBuffer.
  10. // The first retrieval causes the value to be cached, so that
  11. // on the second retrieval, the processor does not have to go to RAM
  12. // to retrieve the value. Instead, it can retrieve it from cache.
  13.  
  14. // The third and the fourth tests are provided as controls to validate that we
  15. // can reproduce the results by performing the same actions in a different order.
  16.  
  17. // This is the size in MB we need to iterate over in order to
  18. // evict the cache. Change this value and check the amount of misses to verify
  19. // the correct eviction size.
  20. var megabytes = 7;
  21.  
  22. // we use this buffer to evict everything from cache
  23. var evictionBuffer = new ArrayBuffer((1024 * 1024 * megabytes) + 3);
  24. var evictionView = new DataView(evictionBuffer);
  25.  
  26. // we use this buffer to test retrieval
  27. var probeBuffer = new ArrayBuffer((1024 * 1024 * megabytes) + 3);
  28. var probeView = new DataView(probeBuffer);
  29.  
  30. // cache line size
  31. var offset = 64;
  32.  
  33. // bogus retrieval variable
  34. var current;
  35.  
  36. // running counts
  37. var flushedTotal = 0;
  38. var unflushedTotal = 0;
  39. var flushedControlTotal = 0;
  40. var unflushedControlTotal = 0;
  41.  
  42. // This is a counter for how many times the flushed cache lookup is
  43. // longer than the unflushed cache lookup. if the cache is properly
  44. // evicted, this should always equal zero.
  45. var misses = 0;
  46.  
  47. // run test 'rounds' number of times
  48. var rounds = 100;
  49.  
  50. for (var c = 0; c < rounds; c++) {
  51.  
  52. // purge cache to prime eviction set
  53. for (var i = 0; i < (1024 * 1024 * megabytes); i++) {
  54. current = evictionView.getUint32(i);
  55. }
  56.  
  57. // first retrieval - comes from RAM
  58. var beginFirst = window.performance.now();
  59. current = probeView.getUint32(0);
  60. var endFirst = window.performance.now();
  61. var diffFirst = endFirst - beginFirst;
  62.  
  63. // second retrieval - comes from cache
  64. var beginSecond = window.performance.now();
  65. current = probeView.getUint32(0);
  66. var endSecond = window.performance.now();
  67. var diffSecond = endSecond - beginSecond;
  68.  
  69. // Control tests:
  70.  
  71. // this test should be comparable to the second
  72. var beginThird = window.performance.now();
  73. current = probeView.getUint32(0);
  74. var endThird = window.performance.now();
  75. var diffThird = endThird - beginThird;
  76.  
  77. // purge cache to prime eviction set
  78. for (var i = 0; i < (1024 * 1024 * megabytes); i++) {
  79. current = evictionView.getUint32(i);
  80. }
  81.  
  82. // this test should be comparable to the first
  83. var beginFourth = window.performance.now();
  84. current = probeView.getUint32(0);
  85. var endFourth = window.performance.now();
  86. var diffFourth = endFourth - beginFourth;
  87.  
  88. // measure differences
  89. flushedTotal += diffFirst;
  90. unflushedTotal += diffSecond;
  91. flushedControlTotal += diffFourth;
  92. unflushedControlTotal += diffThird;
  93.  
  94. // A 'miss' is when the unflushed access tries take longer than
  95. // the flushed access tries. Those should not happen if we have
  96. // the entire cache evicted.
  97. if (diffFirst < diffSecond || diffFirst < diffThird || diffSecond > diffFourth || diffThird > diffFourth) misses++;
  98. }
  99.  
  100. console.log({
  101. flushedAverage: flushedTotal / rounds,
  102. unflushedAverage: unflushedTotal / rounds,
  103. flushedControlAverage: flushedControlTotal / rounds,
  104. unflushedControlAverage: unflushedControlTotal / rounds,
  105. misses: misses
  106. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement