KDOXG

lib_cache

May 27th, 2019
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. /** -------- CACHE --------
  7.  * Funções para se usar na Cache.
  8.  */
  9.  
  10. struct Cache{
  11.     int tag;
  12.     int policyControl;
  13.     char bit_valid;
  14. };
  15.  
  16. struct Miss{
  17.     int compulsory;
  18.     int capacity;
  19.     int conflict;
  20. };
  21.  
  22. typedef struct Endereco{
  23.     int a;
  24.     char nuu;
  25. }Address;
  26.  
  27. void setReplacement(struct Cache **cache, char policy, int i, int assoc, int index, char flag)
  28. {
  29.     if (policy >> 0 == 0)//default
  30.         return;
  31.     if (policy >> 1 == 0)//LRU
  32.     {
  33.         if (cache[i][index].policyControl == assoc-1)
  34.             return;
  35.         int j, id=cache[i][index].policyControl;
  36.         cache[i][index].policyControl = assoc-1;
  37.         for (j=0; j<assoc; j++)
  38.         {
  39.             if (cache[j][index].bit_valid != 0 && cache[j][index].policyControl > id && j != i)
  40.                 cache[j][index].policyControl--;
  41.         }
  42.         return;
  43.     }
  44.     if (policy >> 2 == 0)//LFU
  45.     {
  46.         if (flag == 0b00000001 || flag == 0b00000010)//flags de miss
  47.             cache[i][index].policyControl = 0;
  48.         else
  49.             cache[i][index].policyControl++;
  50.         return;
  51.     }
  52.     if (policy >> 3 == 0)//FIFO
  53.     {
  54.         if (flag == 0b00000000)//flag de hit
  55.         {
  56.             return;
  57.         }
  58.         if (flag == 0b00000010)//flag de miss compulsorio
  59.         {
  60.             int j, pol=-1;
  61.             for (j=0; j<assoc && cache[j][index].bit_valid != 0; j++)
  62.                 pol++;
  63.             cache[j-1][index].policyControl = pol;
  64.             return;
  65.         }
  66.         int j, id;
  67.         for (j=0; j<assoc; j++)
  68.             if (cache[j][index].policyControl == 0)
  69.                 id = j;
  70.         cache[id][index].policyControl = assoc-1;
  71.         for (j=0; j<assoc; j++)
  72.             if (j != id)
  73.                 cache[j][index].policyControl--;
  74.         return;
  75.     }
  76.     if (policy >> 4 == 0)//LIFO
  77.         return;
  78.     return;
  79. }
  80.  
  81. int getReplacement(struct Cache **cache, char policy, int assoc, int index)
  82. {
  83.     if (policy >> 0 == 0)//default
  84.         return rand() % assoc;
  85.     if (policy >> 1 == 0)//LRU
  86.     {
  87.         int i, id, pol=assoc;
  88.         for (i=0; i<assoc; i++)
  89.         {
  90.             if (cache[i][index].policyControl < pol)
  91.             {
  92.                 pol = cache[i][index].policyControl;
  93.                 id = i;
  94.             }
  95.         }
  96.         return id;
  97.     }
  98.     if (policy >> 2 == 0)//LFU
  99.     {
  100.         int i, id, pol=cache[0][index].policyControl;
  101.         for (i=0; i<assoc; i++)
  102.         {
  103.             if (cache[i][index].policyControl < pol)
  104.             {
  105.                 pol = cache[i][index].policyControl;
  106.                 id = i;
  107.             }
  108.         }
  109.         return id;
  110.     }
  111.     if (policy >> 3 == 0)//FIFO
  112.     {
  113.         int i, id, pol=assoc;
  114.         for (i=0; i<assoc; i++)
  115.         {
  116.             if (cache[i][index].policyControl < pol)
  117.             {
  118.                 pol = cache[i][index].policyControl;
  119.                 id = i;
  120.             }
  121.         }
  122.         return id;
  123.     }
  124.     if (policy >> 4 == 0)//LIFO
  125.         return assoc-1;
  126.     return 0;
  127. }
  128.  
  129. /** -------- HACHE --------
  130.  * Funções para se usar na Hache
  131.  */
  132.  
  133. struct Hache{
  134.     struct Hache *chain;
  135.     int tag;
  136.     char valid;
  137. };
  138.  
  139. int hash(long long int tag, int nsets)
  140. {
  141.     tag += 1;
  142.     tag *= tag;
  143.     tag /= nsets;
  144.     tag %= nsets;
  145.     return tag;
  146. }
  147.  
  148. int rehash(int id, int tag, int nsets)
  149. {
  150.     int inc = 1 + (tag % (nsets - 1));
  151.     inc = (id + inc) % nsets;
  152.     return inc;
  153. }
  154.  
  155. void initializeHache(struct Hache *hache, int nsets)
  156. {
  157.     int i;
  158.     for (i=0; i<nsets; i++)
  159.     {
  160.         hache[i].chain = NULL;
  161.         hache[i].tag = 0;
  162.         hache[i].valid = '\0';
  163.     }
  164. }
  165.  
  166. void deleteChain(struct Hache *hache)
  167. {
  168.     if (hache->chain != NULL)
  169.         deleteChain(hache->chain);
  170.     free(hache);
  171. }
  172.  
  173. void deleteHache(struct Hache *hache, int nsets)
  174. {
  175.     int i;
  176.     for (i=0; i<nsets; i++)
  177.         if (hache[i].chain != NULL)
  178.             deleteChain(hache[i].chain);
  179.     free(hache);
  180. }
  181.  
  182. void addChaining(struct Hache *hache, int id, int tag)
  183. {
  184.     if (hache[id].valid == 0)
  185.     {
  186.         hache[id].tag = tag;
  187.         hache[id].valid |= 0b00000001;
  188.     }
  189.     else
  190.     {
  191.         struct Hache *chain_aux = hache[id].chain;
  192.         while (chain_aux != NULL)
  193.             chain_aux = chain_aux->chain;
  194.         chain_aux = malloc(sizeof(struct Hache));
  195.         chain_aux->chain = NULL;
  196.         chain_aux->tag = tag;
  197.         chain_aux->valid = 1;
  198.     }
  199. }
  200.  
  201. int help(char *input){
  202.     if(strcmp(input, "help\n") == 0){
  203.         printf(" ** Simulador de Cache - Kevin e Frederico **\n");
  204.         printf("O simulador de cache deve ser inicializado da seguinte forma:\n");
  205.         printf("\t<politica_de_substituicao> <nsets_L1>:<bsize_L1>:<assoc_L1> arquivo_de_entrada\n\n");
  206.         printf("Para saber mais sobre um determinado parâmetro digite \"help\" seguido por:\n");
  207.         printf("* <politica_de_substituicao> ou <politica>\n* <nsets_L1> ou <nsets>\n* <bsize_L1> ou <bsize>\n* <assoc_L1> ou <assoc>\n* <arquivo_de_entrada> ou <arquivo>\n  Ex: \"help nsets_L1\"\n");
  208.         printf("Mais informações sobre o funcionamento do simulador consulte o relatório que pode ser encontrado em:\n>> http://bit.ly/2Xayout\n\n");
  209.         return 0;
  210.     }
  211.     else if(strcmp(input, "help nsets_L1\n") == 0 || strcmp(input, "help nsets\n") == 0){
  212.         printf("Quantidade de conjuntos para armazenar na cache.\nDefault: 256\n\n");
  213.         return 0;
  214.     }
  215.     else if(strcmp(input, "help bsize_L1\n") == 0 || strcmp(input, "help bsize\n") == 0){
  216.         printf("Tamanho do bloco em bytes de cada endereço da cache.\nDefault: 4\n\n");
  217.         return 0;
  218.     }
  219.     else if(strcmp(input, "help assoc_L1\n") == 0 || strcmp(input, "help assoc\n") == 0){
  220.         printf("Nivel de associatividade dos conjuntos.\nDefault: 1\n\n");
  221.         return 0;
  222.     }
  223.     else if((strcmp(input, "help arquivo_de_entrada\n") == 0) || (strcmp(input, "help arquivo\n") == 0)){
  224.         printf("Nome do arquivo de entrada que armazena todos os enderecos divididos em 4 bytes para a simulacao. Este nome nao pode ser vazio.\n\n");
  225.         return 0;
  226.     }
  227.     else if((strcmp(input, "help politica_de_substituicao\n") == 0) || (strstr(input, "help politica_de_substituicao ") == input) || (strstr(input, "help politica ") == input) || ((strcmp(input, "help politica\n") == 0))){
  228.         if(strstr(input, "LRU"))
  229.             printf("Least Recently Used: substitui o elemento do conjunto que foi chamado a mais tempo\n\n");
  230.         else if(strstr(input, "LFU"))
  231.             printf("Least Frequently Used: substitui o elemento do conjunto que foi chamado menos vezes\n\n");
  232.         else if(strstr(input, "FIFO"))
  233.             printf("First In, First Out: substitui o elemento do conjunto que foi inserido a mais tempo\n\n");
  234.         else if(strstr(input, "LIFO"))
  235.             printf("Last In, First Out: substitui o elemento do conjunto que foi inserido a menos tempo\n\n");
  236.         else if(strstr(input, "RR") || strstr(input, "Random Replacement"))
  237.             printf("Random Replacement: substitui o elemento do conjunto escolhido aleatoriamente\n\n");
  238.         else{
  239.             printf("Configurar a politica de substituição em caches associativas.\nOs macros suportados para configurar esta opção são: 'LRU', 'LFU', 'FIFO' e 'LIFO'.\n");
  240.             printf("O macro padrão, quando nenhum especificado for usado, será 'Random Replacement' ou 'RR'.\n");
  241.             printf("Se a associatividade for igual a 1 ou a palavra inserida não pertencer a estes macros, qualquer parâmetro inserido sera ignorado e o programa usara a opção padrão.\n\n");
  242.             printf("Para saber mais sobre uma politica de substituição digite \"help politica\" ou \"help politica_de_substituicao\" seguido por uma da opções.\n\n");
  243.         }
  244.  
  245.         return 0;
  246.     }
  247.  
  248.     return 1;
  249. }
Add Comment
Please, Sign In to add comment