Skip to content

Commit aa5e069

Browse files
Deliver progress information to the IDE
1 parent 65854b3 commit aa5e069

File tree

3 files changed

+76
-19
lines changed

3 files changed

+76
-19
lines changed

engine/runtime-instrument-common/src/main/java/org/enso/interpreter/service/ExecutionProgressObserver.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ final class ExecutionProgressObserver implements Consumer<ObservedMessage> {
1212
private final UUID nodeId;
1313
private final Thread thread;
1414
private final AutoCloseable handle;
15-
private final Consumer<Double> consumer;
15+
private final ProgressAggregator aggregate;
1616

1717
ExecutionProgressObserver(UUID nodeId, Consumer<Double> c) {
1818
this.nodeId = nodeId;
1919
this.handle = ObservedMessage.observe(PROGRESS, this);
2020
this.thread = Thread.currentThread();
21-
this.consumer = c;
22-
// indeterminate computation has just started
21+
this.aggregate = new ProgressAggregator(c);
22+
// start by notifying indeterminate computation
2323
c.accept(-1.0);
24-
System.err.println("Observing for " + nodeId);
2524
}
2625

2726
UUID nodeId() {
@@ -35,12 +34,29 @@ static ExecutionProgressObserver startComputation(UUID nodeId, Consumer<Double>
3534
@Override
3635
public void accept(ObservedMessage t) {
3736
if (Thread.currentThread() == thread) {
38-
System.err.println(" seeing " + t.getMessage() + " for " + nodeId);
37+
switch (t.getMessage()) {
38+
case "INIT {}:{}@{}" -> {
39+
if (t.getArguments().size() >= 3
40+
&& t.getArguments().get(1) instanceof String msg
41+
&& t.getArguments().get(2) instanceof Number max) {
42+
var key = t.getArguments().get(0);
43+
aggregate.create(key, max.longValue());
44+
}
45+
}
46+
case "ADVANCE {}+{}" -> {
47+
if (t.getArguments().size() >= 2 && t.getArguments().get(1) instanceof Number by) {
48+
var key = t.getArguments().get(0);
49+
aggregate.advanceBy(key, by.longValue());
50+
}
51+
}
52+
default -> {
53+
System.err.println(" seeing " + t.getMessage() + " for " + nodeId);
54+
}
55+
}
3956
}
4057
}
4158

4259
final void finishComputation() {
43-
System.err.println("Finished computing " + nodeId);
4460
try {
4561
handle.close();
4662
} catch (Exception ex) {
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
package org.enso.interpreter.runtime.progress;
1+
package org.enso.interpreter.service;
22

33
import java.util.ArrayDeque;
44
import java.util.Deque;
5+
import java.util.Map;
6+
import java.util.WeakHashMap;
57
import java.util.function.Consumer;
68

79
/**
810
* Example of a progress aggregator able to nest multiple Progress and compute % of aggregated
911
* progress.
1012
*/
1113
public final class ProgressAggregator {
14+
/**
15+
* @GuardedBy("this")
16+
*/
17+
private final Map<Object, Progress> map = new WeakHashMap<>();
18+
1219
private final Consumer<Double> updateStatus;
1320
private final Deque<Progress> stack = new ArrayDeque<>();
1421
private double current;
@@ -24,13 +31,12 @@ public ProgressAggregator(Consumer<Double> updateStatus) {
2431
}
2532

2633
/**
27-
* Starts new progress in the current aggregator's stack.Nests the current progress in the
34+
* Starts new progress in the current aggregator's stack. Nests the current progress in the
2835
* currently executing step of current progress.
2936
*
3037
* @param max maximum number of steps the progress can "advance to"
31-
* @return new instance of a progress to {@link Progress#advance}
3238
*/
33-
public synchronized Progress create(long max) {
39+
public synchronized void create(Object key, long max) {
3440
Progress p;
3541
if (stack.isEmpty()) {
3642
p = new Progress(max, 0.0, 1.0);
@@ -40,11 +46,24 @@ public synchronized Progress create(long max) {
4046
p = new Progress(max, current, current + previous.singleStep());
4147
}
4248
stack.addFirst(p);
43-
return p;
49+
map.put(key, p);
50+
}
51+
52+
public void closeProgress(Object key) {
53+
if (findBy(key) instanceof Progress p) {
54+
p.advance(p.max);
55+
stack.remove(p);
56+
}
57+
}
58+
59+
public void advanceBy(Object key, long steps) {
60+
if (findBy(key) instanceof Progress p) {
61+
p.advance(steps);
62+
}
4463
}
4564

46-
private synchronized void closeProgress(Progress p) {
47-
stack.remove(p);
65+
private synchronized Progress findBy(Object key) {
66+
return map.get(key);
4867
}
4968

5069
private void advanceTo(double now) {
Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.enso.interpreter.runtime.progress;
1+
package org.enso.interpreter.service;
22

33
import static org.junit.Assert.assertEquals;
44

@@ -13,7 +13,7 @@ public ProgressAggregatorTest() {}
1313
@Test
1414
public void threeSteps() {
1515
var agg = new ProgressAggregator(this);
16-
var three = agg.create(3);
16+
var three = create(agg, 3);
1717
three.advance(1);
1818
assertEquals("1/3", 0.333, current, 0.001);
1919
three.advance(1);
@@ -25,7 +25,7 @@ public void threeSteps() {
2525
@Test
2626
public void closeFinishesItAll() {
2727
var agg = new ProgressAggregator(this);
28-
var ten = agg.create(10);
28+
var ten = create(agg, 10);
2929
ten.advance(1);
3030
assertEquals("1/10", 0.1, current, 0.001);
3131
ten.close();
@@ -35,8 +35,8 @@ public void closeFinishesItAll() {
3535
@Test
3636
public void halfAndThird() {
3737
var agg = new ProgressAggregator(this);
38-
var half = agg.create(2);
39-
var firstThird = agg.create(3);
38+
var half = create(agg, 2);
39+
var firstThird = create(agg, 3);
4040
firstThird.advance(1);
4141
assertEquals("1/6", 1.0 / 6.0, current, 0.001);
4242
firstThird.advance(1);
@@ -46,7 +46,7 @@ public void halfAndThird() {
4646
half.advance(1);
4747
assertEquals("Topmost 1/2", 0.5, current, 0.001);
4848

49-
var secondThird = agg.create(3);
49+
var secondThird = create(agg, 3);
5050
secondThird.advance(2);
5151
assertEquals("5/6", 5.0 / 6.0, current, 0.001);
5252

@@ -59,4 +59,26 @@ public void halfAndThird() {
5959
public void accept(Double t) {
6060
this.current = t;
6161
}
62+
63+
private Handle create(ProgressAggregator agg, long max) {
64+
var h = new Handle(agg);
65+
agg.create(h, max);
66+
return h;
67+
}
68+
69+
private final class Handle {
70+
private final ProgressAggregator agg;
71+
72+
Handle(ProgressAggregator agg) {
73+
this.agg = agg;
74+
}
75+
76+
final void advance(long n) {
77+
agg.advanceBy(this, n);
78+
}
79+
80+
final void close() {
81+
agg.closeProgress(this);
82+
}
83+
}
6284
}

0 commit comments

Comments
 (0)