SimuladorCache.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.inserirErroRedundancia(0, 0, 0);
  72. c.requisicaoParaCache(0, true);
  73. c.requisicaoParaCache(4096, true);
  74. c.requisicaoParaCache(8192, true);
  75. c.requisicaoParaCache(12288, true);
  76. c.requisicaoParaCache(16384, true);
  77. return c;
  78. }
  79. public static ArrayList<Erro> trataString(String erro){
  80. String[] erros = erro.split(" ");
  81. ArrayList<Erro> arrayDeErros = new ArrayList<>();
  82. for (String erro1 : erros){
  83. String[] partes = erro1.split("/");
  84. Integer conjunto = Integer.parseInt(partes[0]);
  85. Integer bloco = Integer.parseInt(partes[1]);
  86. Integer palavra = Integer.parseInt(partes[2]);
  87. Erro e = new Erro(conjunto, bloco, palavra);
  88. arrayDeErros.add(e);
  89. }
  90. return arrayDeErros;
  91. }
  92. /**
  93. * Retorna um array onde a posição zero tem os erros da cache o a posicao 1 tem os erros da red
  94. * @param path
  95. * @return
  96. * @throws FileNotFoundException
  97. */
  98. public static Erros initErro(String path) throws FileNotFoundException{
  99. Scanner in = new Scanner(new FileReader(path));
  100. String s = in.nextLine();
  101. String []erros = s.split(";");
  102. String erroCache = erros[0];
  103. String erroRed;
  104. ArrayList<Erro> red = null;
  105. ArrayList<Erro> cache = trataString(erroCache);
  106. if(erros.length > 1){
  107. erroRed = erros[1];
  108. red = trataString(erroRed);
  109. }
  110. Erros aux = new Erros(cache, red);
  111. // System.err.println(teste.toString());
  112. return aux;
  113. }
  114. /**
  115. * @param args the command line arguments
  116. */
  117. public static void main(String[] args) throws FileNotFoundException {
  118. /*/Area de debug
  119. Cache cache = new Cache();
  120. cache.inserirErro(0, 0, 5);
  121. cache.inserirErro(4, 0, 2);
  122. cache.requisicaoParaCache(20, true);
  123. //cache.requisicaoParaCache(20, true);
  124. // cache.requisicaoParaCache(4, true);
  125. // cache.requisicaoParaCache(512, true);
  126. // cache.requisicaoParaCache(512, true);
  127. System.out.println("Hit = " + cache.getHit());
  128. System.out.println("Miss = " + cache.getMiss());
  129. System.out.println("Writeback = " + cache.getWB());
  130. System.out.println("WriteBackForcado = " + cache.getWBForcado());
  131. System.out.println("LeituraEspecial = " + cache.getcontadorLeituraEspecial());
  132. System.out.println("LeituraCache = " + cache.getLeituraCache());
  133. System.out.println("EscritaCache = " + cache.getEscritaCache());
  134. System.out.println("LeituraCacheRedundante = " + cache.getLeituraRedundancia());
  135. System.out.println("EscritaCacheRedundante = " + cache.getEscritaRedundancia());
  136. System.out.println("Ciclos = " + cache.getCiclos());
  137. // */
  138. Cache cache = new Cache();
  139. String pathErros = args[0];
  140. String pathTrace = args[1];
  141. Erros errosInit = initErro(pathErros);
  142. //inicializa os erros da cache
  143. for (Erro arg : errosInit.getErroCache()) {
  144. cache.inserirErro(arg.getConjunto(), arg.getBloco(), arg.getPalavra());
  145. }
  146. //inicializa os erros da redundancia
  147. if(errosInit.getErroRed() != null){
  148. for (Erro arg : errosInit.getErroRed()) {
  149. cache.inserirErroRedundancia(arg.getConjunto(), arg.getBloco(), arg.getPalavra());
  150. }
  151. }
  152. //Executa as requisições do trace
  153. Arquivo trace = new Arquivo(pathTrace);
  154. while(trace.lerArquivo()){
  155. Requisicao atual = trace.getRequisicao();
  156. cache.requisicaoParaCache(atual.endereco, atual.read);
  157. }
  158. System.out.println("Hit = " + cache.getHit());
  159. System.out.println("Miss = " + cache.getMiss());
  160. System.out.println("Writeback = " + cache.getWB());
  161. System.out.println("WriteBackForcado = " + cache.getWBForcado());
  162. System.out.println("LeituraEspecial = " + cache.getcontadorLeituraEspecial());
  163. System.out.println("LeituraCache = " + cache.getLeituraCache());
  164. System.out.println("EscritaCache = " + cache.getEscritaCache());
  165. System.out.println("LeituraCacheRedundante = " + cache.getLeituraRedundancia());
  166. System.out.println("EscritaCacheRedundante = " + cache.getEscritaRedundancia());
  167. System.out.println("Ciclos = " + cache.getCiclos());
  168. System.out.println("Incachavel = " + cache.getContadorIncachavel());
  169. System.out.println("EscritaSDRAM = "+cache.getEscritaSDRAM());
  170. System.out.println("LeituraSDRAM = "+cache.getLeituraSDRAM());
  171. // */
  172. }
  173. }