
#########
# A T P #
#########
	
	sub atp{
		local($i, $d, $klass);
		if($all_free*0.1 < @list[5]){
			$atp_miss++;	$atp_wmiss += @list[5];
			$atp_access++;	$atp_waccess+=@list[5];
			return 0;
		}elsif(defined $atp_cache{@list[7]}){
			#found in the cache
			$atp_hits++;
			$atp_whits += @list[5];
			$d = @list[0] - $atp_access_cache{@list[7]};
			#print "d = $d    ";
			for($i=0; $i<10; $i++){	#estimate position in the table
				if($d <= @t_val[$i]) {$klass = $i;$i=10;}
			}
			$ota = $atp_cache{@list[7]};	#objects time access
			if(!defined ($atp_apt{$ota}))
			  {$atp_apt{$ota}=$ota;}
			$ta = $atp_apt{$ota};				#global time access from access patern table
			substr($ta, $klass, 1) = "1";
			$atp_apt{$ota}=$ta;			#update table
			$atp_cache{@list[7]}=$ta;		#update object's state
			#print "ta = $ta    ";
			$klass = index($ta, "1", 0);
			#$atp_sorted_access{@t_val[$klass]} = $atp_sorted_access{@t_val[$klass]}." ".@list[7];
		}else{
			#not found in the cache
			$atp_miss++;
			$atp_wmiss += @list[5];
			
			#print "atp ima: ".$atp_free." treba: ". @list[5]."\n";
			# if there is no free space in the cache...
			while($atp_free < @list[5]){
				#...free some space

				#@sorted_access = sort {$atp_access_cache{$a} <=> $atp_access_cache{$b}} keys %atp_cache;
				#$max_url = @sorted_access[0];
				#$max_delay = @list[0] - $atp_access_cache{$max_url};#0; 
				#print "maxd = $max_delay  lat = $atp_access_cache{$max_url} \n";
				$max_delay = -1;
				foreach $url (keys %atp_cache){
					$d = @list[0] - $atp_access_cache{$url};
					if ($max_delay < $d){
					 	$max_url = $url; $max_delay = $d;
					}else{
						for($i=0; $i<10; $i++){	#estimate position in the table
							if($d <= @t_val[$i]) {$klass = $i;$i=10;}
						}
						$i = index($atp_apt{$atp_cache{$url}}, "1", $klass);	#first '1' right from $klass position
						#print "klass = $klass  i= $i ";
						if($i>=0){	#delay could be foun in table
						$d = @t_val[$i];	#estimated delay
						}#else {
							#delay is bigger then all previous delays
							#posible solutions 
							#$d = infinity or $d = cur_time - last_acc_time
							#$d = @list[0] - $atp_access_cache{$url};
						#}
						#print "d = $d    \n";
					}
				}
				#$pom1 = $atp_cache{$max_url};
				$pom2 = $atp_size_cache{$max_url};
				#delete entry in the cache
				delete($atp_cache{$max_url});
				delete($atp_size_cache{$max_url});
				delete($atp_access_cache{$max_url});
				#print "atp no space => remove: ".$pom1." ". $pom2."\n";
				$atp_free += $pom2;
			}
			
			#now there is free space in cache
			#reserve some space
			$atp_free -= @list[5];
			$atp_cache{@list[7]} = "0000000000";	#new entry

		}
		
		#update cache for all cases
		$atp_access_cache{@list[7]} = @list[0];	#new last access time
		$atp_size_cache{@list[7]} = @list[5];	#new size
		$atp_access++;
		$atp_waccess+=@list[5];
	}

