Advertisement
Guest User

Untitled

a guest
Jun 7th, 2020
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7.  
  8. #define CACHE_SIZE (64*64*8)
  9. unsigned char *cache_L1;
  10. unsigned long *T = NULL;
  11.  
  12. void measures(){
  13.  
  14.     asm volatile(
  15.         "mov $1000000,%%r11\n\t"// number of loop iteration
  16.         "mov %%rax,%%r12\n\t"
  17.         "mov %%rbx,%%r10\n\t"
  18.  
  19.         "loop:\n\t"
  20.  
  21.             // Serialize before beginning
  22.             "xor %%rax,%%rax\n\tcpuid\n\t"  
  23.  
  24.             // Make sure that [r12+4096] is not is cache
  25.             "mov %%r12,%%rax\n\t"
  26.             "add $4096, %%rax\n\t" // For experiment 1, this line is changed to 'add $0,%%rax'
  27.             "clflush (%%rax)\n\t"
  28.  
  29.             // Make sure that [r12 + 32768] is not in cache
  30.             "mov %%r12,%%rax\n\t"
  31.             "add $32768, %%rax\n\t"
  32.             "clflush (%%rax)\n\t"
  33.            
  34.  
  35.             // Serialize to make sure that all previous operation are finished before continuing.
  36.             "xor %%rax,%%rax\n\tcpuid\n\t"  // 1 load
  37.  
  38.             // Put [r12 + 4096] in cache
  39.             "mov %%r12,%%rax\n\t"
  40.             "add $4096,%%rax\n\t" // For experiment 1, this line is changed to 'add $0,%%rax'
  41.                         "mov (%%rax),%%r8\n\t"
  42.  
  43.             // Serialize to make sure that all previous operation are finished before continuing.
  44.             "xor %%rax,%%rax\n\tcpuid\n\t"
  45.  
  46.             // ------ Now I will try to access [r12 + 32768]. Since it was fflushed, I expect it is not in cache and I expect to measure a cache miss when reading [r12 + 32768].
  47.                        
  48.  
  49.             // Read PMC configured with MEM_LOAD_UOPS_RETIRED.L1_MISS
  50.             "xor %%rcx,%%rcx\n\trdpmc\n\tshl $32,%%rdx\n\tor %%rdx,%%rax\n\tmov %%rax,%%r9\n\t"
  51.            
  52.             // Serialize to make sure that all previous operation are finished before continuing.
  53.             "xor %%rax,%%rax\n\tcpuid\n\t"  // 1 load
  54.  
  55.  
  56.             // Access [r12 + 32768 ]
  57.             "mov  %%r12, %%rax\n\t"
  58.             "add $32768,%%rax\n\t"
  59.             "mov (%%rax),%%r8\n\t"
  60.  
  61.             // Serialize to make sure that all previous operation are finished before continuing.
  62.             "xor %%rax,%%rax\n\tcpuid\n\t" // 1 load
  63.                        
  64.             // Read PMC configured with MEM_LOAD_UOPS_RETIRED.L1_MISS. Substract with the first read. Store the result in r15
  65.                         "xor %%rcx,%%rcx\n\trdpmc\n\tshl $32,%%rdx\n\tor %%rdx,%%rax\n\tmov %%rax,%%r15\n\t"
  66.            
  67.             // Serialize to make sure that all previous operation are finished before continuing.
  68.             "xor %%rax,%%rax\n\tcpuid\n\t" // 1 load
  69.  
  70.             // STORE
  71.             "mov %%r10,%%rbx\n\t"
  72.             "sub %%r9,%%r15\n\t"
  73.             "movq %%r15, (%%rbx)\n\t"
  74.             "clflush (%%r10)\n\t"
  75.             "add $8,%%r10\n\t"
  76.            
  77.             // Serialize to make sure that all previous operation are finished before continuing.          
  78.             "xor %%rax,%%rax\n\tcpuid\n\t" // 1 load
  79.  
  80.  
  81.         "dec %%r11\n\t"
  82.         "jnz loop\n\t"
  83.         "endloop:"
  84.            
  85.             :
  86.             :"a"(cache_L1),"b"(T)
  87.             :"rcx","rdx","r8","r9","r10","r11","r15");
  88.    
  89.    
  90.  
  91. }
  92.  
  93.  
  94. int main()
  95. {
  96.     // Configure PMC by writting in /dev/CONFIG_MODULE0 (This file is created by a kernel module I made)
  97.     int pmcfd;
  98.     char *pmcname;
  99.     pmcname = "MEM_LOAD_UOPS_RETIRED.L1_MISS";
  100.     pmcfd = open("/dev/CONFIG_MODULE0", O_RDWR);
  101.     assert(pmcfd != -1);
  102.     write(pmcfd, "CHANGEPMC=0",strlen("CHANGEPMC=0"));
  103.     write(pmcfd, pmcname,strlen(pmcname));
  104.     close(pmcfd);
  105.  
  106.     // Allocate a memory buffer and align it to L1 set 0
  107.     cache_L1 = calloc(6*CACHE_SIZE,1);
  108.     unsigned long currentSet = ((unsigned long)cache_L1 >> 6) & 63;
  109.     while(currentSet != 0){
  110.         cache_L1 += 64; // cache line size to jump to the next set
  111.         currentSet = ((long)cache_L1 >> 6) & 63;
  112.     }
  113.  
  114.     // Allocate an array to store results
  115.     T = calloc(1000000,sizeof(unsigned long));
  116.    
  117.  
  118.     // Perform the measures
  119.     measures();
  120.  
  121.     // Print the measures
  122.     for(int nbrmes=0;nbrmes<1000000;++nbrmes)
  123.         printf("%ld\n",T[nbrmes]);
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement