I'm having some difficulties with the code snippet below:
The call to this function is:
cache = OperaCache(true , endereco, op, cache, descricao, read_hits, read_misses, write_hits, write_misses);
VETOR_CACHE OperaCache(bool contar, unsigned long long int endereco, char op, VETOR_CACHE cache, CACHEDESC descricao, int *read_hits, int *read_misses, int *write_hits, int *write_misses)
clock_t tempo_menor = clock();//tempo maior que os outros
int trocaIndex=-1, indexRead=-1, indexWrite=-1;//inicia sem troca
bool readAchou=false, writeAchou=false;
unsigned int i;
for(i=0; i<descricao.number_of_lines; i++)
{
if(index==cache.vetor[i].index)
{
switch (op)
{
case 'R':
if(!readAchou && (tag==cache.vetor[i].tag))
{
readAchou=true;
indexRead=i;
}
break;
case 'W':
if(!writeAchou)
{
if(tag==cache.vetor[i].tag)
{
writeAchou=true;
indexWrite=i;
}
else if(cache.vetor[i].time<=tempo_menor)
{
tempo_menor=cache.vetor[i].time;
trocaIndex=i;
}
}
break;
default:
printf("Operacao invalida encontrada.");
}
}
}
switch (op)
{
case 'R':
if(readAchou)
{
if((strcmp(descricao.replacement_policy,"LRU")) == 0)
cache.vetor[indexRead].time=clock();
if(contar)
*read_hits+=1;
}
else
{
if(contar)
*read_misses+=1;
cache = OperaCache(false, endereco, 'W', cache, descricao, read_hits, read_misses, write_hits, write_misses);
}
break;
case 'W':
if(writeAchou)
{
cache.vetor[indexWrite].time=clock();
if(contar)
*write_hits+=1;
}
else if(trocaIndex>-1)
{
if(contar)
*write_misses+=1;
cache.vetor[trocaIndex].tag=tag;
cache.vetor[trocaIndex].time=clock();
}
break;
default: printf("Operacao invalida encontrada.");
}
The output should be:
But for some reason it's being:
Before:
In short, my problem is (I imagine) in the flags
readAchou
,writeAchou
and / orcontar
that for some reason are jumping misses cases and considering everything hits ...
Correcting, the error is actually in the contar
flag, however I have no idea how to fix it.