summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-09-18 11:51:05 (EDT)
committerEike Stepper2007-09-18 11:51:05 (EDT)
commitc561762431c67d37c46ca1de7a0788473a1b3c6d (patch)
tree779c77a1aeb80b4ba065323af8c298475b5439d7
parentf89cf319aa1a1a725882947c9702a8e4ba52515d (diff)
downloadcdo-c561762431c67d37c46ca1de7a0788473a1b3c6d.zip
cdo-c561762431c67d37c46ca1de7a0788473a1b3c6d.tar.gz
cdo-c561762431c67d37c46ca1de7a0788473a1b3c6d.tar.bz2
[203538] Limit memory consumption in CDORevisionResolverImpl
https://bugs.eclipse.org/bugs/show_bug.cgi?id=203538
-rw-r--r--plugins/org.eclipse.emf.cdo.tests/CDO2 ContainmentTest.launch20
-rw-r--r--plugins/org.eclipse.emf.cdo.tests/CDO2 CrossReferenceTest.launch20
-rw-r--r--plugins/org.eclipse.emf.cdo.tests/CDO2 InitialTest.launch20
-rw-r--r--plugins/org.eclipse.emf.cdo.tests/CDO2 InvalidationTest.launch20
-rw-r--r--plugins/org.eclipse.net4j.tests/CacheTest.launch (renamed from plugins/org.eclipse.net4j.tests/Net4j MonitorTest.launch)6
-rw-r--r--plugins/org.eclipse.net4j.tests/Net4j SignalTest.launch16
-rw-r--r--plugins/org.eclipse.net4j.tests/Net4j SynchronizingCorrelatorTest.launch16
-rw-r--r--plugins/org.eclipse.net4j.tests/Net4j TCPTransportTest.launch16
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/CacheTest.java54
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/Revision.java81
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/RevisionManager.java156
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/cache/Cache.java108
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/cache/CacheMonitor.java73
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheProbe.java8
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheRegistration.java8
15 files changed, 469 insertions, 153 deletions
diff --git a/plugins/org.eclipse.emf.cdo.tests/CDO2 ContainmentTest.launch b/plugins/org.eclipse.emf.cdo.tests/CDO2 ContainmentTest.launch
deleted file mode 100644
index ec54fc4..0000000
--- a/plugins/org.eclipse.emf.cdo.tests/CDO2 ContainmentTest.launch
+++ /dev/null
@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/tdd/ContainmentTest.java"/>
-</listAttribute>
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
-<listEntry value="1"/>
-</listAttribute>
-<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
-<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
-<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
-</listAttribute>
-<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
-<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
-<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
-<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit3"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.emf.cdo.tests.ContainmentTest"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.emf.cdo.tests"/>
-<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Ddebug=true"/>
-</launchConfiguration>
diff --git a/plugins/org.eclipse.emf.cdo.tests/CDO2 CrossReferenceTest.launch b/plugins/org.eclipse.emf.cdo.tests/CDO2 CrossReferenceTest.launch
deleted file mode 100644
index bbdd063..0000000
--- a/plugins/org.eclipse.emf.cdo.tests/CDO2 CrossReferenceTest.launch
+++ /dev/null
@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/tdd/CrossReferenceTest.java"/>
-</listAttribute>
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
-<listEntry value="1"/>
-</listAttribute>
-<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
-<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
-<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
-</listAttribute>
-<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
-<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
-<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
-<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit3"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.emf.cdo.tests.CrossReferenceTest"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.emf.cdo.tests"/>
-<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Ddebug=true"/>
-</launchConfiguration>
diff --git a/plugins/org.eclipse.emf.cdo.tests/CDO2 InitialTest.launch b/plugins/org.eclipse.emf.cdo.tests/CDO2 InitialTest.launch
deleted file mode 100644
index 280c620..0000000
--- a/plugins/org.eclipse.emf.cdo.tests/CDO2 InitialTest.launch
+++ /dev/null
@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/tdd/InitialTest.java"/>
-</listAttribute>
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
-<listEntry value="1"/>
-</listAttribute>
-<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
-<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
-<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
-</listAttribute>
-<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
-<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
-<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
-<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit3"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.emf.cdo.tests.InitialTest"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.emf.cdo.tests"/>
-<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Ddebug=true"/>
-</launchConfiguration>
diff --git a/plugins/org.eclipse.emf.cdo.tests/CDO2 InvalidationTest.launch b/plugins/org.eclipse.emf.cdo.tests/CDO2 InvalidationTest.launch
deleted file mode 100644
index f2d027e..0000000
--- a/plugins/org.eclipse.emf.cdo.tests/CDO2 InvalidationTest.launch
+++ /dev/null
@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/tdd/InvalidationTest.java"/>
-</listAttribute>
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
-<listEntry value="1"/>
-</listAttribute>
-<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
-<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
-<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
-</listAttribute>
-<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
-<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
-<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
-<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit3"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.emf.cdo.tests.InvalidationTest"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.emf.cdo.tests"/>
-<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Ddebug=true"/>
-</launchConfiguration>
diff --git a/plugins/org.eclipse.net4j.tests/Net4j MonitorTest.launch b/plugins/org.eclipse.net4j.tests/CacheTest.launch
index 42c7782..63894f3 100644
--- a/plugins/org.eclipse.net4j.tests/Net4j MonitorTest.launch
+++ b/plugins/org.eclipse.net4j.tests/CacheTest.launch
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/org.eclipse.net4j.tests/src/org/eclipse/net4j/tests/MonitorTest.java"/>
+<listEntry value="/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/CacheTest.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
@@ -9,11 +9,13 @@
<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
+<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit3"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.net4j.util.tests.MonitorTest"/>
+<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.net4j.util.tests.cache.CacheTest"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.net4j.tests"/>
+<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xms10M&#13;&#10;-Xmx100M"/>
</launchConfiguration>
diff --git a/plugins/org.eclipse.net4j.tests/Net4j SignalTest.launch b/plugins/org.eclipse.net4j.tests/Net4j SignalTest.launch
deleted file mode 100644
index 666182b..0000000
--- a/plugins/org.eclipse.net4j.tests/Net4j SignalTest.launch
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
-<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.net4j.tests.SignalTest"/>
-<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
-<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.net4j.tests"/>
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
-<listEntry value="1"/>
-</listAttribute>
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/org.eclipse.net4j.tests/src/org/eclipse/net4j/tests/SignalTest.java"/>
-</listAttribute>
-<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
-<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
-<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit3"/>
-</launchConfiguration>
diff --git a/plugins/org.eclipse.net4j.tests/Net4j SynchronizingCorrelatorTest.launch b/plugins/org.eclipse.net4j.tests/Net4j SynchronizingCorrelatorTest.launch
deleted file mode 100644
index 1dee275..0000000
--- a/plugins/org.eclipse.net4j.tests/Net4j SynchronizingCorrelatorTest.launch
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/org.eclipse.net4j.tests"/>
-</listAttribute>
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
-<listEntry value="4"/>
-</listAttribute>
-<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
-<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
-<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
-<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
-<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit3"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.net4j.util.tests.SynchronizingCorrelatorTest"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.net4j.tests"/>
-</launchConfiguration>
diff --git a/plugins/org.eclipse.net4j.tests/Net4j TCPTransportTest.launch b/plugins/org.eclipse.net4j.tests/Net4j TCPTransportTest.launch
deleted file mode 100644
index 7d0ee89..0000000
--- a/plugins/org.eclipse.net4j.tests/Net4j TCPTransportTest.launch
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/org.eclipse.net4j.tests/src/org/eclipse/net4j/tests/TCPTransportTest.java"/>
-</listAttribute>
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
-<listEntry value="1"/>
-</listAttribute>
-<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
-<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
-<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
-<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
-<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit3"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.eclipse.net4j.tests.TCPTransportTest"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.net4j.tests"/>
-</launchConfiguration>
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/CacheTest.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/CacheTest.java
new file mode 100644
index 0000000..2d1f72a
--- /dev/null
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/CacheTest.java
@@ -0,0 +1,54 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.util.tests.cache;
+
+import org.eclipse.net4j.internal.util.cache.CacheMonitor;
+import org.eclipse.net4j.internal.util.cache.ThresholdConditionPolicy;
+import org.eclipse.net4j.util.cache.ICacheMonitor.ConditionPolicy;
+import org.eclipse.net4j.util.tests.AbstractOMTest;
+
+/**
+ * @author Eike Stepper
+ */
+public class CacheTest extends AbstractOMTest
+{
+ public void testLifecycle() throws Exception
+ {
+ ConditionPolicy conditionPolicy = new ThresholdConditionPolicy(1000000L, 10000000L);
+
+ CacheMonitor cacheMonitor = new CacheMonitor();
+ cacheMonitor.setConditionPolicy(conditionPolicy);
+ cacheMonitor.setPauseRED(100L);
+ cacheMonitor.setPauseYELLOW(100L);
+ cacheMonitor.setPauseGREEN(100L);
+ cacheMonitor.setDaemon(true);
+ cacheMonitor.activate();
+
+ RevisionManager revisionManager = new RevisionManager();
+ revisionManager.setCacheMonitor(cacheMonitor);
+ revisionManager.activate();
+
+ for (int version = 1; version <= 10; version++)
+ {
+ for (int id = 1; id <= 100; id++)
+ {
+ Revision revision = revisionManager.getRevision(id, version);
+ Thread.sleep(20);
+ }
+
+ System.gc();
+ Thread.sleep(1000);
+ }
+
+ revisionManager.deactivate();
+ cacheMonitor.deactivate();
+ }
+}
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/Revision.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/Revision.java
new file mode 100644
index 0000000..1dd267e
--- /dev/null
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/Revision.java
@@ -0,0 +1,81 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.util.tests.cache;
+
+import java.lang.ref.PhantomReference;
+import java.lang.ref.ReferenceQueue;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author Eike Stepper
+ */
+public class Revision
+{
+ private int id;
+
+ private int version;
+
+ private byte[] data = new byte[100000];
+
+ public Revision(int id, int version)
+ {
+ this.id = id;
+ this.version = version;
+ }
+
+ public int getID()
+ {
+ return id;
+ }
+
+ public int getVersion()
+ {
+ return version;
+ }
+
+ public byte[] getData()
+ {
+ return data;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "R" + id + "v" + version;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static Set finalized = new HashSet();
+
+ @SuppressWarnings("unchecked")
+ private static List refs = new ArrayList();
+
+ @SuppressWarnings("unchecked")
+ private static ReferenceQueue queue = new ReferenceQueue();
+
+ @Override
+ protected void finalize() throws Throwable
+ {
+ System.err.println("FINALIZE " + this);
+ long token = id;
+ token <<= 32;
+ token |= version;
+ if (!finalized.add(token))
+ {
+ System.err.println("************************************************************************");
+ }
+
+ refs.add(new PhantomReference<Revision>(this, queue));
+ }
+}
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/RevisionManager.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/RevisionManager.java
new file mode 100644
index 0000000..9b4fa66
--- /dev/null
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/cache/RevisionManager.java
@@ -0,0 +1,156 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.util.tests.cache;
+
+import org.eclipse.net4j.internal.util.cache.Cache;
+
+import java.lang.ref.Reference;
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Eike Stepper
+ */
+public class RevisionManager extends Cache<Revision>
+{
+ private Map<Integer, TimeLine> timeLines = new HashMap<Integer, TimeLine>();
+
+ public RevisionManager()
+ {
+ }
+
+ public Revision getRevision(int id, int version)
+ {
+ TimeLine timeLine = timeLines.get(id);
+ if (timeLine == null)
+ {
+ timeLine = new TimeLine(id);
+ timeLines.put(id, timeLine);
+ }
+
+ return timeLine.getRevision(version);
+ }
+
+ public void evictElements(int elementCount)
+ {
+ }
+
+ protected Revision loadRevision(int id, int version)
+ {
+ Revision revision = new Revision(id, version);
+ System.out.println("Loaded " + revision + " (free=" + Runtime.getRuntime().freeMemory() + ", total="
+ + Runtime.getRuntime().totalMemory() + ", max=" + Runtime.getRuntime().maxMemory() + ")");
+ return revision;
+ }
+
+ protected int getRevisionSize(Revision revision)
+ {
+ return 1;
+ }
+
+ @Override
+ protected void unreachableElement(Reference<? extends Revision> reference)
+ {
+ System.out.println("Unreachable: " + reference);
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private class TimeLine
+ {
+ private int id;
+
+ private List<Reference<Revision>> revisions = new ArrayList<Reference<Revision>>();
+
+ public TimeLine(int id)
+ {
+ this.id = id;
+ }
+
+ public int getID()
+ {
+ return id;
+ }
+
+ public Revision getRevision(int version)
+ {
+ for (Iterator<Reference<Revision>> it = revisions.iterator(); it.hasNext();)
+ {
+ Reference<Revision> reference = it.next();
+ Revision revision = reference.get();
+ if (revision != null)
+ {
+ if (revision.getVersion() == version)
+ {
+ return revision;
+ }
+ }
+ else
+ {
+ it.remove();
+ break;
+ }
+ }
+
+ long time = System.currentTimeMillis();
+ Revision revision = loadRevision(id, version);
+ time = System.currentTimeMillis() - time;
+ int size = getRevisionSize(revision);
+
+ revisions.add(new CacheElement(revision, getReferenceQueue()));
+ return revision;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private static final class CacheElement extends WeakReference<Revision>
+ {
+ private int id;
+
+ private int version;
+
+ public CacheElement(Revision revision, ReferenceQueue<Revision> queue)
+ {
+ super(revision, queue);
+ id = revision.getID();
+ version = revision.getVersion();
+ }
+
+ public int getID()
+ {
+ return id;
+ }
+
+ public int getVersion()
+ {
+ return version;
+ }
+
+ public Revision getRevision()
+ {
+ return get();
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Ref" + id + "v" + version + (get() == null ? " cleared" : " set");
+ }
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/cache/Cache.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/cache/Cache.java
new file mode 100644
index 0000000..d9b141a
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/cache/Cache.java
@@ -0,0 +1,108 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.internal.util.cache;
+
+import org.eclipse.net4j.internal.util.bundle.OM;
+import org.eclipse.net4j.internal.util.lifecycle.Worker;
+import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
+import org.eclipse.net4j.util.cache.ICache;
+import org.eclipse.net4j.util.cache.ICacheMonitor;
+import org.eclipse.net4j.util.cache.ICacheProbe;
+
+import java.lang.ref.Reference;
+import java.lang.ref.ReferenceQueue;
+
+/**
+ * @author Eike Stepper
+ */
+public abstract class Cache<E> extends Worker implements ICache
+{
+ private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, Cache.class);
+
+ private ICacheMonitor cacheMonitor;
+
+ private ICacheProbe cacheProbe;
+
+ private ReferenceQueue<E> referenceQueue = new ReferenceQueue<E>();
+
+ public Cache()
+ {
+ }
+
+ public ICacheMonitor getCacheMonitor()
+ {
+ return cacheMonitor;
+ }
+
+ public void setCacheMonitor(ICacheMonitor cacheMonitor)
+ {
+ this.cacheMonitor = cacheMonitor;
+ }
+
+ protected ICacheProbe getCacheProbe()
+ {
+ return cacheProbe;
+ }
+
+ protected ReferenceQueue<E> getReferenceQueue()
+ {
+ return referenceQueue;
+ }
+
+ @Override
+ protected void doBeforeActivate() throws Exception
+ {
+ super.doBeforeActivate();
+ if (cacheMonitor == null)
+ {
+ throw new IllegalStateException("cacheMonitor == null");
+ }
+ }
+
+ @Override
+ protected void doActivate() throws Exception
+ {
+ super.doActivate();
+ cacheProbe = cacheMonitor.registerCache(this);
+ }
+
+ @Override
+ protected void doDeactivate() throws Exception
+ {
+ cacheMonitor.deregisterCache(this);
+ cacheProbe = null;
+ super.doDeactivate();
+ }
+
+ @Override
+ protected void work(WorkContext context) throws Exception
+ {
+ Reference<? extends E> reference = referenceQueue.remove(200);
+ if (reference != null)
+ {
+ unreachableElement(reference);
+ }
+ }
+
+ protected void unreachableElement(Reference<? extends E> reference)
+ {
+ E element = reference.get();
+ if (element != null)
+ {
+ unreachableElement(element);
+ }
+ }
+
+ protected void unreachableElement(E element)
+ {
+ if (TRACER.isEnabled()) TRACER.trace("Unreachable: " + element);
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/cache/CacheMonitor.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/cache/CacheMonitor.java
index 920ae06..055cc66 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/cache/CacheMonitor.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/cache/CacheMonitor.java
@@ -10,8 +10,10 @@
**************************************************************************/
package org.eclipse.net4j.internal.util.cache;
+import org.eclipse.net4j.internal.util.bundle.OM;
import org.eclipse.net4j.internal.util.event.Event;
import org.eclipse.net4j.internal.util.lifecycle.Worker;
+import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
import org.eclipse.net4j.util.ImplementationError;
import org.eclipse.net4j.util.cache.ICache;
import org.eclipse.net4j.util.cache.ICacheMonitor;
@@ -26,6 +28,11 @@ import java.util.Map;
*/
public class CacheMonitor extends Worker implements ICacheMonitor
{
+ // percentFreeAllocated = Round((freeMemory / totalMemory) * 100);
+ // percentAllocated = Round((totalMemory / maxMemory ) * 100);
+
+ private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, CacheMonitor.class);
+
private static final long DEFAULT_PAUSE_GREEN = 60L * 1000L; // 1 minute
private static final long DEFAULT_PAUSE_YELLOW = 5L * 1000L; // 5 seconds
@@ -42,7 +49,7 @@ public class CacheMonitor extends Worker implements ICacheMonitor
private Condition condition;
- private Map<ICache, ICacheRegistration> caches = new HashMap<ICache, ICacheRegistration>();
+ private Map<ICache, ICacheRegistration> registrations = new HashMap<ICache, ICacheRegistration>();
public CacheMonitor()
{
@@ -93,34 +100,44 @@ public class CacheMonitor extends Worker implements ICacheMonitor
return condition;
}
- public void setCondition(Condition newCondition)
+ public ICacheRegistration[] getRegistrations()
{
- if (newCondition == null)
+ synchronized (registrations)
{
- throw new ImplementationError("newCondition == null");
- }
-
- Condition oldCondition = condition;
- if (newCondition != oldCondition)
- {
- condition = newCondition;
- fireEvent(new CacheMonitorEvent(oldCondition, newCondition));
+ return registrations.values().toArray(new ICacheRegistration[registrations.size()]);
}
}
public ICacheRegistration registerCache(ICache cache)
{
+ if (TRACER.isEnabled()) TRACER.trace("Registering cache " + cache);
ICacheRegistration registration = new CacheRegistration(this, cache);
- caches.put(cache, registration);
+ ICacheRegistration oldRegistration;
+ synchronized (registrations)
+ {
+ oldRegistration = registrations.put(cache, registration);
+ }
+
+ if (oldRegistration != null)
+ {
+ oldRegistration.dispose();
+ }
+
return registration;
}
public void deregisterCache(ICache cache)
{
- ICacheRegistration registration = caches.remove(cache);
+ ICacheRegistration registration;
+ synchronized (registrations)
+ {
+ registration = registrations.remove(cache);
+ }
+
if (registration != null)
{
registration.dispose();
+ if (TRACER.isEnabled()) TRACER.trace("Deregistered cache " + cache);
}
}
@@ -135,6 +152,18 @@ public class CacheMonitor extends Worker implements ICacheMonitor
}
@Override
+ protected void doDeactivate() throws Exception
+ {
+ for (ICacheRegistration registration : getRegistrations())
+ {
+ registration.dispose();
+ }
+
+ registrations.clear();
+ super.doDeactivate();
+ }
+
+ @Override
protected void work(WorkContext context) throws Exception
{
Condition newCondition = conditionPolicy.getNewCondition(condition);
@@ -157,10 +186,24 @@ public class CacheMonitor extends Worker implements ICacheMonitor
}
}
+ protected void setCondition(Condition newCondition)
+ {
+ if (newCondition == null)
+ {
+ throw new ImplementationError("newCondition == null");
+ }
+
+ Condition oldCondition = condition;
+ if (newCondition != oldCondition)
+ {
+ condition = newCondition;
+ fireEvent(new CacheMonitorEvent(oldCondition, newCondition));
+ }
+ }
+
protected void handleConditionRED()
{
- // TODO Implement method CacheMonitor.handleConditionRED()
- throw new UnsupportedOperationException("Not yet implemented");
+ System.err.println("\n\n\nCache monitor condition RED\n\n\n\n");
}
/**
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheProbe.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheProbe.java
index 66805fa..5d9c260 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheProbe.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheProbe.java
@@ -22,4 +22,12 @@ public interface ICacheProbe
public void elementEvicted(int elementSize);
public void elementReconstructed(long reconstructionTime);
+
+ public int getElementCount();
+
+ public long getCacheSize();
+
+ public long getAverageElementSize();
+
+ public long getReconstructionCost();
}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheRegistration.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheRegistration.java
index ac48580..7dbb386 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheRegistration.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/cache/ICacheRegistration.java
@@ -20,12 +20,4 @@ public interface ICacheRegistration extends ICacheProbe
public ICache getCache();
public void dispose();
-
- public int getElementCount();
-
- public long getCacheSize();
-
- public long getAverageElementSize();
-
- public long getReconstructionCost();
}