Advertisement
Guest User

main.cpp

a guest
Dec 20th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. #include <unistd.h>
  5.  
  6. #define L2_cache 3145728 // 3 mb - L2 cache size
  7. #define string_size 16 // 64b - cache string size
  8.  
  9. using namespace std;
  10.  
  11. const int N = 3145728;
  12.  
  13. union ticks {
  14.     unsigned long long t64;
  15.     struct s32 { long th, tl; } t32;
  16. } start, end;
  17.  
  18. void getT (ostream& F, int* array, int offset){ // function that counts time taken for element
  19.  
  20.     double minCycles = 0;
  21.  
  22.     if (array[0] == 0){ // first cache miss
  23.         if (array[string_size * 2] == 0){ // second cache miss
  24.  
  25.             usleep(100);
  26.  
  27.             asm("rdtsc\n":"=a"(start.t32.th),"=d"(start.t32.tl));
  28.  
  29.             if (array[string_size * 4 + offset] == 0){ // acess to the element
  30.                 asm("rdtsc\n":"=a"(end.t32.th),"=d"(end.t32.tl));
  31.             }
  32.         }
  33.     }
  34.  
  35.     minCycles = (end.t64 - start.t64); // counting time
  36.  
  37.     F << minCycles << endl; // printing time to the file
  38.  
  39. }
  40.  
  41. int main(){
  42.  
  43.     int *array = new int[N];
  44.     int *clearCacheArray = new int[L2_cache];
  45.     int k = 0;
  46.  
  47.     ofstream F ("measure.log", ofstream::out);
  48.  
  49.     memset(array, 0, N * sizeof(int));
  50.  
  51.     memset(clearCacheArray, 0, L2_cache * sizeof(int));
  52.  
  53.     for (volatile int i = 1; i < 1025; ++i){
  54.  
  55.         getT(F, array, i);
  56.  
  57.         for (volatile int j = 0; j < L2_cache; ++j)
  58.             if (clearCacheArray[j] == 0)
  59.                 k++;
  60.     }
  61.  
  62.     cout << "Job done" << endl;
  63.  
  64.     F.close();
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement