Cache.java 8.1 KB

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