I finally got around to porting my actor process ring code (Erlang, Scala) to run with the Kilim library tonight.
Kilim is a Java library for lightweight message-passing. The three main things to know about are Tasks, Mailboxes, and @pausable. In Kilim, actors are replaced with Tasks, which are just lightweight runnable objects that serves basically the same role.
Mailboxes are designed for multiple-producer, single-consumer access just like Erlang and Scala actor mailboxes. In Kilim though, Mailbox is just a class and Tasks don’t necessarily have a Mailbox or could even have more than one. Mailboxes are also generic and typed by the kind of message they should receieve. This opens up new ways to compose Tasks and Mailboxes into a broader range of structures. Messages are typically simple (potentially mutable) classes.
Finally, there is an annotation called @pausable. This is used to specify that a method can be paused, continuation-style, during pausable calls; sleep and yield are two provided hooks and the mailbox get / put methods are also pausable. @pausable is also used to mark classes for instrumentation.
That leads me to the compile-time weaver. After you’ve compiled your classes, you need to run a compile-time weaver to modify the bytecode so that the continuation-style pausing is available. At runtime, pausing is used to schedule a large number of actors over a small number of kernel threads.
In all, this is a really interesting set of features that adapts the Erlang/Scala actor model into the statically typed world of Java pretty nicely.
From a usage point of view, it’s kind of a pain. Compile-time weaving sucks rocks as it puts a kink in every tool chain plus there’s nothing happening that couldn’t be done at runtime with a Java agent, as far as I can tell. I also had a lot of time to get the Weaver to run on my first stab at the code. It was far from obvious that the error messages were indicating I had forgotten @pausable on the non-Actor classes. Fortunately, I know enough about ASM to tell what I was missing. And once I got it running I got some weird NoSuchMethodErrors due to incorrectly specifying @pausable on methods that didn’t need it.
These bumps didn’t bother me too much though – this is a new project and I understand that it’s early for that kind of help.
Now to a little code. This is really pretty much a port from the Scala code into Java, which was pretty close. I broke the code (previously in one file) into Ring (the main code), Message, TokenMessage, NodeActor, and TimerActor. You’ll notice there is a LOT more code with the Java version than the Scala or Erlang versions. [source:java]
import java.util.ArrayList;
import java.util.List;</p>
import kilim.Mailbox;
import kilim.pausable;
@pausable
public class Ring {
public static void main(String arg[]) {
new Ring().startRing(Integer.parseInt(arg[0]));
}
public void startRing(int n) {
List
Mailbox
mailbox.putnb(Message.START);
try { Thread.sleep(100000000); } catch(InterruptedException e) {}
}
private TimerActor startTimer() {
TimerActor actor = new TimerActor(new Mailbox
actor.start();
return actor;
}
private List
System.out.println(“constructing nodes”);
long startConstructing = System.currentTimeMillis();
List
for(int i=0; i<n; i++) {
nodes.add(new NodeActor(i, new Mailbox
nodes.get(i).start();
}
long endConstructing = System.currentTimeMillis();
System.out.println(“Took ” + (endConstructing-startConstructing) + ” ms to construct ” + n + ” nodes”);
return nodes;
}
private Mailbox
System.out.println(“connecting nodes”);
nodes.add(nodes.get(0));
for(int i=0; i<n; i++) {
nodes.get(i).connect(nodes.get(i+1).getInbox());
}
return nodes.get(0).getInbox();
}
}
[/source]
It’s very important that the Ring class is marked @pausable or the weaver won’t work. Now the message classes. I defined some immutable singleton messages in Message and a mutable TokenMessage: [source:java]
public class Message {
public static final Message START = new Message();
public static final Message STOP = new Message();
public static final Message CANCEL = new Message();
}
public class TokenMessage extends Message {
public final int source;
public int value;
public TokenMessage(int source, int value) {
this.source = source;
this.value = value;
}
}
[/source]
And here’s the node actor translated into a Kilim Task (note the @pausable annotation on the execute() method, which is defined in Task): [source:java]
import kilim.Mailbox;
import kilim.Task;
import kilim.pausable;
public class NodeActor extends Task {
private final int nodeId;
private final Mailbox
private final Mailbox
private Mailbox
public NodeActor(int nodeId, Mailbox
this.nodeId = nodeId;
this.inbox = inbox;
this.timerInbox = timerInbox;
}
public Mailbox
return this.inbox;
}
public void connect(Mailbox
this.nextInbox = nextInbox;
}
@pausable
public void execute() throws Exception {
while(true) {
Message message = inbox.get();
if(message.equals(Message.START)) {
System.out.println(System.currentTimeMillis() + ” ” + nodeId + “: Starting messages”);
timerInbox.putnb(Message.START);
nextInbox.putnb(new TokenMessage(nodeId, 0));
} else if(message.equals(Message.STOP)) {
//System.out.println(System.currentTimeMillis() + ” ” + nodeId + “: Stopping”);
nextInbox.putnb(Message.STOP);
break;
} else if(message instanceof TokenMessage) {
TokenMessage token = (TokenMessage)message;
if(token.source == nodeId) {
int nextVal = token.value+1;
if(nextVal % 10000 == 0) {
System.out.println(System.currentTimeMillis() + ” ” + nodeId + “: Around ring ” + nextVal + ” times”);
}
if(nextVal == 1000000) {
timerInbox.putnb(Message.STOP);
timerInbox.putnb(Message.CANCEL);
nextInbox.putnb(Message.STOP);
break;
} else {
token.value = nextVal;
nextInbox.putnb(token);
}
} else {
nextInbox.putnb(token);
}
}
}
}
}
[/source]
And for completeness, here’s the Timer Actor: [source:java]
import kilim.Mailbox;
import kilim.Task;
import kilim.pausable;
public class TimerActor extends Task {
private final Mailbox
private boolean timing;
private long startTime;
public TimerActor(Mailbox
this.inbox = inbox;
}
public Mailbox
return this.inbox;
}
@pausable
public void execute() throws Exception {
while(true) {
Message message = inbox.get();
if(message.equals(Message.START) && !timing) {
startTime = System.currentTimeMillis();
timing = true;
} else if(message.equals(Message.STOP) && timing) {
long endTime = System.currentTimeMillis();
System.out.println(“Start=” + startTime + ” Stop=” + endTime + ” Elapsed=” + (endTime-startTime));
timing = false;
} else if(message.equals(Message.CANCEL)) {
break;
}
}
}
}
[/source]
To compile and weave, you’ll do something like this: [source:java]
$ export KCP=$KILIM/libs/asm-all-2.2.3.jar:$KILIM/classes
$ javac -cp $KCP -d bin src/*.java
$ java -cp $KCP kilim.tools.Weaver -d ./bin ./bin
$ java -cp $KCPL./bin Ring 100
[/source]
And finally the results, here in comparison with Erlang and Scala 2.7.3/JDK 1.6:
Language | Spawn 100 | Send 100M messages | Spawn 20k |
---|---|---|---|
Erlang R12B | 0.2 ms | 77354 ms | 120 ms |
Scala 2.7.3 | 10 ms | 121712 ms | 315 ms |
Kilim | 3 ms | 78390 ms | 192 ms |
So, Kilim demonstrates process creation faster than Scala (but still slower than Erlang) and message-passing in the realm of Erlang and significantly faster than Scala. That’s pretty darn impressive! I’m looking forward to watching where this library goes.