Browse Source

Merge "ProgressMonitor: Refactor to use background alarms"

stable-0.12
Chris Aniszczyk 14 years ago committed by Code Review
parent
commit
268ccbebe3
  1. 5
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java
  2. 271
      org.eclipse.jgit/src/org/eclipse/jgit/lib/BatchingProgressMonitor.java
  3. 152
      org.eclipse.jgit/src/org/eclipse/jgit/lib/TextProgressMonitor.java
  4. 141
      org.eclipse.jgit/src/org/eclipse/jgit/transport/SideBandProgressMonitor.java

5
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java

@ -52,6 +52,7 @@ import java.io.BufferedOutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.diff.DiffAlgorithm; import org.eclipse.jgit.diff.DiffAlgorithm;
import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm; import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
@ -189,7 +190,9 @@ class Diff extends TextBuiltin {
} else if (newTree == null) } else if (newTree == null)
newTree = new FileTreeIterator(db); newTree = new FileTreeIterator(db);
diffFmt.setProgressMonitor(new TextProgressMonitor()); TextProgressMonitor pm = new TextProgressMonitor();
pm.setDelayStart(2, TimeUnit.SECONDS);
diffFmt.setProgressMonitor(pm);
diffFmt.setPathFilter(pathFilter); diffFmt.setPathFilter(pathFilter);
if (detectRenames != null) if (detectRenames != null)
diffFmt.setDetectRenames(detectRenames.booleanValue()); diffFmt.setDetectRenames(detectRenames.booleanValue());

271
org.eclipse.jgit/src/org/eclipse/jgit/lib/BatchingProgressMonitor.java

@ -0,0 +1,271 @@
/*
* Copyright (C) 2008-2011, Google Inc.
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.lib;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/** ProgressMonitor that batches update events. */
public abstract class BatchingProgressMonitor implements ProgressMonitor {
private static final ScheduledThreadPoolExecutor alarmQueue;
static final Object alarmQueueKiller;
static {
// To support garbage collection, start our thread but
// swap out the thread factory. When our class is GC'd
// the alarmQueueKiller will finalize and ask the executor
// to shutdown, ending the worker.
//
int threads = 1;
alarmQueue = new ScheduledThreadPoolExecutor(threads,
new ThreadFactory() {
public Thread newThread(Runnable taskBody) {
Thread thr = new Thread("JGit-AlarmQueue");
thr.setDaemon(true);
return thr;
}
});
alarmQueue.allowCoreThreadTimeOut(false);
alarmQueue.setMaximumPoolSize(alarmQueue.getCorePoolSize());
alarmQueue.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
alarmQueue.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
alarmQueue.prestartAllCoreThreads();
// Now that the threads are running, its critical to swap out
// our own thread factory for one that isn't in the ClassLoader.
// This allows the class to GC.
//
alarmQueue.setThreadFactory(Executors.defaultThreadFactory());
alarmQueueKiller = new Object() {
@Override
protected void finalize() {
alarmQueue.shutdownNow();
}
};
}
private long delayStartTime;
private TimeUnit delayStartUnit = TimeUnit.MILLISECONDS;
private Task task;
/**
* Set an optional delay before the first output.
*
* @param time
* how long to wait before output. If 0 output begins on the
* first {@link #update(int)} call.
* @param unit
* time unit of {@code time}.
*/
public void setDelayStart(long time, TimeUnit unit) {
delayStartTime = time;
delayStartUnit = unit;
}
public void start(int totalTasks) {
// Ignore the number of tasks.
}
public void beginTask(String title, int work) {
endTask();
task = new Task(title, work);
if (delayStartTime != 0)
task.delay(delayStartTime, delayStartUnit);
}
public void update(int completed) {
if (task != null)
task.update(this, completed);
}
public void endTask() {
if (task != null) {
task.end(this);
task = null;
}
}
public boolean isCancelled() {
return false;
}
/**
* Update the progress monitor if the total work isn't known,
*
* @param taskName
* name of the task.
* @param workCurr
* number of units already completed.
*/
protected abstract void onUpdate(String taskName, int workCurr);
/**
* Finish the progress monitor when the total wasn't known in advance.
*
* @param taskName
* name of the task.
* @param workCurr
* total number of units processed.
*/
protected abstract void onEndTask(String taskName, int workCurr);
/**
* Update the progress monitor when the total is known in advance.
*
* @param taskName
* name of the task.
* @param workCurr
* number of units already completed.
* @param workTotal
* estimated number of units to process.
* @param percentDone
* {@code workCurr * 100 / workTotal}.
*/
protected abstract void onUpdate(String taskName, int workCurr,
int workTotal, int percentDone);
/**
* Finish the progress monitor when the total is known in advance.
*
* @param taskName
* name of the task.
* @param workCurr
* total number of units processed.
* @param workTotal
* estimated number of units to process.
* @param percentDone
* {@code workCurr * 100 / workTotal}.
*/
protected abstract void onEndTask(String taskName, int workCurr,
int workTotal, int percentDone);
private static class Task implements Runnable {
/** Title of the current task. */
private final String taskName;
/** Number of work units, or {@link ProgressMonitor#UNKNOWN}. */
private final int totalWork;
/** True when timer expires and output should occur on next update. */
private volatile boolean display;
/** Scheduled timer, supporting cancellation if task ends early. */
private Future<?> timerFuture;
/** True if the task has displayed anything. */
private boolean output;
/** Number of work units already completed. */
private int lastWork;
/** Percentage of {@link #totalWork} that is done. */
private int lastPercent;
Task(String taskName, int totalWork) {
this.taskName = taskName;
this.totalWork = totalWork;
this.display = true;
}
void delay(long time, TimeUnit unit) {
display = false;
timerFuture = alarmQueue.schedule(this, time, unit);
}
public void run() {
display = true;
}
void update(BatchingProgressMonitor pm, int completed) {
lastWork += completed;
if (totalWork == UNKNOWN) {
// Only display once per second, as the alarm fires.
if (display) {
pm.onUpdate(taskName, lastWork);
output = true;
restartTimer();
}
} else {
// Display once per second or when 1% is done.
int currPercent = lastWork * 100 / totalWork;
if (display) {
pm.onUpdate(taskName, lastWork, totalWork, currPercent);
output = true;
restartTimer();
lastPercent = currPercent;
} else if (currPercent != lastPercent) {
pm.onUpdate(taskName, lastWork, totalWork, currPercent);
output = true;
lastPercent = currPercent;
}
}
}
private void restartTimer() {
display = false;
timerFuture = alarmQueue.schedule(this, 1, TimeUnit.SECONDS);
}
void end(BatchingProgressMonitor pm) {
if (output) {
if (totalWork == UNKNOWN) {
pm.onEndTask(taskName, lastWork);
} else {
int pDone = lastWork * 100 / totalWork;
pm.onEndTask(taskName, lastWork, totalWork, pDone);
}
}
if (timerFuture != null)
timerFuture.cancel(false /* no interrupt */);
}
}
}

152
org.eclipse.jgit/src/org/eclipse/jgit/lib/TextProgressMonitor.java

@ -44,99 +44,103 @@
package org.eclipse.jgit.lib; package org.eclipse.jgit.lib;
/** import java.io.IOException;
* A simple progress reporter printing on stderr import java.io.PrintWriter;
*/ import java.io.Writer;
public class TextProgressMonitor implements ProgressMonitor {
private boolean output;
private long taskBeganAt;
private String msg;
private int lastWorked; /** A simple progress reporter printing on a stream. */
public class TextProgressMonitor extends BatchingProgressMonitor {
private final Writer out;
private int totalWork; private boolean write;
/** Initialize a new progress monitor. */ /** Initialize a new progress monitor. */
public TextProgressMonitor() { public TextProgressMonitor() {
taskBeganAt = System.currentTimeMillis(); this(new PrintWriter(System.err));
} }
public void start(final int totalTasks) { /**
// Ignore the number of tasks. * Initialize a new progress monitor.
taskBeganAt = System.currentTimeMillis(); *
* @param out
* the stream to receive messages on.
*/
public TextProgressMonitor(Writer out) {
this.out = out;
this.write = true;
} }
public void beginTask(final String title, final int total) { @Override
endTask(); protected void onUpdate(String taskName, int workCurr) {
msg = title; StringBuilder s = new StringBuilder();
lastWorked = 0; format(s, taskName, workCurr);
totalWork = total; send(s);
} }
public void update(final int completed) { @Override
if (msg == null) protected void onEndTask(String taskName, int workCurr) {
return; StringBuilder s = new StringBuilder();
format(s, taskName, workCurr);
final int cmp = lastWorked + completed; s.append("\n");
if (!output && System.currentTimeMillis() - taskBeganAt < 500) send(s);
return;
if (totalWork == UNKNOWN) {
display(cmp);
System.err.flush();
} else {
if ((cmp * 100 / totalWork) != (lastWorked * 100) / totalWork) {
display(cmp);
System.err.flush();
}
}
lastWorked = cmp;
output = true;
} }
private void display(final int cmp) { private void format(StringBuilder s, String taskName, int workCurr) {
final StringBuilder m = new StringBuilder(); s.append("\r");
m.append('\r'); s.append(taskName);
m.append(msg); s.append(": ");
m.append(": "); while (s.length() < 25)
while (m.length() < 25) s.append(' ');
m.append(' '); s.append(workCurr);
}
if (totalWork == UNKNOWN) { @Override
m.append(cmp); protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt) {
} else { StringBuilder s = new StringBuilder();
final String twstr = String.valueOf(totalWork); format(s, taskName, cmp, totalWork, pcnt);
String cmpstr = String.valueOf(cmp); send(s);
while (cmpstr.length() < twstr.length()) }
cmpstr = " " + cmpstr;
final int pcnt = (cmp * 100 / totalWork);
if (pcnt < 100)
m.append(' ');
if (pcnt < 10)
m.append(' ');
m.append(pcnt);
m.append("% (");
m.append(cmpstr);
m.append("/");
m.append(twstr);
m.append(")");
}
System.err.print(m); @Override
protected void onEndTask(String taskName, int cmp, int totalWork, int pcnt) {
StringBuilder s = new StringBuilder();
format(s, taskName, cmp, totalWork, pcnt);
s.append("\n");
send(s);
} }
public boolean isCancelled() { private void format(StringBuilder s, String taskName, int cmp,
return false; int totalWork, int pcnt) {
s.append("\r");
s.append(taskName);
s.append(": ");
while (s.length() < 25)
s.append(' ');
String endStr = String.valueOf(totalWork);
String curStr = String.valueOf(cmp);
while (curStr.length() < endStr.length())
curStr = " " + curStr;
if (pcnt < 100)
s.append(' ');
if (pcnt < 10)
s.append(' ');
s.append(pcnt);
s.append("% (");
s.append(curStr);
s.append("/");
s.append(endStr);
s.append(")");
} }
public void endTask() { private void send(StringBuilder s) {
if (output) { if (write) {
if (totalWork != UNKNOWN) try {
display(totalWork); out.write(s.toString());
System.err.println(); out.flush();
} catch (IOException err) {
write = false;
}
} }
output = false;
msg = null;
} }
} }

