Cache.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package cache;
  7. import java.util.ArrayList;
  8. /**
  9. *
  10. * @author Juninho Carlos
  11. */
  12. public class Cache {
  13. private long escritaCache; //Done
  14. private long leituraCache; //Done
  15. private long contadorDeCiclos; //ToDo
  16. private long contadorDeWB; //Done
  17. private long contadorDeHit; //Done
  18. private long contadorDeMiss; //Done
  19. private long escritaSDRAM;
  20. private long leituraSDRAM;
  21. private Conjunto[] conjuntos;
  22. public Cache() {
  23. this.escritaCache = 0;
  24. this.leituraCache = 0;
  25. this.contadorDeCiclos = 0;
  26. this.contadorDeWB = 0;
  27. this.contadorDeHit = 0;
  28. this.contadorDeMiss = 0;
  29. this.escritaSDRAM = 0;
  30. this.leituraSDRAM = 0;
  31. this.conjuntos = new Conjunto[32];
  32. for (int i = 0; i < 32; i++) {
  33. this.conjuntos[i] = new Conjunto();
  34. }
  35. }
  36. public long getHit() {
  37. return this.contadorDeHit;
  38. }
  39. public long getMiss() {
  40. return this.contadorDeMiss;
  41. }
  42. public long getEscritaSDRAM(){
  43. return this.escritaSDRAM;
  44. }
  45. public long getLeituraSDRAM(){
  46. return this.leituraSDRAM;
  47. }
  48. public long getEscritaCache() {
  49. return this.escritaCache;
  50. }
  51. public long getLeituraCache() {
  52. return this.leituraCache;
  53. }
  54. /**
  55. *
  56. * @param address endereço solicitado à cache
  57. * @param read true -> leitura(lw), false -> escrita(sw)
  58. */
  59. public void requisicaoParaCache(long address, boolean read) {
  60. Requisicao end = new Requisicao(address, read);
  61. // System.out.println("pal = "+end.palavra);
  62. if (isHit(end)) { //Verifica se é hit, e, se for, atualiza o bit de sujeira, caso necessário.
  63. this.contadorDeHit++;
  64. // System.out.println("HIT: endereco: " + end.endereco);
  65. // System.err.println("");
  66. if (read) {
  67. this.contadorDeCiclos += ConstanteDeTempo.hit_read;
  68. this.leituraCache++;
  69. } else {
  70. this.contadorDeCiclos += ConstanteDeTempo.hit_write; //contabiliza ciclos, inclusive os da comparação
  71. this.escritaCache++;
  72. }
  73. } else {
  74. //Deu miss
  75. //System.out.println("MISS " + address);
  76. this.contadorDeMiss++;
  77. if (read) {
  78. this.contadorDeCiclos += ConstanteDeTempo.leitura_miss;
  79. } else {
  80. this.contadorDeCiclos += ConstanteDeTempo.escrita_miss;
  81. }
  82. this.requisicaoDeBlocoNoConjunto(end);
  83. //conjuntos[end.conjunto].requisicaoDeBlocoNoConjunto(end,this.redundancia);
  84. }
  85. }
  86. public long getWB() {
  87. return this.contadorDeWB;
  88. }
  89. public long getCiclos() {
  90. return this.contadorDeCiclos;
  91. }
  92. private void tratarWB(Requisicao req, int numBloco) {
  93. Conjunto c = this.conjuntos[req.conjunto];
  94. //System.out.println("WB do bloco - "+numBloco);
  95. if (c.getBloco(numBloco).isSujo()) {
  96. this.leituraCache += 32;
  97. //Contabilizar um WB
  98. this.contadorDeCiclos += ConstanteDeTempo.WB;
  99. this.contadorDeWB++;
  100. this.escritaSDRAM += 32;
  101. // System.err.println("fazer WB");
  102. }
  103. c.desalocaBloco(numBloco);
  104. }
  105. public Conjunto[] getConjuntos(){
  106. return this.conjuntos;
  107. }
  108. private void requisicaoDeBlocoNoConjunto(Requisicao req) {
  109. Conjunto c = this.conjuntos[req.conjunto];
  110. int blocoSelecionado;
  111. if (c.hasBlocoDisponivel()) { //VAI USAR RR
  112. this.escritaCache += 32;
  113. this.leituraSDRAM += 32;
  114. // System.out.println("usando RR");
  115. blocoSelecionado = c.getRR();
  116. if(c.getBloco(blocoSelecionado).hasErrorInBloco()){
  117. c.setarBlocoAsInvalid(blocoSelecionado);
  118. //verificar se é escrita. Se for, fazer "WB"
  119. if(!req.read){
  120. this.escritaSDRAM += 1;
  121. this.contadorDeCiclos += ConstanteDeTempo.escrita_1_palavra_na_SDRAM;
  122. }
  123. //Contabiliza o que for necessário aqui em embaixo
  124. }else{
  125. if(req.read){
  126. this.leituraCache++;
  127. }else{
  128. this.escritaCache += 1;
  129. }
  130. c.alocarBloco(req, blocoSelecionado);
  131. c.lru.atualizaLRU(blocoSelecionado);
  132. }
  133. //Atualiza as variáves da política de substituição de bloco
  134. c.atualizaRR();
  135. } else {
  136. //System.out.println("LRU");
  137. //Usa LRU
  138. blocoSelecionado = c.lru.getBlocoFromLru();
  139. if(blocoSelecionado == -1){
  140. //Contabilizar ao acesso à memória principal e seus ciclos
  141. //System.out.println("Conjunto incachavel");
  142. if(req.read){
  143. this.leituraSDRAM += 1;
  144. }else{
  145. this.contadorDeCiclos += ConstanteDeTempo.escrita_1_palavra_na_SDRAM;
  146. this.escritaSDRAM += 1;
  147. }
  148. }else{
  149. this.leituraSDRAM += 32;
  150. this.escritaCache += 32;
  151. if(req.read){
  152. this.leituraCache++;
  153. }else{
  154. this.escritaCache += 1;
  155. }
  156. this.tratarWB(req, blocoSelecionado); //Já contabiliza ciclos e WB
  157. c.alocarBloco(req, blocoSelecionado);
  158. c.lru.atualizaLRU(blocoSelecionado);
  159. }
  160. }
  161. }
  162. /**
  163. * Insere erro na memória cache
  164. *
  165. * @param conjunto - Número do conjunto
  166. * @param bloco - Número do bloco
  167. * @param palavra - Número da palavra
  168. */
  169. public void inserirErro(int conjunto, int bloco, int palavra) {
  170. this.conjuntos[conjunto].getBloco(bloco).inserirErro(palavra);
  171. }
  172. private boolean isHit(Requisicao address) {
  173. return conjuntos[address.conjunto].verificaHit(address);
  174. }
  175. @Override
  176. public String toString() {
  177. String s = "";
  178. Bloco[] b;
  179. int i = 0;
  180. for (Conjunto conjunto : conjuntos) {
  181. s += "Conjunto " + i + "\n";
  182. //s += conjunto;
  183. b = conjunto.getBlocos();
  184. int j = 0;
  185. for (Bloco bloco : b) {
  186. s += "Bloco " + j + "\n";
  187. s += bloco + "\n";
  188. j++;
  189. }
  190. s += "\n\n\n";
  191. i++;
  192. }
  193. return s; //To change body of generated methods, choose Tools | Templates.
  194. }
  195. }