📄 cachefilemanagerimpl.java
字号:
/*
* Created on 03-Aug-2004
* Created by Paul Gardner
* Copyright (C) 2004 Aelitis, All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* AELITIS, SARL au capital de 30,000 euros
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*
*/
package com.aelitis.azureus.core.diskmanager.cache.impl;
/**
* @author parg
*
*/
import java.io.File;
import java.util.*;
import org.gudy.azureus2.core3.torrent.*;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.core3.config.*;
import org.gudy.azureus2.core3.logging.*;
import com.aelitis.azureus.core.diskmanager.cache.*;
import com.aelitis.azureus.core.diskmanager.file.*;
public class
CacheFileManagerImpl
implements CacheFileManager
{
public static final boolean DEBUG = false;
public static final int CACHE_CLEANER_TICKS = 60; // every 60 seconds
public static final int STATS_UPDATE_FREQUENCY = 1*1000; // 1 sec
public static final long DIRTY_CACHE_WRITE_MAX_AGE = 120*1000; // 2 mins
static{
if ( DEBUG ){
System.out.println( "**** Cache consistency debugging on ****" );
}
}
protected boolean cache_enabled;
protected boolean cache_read_enabled;
protected boolean cache_write_enabled;
protected long cache_size;
protected long cache_files_not_smaller_than;
protected long cache_minimum_free_size;
protected long cache_space_free;
protected FMFileManager file_manager;
// copy on update semantics
protected WeakHashMap cache_files = new WeakHashMap();
protected WeakHashMap updated_cache_files = null;
// access order
protected LinkedHashMap cache_entries = new LinkedHashMap(1024, 0.75f, true );
protected CacheFileManagerStatsImpl stats;
// copy on update semantics
protected Map torrent_to_cache_file_map = new HashMap();
protected long cache_bytes_written;
protected long cache_bytes_read;
protected long file_bytes_written;
protected long file_bytes_read;
protected long cache_read_count;
protected long cache_write_count;
protected long file_read_count;
protected long file_write_count;
protected AEMonitor this_mon = new AEMonitor( "CacheFileManager" );
public
CacheFileManagerImpl()
{
file_manager = FMFileManagerFactory.getSingleton();
boolean enabled = COConfigurationManager.getBooleanParameter( "diskmanager.perf.cache.enable" );
boolean enable_read = COConfigurationManager.getBooleanParameter( "diskmanager.perf.cache.enable.read" );
boolean enable_write = COConfigurationManager.getBooleanParameter( "diskmanager.perf.cache.enable.write" );
// units are MB
int size = 1024*1024*COConfigurationManager.getIntParameter( "diskmanager.perf.cache.size" );
// units are KB
int not_smaller_than = 1024*COConfigurationManager.getIntParameter( "notsmallerthan" );
if ( size <= 0 ){
Debug.out( "Invalid cache size parameter (" + size + "), caching disabled" );
enabled = false;
}
initialise( enabled, enable_read, enable_write, size, not_smaller_than );
}
protected void
initialise(
boolean enabled,
boolean enable_read,
boolean enable_write,
long size,
long not_smaller_than )
{
cache_enabled = enabled && ( enable_read || enable_write );
cache_read_enabled = enabled && enable_read;
cache_write_enabled = enabled && enable_write;
cache_size = size;
cache_files_not_smaller_than = not_smaller_than;
cache_minimum_free_size = cache_size/4;
cache_space_free = cache_size;
stats = new CacheFileManagerStatsImpl( this );
AEThread t =
new AEThread( "CacheStatsAndCleaner")
{
public void
runSupport()
{
cacheStatsAndCleaner();
}
};
t.setDaemon(true);
t.start();
LGLogger.log( "DiskCache: enabled = " + cache_enabled + ", read = " + cache_read_enabled + ", write = " + cache_write_enabled + ", size = " + cache_size + " MB" );
}
protected boolean
isWriteCacheEnabled()
{
return( cache_write_enabled );
}
protected boolean
isReadCacheEnabled()
{
return( cache_read_enabled );
}
public CacheFile
createFile(
final CacheFileOwner owner,
File file )
throws CacheFileManagerException
{
try{
FMFile fm_file =
file_manager.createFile(
new FMFileOwner()
{
public String
getName()
{
return( owner.getCacheFileOwnerName());
}
public TOTorrentFile
getTorrentFile()
{
return( owner.getCacheFileTorrentFile());
}
}, file );
TOTorrentFile tf = owner.getCacheFileTorrentFile();
CacheFile cf;
if ( tf != null && tf.getLength() < cache_files_not_smaller_than ){
cf = new CacheFileWithoutCache( this, fm_file, tf );
}else{
cf = new CacheFileWithCache( this, fm_file, tf );
try{
this_mon.enter();
if ( updated_cache_files == null ){
updated_cache_files = new WeakHashMap( cache_files );
}
// copy on write so readers don't need to synchronize or copy
updated_cache_files.put( cf, null );
if ( tf != null ){
Map new_map = new HashMap( torrent_to_cache_file_map );
new_map.put( tf, cf );
torrent_to_cache_file_map = new_map;
}
}finally{
this_mon.exit();
}
}
return( cf );
}catch( FMFileManagerException e ){
rethrow( e );
return( null );
}
}
public CacheFileManagerStats
getStats()
{
return( stats );
}
protected boolean
isCacheEnabled()
{
return( cache_enabled );
}
/**
* allocates space but does NOT add it to the cache list due to synchronization issues. Basically
* the caller mustn't hold their monitor when calling allocate, as a flush may result in one or more
* other files being flushed which results in their monitor being taken, and we've got an A->B and
* B->A classic deadlock situation. However, we must keep the file's cache and our cache in step.
* It is not acceptable to have an entry inserted into our records but not in the file's as this
* then screws up the flush algorithm (which assumes that if it finds an entry in our list, a flush
* of that file is guaranteed to release space). Therefore we add the cache entry in addCacheSpace
* so that the caller can safely do this while synchronised firstly on its monitor and then we can
* sync on our. Hence we only ever get A->B monitor grabs which won't deadlock
* @param file
* @param buffer
* @param file_position
* @param length
* @return
* @throws CacheFileManagerException
*/
protected CacheEntry
allocateCacheSpace(
int entry_type,
CacheFileWithCache file,
DirectByteBuffer buffer,
long file_position,
int length )
throws CacheFileManagerException
{
boolean ok = false;
boolean log = false;
while( !ok ){
// musn't invoke synchronised CacheFile methods while holding manager lock as this
// can cause deadlocks (as CacheFile calls manager methods with locks)
CacheEntry oldest_entry = null;
try{
this_mon.enter();
if ( length < cache_space_free || cache_space_free == cache_size ){
ok = true;
}else{
oldest_entry = (CacheEntry)cache_entries.keySet().iterator().next();
}
}finally{
this_mon.exit();
}
if ( !ok ){
log = true;
long old_free = cache_space_free;
CacheFileWithCache oldest_file = oldest_entry.getFile();
oldest_file.flushCache( oldest_entry.getFilePosition(), true, cache_minimum_free_size );
long flushed = cache_space_free - old_free;
LGLogger.log( "DiskCache: cache full, flushed " + ( flushed ) + " from " + oldest_file.getName());
if ( flushed == 0 ){
try{
this_mon.enter();
if ( cache_entries.size() > 0 &&
(CacheEntry)cache_entries.keySet().iterator().next() == oldest_entry ){
// hmm, something wrong with cache as the flush should have got rid
// of at least the oldest entry
throw( new CacheFileManagerException( "Cache inconsistent: 0 flushed"));
}
}finally{
this_mon.exit();
}
}
}
}
CacheEntry entry = new CacheEntry( entry_type, file, buffer, file_position, length );
if ( log ){
LGLogger.log(
"DiskCache: cr=" + cache_bytes_read + ",cw=" + cache_bytes_written+
",fr=" + file_bytes_read + ",fw=" + file_bytes_written );
}
return( entry );
}
protected void
cacheStatsAndCleaner()
{
long cleaner_ticks = CACHE_CLEANER_TICKS;
while( true ){
try{
Thread.sleep( STATS_UPDATE_FREQUENCY );
}catch( InterruptedException e ){
Debug.printStackTrace( e );
break;
}
stats.update();
// System.out.println( "cache file count = " + cache_files.size());
Iterator cf_it = cache_files.keySet().iterator();
while(cf_it.hasNext()){
((CacheFileWithCache)cf_it.next()).updateStats();
}
if ( --cleaner_ticks == 0 ){
cleaner_ticks = CACHE_CLEANER_TICKS;
Set dirty_files = new HashSet();
long now = SystemTime.getCurrentTime();
long oldest = now - DIRTY_CACHE_WRITE_MAX_AGE;
try{
this_mon.enter();
if ( updated_cache_files != null ){
cache_files = updated_cache_files;
updated_cache_files = null;
}
if ( cache_entries.size() > 0 ){
Iterator it = cache_entries.keySet().iterator();
while( it.hasNext()){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -