From: Sasa M. <sa...@us...> - 2004-07-16 07:40:58
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20380/org/jrobin/core Modified Files: RrdNioBackend.java Log Message: Completely redesigned and simplified RRDTool commander... Index: RrdNioBackend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdNioBackend.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** RrdNioBackend.java 15 Jul 2004 14:22:12 -0000 1.12 --- RrdNioBackend.java 16 Jul 2004 07:40:49 -0000 1.13 *************** *** 37,40 **** --- 37,62 ---- */ public class RrdNioBackend extends RrdFileBackend { + /** + * Defines if the <code>System.gc()</code> method should be executed when necessary. + * NIO backend uses large in-memory buffer to cache file data. The buffer remains 'active' + * (by prohibiting file re-creation, for example) as long as it is not garbage collected. + * By forcing <code>gc()</code> where appropriate, memory gets freed sooner and file + * re-creation won't fail.<p> + * + * The constant is set to false initially and currently there is no API to change it + * during runtime.<p> + */ + public static final boolean SHOULD_GC = true; + + static { + if(SHOULD_GC) { + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + System.gc(); + } + }); + } + } + private static final Timer syncTimer = new Timer(true); *************** *** 46,53 **** throws IOException { super(path, readOnly, lockMode); this.syncMode = syncMode; - FileChannel.MapMode mapMode = - readOnly? FileChannel.MapMode.READ_ONLY: FileChannel.MapMode.READ_WRITE; - this.byteBuffer = channel.map(mapMode, 0, getLength()); if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) { createSyncTask(syncPeriod); --- 68,73 ---- throws IOException { super(path, readOnly, lockMode); + map(false, readOnly); this.syncMode = syncMode; if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) { createSyncTask(syncPeriod); *************** *** 55,58 **** --- 75,93 ---- } + private void map(boolean isShorter, boolean readOnly) throws IOException { + if(isShorter) { + byteBuffer = null; + if(SHOULD_GC) { + System.gc(); + } + } + long newLength = getLength(); + if(newLength > 0) { + FileChannel.MapMode mapMode = + readOnly? FileChannel.MapMode.READ_ONLY: FileChannel.MapMode.READ_WRITE; + byteBuffer = channel.map(mapMode, 0, newLength); + } + } + private void createSyncTask(int syncPeriod) { syncTask = new TimerTask() { *************** *** 67,76 **** * Sets length of the underlying RRD file. This method is called only once, immediately * after a new RRD file gets created. ! * @param length Length of the RRD file * @throws IOException Thrown in case of I/O error. */ ! protected void setLength(long length) throws IOException { ! super.setLength(length); ! byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, length); } --- 102,112 ---- * Sets length of the underlying RRD file. This method is called only once, immediately * after a new RRD file gets created. ! * @param newLength Length of the RRD file * @throws IOException Thrown in case of I/O error. */ ! protected void setLength(long newLength) throws IOException { ! boolean isShorter = newLength < getLength(); ! super.setLength(newLength); ! map(isShorter, false); } *************** *** 120,126 **** */ public void sync() { ! synchronized(byteBuffer) { ! // System.out.println("** SYNC **"); ! byteBuffer.force(); } } --- 156,164 ---- */ public void sync() { ! if(byteBuffer != null) { ! synchronized(byteBuffer) { ! // System.out.println("** SYNC **"); ! byteBuffer.force(); ! } } } |