SimuladorCache.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 simuladorcache;
  7. import cache.*;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileReader;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12. /**
  13. *
  14. * @author Juninho Carlos
  15. */
  16. public class SimuladorCache {
  17. public static Cache teste1() {
  18. Cache c = new Cache();
  19. c.inserirErro(0, 0, 0);
  20. c.inserirErro(0, 0, 5);
  21. c.inserirErro(0, 0, 12);
  22. c.inserirErro(0, 0, 15);
  23. c.requisicaoParaCache(0, false);
  24. c.requisicaoParaCache(16, true);
  25. c.requisicaoParaCache(4096, false);
  26. c.requisicaoParaCache(8192, true);
  27. c.requisicaoParaCache(12288, false);
  28. c.requisicaoParaCache(16384, true);
  29. return c;
  30. }
  31. public static Cache teste2() {
  32. Cache c = new Cache();
  33. c.inserirErro(0, 0, 0);
  34. c.inserirErro(4, 0, 5);
  35. c.inserirErro(8, 0, 12);
  36. c.inserirErro(8, 0, 13);
  37. c.inserirErro(12, 0, 15);
  38. c.inserirErro(16, 0, 0);
  39. c.inserirErro(16, 0, 5);
  40. c.inserirErro(16, 0, 12);
  41. c.inserirErro(16, 0, 15);
  42. c.requisicaoParaCache(0, false);
  43. c.requisicaoParaCache(4, false);
  44. c.requisicaoParaCache(512, false);
  45. c.requisicaoParaCache(1024, false);
  46. c.requisicaoParaCache(1536, false);
  47. c.requisicaoParaCache(2048, true);
  48. return c;
  49. }
  50. public static Cache teste3() {
  51. Cache c = new Cache();
  52. c.inserirErro(0, 0, 0);
  53. c.inserirErro(4, 0, 5);
  54. c.inserirErro(8, 0, 12);
  55. c.inserirErro(8, 0, 13);
  56. c.inserirErro(12, 0, 15);
  57. c.inserirErro(16, 0, 0);
  58. c.inserirErro(16, 0, 5);
  59. c.inserirErro(16, 0, 12);
  60. c.inserirErro(16, 0, 15);
  61. c.requisicaoParaCache(0, false);
  62. c.requisicaoParaCache(512, true);
  63. c.requisicaoParaCache(1024, true);
  64. c.requisicaoParaCache(1536, true);
  65. c.requisicaoParaCache(2048, true);
  66. return c;
  67. }
  68. public static Cache teste4() {
  69. Cache c = new Cache();
  70. c.inserirErro(0, 0, 0);
  71. c.requisicaoParaCache(0, true);
  72. c.requisicaoParaCache(4096, true);
  73. c.requisicaoParaCache(8192, true);
  74. c.requisicaoParaCache(12288, true);
  75. c.requisicaoParaCache(16384, true);
  76. return c;
  77. }
  78. public static ArrayList<Erro> trataString(String erro){
  79. String[] erros = erro.split(" ");
  80. ArrayList<Erro> arrayDeErros = new ArrayList<>();
  81. for (String erro1 : erros){
  82. String[] partes = erro1.split("/");
  83. Integer conjunto = Integer.parseInt(partes[0]);
  84. Integer bloco = Integer.parseInt(partes[1]);
  85. Integer palavra = Integer.parseInt(partes[2]);
  86. Erro e = new Erro(conjunto, bloco, palavra);
  87. arrayDeErros.add(e);
  88. }
  89. return arrayDeErros;
  90. }
  91. /**
  92. * Retorna um array onde a posição zero tem os erros da cache o a posicao 1 tem os erros da red
  93. * @param path
  94. * @return
  95. * @throws FileNotFoundException
  96. */
  97. public static Erros initErro(String path) throws FileNotFoundException{
  98. Scanner in = new Scanner(new FileReader(path));
  99. String s = in.nextLine();
  100. String []erros = s.split(";");
  101. String erroCache = erros[0];
  102. String erroRed;
  103. ArrayList<Erro> red = null;
  104. ArrayList<Erro> cache = trataString(erroCache);
  105. if(erros.length > 1){
  106. erroRed = erros[1];
  107. red = trataString(erroRed);
  108. }
  109. Erros aux = new Erros(cache, red);
  110. // System.err.println(teste.toString());
  111. return aux;
  112. }
  113. /**
  114. * @param args the command line arguments
  115. */
  116. public static void main(String[] args) throws FileNotFoundException {
  117. /*/Area de debug
  118. Cache cache = new Cache();
  119. cache.inserirErro(0, 0, 5);
  120. cache.inserirErro(0, 1, 5);
  121. // cache.inserirErro(0, 2, 5);
  122. // cache.inserirErro(0, 3, 5);
  123. cache.requisicaoParaCache(20, true);
  124. cache.requisicaoParaCache(20, false);
  125. cache.requisicaoParaCache(20, true);
  126. cache.requisicaoParaCache(4096, false);
  127. cache.requisicaoParaCache(8192, true);
  128. cache.requisicaoParaCache(12288, true);
  129. cache.requisicaoParaCache(16384, true);
  130. cache.requisicaoParaCache(20, false);
  131. // cache.requisicaoParaCache(1024, true);
  132. // System.out.println("LRU: "+cache.getConjuntos()[0].lru);
  133. // cache.requisicaoParaCache(512, true);
  134. // cache.requisicaoParaCache(512, true);
  135. // System.out.println("Cache: "+ cache);
  136. System.out.println("Hit = " + cache.getHit());
  137. System.out.println("Miss = " + cache.getMiss());
  138. System.out.println("Writeback = " + cache.getWB());
  139. System.out.println("LeituraCache = " + cache.getLeituraCache());
  140. System.out.println("EscritaCache = " + cache.getEscritaCache());
  141. System.out.println("LeituraSDRAM = " + cache.getLeituraSDRAM());
  142. System.out.println("EscritaSDRAM = " + cache.getEscritaSDRAM());
  143. System.out.println("Ciclos = " + cache.getCiclos());
  144. // */
  145. ///*
  146. Cache cache = new Cache();
  147. String pathErros = args[0];
  148. String pathTrace = args[1];
  149. Erros errosInit = initErro(pathErros);
  150. //inicializa os erros da cache
  151. for (Erro arg : errosInit.getErroCache()) {
  152. cache.inserirErro(arg.getConjunto(), arg.getBloco(), arg.getPalavra());
  153. }
  154. //Executa as requisições do trace
  155. Arquivo trace = new Arquivo(pathTrace);
  156. while(trace.lerArquivo()){
  157. Requisicao atual = trace.getRequisicao();
  158. cache.requisicaoParaCache(atual.endereco, atual.read);
  159. }
  160. System.out.println("Hit = " + cache.getHit());
  161. System.out.println("Miss = " + cache.getMiss());
  162. System.out.println("Writeback = " + cache.getWB());
  163. System.out.println("LeituraCache = " + cache.getLeituraCache());
  164. System.out.println("EscritaCache = " + cache.getEscritaCache());
  165. System.out.println("LeituraSDRAM = " + cache.getLeituraSDRAM());
  166. System.out.println("EscritaSDRAM = " + cache.getEscritaSDRAM());
  167. System.out.println("LeituraSDRAMDevidoAERRO = " + cache.getLeituraSDRAMDevidoErro());
  168. System.out.println("EscritaSDRAMDevidoAERRO = " + cache.getEscritaSDRAMDevidoErro());
  169. System.out.println("Ciclos = " + cache.getCiclos());
  170. // */
  171. }
  172. }