141
org.eclipse.jgit/src/org/eclipse/jgit/transport/SideBandProgressMonitor.java

@ -43,110 +43,85 @@
package org.eclipse.jgit.transport; package org.eclipse.jgit.transport;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import org.eclipse.jgit.lib.BatchingProgressMonitor;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ProgressMonitor;
/** Write progress messages out to the sideband channel. */ /** Write progress messages out to the sideband channel. */
class SideBandProgressMonitor implements ProgressMonitor { class SideBandProgressMonitor extends BatchingProgressMonitor {
private PrintWriter out; private final OutputStream out;
private boolean output; private boolean write;
private long taskBeganAt;
private long lastOutput;
private String msg;
private int lastWorked;
private int totalWork;
SideBandProgressMonitor(final OutputStream os) { SideBandProgressMonitor(final OutputStream os) {
out = new PrintWriter(new OutputStreamWriter(os, Constants.CHARSET)); out = os;
write = true;
} }
public void start(final int totalTasks) { @Override
// Ignore the number of tasks. protected void onUpdate(String taskName, int workCurr) {
taskBeganAt = System.currentTimeMillis(); StringBuilder s = new StringBuilder();
lastOutput = taskBeganAt; format(s, taskName, workCurr);
s.append(" \r");
send(s);
} }
public void beginTask(final String title, final int total) { @Override
endTask(); protected void onEndTask(String taskName, int workCurr) {
msg = title; StringBuilder s = new StringBuilder();
lastWorked = 0; format(s, taskName, workCurr);
totalWork = total; s.append(", done\n");
send(s);
} }
public void update(final int completed) { private void format(StringBuilder s, String taskName, int workCurr) {
if (msg == null) s.append(taskName);
return; s.append(": ");
s.append(workCurr);
final int cmp = lastWorked + completed;
final long now = System.currentTimeMillis();
if (!output && now - taskBeganAt < 500)
return;
if (totalWork == UNKNOWN) {
if (now - lastOutput >= 500) {
display(cmp, null);
lastOutput = now;
}
} else {
if ((cmp * 100 / totalWork) != (lastWorked * 100) / totalWork
|| now - lastOutput >= 500) {
display(cmp, null);
lastOutput = now;
}
}
lastWorked = cmp;
output = true;
} }
private void display(final int cmp, final String eol) { @Override
final StringBuilder m = new StringBuilder(); protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt) {
m.append(msg); StringBuilder s = new StringBuilder();
m.append(": "); format(s, taskName, cmp, totalWork, pcnt);
s.append(" \r");
send(s);
}
if (totalWork == UNKNOWN) { @Override
m.append(cmp); protected void onEndTask(String taskName, int cmp, int totalWork, int pcnt) {
} else { StringBuilder s = new StringBuilder();
final int pcnt = (cmp * 100 / totalWork); format(s, taskName, cmp, totalWork, pcnt);
if (pcnt < 100) s.append("\n");
m.append(' '); send(s);
if (pcnt < 10)
m.append(' ');
m.append(pcnt);
m.append("% (");
m.append(cmp);
m.append("/");
m.append(totalWork);
m.append(")");
}
if (eol != null)
m.append(eol);
else
m.append(" \r");
out.print(m);
out.flush();
} }
public boolean isCancelled() { private void format(StringBuilder s, String taskName, int cmp,
return false; int totalWork, int pcnt) {
s.append(taskName);
s.append(": ");
if (pcnt < 100)
s.append(' ');
if (pcnt < 10)
s.append(' ');
s.append(pcnt);
s.append("% (");
s.append(cmp);
s.append("/");
s.append(totalWork);
s.append(")");
} }
public void endTask() { private void send(StringBuilder s) {
if (output) { if (write) {
if (totalWork == UNKNOWN) try {
display(lastWorked, ", done\n"); out.write(Constants.encode(s.toString()));
else out.flush();
display(totalWork, "\n"); } catch (IOException err) {
write = false;
}
} }
output = false;
msg = null;
} }
} }

Loading…
Cancel
Save