#########
# A T P #
#########
	
	sub atp{
		local($i, $d, $klass);
		if($all_free*0.1 < @list[5]){
			$atp_miss++;	$atp_wmiss += @list[5];
			$atp_access++;	$atp_waccess+=@list[5];
			return 0;
		}elsif(defined $atp_cache{@list[7]}){
			#found in the cache
			$atp_hits++;
			$atp_whits += @list[5];
			$d = @list[0] - $atp_access_cache{@list[7]};
			#print "d = $d    ";
			for($i=0; $i<10; $i++){	#estimate position in the table
				if($d <= @t_val[$i]) {$klass = $i;last;}
			}
			$ota = $atp_cache{@list[7]};	#objects time access
			if(!defined ($atp_apt{$ota}))
			  {$atp_apt{$ota}=$ota;}
			$ta = $atp_apt{$ota};				#global time access from access patern table
			substr($ta, $klass, 1) = "1";
			$atp_apt{$ota}=$ta;			#update table
			$atp_cache{@list[7]}=$ta;		#update object's state
			#print "ta = $ta    ";
			$klass = index($ta, "1", 0);
		}else{
			#not found in the cache
			$atp_miss++;
			$atp_wmiss += @list[5];
			
			#print "atp ima: ".$atp_free." treba: ". @list[5]."\n";
			$max_delay = -1;
			if($atp_last_pred+1800<=@list[0] && $atp_free < @list[5]){
				foreach $url (keys %atp_cache){
					$d = @list[0] - $atp_access_cache{$url};
					if ($max_delay < $d){
					 	$max_url = $url; $max_delay = $d;
					}else{
						for($i=0; $i<10; $i++){	#estimate position in the table
							if($d <= @t_val[$i]) {$klass = $i;last;}
						}
						$i = index($atp_apt{$atp_cache{$url}}, "1", $klass);	#first '1' right from $klass position
						if($i>=0){	#delay could be foun in table
							$d = @t_val[$i];	#estimated delay
						}
					}
					$atp_delays{$d} .= " ".$url;
					push(@atp_sorted_delays, $d)
				}
				$atp_last_pred = @list[0];
			}
			if($atp_last_sort+1800<=@list[0] && $atp_free < @list[5]){
				$atp_last_sort = @list[0];
				sort @atp_sorted_delays;
			}

			# if there is no free space in the cache...
			for($atp_free < @list[5]){
				#...free some space
				$d = pop(@atp_sorted_delays);
				@pomlist = split(' ', $atp_delays{$d});
				$url = shift @pomlist;
				$atp_free += $atp_size_cache{$url};
				#delete entry in the cache
				delete($atp_cache{$url});
				delete($atp_size_cache{$url});
				delete($atp_access_cache{$url});
				if(@pomlist){$atp_delays{$d} = ' '.join(' ', @pomlist);}
				else {delete($atp_delays{$d});}
			}
			
			#now there is free space in cache
			#reserve some space
			$atp_free -= @list[5];
			$atp_cache{@list[7]} = "0000000000";	#new entry

		}
		
		#update cache for all cases
		$atp_access_cache{@list[7]} = @list[0];	#new last access time
		$atp_size_cache{@list[7]} = @list[5];	#new size
		$atp_access++;
		$atp_waccess+=@list[5];
	}


