	#!/usr/bin/perl
	#
	# FIFO algorithm
	#
	
	#define constants

	#format of new traces
	$_Timestamp=0; 	$_Elapsed_Time=1;	$_HTTP_code=2;  
	$_Size=3; 		$_Method=4; 		$_URL=5;
	$_Exp_Date=6;	$_Next_Acc=7; 		$_Cacheable=8;

	$all_free = 100000000;						#cache size in bytes
	$fifo_free = $all_free;						#fifo cache size
	$elapsed_time = 0;							#simulation time
	$path="";									#path to the working directory

   	#open log file
	$logname="rez_fifo.txt";
	open(LOG, "+<$path$logname") || die "$0: $path$logname will not open.";
	seek(LOG, 0,2);

	#process all files in directory with extension ".new"
	foreach $file (<$path*.new>) {
       	open(TRACES, "+<$file") || die "$file will not open.";
		print "\nOpened: ". $file. "\n";
		seek(TRACES, 0,2); $file_size = tell(TRACES); seek(TRACES, 0,0); 
		
		$pos = tell(TRACES);
		$prev_pos = tell(TRACES);
		$start_time = time();
		while(<TRACES>) { # takes data from the $file
			$proc=tell(TRACES)/$file_size;
			print "\r$cur_time done: ".$proc." of 1     ";
			#if($proc>0.025){last;}
			@list = split(" ", $_);
			if($list[$_Cacheable]){
				&fifo;
			}
		}
		$end_time = time();
		close(TRACES);
		$elapsed_time += ($end_time-$start_time);
    }

	&std_stat;											#stat to screen
	&log_stat;											#stat to log file

	close(LOG);
	print "\nClosed: $path$logname\n";

## end ##


sub std_stat{
		print "Size = $all_free bytes, elapsed $elapsed_time seconds	atp refresh=$atp_refresh\n";
		if($fifo_access){print "FIFO     : Acc = $fifo_access, Hits = $fifo_hits (".($fifo_hits/$fifo_access*100)."%), Miss = $fifo_miss \n".
							   "		   WAcc = $fifo_waccess, WHits = $fifo_whits (".($fifo_whits/$fifo_waccess*100)."%), WMiss = $fifo_wmiss \n";}
}
sub log_stat{
		print LOG "Size = $all_free, elapsed $elapsed_time seconds	atp refresh=$atp_refresh\n";
		if($fifo_access){print LOG "FIFO     : Acc = $fifo_access, Hits = $fifo_hits (".($fifo_hits/$fifo_access*100)."%), Miss = $fifo_miss \n".
										"WAcc = $fifo_waccess, WHits = $fifo_whits (".($fifo_whits/$fifo_waccess*100)."%), WMiss = $fifo_wmiss \n";}
}
#############################################
# algorythms
#############################################


###########
# F I F O #
###########

	sub fifo{
		my($i);
		if($all_free*0.1 < $list[$_Size]){			#if filesize > 10% of cache size => file is not cacheable
			$fifo_miss++;		$fifo_wmiss += $list[$_Size];
			$fifo_access++;		$fifo_waccess+=$list[$_Size];
			return 0;
		}elsif(defined $fifo_cache{$list[$_URL]}){
			#found in the cache
			$fifo_hits++;
			$fifo_whits += $list[$_Size];
		}else{
			#not found in the cache
			$fifo_miss++;
			$fifo_wmiss += $list[$_Size];
			
			# if there is no free space in the cache...
			while($fifo_free < $list[$_Size]){
				#...free some space
				$fifo_free += $fifo_cache{$fifo_sorted_access[0]};
				delete($fifo_cache{$fifo_sorted_access[0]});
				shift(@fifo_sorted_access);
			}
			
			#now there is free space in cache
			$fifo_free -= $list[$_Size];				#reserve some space
			push(@fifo_sorted_access, $list[$_URL]);	#add to the tail of the list
		}
		
		#update cache for all cases
		$fifo_cache{$list[$_URL]} = $list[$_Size];		#new last access time
		$fifo_access++;
		$fifo_waccess+=$list[$_Size];
	}
