Professional Documents
Culture Documents
21
Aim: Simulation of code memory for LRU, FIFO and RAND replacement
policies
Theory
In computing, cache algorithms (also frequently called cache
replacement algorithms or cache replacement policies) are optimizing
instructions, or algorithms, that a computer program or a
hardware-maintained structure can utilize in order to manage a cache
of information stored on the computer.
There are two primary figures of merit of a cache: The latency, and the
hit rate. There are also a number of secondary factors affecting cache
performance. The "hit ratio" of a cache describes how often a
searched-for item is actually found in the cache. More efficient
replacement policies keep track of more useful information in order to
improve the hit rate (for a given cache size). The "latency" of a cache
describes how long after requesting a desired item the cache can
return that item (when there is a hit). Faster replacement strategies
typically keep track of less usage information—or, in the case of
direct-mapped cache, no information—to reduce the amount of time
required to update that information. Each replacement strategy is a
compromise between hit rate and latency. Hit rate measurements are
typically performed on benchmark applications. The actual hit ratio
varies widely from one application to another. In particular, video and
audio streaming applications often have a hit ratio close to zero,
because each bit of data in the stream is read once for the first time (a
compulsory miss), used, and then never read or written again. Even
worse, many cache algorithms (in particular, LRU) allow this
streaming data to fill the cache, pushing out of the cache information
that will be used again soon
Amogh Gajare D11A Roll no. 21
Code
#include <stdio.h> //Use dynamic allocation using
#include <stdlib.h> malloc to optimise space
#include <string.h> char input_stream[100];
#include <time.h>
printf("Enter cache size:");
void FIFO_sim(int,char scanf("%d",&cache_size);
input_stream[100]); printf("Enter input stream
void LRU_sim(int,char (without spaces):");
input_stream[100]); scanf("%s",&input_stream);
void RAND_sim(int,char
input_stream[100]); printf("MENU\n1.LRU
int search(char arr[100],char); \n2.FIFO\n3.RAND\nWhich
int search2(int arr[100],int,int); replacement policy do you want
void single_inc(int to simulate?:");
arr[100],int,int); scanf("%d",&choice);
void my_copy(char s1[100],char
s2[100],int); switch (choice)
int max(int arr[100],int); {
case
1:LRU_sim(cache_size,input_str
int main() eam);
{ break;
int choice,cache_size; case
2:FIFO_sim(cache_size,input_str
//Maximum input stream size eam);
is 100 break;
//Modify this number if you case
want 3:RAND_sim(cache_size,input_s
tream);
Amogh Gajare D11A Roll no. 21
break; }
default:printf("Please choose
an option from 1,2 or 3"); // an utility function to
break; increment all values except at a
} given index
void single_inc(int arr[100],int
return 0; index,int n)
} {
int i;
//an utility function to quickly for(i=0;i<n;i++)
search in an array {
int search2(int arr[100],int if(i!=index){arr[i]++;}
key,int n) }
{ }
int i;
for(i=0; i<n; i++) // strcpy fails for initialised
{ string literals
if(arr[i] == key) {return i;} void my_copy(char s1[100],char
} s2[100],int cache_size)
return -1; {
} int i;
for(i=0;i<cache_size;i++)
//an utility function to quickly {
search in an array s1[i]=s2[i];
int search(char arr[100],char }
key) }
{
int i; // return the index of the largest
for(i=0; i<strlen(arr); i++) value in an array
{ int max(int arr[100],int n)
if(arr[i] == key) {return i;} {
} int m = arr[0],mi=0,i;
return -1; for(i=1;i<n;i++)
Amogh Gajare D11A Roll no. 21
{ {
if(arr[i]>m){m=arr[i];mi=i;} if(i!=0)
} {
return mi;
} my_copy(cache_state[i],cache_s
tate[i-1],cache_size);
void FIFO_sim(int }
cache_size,char
input_stream[100]) if(search(cache_state[i],
{ input_stream[i])!=-1)
int i,j; {
int target_index = 0,hits = hits++;
0,faults = 0,filled = 0; }
int targets[3] = {1,2,0}; else
char {
cache_state[strlen(input_stream filled < cache_size?
)][cache_size]; filled++:faults++;
cache_state[i][target_index] =
/*initialise your cache states input_stream[i];
for the simulation, note that target_index =
single line initialisation fails targets[target_index];
for non constant integer }
dimensions */ }
for(i=0;input_stream[i]!='\0';i++)
{ //Display the simulation output
for(j=0; j<cache_size; j++) for(i=0;i<cache_size;i++)
{ {
cache_state[i][j]=' '; for(j=0; j<strlen(input_stream);
} j++)
} {
printf("%c ",cache_state[j][i]);
for(i=0; input_stream[i]!='\0'; }
i++) printf("\n");
Amogh Gajare D11A Roll no. 21
} srand(time(NULL));
printf("Hits = %d\n",hits);
printf("Faults = %d\n",faults); for(i=0; input_stream[i]!='\0';
printf("Hit ratio = i++)
%f\n",(float)hits/((float)hits+(floa {
t)faults)); if(i!=0)
{
} my_copy(cache_state[i],
cache_state[i-1],cache_size);
}
void RAND_sim(int
cache_size,char if(search(cache_state[i],
input_stream[100]) input_stream[i])!=-1)
{ {
int i,j; hits++;
int target_index = 0,hits = }
0,faults = 0,filled = 0; else
int targets[3] = {1,2,0}; {
char if(filled < cache_size)
cache_state[strlen(input_stream {
)][cache_size]; filled++;
cache_state[i][target_index]
= input_stream[i];
for(i=0;input_stream[i]!='\0';i++) target_index =
{ targets[target_index];
for(j=0; j<cache_size; j++) }
{ else
cache_state[i][j]=' '; {
} faults++;
} //don't enumerate randomly
until cache has been filled at
//initialise random number least once
generator
Amogh Gajare D11A Roll no. 21
cache_state[i][rand() % 3] = char
input_stream[i]; cache_state[strlen(input_stream
} )][cache_size];
}
}
for(i=0;input_stream[i]!='\0';i++)
for(i=0;i<cache_size;i++) {
{ for(j=0; j<cache_size; j++)
for(j=0; j<strlen(input_stream); {
j++) cache_state[i][j]=' ';
{ }
printf("%c ",cache_state[j][i]); }
}
printf("\n"); for(i=0;input_stream[i]!='\0';i++)
} {
printf("Hits = %d\n",hits); if(i!=0)
printf("Faults = %d\n",faults); {
printf("Hit ratio = my_copy(cache_state[i],
%f\n",(float)hits/((float)hits+(floa cache_state[i-1],cache_size);
t)faults)); }
}
if(filled < cache_size)
{
if(search(cache_state[i],
void LRU_sim(int input_stream[i])!=-1)
cache_size,char {
input_stream[100]) hits++;
{ int s_res =
int i,j; search2(last_used[0],
int target_index = 0,hits = input_stream[i], cache_size);
0,faults = 0,filled = 0; last_used[1][s_res] = 0;
int targets[3] = {1,2,0}, single_inc(last_used[1],
last_used[2][cache_size]; s_res, cache_size);
Amogh Gajare D11A Roll no. 21
} faults++;
else int max_i =
{ max(last_used[1], cache_size);
filled++;
cache_state[i][target_index] cache_state[i][search(cache_stat
= input_stream[i]; e[i], (char)last_used[0][max_i])] =
last_used[0][target_index] = input_stream[i];
(int)input_stream[i]; last_used[0][max_i] =
last_used[1][target_index] = (int)input_stream[i];
0; last_used[1][max_i] = 0;
single_inc(last_used[1],
single_inc(last_used[1],target_in max_i, cache_size);
dex,cache_size); }
target_index = }
targets[target_index]; }
}
} for(i=0;i<cache_size;i++)
else {
{ for(j=0; j<strlen(input_stream);
if(search(cache_state[i], j++)
input_stream[i])!=-1) {
{ printf("%c ",cache_state[j][i]);
hits++; }
int s_res = printf("\n");
search2(last_used[0], }
input_stream[i], cache_size); printf("Hits = %d\n",hits);
last_used[1][s_res] = 0; printf("Faults = %d\n",faults);
single_inc(last_used[1], printf("Hit ratio =
s_res, cache_size); %f\n",(float)hits/((float)hits+(floa
} t)faults));
else }
{
Amogh Gajare D11A Roll no. 21
Results
LRU
FIFO
Amogh Gajare D11A Roll no. 21
RAND
Amogh Gajare D11A Roll no. 21
Conclusion: Thus the replacement algorithms of LRU, FIFO, RAND
were implemented using C