#########
# A T P #
#########
	
	sub atp{
		local($i, $d, $klass, $key, $pom);
		if($all_free*0.1 < $list[5]){
			$atp_miss++;	$atp_wmiss += $list[5];
			$atp_access++;	$atp_waccess+=$list[5];
			return 0;
		}
		if(defined $atp_key_cache{$list[7]}){
			#found in the cache
			$key = $atp_key_cache{$list[7]};
			$atp_hits++;
			$atp_whits += $list[5];
			$d = $list[0] - $$key->[0];
			#print "d = $d   $$key->[2] ";
			for($i=0; $i<10; $i++){	#estimate position in the table
				if($d <= $t_val[$i]) {$klass = $i;last;}
			}
			if($i==10){$klass--;}
			$ota = $$key->[3];	#objects time access
			if(!defined ($atp_apt{$ota})){
				$ta=$ota;
			}else{
				$ta = $atp_apt{$ota};				#global time access from access patern table
			}
			if(length($ta)!=10){print "\n\a klass=$klass ta=$ta ota=$ota arp=$atp_apt{$ota}\n";}
			substr($ta, $klass, 1) = "1";
			$atp_apt{$ota}=$ta;			#update table
			#$atp_cache{$key}=$ta;
			$$key->[3] = $ta;			#update object's state
			#print "ta = $ta    ";
			$klass = index($ta, "1", 0);
			#$atp_key_cache{$list[7]} = \[$list[0], $list[5], $list[7]];	#new entry
			$$key->[0] = $list[0]; $$key->[1] = $list[5]; $$key->[2] = $list[7]; 
		}else{
			#not found in the cache
			$atp_miss++;
			$atp_wmiss += $list[5];
			
			#print "atp ima: ".$atp_free." treba: ". @list[5]."\n";
			$max_delay = -1;
			if($atp_last_pred+1800<=$list[0] && $atp_free < $list[5]){
				undef @atp_sorted_delays;
				undef @urlkeys;
				undef %atp_delays;
				@urlkeys = values %atp_key_cache;
				for($keycount=0; $keycount<scalar(@urlkeys); $keycount++){
				    $urlkey = $urlkeys[$keycount];
					$d = @list[0] - $$urlkey->[0];
					#if ($max_delay < $d){
					# 	$max_urlkey = $urlkey; $max_delay = $d;
					#}else{
						for($i=0; $i<10; $i++){	#estimate position in the table
							if($d <= $t_val[$i]) {$klass = $i;last;}
						}
						$i = index($atp_apt{$$urlkey->[3]}, "1", $klass);	#first '1' right from $klass position
						
						if($i>=0 && $i<10){	#delay could be found in table
							$d = $t_val[$i];	#estimated delay
						}elsif($i<0){print "\ni<0\n"}
					#}
					if(defined($atp_delays{$d})){
						$pom = $atp_delays{$d};
						push(@$pom, $urlkey);
					}
					else{@new = ($urlkey);
						$pom = \@new;
					}
					$atp_delays{$d} = $pom;
					push(@atp_sorted_delays, $d);
				}
				$atp_last_pred = $list[0];
				
				@atp_sorted_delays=sort @atp_sorted_delays;
				print "t=$list[0] atp_sorted_delays=".scalar @atp_sorted_delays." urlkeys=".scalar @urlkeys." fadded=$fadded fdeleted=$fdeleted; pomdeleted=$pomdeleted realdeleted=$frealdeleted\n";
			}


			# if there is no free space in the cache...
			while($atp_free < $list[5]){
				#...free some space

				$d = pop(@atp_sorted_delays);
				$pom = $atp_delays{$d};
				$key = shift (@$pom);
																					if(!defined($key)){print "\n".scalar(@$pom)."	atp_free=$atp_free key=$key d=$d"; }
																					#print "\n".scalar(@$pom)."	atp_free=$atp_free	url=$$key->[2]	size=\n";
				$atp_free += $$key->[1];
				#delete entry in the cache
																					if(!defined($atp_key_cache{$$key->[2]})){print "\a\n".scalar(@$pom)."d=$d key=$key url=$$key->[2]"};
																					$fdeleted++;				
				delete($atp_key_cache{$$key->[2]});
																					if(!defined($atp_key_cache{$$key->[2]})){$frealdeleted++};
				if(!scalar(@$pom)){
					delete($atp_delays{$d}); 
																					print"\a\npom deleted d=$d";
																					print "t=$list[0] atp_sorted_delays=".scalar @atp_sorted_delays." urlkeys=".scalar @urlkeys." fadded=$fadded fdeleted=$fdeleted; pomdeleted=$pomdeleted realdeleted=$frealdeleted\n";
				} 
				if(!defined($atp_delays{$d})){
																					$pomdeleted++;
																					print "t=$list[0] atp_sorted_delays=".scalar @atp_sorted_delays." urlkeys=".scalar @urlkeys." fadded=$fadded fdeleted=$fdeleted; pomdeleted=$pomdeleted realdeleted=$frealdeleted\n";
				}
				else{
					$atp_delays{$d} = $pom;
																					if(!scalar(@$pom)){print "\nadded null "}
				}
				undef $$key;
				undef $key;
			}
			undef $pom;
			#now there is free space in cache
			#reserve some space
			$atp_free -= $list[5];
			$atp_key_cache{$list[7]} = \[$list[0], $list[5], $list[7], "0000000000"];	#new entry
			$key = $atp_key_cache{$list[7]};
			$fadded++;

		}
		
		#update cache for all cases
		$atp_access++;
		$atp_waccess+=$list[5];
	}


