123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- /*
- * 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 long leituraSDRAMDevidoAERRO;
- private long escritaSDRAMDevidoAERRO;
-
- 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.leituraSDRAMDevidoAERRO = 0;
- this.escritaSDRAMDevidoAERRO = 0;
-
- this.conjuntos = new Conjunto[32];
- for (int i = 0; i < 32; i++) {
- this.conjuntos[i] = new Conjunto();
- }
- }
-
- public long getLeituraSDRAMDevidoErro(){
- return this.leituraSDRAMDevidoAERRO;
- }
-
- public long getEscritaSDRAMDevidoErro(){
- return this.escritaSDRAMDevidoAERRO;
- }
-
- 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.usaRR()) { //VAI USAR RR
- //System.out.println("usando RR");
- blocoSelecionado = c.getRR();
- if (!c.getBloco(blocoSelecionado).isCachavel()) {
- // System.out.println("Mapeado para um bloco não cachavel");
- if (req.read) {
- this.leituraSDRAMDevidoAERRO += 1;
- this.leituraSDRAM += 1;
- this.contadorDeCiclos += ConstanteDeTempo.acesso_1_palavra_na_SDRAM;
- }else{
- this.escritaSDRAMDevidoAERRO += 1;
- this.escritaSDRAM += 1;
- this.contadorDeCiclos += ConstanteDeTempo.acesso_1_palavra_na_SDRAM;
- }
- } else if (c.getBloco(blocoSelecionado).hasErrorInBloco()) {
- c.setarBlocoAsInvalid(blocoSelecionado);
-
- this.escritaCache += 32;
- //verificar se é escrita. Se for, fazer "WB"
- if (!req.read) {
- this.escritaSDRAMDevidoAERRO += 1;
- this.escritaSDRAM += 1;
- this.contadorDeCiclos += ConstanteDeTempo.acesso_1_palavra_na_SDRAM;
- }
- //Contabiliza o que for necessário aqui em embaixo
- } else {
- this.escritaCache += 32;
-
- if(!req.read){
- this.escritaCache++;
- }
- //verifica WB
- if(c.getBloco(blocoSelecionado).isValido() && c.getBloco(blocoSelecionado).isSujo()){
- this.tratarWB(req, blocoSelecionado);
- }
-
- c.alocarBloco(req, blocoSelecionado);
- }
- //Atualiza as variáves da política de substituição de bloco
- c.lru.atualizaLRU(blocoSelecionado);
- c.atualizaRR();
- } else {
- //System.out.println("LRU");
- //Usa LRU
- blocoSelecionado = c.lru.getBlocoFromLru();
- if (!c.getBloco(blocoSelecionado).isCachavel()) {
- //Contabilizar ao acesso à memória principal e seus ciclos
- //System.out.println("Conjunto incachavel");
- if (req.read) {
- this.leituraSDRAMDevidoAERRO += 1;
- this.leituraSDRAM += 1;
- } else {
- this.escritaSDRAMDevidoAERRO += 1;
- this.contadorDeCiclos += ConstanteDeTempo.acesso_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.
- }
- }
|