-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.ui.jobs;\r
-\r
-import java.util.function.Consumer;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.core.runtime.jobs.Job;\r
-import org.simantics.DatabaseJob;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.util.SessionGarbageCollection;\r
-import org.simantics.db.service.LifecycleSupport;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class SessionGarbageCollectorJob extends Job {\r
-\r
- private static SessionGarbageCollectorJob instance;\r
-\r
- public synchronized static SessionGarbageCollectorJob getInstance() {\r
- if (instance == null)\r
- instance = new SessionGarbageCollectorJob();\r
- return instance;\r
- }\r
-\r
- private static final boolean TRACE = false;\r
-\r
- /**\r
- * At least 60 seconds between executions.\r
- */\r
- private static final long DEFAULT_QUIET_TIME = 5000;\r
-\r
- private long start;\r
- private long quietTime;\r
- private long userDefinedQuietTime;\r
-\r
- private boolean enabled = true;\r
-\r
- /**\r
- * \r
- */\r
- public SessionGarbageCollectorJob() {\r
- this(DEFAULT_QUIET_TIME);\r
- }\r
-\r
- /**\r
- * @param delayBetweenExecutions\r
- */\r
- public SessionGarbageCollectorJob(long delayBetweenExecutions) {\r
- super("Database Garbage Collector");\r
- setPriority(Job.DECORATE);\r
-\r
- // Hide job from users.\r
- setSystem(true);\r
-\r
- this.start = System.currentTimeMillis();\r
- this.quietTime = delayBetweenExecutions;\r
- this.userDefinedQuietTime = delayBetweenExecutions;\r
- }\r
-\r
- /**\r
- * Cancels the currently scheduled execution of this job and reschedules\r
- * another execution after the current quiet time. This can be used for\r
- * easily pushing GC back a bit while performing an operation during which\r
- * it is not efficient to GC.\r
- *\r
- * @see #setQuietTime(long)\r
- */\r
- public void rescheduleAfterQuietTime() {\r
- cancel();\r
- scheduleAfterQuietTime();\r
- }\r
-\r
- /**\r
- * Cancels the currently scheduled execution of this job and reschedules\r
- * another with no delay. This can be used for immediate forced execution of\r
- * session GC.\r
- */\r
- public void rescheduleNow() {\r
- cancel();\r
- schedule();\r
- }\r
-\r
- /**\r
- * @param enabled\r
- * @return\r
- */\r
- public SessionGarbageCollectorJob setEnabled(boolean enabled) {\r
- this.enabled = enabled;\r
- return this;\r
- }\r
-\r
-// /**\r
-// * @param quietTime quiet time between collections in\r
-// * milliseconds\r
-// */\r
-// public SessionGarbageCollectorJob setQuietTime(long quietTime) {\r
-// this.quietTime = quietTime;\r
-// return this;\r
-// }\r
-\r
- /**\r
- * Schedules this job after the currently set quiet time.\r
- * \r
- * @see #setQuietTime(long)\r
- */\r
- public void scheduleAfterQuietTime() {\r
- schedule(quietTime);\r
- }\r
-\r
- @Override\r
- public boolean shouldSchedule() {\r
- return enabled;\r
- }\r
-\r
- @Override\r
- public boolean shouldRun() {\r
- if (TRACE) {\r
- if (!enabled) {\r
- long t = System.currentTimeMillis();\r
- System.out.println("GC disabled, not running @ " + ((double) (t - start) * 1e-3) + " seconds");\r
- }\r
- }\r
- return enabled;\r
- }\r
-\r
- /**\r
- * Only invoked with actual errors, not <code>null</code> values.\r
- */\r
- Consumer<DatabaseException> errorCallback = e -> ErrorLogger.defaultLogError(e);\r
-\r
- @Override\r
- protected IStatus run(IProgressMonitor monitor) {\r
- long interval = quietTime;\r
- try {\r
- Session session = Simantics.peekSession();\r
- if (session == null)\r
- return Status.CANCEL_STATUS;\r
- LifecycleSupport lfs = session.peekService(LifecycleSupport.class);\r
- if (lfs == null || lfs.isClosed() || lfs.isClosing())\r
- return Status.CANCEL_STATUS;\r
-\r
- // Never run while a heavy database job is in progress.\r
- if (DatabaseJob.inProgress()) {\r
- // Schedule again in at most 10 seconds instead of\r
- // waiting for the whole quiet time period again.\r
- interval = Math.min(10, quietTime);\r
- return Status.CANCEL_STATUS;\r
- }\r
-\r
-// // Don't run if there are currently write requests in progress\r
-// TransactionSupport ts = session.getService(TransactionSupport.class);\r
-// if (ts.getWriteCount() > 0) {\r
-// if (TRACE) {\r
-// long t = System.currentTimeMillis();\r
-// System.out.println("Write in progress, no GC @ " + ((double) (t - start) * 1e-3) + " seconds");\r
-// }\r
-// return Status.CANCEL_STATUS;\r
-// }\r
-\r
- long begin = System.currentTimeMillis();\r
- if (TRACE) {\r
- System.out.println("running GC @ " + ((double) (begin - start) * 1e-3) + " seconds");\r
- }\r
-\r
- boolean busy = SessionGarbageCollection.gc(monitor, session, true, errorCallback);\r
- if(busy) {\r
- quietTime = Math.max((3*quietTime)/4, 100);\r
- } else {\r
- if(quietTime < userDefinedQuietTime)\r
- quietTime = Math.min(userDefinedQuietTime, (long)(1.2*quietTime));\r
- }\r
-\r
- if (TRACE)\r
- if(busy)\r
- System.err.println("Session GC ended busy. New quiet time is " + quietTime);\r
- \r
- // This quiet time might have been adjusted in GC\r
- interval = quietTime;\r
-\r
- long intermediate = System.currentTimeMillis();\r
-\r
- // Clean up heap of all garbage left behind by SessionGarbageCollection \r
- //System.gc();\r
-\r
- if (TRACE) {\r
- //long end = System.currentTimeMillis();\r
- //System.out.println("Session.GC " + (intermediate - begin) + "ms, System.GC " + (end-intermediate) + "ms, total " + (end - begin) + "ms");\r
- System.out.println("Session.GC " + (intermediate - begin) + "ms");\r
- }\r
-\r
- // Reschedule after a quiet period.\r
- return Status.OK_STATUS;\r
- } finally {\r
- schedule(interval);\r
- monitor.done();\r
- }\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * 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:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.ui.jobs;
+
+import java.util.function.Consumer;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.db.Session;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.SessionGarbageCollection;
+import org.simantics.db.service.LifecycleSupport;
+import org.simantics.utils.ui.ErrorLogger;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class SessionGarbageCollectorJob extends Job {
+
+ private static SessionGarbageCollectorJob instance;
+
+ public synchronized static SessionGarbageCollectorJob getInstance() {
+ if (instance == null)
+ instance = new SessionGarbageCollectorJob();
+ return instance;
+ }
+
+ private static final boolean TRACE = false;
+
+ /**
+ * At least 60 seconds between executions.
+ */
+ private static final long DEFAULT_QUIET_TIME = 5000;
+
+ private long start;
+ private long quietTime;
+ private long userDefinedQuietTime;
+
+ private boolean enabled = true;
+
+ /**
+ *
+ */
+ public SessionGarbageCollectorJob() {
+ this(DEFAULT_QUIET_TIME);
+ }
+
+ /**
+ * @param delayBetweenExecutions
+ */
+ public SessionGarbageCollectorJob(long delayBetweenExecutions) {
+ super("Database Garbage Collector");
+ setPriority(Job.DECORATE);
+
+ // Hide job from users.
+ setSystem(true);
+
+ this.start = System.currentTimeMillis();
+ this.quietTime = delayBetweenExecutions;
+ this.userDefinedQuietTime = delayBetweenExecutions;
+ }
+
+ /**
+ * Cancels the currently scheduled execution of this job and reschedules
+ * another execution after the current quiet time. This can be used for
+ * easily pushing GC back a bit while performing an operation during which
+ * it is not efficient to GC.
+ *
+ * @see #setQuietTime(long)
+ */
+ public void rescheduleAfterQuietTime() {
+ cancel();
+ scheduleAfterQuietTime();
+ }
+
+ /**
+ * Cancels the currently scheduled execution of this job and reschedules
+ * another with no delay. This can be used for immediate forced execution of
+ * session GC.
+ */
+ public void rescheduleNow() {
+ cancel();
+ schedule();
+ }
+
+ /**
+ * @param enabled
+ * @return
+ */
+ public SessionGarbageCollectorJob setEnabled(boolean enabled) {
+ this.enabled = enabled;
+ return this;
+ }
+
+// /**
+// * @param quietTime quiet time between collections in
+// * milliseconds
+// */
+// public SessionGarbageCollectorJob setQuietTime(long quietTime) {
+// this.quietTime = quietTime;
+// return this;
+// }
+
+ /**
+ * Schedules this job after the currently set quiet time.
+ *
+ * @see #setQuietTime(long)
+ */
+ public void scheduleAfterQuietTime() {
+ schedule(quietTime);
+ }
+
+ @Override
+ public boolean shouldSchedule() {
+ return enabled;
+ }
+
+ @Override
+ public boolean shouldRun() {
+ if (TRACE) {
+ if (!enabled) {
+ long t = System.currentTimeMillis();
+ System.out.println("GC disabled, not running @ " + ((double) (t - start) * 1e-3) + " seconds");
+ }
+ }
+ return enabled;
+ }
+
+ /**
+ * Only invoked with actual errors, not <code>null</code> values.
+ */
+ Consumer<DatabaseException> errorCallback = e -> ErrorLogger.defaultLogError(e);
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ long interval = quietTime;
+ try {
+ Session session = Simantics.peekSession();
+ if (session == null)
+ return Status.CANCEL_STATUS;
+ LifecycleSupport lfs = session.peekService(LifecycleSupport.class);
+ if (lfs == null || lfs.isClosed() || lfs.isClosing())
+ return Status.CANCEL_STATUS;
+
+ // Never run while a heavy database job is in progress.
+ if (DatabaseJob.inProgress()) {
+ // Schedule again in at most 10 seconds instead of
+ // waiting for the whole quiet time period again.
+ interval = Math.min(10, quietTime);
+ return Status.CANCEL_STATUS;
+ }
+
+// // Don't run if there are currently write requests in progress
+// TransactionSupport ts = session.getService(TransactionSupport.class);
+// if (ts.getWriteCount() > 0) {
+// if (TRACE) {
+// long t = System.currentTimeMillis();
+// System.out.println("Write in progress, no GC @ " + ((double) (t - start) * 1e-3) + " seconds");
+// }
+// return Status.CANCEL_STATUS;
+// }
+
+ long begin = System.currentTimeMillis();
+ if (TRACE) {
+ System.out.println("running GC @ " + ((double) (begin - start) * 1e-3) + " seconds");
+ }
+
+ boolean busy = SessionGarbageCollection.gc(monitor, session, true, errorCallback);
+ if(busy) {
+ quietTime = Math.max((3*quietTime)/4, 100);
+ } else {
+ if(quietTime < userDefinedQuietTime)
+ quietTime = Math.min(userDefinedQuietTime, (long)(1.2*quietTime));
+ }
+
+ if (TRACE)
+ if(busy)
+ System.err.println("Session GC ended busy. New quiet time is " + quietTime);
+
+ // This quiet time might have been adjusted in GC
+ interval = quietTime;
+
+ long intermediate = System.currentTimeMillis();
+
+ // Clean up heap of all garbage left behind by SessionGarbageCollection
+ //System.gc();
+
+ if (TRACE) {
+ //long end = System.currentTimeMillis();
+ //System.out.println("Session.GC " + (intermediate - begin) + "ms, System.GC " + (end-intermediate) + "ms, total " + (end - begin) + "ms");
+ System.out.println("Session.GC " + (intermediate - begin) + "ms");
+ }
+
+ // Reschedule after a quiet period.
+ return Status.OK_STATUS;
+ } finally {
+ schedule(interval);
+ monitor.done();
+ }
+ }
+
+}