#########
# A T P #
#########
	
	sub atp{
		local($i, $d, $klass, $key, $pom);
		if($all_free*0.1 < $list[5]){
			$atp_miss++;	$atp_wmiss += $list[5];
			$atp_access++;	$atp_waccess+=$list[5];
			return 0;
		}
		if(defined $atp_key_cache{$list[7]}){
			#found in the cache
			$key = $atp_key_cache{$list[7]};
			$atp_hits++;
			$atp_whits += $list[5];
			$d = $list[0] - $$key->[0];
			#print "d = $d   $$key->[2] ";
			for($i=0; $i<10; $i++){	#estimate position in the table
				if($d <= $t_val[$i]) {$klass = $i;last;}
			}
			if($i==10){$klass--;}
			$ota = $$key->[3];	#objects time access
			if(!defined ($atp_apt{$ota})){
				$ta=$ota;
			}else{
				$ta = $atp_apt{$ota};				#global time access from access patern table
			}
			if(length($ta)!=10){print "\n\a klass=$klass ta=$ta ota=$ota arp=$atp_apt{$ota}\n";}
			substr($ta, $klass, 1) = "1";
			$atp_apt{$ota}=$ta;			#update table
			$$key->[3] = $ta;			#update object's state
			#print "ta = $ta    ";
			$klass = index($ta, "1", 0);
			$$key->[0] = $list[0]; $$key->[1] = $list[5]; $$key->[2] = $list[7]; 
		}else{
			#not found in the cache
			$atp_miss++;
			$atp_wmiss += $list[5];
			
			#print "atp ima: ".$atp_free." treba: ". @list[5]."\n";
			$max_delay = -1;
			if($atp_last_pred+1800<=$list[0] && $atp_free < $list[5]){
				undef @atp_sorted_delays;
				undef @urlkeys;
				undef @atp_delay_keys;
				@urlkeys = values %atp_key_cache;
				for($keycount=0; $keycount<scalar(@urlkeys); $keycount++){
				    $urlkey = $urlkeys[$keycount];
					$d = @list[0] - $$urlkey->[0];
					#if ($max_delay < $d){
					# 	$max_urlkey = $urlkey; $max_delay = $d;
					#}else{
						for($i=0; $i<10; $i++){	#estimate position in the table
							if($d <= $t_val[$i]) {$klass = $i;last;}
						}
						$i = index($atp_apt{$$urlkey->[3]}, "1", $klass);	#first '1' right from $klass position
						
						if($i>=0 && $i<10){	#delay could be found in table
							$d = $t_val[$i];	#estimated delay
						}elsif($i<0){print "\ni<0\n"}
					#}
					push(@atp_sorted_delays, $d);
					push(@atp_delay_keys, $urlkey)
				}
				$atp_last_pred = $list[0];
				
				{#sort lists
					my($i, $j, $temp);
					print "\n";
					for($i=0; $i<(scalar @atp_sorted_delays)-1; $i++){
						for($j=$i+1; $j<(scalar @atp_sorted_delays); $j++){
							if($atp_sorted_delays[$j]<$atp_sorted_delays[$i]){
								$temp=$atp_sorted_delays[$i]; $atp_sorted_delays[$i]=$atp_sorted_delays[$j]; $atp_sorted_delays[$j]=$temp;
								$temp=$atp_delay_keys[$i]; $atp_delay_keys[$i]=$atp_delay_keys[$j]; $atp_delay_keys[$j]=$temp;
							}
						}
						print "\rsorted: ".$i/scalar @atp_sorted_delays;
					}
					undef $temp;
				}
				print "t=$list[0] atp_sorted_delays=".scalar @atp_sorted_delays." urlkeys=".scalar @urlkeys." fadded=$fadded fdeleted=$fdeleted; pomdeleted=$pomdeleted realdeleted=$frealdeleted\n";
			}


			# if there is no free space in the cache...
			while($atp_free < $list[5]){
				#...free some space

				$d = pop(@atp_sorted_delays);
				$key = pop (@atp_delay_keys);
																					if(!defined($key)){print "\n".scalar(@$pom)."	atp_free=$atp_free key=$key d=$d"; }
																					#print "\n".scalar(@$pom)."	atp_free=$atp_free	url=$$key->[2]	size=\n";
				$atp_free += $$key->[1];
				#delete entry in the cache
																					if(!defined($atp_key_cache{$$key->[2]})){print "\a\n".scalar(@$pom)."d=$d key=$key url=$$key->[2]"};
																					$fdeleted++;				
				delete($atp_key_cache{$$key->[2]});
																					if(!defined($atp_key_cache{$$key->[2]})){$frealdeleted++};
				undef $$key;
				undef $key;
			}
			undef $pom;
			#now there is free space in cache
			#reserve some space
			$atp_free -= $list[5];
			$atp_key_cache{$list[7]} = \[$list[0], $list[5], $list[7], "0000000000"];	#new entry
			$key = $atp_key_cache{$list[7]};
			$fadded++;

		}
		
		#update cache for all cases
		$atp_access++;
		$atp_waccess+=$list[5];
	}


