/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cache; import java.util.ArrayList; /** * * @author Juninho Carlos */ public class Cache { private long escritaCache; //Done private long leituraCache; //Done private long contadorDeCiclos; //ToDo private long contadorDeWB; //Done private long contadorDeHit; //Done private long contadorDeMiss; //Done private long escritaSDRAM; private long leituraSDRAM; private Conjunto[] conjuntos; public Cache() { this.escritaCache = 0; this.leituraCache = 0; this.contadorDeCiclos = 0; this.contadorDeWB = 0; this.contadorDeHit = 0; this.contadorDeMiss = 0; this.escritaSDRAM = 0; this.leituraSDRAM = 0; this.conjuntos = new Conjunto[32]; for (int i = 0; i < 32; i++) { this.conjuntos[i] = new Conjunto(); } } public long getHit() { return this.contadorDeHit; } public long getMiss() { return this.contadorDeMiss; } public long getEscritaSDRAM(){ return this.escritaSDRAM; } public long getLeituraSDRAM(){ return this.leituraSDRAM; } public long getEscritaCache() { return this.escritaCache; } public long getLeituraCache() { return this.leituraCache; } /** * * @param address endereço solicitado à cache * @param read true -> leitura(lw), false -> escrita(sw) */ public void requisicaoParaCache(long address, boolean read) { Requisicao end = new Requisicao(address, read); // System.out.println("pal = "+end.palavra); if (isHit(end)) { //Verifica se é hit, e, se for, atualiza o bit de sujeira, caso necessário. this.contadorDeHit++; // System.out.println("HIT: endereco: " + end.endereco); // System.err.println(""); if (read) { this.contadorDeCiclos += ConstanteDeTempo.hit_read; this.leituraCache++; } else { this.contadorDeCiclos += ConstanteDeTempo.hit_write; //contabiliza ciclos, inclusive os da comparação this.escritaCache++; } } else { //Deu miss //System.out.println("MISS " + address); this.contadorDeMiss++; if (read) { this.contadorDeCiclos += ConstanteDeTempo.leitura_miss; } else { this.contadorDeCiclos += ConstanteDeTempo.escrita_miss; } this.requisicaoDeBlocoNoConjunto(end); //conjuntos[end.conjunto].requisicaoDeBlocoNoConjunto(end,this.redundancia); } } public long getWB() { return this.contadorDeWB; } public long getCiclos() { return this.contadorDeCiclos; } private void tratarWB(Requisicao req, int numBloco) { Conjunto c = this.conjuntos[req.conjunto]; //System.out.println("WB do bloco - "+numBloco); if (c.getBloco(numBloco).isSujo()) { this.leituraCache += 32; //Contabilizar um WB this.contadorDeCiclos += ConstanteDeTempo.WB; this.contadorDeWB++; this.escritaSDRAM += 32; // System.err.println("fazer WB"); } c.desalocaBloco(numBloco); } public Conjunto[] getConjuntos(){ return this.conjuntos; } private void requisicaoDeBlocoNoConjunto(Requisicao req) { Conjunto c = this.conjuntos[req.conjunto]; int blocoSelecionado; if (c.hasBlocoDisponivel()) { //VAI USAR RR this.escritaCache += 32; this.leituraSDRAM += 32; // System.out.println("usando RR"); blocoSelecionado = c.getRR(); if(c.getBloco(blocoSelecionado).hasErrorInBloco()){ c.setarBlocoAsInvalid(blocoSelecionado); //verificar se é escrita. Se for, fazer "WB" if(!req.read){ this.escritaSDRAM += 1; this.contadorDeCiclos += ConstanteDeTempo.escrita_1_palavra_na_SDRAM; } //Contabiliza o que for necessário aqui em embaixo }else{ if(req.read){ this.leituraCache++; }else{ this.escritaCache += 1; } c.alocarBloco(req, blocoSelecionado); c.lru.atualizaLRU(blocoSelecionado); } //Atualiza as variáves da política de substituição de bloco c.atualizaRR(); } else { //System.out.println("LRU"); //Usa LRU blocoSelecionado = c.lru.getBlocoFromLru(); if(blocoSelecionado == -1){ //Contabilizar ao acesso à memória principal e seus ciclos //System.out.println("Conjunto incachavel"); if(req.read){ this.leituraSDRAM += 1; }else{ this.contadorDeCiclos += ConstanteDeTempo.escrita_1_palavra_na_SDRAM; this.escritaSDRAM += 1; } }else{ this.leituraSDRAM += 32; this.escritaCache += 32; if(req.read){ this.leituraCache++; }else{ this.escritaCache += 1; } this.tratarWB(req, blocoSelecionado); //Já contabiliza ciclos e WB c.alocarBloco(req, blocoSelecionado); c.lru.atualizaLRU(blocoSelecionado); } } } /** * Insere erro na memória cache * * @param conjunto - Número do conjunto * @param bloco - Número do bloco * @param palavra - Número da palavra */ public void inserirErro(int conjunto, int bloco, int palavra) { this.conjuntos[conjunto].getBloco(bloco).inserirErro(palavra); } private boolean isHit(Requisicao address) { return conjuntos[address.conjunto].verificaHit(address); } @Override public String toString() { String s = ""; Bloco[] b; int i = 0; for (Conjunto conjunto : conjuntos) { s += "Conjunto " + i + "\n"; //s += conjunto; b = conjunto.getBlocos(); int j = 0; for (Bloco bloco : b) { s += "Bloco " + j + "\n"; s += bloco + "\n"; j++; } s += "\n\n\n"; i++; } return s; //To change body of generated methods, choose Tools | Templates. } }