#########
# A T P #
#########
	
	sub atp{
		local($i, $d, $klass, $key, $pom);
		if($all_free*0.1 < $list[5]){
			$atp_miss++;	$atp_wmiss += $list[5];
			$atp_access++;	$atp_waccess+=$list[5];
			return 0;
		}
		if(defined $atp_key_cache{$list[7]}){
			#found in the cache
			$key = $atp_key_cache{$list[7]};
			$atp_hits++;
			$atp_whits += $list[5];
			$d = $list[0] - $$key->[0];
			#print "d = $d   $$key->[2] ";
			for($i=0; $i<10; $i++){	#estimate position in the table
				if($d <= $t_val[$i]) {$klass = $i;last;}
			}
			if($i==10){$klass--;}
			$ota = $$key->[3];	#objects time access
			if(!defined ($atp_apt{$ota})){
				$ta=$ota;
			}else{
				$ta = $atp_apt{$ota};				#global time access from access patern table
			}
																				#if(length($ta)!=10){print "\n\a klass=$klass ta=$ta ota=$ota arp=$atp_apt{$ota}\n";}
			substr($ta, $klass, 1) = "1";
			$atp_apt{$ota}=$ta;			#update table
			$$key->[3] = $ta;			#update object's state
			#print "ta = $ta    ";
			$klass = index($ta, "1", 0);
			$$key->[0] = $list[0]; $$key->[1] = $list[5]; $$key->[2] = $list[7]; 
		}else{
			#not found in the cache
			$atp_miss++;
			$atp_wmiss += $list[5];
			
			#print "atp ima: ".$atp_free." treba: ". @list[5]."\n";
			$max_delay = -1;
			if($atp_last_pred+1800<=$list[0] && $atp_free < $list[5]){
				undef @atp_sorted_keys;
				@urlkeys = values %atp_key_cache;
				print "\nscheduling";
				for($keycount=0; $keycount<scalar(@urlkeys); $keycount++){
				    $urlkey = $urlkeys[$keycount];
					$d = @list[0] - $$urlkey->[0];

					for($klass=0; $klass<10; $klass++){	#estimate position in the table
						if($d <= $t_val[$klass]) {last;}
					}
					if($klass==10){$i=10;}else{
						$i = index($atp_apt{$$urlkey->[3]}, "1", $klass);}	#first '1' right from $klass position
					if($i>=0 && $i<10){	#delay could be found in table
						$d = $t_val[$i];	#estimated delay
					}elsif($i<0){$d=$t_val[$klass];
					}else{$d=$t_val[10];}

					push(@atp_sorted_delays, $d);
					$atp_delay_keys{$urlkey}=$d;
				}
				$atp_last_pred = $list[0];
				
				print "\nsorting".scalar(@urlkeys);
				@atp_sorted_delays = sort{$a <=> $b} @atp_sorted_delays;
				print "\narangeing".scalar(@urlkeys);
				while($d=pop(@atp_sorted_delays)){
					for($urlkey=pop(@urlkeys);
						$atp_delay_keys{$urlkey}!=$d;
						$urlkey=pop(@urlkeys)){
						unshift(@urlkeys, $urlkey);
					}
					unshift(@atp_sorted_keys, $urlkey);
					print "\r$d ".scalar @atp_sorted_keys;
				}

				print "\nt=$list[0] atp_sorted_keys=".scalar @atp_sorted_keys." fadded=$fadded fdeleted=$fdeleted; pomdeleted=$pomdeleted realdeleted=$frealdeleted\n";
				undef @atp_sorted_delays;
				undef @urlkeys;
				undef %atp_delay_keys;
			}


			# if there is no free space in the cache...
			while($atp_free < $list[5]){
				#...free some space

				$key = pop (@atp_sorted_keys);
																					if(!defined($key)){print "\n".scalar(@$pom)."	atp_free=$atp_free key=$key d=$d"; }
																					#print "\n".scalar(@$pom)."	atp_free=$atp_free	url=$$key->[2]	size=\n";
				$atp_free += $$key->[1];
				#delete entry in the cache
																					if(!defined($atp_key_cache{$$key->[2]})){print "\a\n".scalar(@$pom)."d=$d key=$key url=$$key->[2]"};
																					$fdeleted++;				
				delete($atp_key_cache{$$key->[2]});
																					if(!defined($atp_key_cache{$$key->[2]})){$frealdeleted++};
				undef $$key;
				undef $key;
			}
			undef $pom;
			#now there is free space in cache
			#reserve some space
			$atp_free -= $list[5];
			$atp_key_cache{$list[7]} = \[$list[0], $list[5], $list[7], "0000000000"];	#new entry
			$key = $atp_key_cache{$list[7]};
			$fadded++;

		}
		
		#update cache for all cases
		$atp_access++;
		$atp_waccess+=$list[5];
	}


#########
# A T P #
#########
	
	sub atp{
		local($i, $d, $klass, $key, $pom);
		if($all_free*0.1 < $list[5]){
			$atp_miss++;	$atp_wmiss += $list[5];
			$atp_access++;	$atp_waccess+=$list[5];
			return 0;
		}
		if(defined $atp_key_cache{$list[7]}){
			#found in the cache
			$key = $atp_key_cache{$list[7]};
			$atp_hits++;
			$atp_whits += $list[5];
			$d = $list[0] - $$key->[0];
			#print "d = $d   $$key->[2] ";
			for($i=0; $i<11; $i++){	#estimate position in the table
				if($d <= $t_val[$i]) {$klass = $i;last;}
			}
			if($i==11){$klass--;}
			$ota = $$key->[3];	#objects time access
			if(!defined ($atp_apt{$ota})){
				$ta=$ota;
			}else{
				$ta = "0000000000";				#global time access from access patern table
			}
																				#if(length($ta)!=11){print "\n\a klass=$klass ta=$ta ota=$ota arp=$atp_apt{$ota}\n";}
			substr($ta, $klass, 1) = "1";
			$atp_apt{$ota}=$ta;			#update table
			$atp_apt_cnt{$ota}++;			#update table counter
			$$key->[3] = $ta;			#update object's state
			#print "ta = $ta    ";
			$klass = index($ta, "1", 0);
			$$key->[0] = $list[0]; $$key->[1] = $list[5]; $$key->[2] = $list[7]; 
		}else{
			#not found in the cache
			$atp_miss++;
			$atp_wmiss += $list[5];
			
			#print "atp ima: ".$atp_free." treba: ". $list[5]."\n";
			$max_delay = -1;
			if($atp_last_pred+1800<=$list[0] && $atp_free < $list[5]){
				undef @atp_sorted_keys;
				@urlkeys = values %atp_key_cache;
				print "\nscheduling";
				for($keycount=0; $keycount<scalar(@urlkeys); $keycount++){
				    $urlkey = $urlkeys[$keycount];
					$d = $list[0] - $$urlkey->[0];

					for($klass=0; $klass<11; $klass++){	#estimate position in the table
						if($d <= $t_val[$klass]) {last;}
					}
					if($klass==11){$i=11;}else{
						$i = index($atp_apt{$$urlkey->[3]}, "1", $klass);}	#first '1' right from $klass position
					if($i>=0 && $i<11){	#delay could be found in table
						$d = $t_val[$i];	#estimated delay
					}elsif($i<0){$d=$t_val[$klass];
					}else{$d=$t_val[11];}

					push(@atp_sorted_delays, $d);
					$atp_delay_keys{$urlkey}=$d;
				}
				$atp_last_pred = $list[0];
				
				print "\nsorting".scalar(@urlkeys);
				@atp_sorted_delays = sort {$a<=>$b} @atp_sorted_delays;
				print "\narangeing".scalar(@urlkeys);
				while($d=pop(@atp_sorted_delays)){
					for($urlkey=pop(@urlkeys);
						$atp_delay_keys{$urlkey}!=$d;
						$urlkey=pop(@urlkeys)){
						unshift(@urlkeys, $urlkey);
					}
					unshift(@atp_sorted_keys, $urlkey);
					print "\r$d ".scalar @atp_sorted_keys;
				}

				print "\nt=$list[0] atp_free=$atp_free atp_sorted_keys=".scalar @atp_sorted_keys." fadded=$fadded fdeleted=$fdeleted; realdeleted=$frealdeleted\n";
				undef @atp_sorted_delays;
				undef @urlkeys;
				undef %atp_delay_keys;
			}


			# if there is no free space in the cache...
			while($atp_free < $list[5]){
				#...free some space

				$key = pop (@atp_sorted_keys);
																					if(!defined($key)){print "\n".scalar(@$pom)."	atp_free=$atp_free key=$key d=$d"; }
																					#print "\n".scalar(@$pom)."	atp_free=$atp_free	url=$$key->[2]	size=\n";
				$atp_free += $$key->[1];
				#delete entry in the cache
																					if(!defined($atp_key_cache{$$key->[2]})){print "\a\n".scalar(@$pom)."d=$d key=$key url=$$key->[2]"};
																					$fdeleted++;				
				delete($atp_key_cache{$$key->[2]});
																					if(!defined($atp_key_cache{$$key->[2]})){$frealdeleted++};
				undef $$key;
				undef $key;
			}
			undef $pom;
			#now there is free space in cache
			#reserve some space
			$atp_free -= $list[5];
			$atp_key_cache{$list[7]} = \[$list[0], $list[5], $list[7], "0000000000"];	#new entry
			$key = $atp_key_cache{$list[7]};
			$fadded++;

		}
		
		#update cache for all cases
		$atp_access++;
		$atp_waccess+=$list[5];
	}

