Lambda the Ultimate

inactiveTopic Why Events Are a Bad Idea
started 1/23/2004; 9:44:55 AM - last post 1/27/2004; 12:12:37 PM
Luke Gorrie - Why Events Are a Bad Idea  blueArrow
1/23/2004; 9:44:55 AM (reads: 11201, responses: 21)
Why Events Are a Bad Idea
posted in the discussion group by Matt Brubeck.

Event-based programming has been highly touted in recent years as the best way to write highly concurrent applications. Having worked on several of these systems, we now believe this approach to be a mistake. Specifically, we believe that threads can achieve all of the strengths of events, including support for high concurrency, low overhead, and a simple concurrency model. Moreover, we argue that threads allow a simpler and more natural programming style.

A recent paper from the networking community that considers higher-level approaches to concurrent programming, and cites both Erlang and Concurrent ML (albeit in passing).

Unfortunately they take only a small step towards language-level concurrency. They are enthusiastic about whole-program static analysis to discover concurrency/atomicity, but don't consider the Erlang approach that makes this simple and explicit in the language. It will be exciting to see what the networking community make of that when they evaluate it seriously.

(Continuation-savvy LtU readers may groan at some of the State Machine vs. Threads myths that they have to dispel.)
Posted to implementation by Luke Gorrie on 1/23/04; 10:05:24 AM

Tom Duff - Re: Why Events Are a Bad Idea  blueArrow
1/23/2004; 10:43:00 AM (reads: 1327, responses: 1)
Rob Pike and Luca Cardelli discussed something related to this in their 1985 paper Sqeak: A Language for Communicating with Mice (not to be confused with the later Squeak Smalltalk.) Threads in Squeak required no run-time support, being completely compiled away. Rob's Newsqueak develops the language further, but requires runtime thread support.

Luke Gorrie - Re: Why Events Are a Bad Idea  blueArrow
1/23/2004; 10:53:54 AM (reads: 1329, responses: 0)
Rob Pike's concurrent programming has been a recent topic on the Erlang mailing list too, where Joe Armstrong has been experimenting with an X11 binding. I especially liked the related paper Acme: A User Interface for Programmers.

Mark Evans - Re: Why Events Are a Bad Idea  blueArrow
1/23/2004; 11:55:01 AM (reads: 1293, responses: 0)

See also last month's LtU post about the Sun/IBM/Cray initiative to develop a highly concurrent intermediate language.

Peter Van Roy - Re: Why Events Are a Bad Idea  blueArrow
1/23/2004; 1:41:05 PM (reads: 1221, responses: 0)
Sigh, it's an interesting step but they are locked into the shared-state concurrency model. They barely mention Erlang (message-passing concurrency). They should learn about declarative concurrency, which really was unjustly forgotten even though it is just as old and respectable as monitors (see the 1974 article by Gilles Kahn that introduces it).

Olivier Lefevre - Re: Why Events Are a Bad Idea  blueArrow
1/23/2004; 4:15:10 PM (reads: 1153, responses: 2)
Speaking of Erlang sightings, if you go to the ultimate source of that paper, i.e., the HotOS IX proceeding (which are in free access) and scroll down, there's another one titled Crash-only software. Although it does not mention Erlang, unless I am mistaken what they advocate sits rather well with the Erlang approach, no?

Luke Gorrie - Re: Why Events Are a Bad Idea  blueArrow
1/23/2004; 4:40:59 PM (reads: 1145, responses: 0)
Yes! That's exactly like the Erlang Let It Crash philosophy, and beautifully described too.

I bet they have great success with it :-)

Isaac Gouy - Re: Why Events Are a Bad Idea  blueArrow
1/23/2004; 4:44:28 PM (reads: 1141, responses: 0)
The Crash Only Software paper is referenced in Joe Armstrong's thesis.

Mark Evans - Re: Why Events Are a Bad Idea  blueArrow
1/23/2004; 6:51:29 PM (reads: 1098, responses: 0)

PVR - I assume you refer to the Sun/IBM/Cray VM project? If so have you contacted anyone over there, such as Guy Steele? Do they know about SEAM?

Pierre Phaneuf - Re: Why Events Are a Bad Idea  blueArrow
1/23/2004; 8:45:19 PM (reads: 1084, responses: 0)
I'm a bit disappointed. The threads promoted are cooperative, which offer no additionnal concurrency compared to event-driven systems, and do introduce some of the tricky problems of threads. WvStreams, a C++ package that I often use, is event-driven, but provides a generic continuation functor, and supports using functors as event handlers, making the programming style selectable per stream.

The problem really is preemption and real parallelism, where cache effects like preemption in the middle of a cache-sensitive operation or invalidation of cache lines dues to cache coherency issues often reduce the effectiveness of fully shared address space execution context (threads). There is a rise in popularity in the use of thread-local storage, for this very reason, and many results show that an hybrid architecture employing partially shared address space execution contexts (threads using TLS or Unix-style processes using shared memory), often one or two maximum per available CPU, each running an event-driven processing, provide the highest throughput.

Again, nothing prevents you in such an architecture of using continuations in your event handlers. There isn't very many advantages of putting the state either in explicit variables or on the stack, for either approach.

The last paragraph of section 5 is particularly misguided, IMHO. SEDA is well known to have rather severe design defects, for example using too many queuing operations and context switching. They say that the event model is "deeply tied" to creating many temporary objects and relying heavily on garbage collection, which is blatantly false. The main product of the company where I work (http://open.nit.ca/) is event driven and has no such requirements, being written in C++ with explicit memory management. If you think a CPU pipeline stall is a problem, consider the effect that having your thread preempted has on the cache. With the ever larger gap between CPU speeds and memory speed, I'd rather stall a pipeline than do a cold cache memory load!

In section 6, they misguide the reader, mentioning Ousterhout recommends threads for true concurrency and saying that this is the case for their target space, but they used cooperative multithreading that is specifically not true concurrency (it cannot use multiple processors).

Their conclusion saying that threads can have similar or better performance is simply not true. If they would have picked a better event driven system, this would have been very different. For example, http://www.cs.princeton.edu/~yruan/debox/debox.pdf shows further improvements on Flash, and Haboob is mentioned, while not incorporated in the graphed results, as having a mean latency 70-500 times greater than that of their version of Flash (last paragraph of section 8).

todd - Re: Why Events Are a Bad Idea  blueArrow
1/24/2004; 8:52:37 AM (reads: 957, responses: 0)
I have found over many projects that developers, especially in real-time systems, prefer threading because they prefer synchronous logic that executes at a known priority. The primary issue is simply that preemption is necessary for high priority work. Though that's not enough when shared locks are involved because you can priority inherit for too long also. Erlang and a simple event model won't work in these environments.

Where threads break down is when trying to apply quality of service to incomming request/event streams. This is really only an issue under load and then it really matters. Under lite load you can do almost anything you want and it will work. But quality of service requires very smart logic be applied to queues and threading priorities and limiting CPU usage. I don't think QOS can be addressed with threads and our very simple OS schedulers. If thread scheduling could be pluggable, maybe you could do better.

One model doesn't really fit all. I think threading is more general because it allows synchronous code, preemption, and priority. Yet events are necessary for some applications as well.

I am working to combine them both in the system i am working on. After going all threads and all events in the past, it's not so easy.

Olivier Lefevre - Re: Why Events Are a Bad Idea  blueArrow
1/24/2004; 9:52:17 AM (reads: 942, responses: 0)
many results show that an hybrid architecture employing partially shared address space execution contexts [...], each running an event-driven processing, provide the highest throughput

Can you provide a couple of references?

Luke Gorrie - Re: Why Events Are a Bad Idea  blueArrow
1/24/2004; 10:41:08 AM (reads: 931, responses: 0)
Note: Erlang supports preemption and priorities. An Erlang process is like a Unix process, except that it's as cheap as a state machine and it has very convenient high-level IPC (which also works over networks).

Works for us.

todd - Re: Why Events Are a Bad Idea  blueArrow
1/24/2004; 12:45:57 PM (reads: 913, responses: 0)
Can you point to a reference? I have brought this up on the erlang list and have read the documentation and have never seen preemption or priority in erlan processes. If you are referencing the process erlang runs in, that's not the same thing.

Peter Van Roy - Re: Why Events Are a Bad Idea  blueArrow
1/24/2004; 1:04:05 PM (reads: 902, responses: 0)
PVR - I assume you refer to the Sun/IBM/Cray VM project? If so have you contacted anyone over there, such as Guy Steele? Do they know about SEAM?

Sorry for the confusion, I was referring to the beginning of the discussion (although it's probably true of the Sun/IBM/Cray project as well).

Mark Evans - Re: Why Events Are a Bad Idea  blueArrow
1/24/2004; 1:17:28 PM (reads: 901, responses: 0)
PVR - Contact them and find out, and if so, try to persuade them of your case. Maybe they already agree!

Luke Gorrie - Re: Why Events Are a Bad Idea  blueArrow
1/24/2004; 1:25:12 PM (reads: 904, responses: 0)
Erlang process priorities are documented in the grab-bag of builtins in the erlang manpage. The BIF is process_flag(priority, low|medium|high). Priorities are pretty esoteric, I've never used them myself. Quality-of-Service type of things would usually be handled using selective receive expressions and flow-control messages I would say.

I don't know if preemption is documented anywhere in particular, but it is fundamental.

Luke Gorrie - Re: Why Events Are a Bad Idea  blueArrow
1/25/2004; 10:46:12 AM (reads: 789, responses: 0)
Also: although event programming, threads, and processes all have their various degrees of trickiness, for a real challenge it's hard to beat juggling synchronous and asynchronous interfaces together using a recursive global event-loop.

That's right, real men program in Emacs Lisp!

Pierre Phaneuf - Re: Why Events Are a Bad Idea  blueArrow
1/26/2004; 7:58:16 AM (reads: 649, responses: 0)
Here is a well known reference:

http://www.cs.princeton.edu/~vivek/flash/

It's somewhat old, and I remember some other papers describing further improvements (of rather low-level scope, like switching from mincore() to a non-blocking variant of sendfile(), or something like that), but it's still much faster than Haboob, SEDA's web server. While they both use an event-driven architecture, they are still rather different and perform at very different levels too.

I find it rather "handy" for the "Why Events Are a Bad Idea" paper to mention Flash as a similar design (which it isn't that much) and then use the "equivalent" Haboob, which has much more shoddy performance, making it not that equivalent after all!

I will give this to todd, event-driven system are easily prone to latency problems, making them a not-so-great idea for many realtime systems. Basically, it's a matter of latency vs throughput, threads providing lower latency and events providing higher throughput.

I think that in CS, too often absolutist statements are made, attempting to distill some ultimate abstraction ("pure this" and "pure that", for example "purely object-oriented" or "pure functional"). Every so often, you see CS persons demonstrate how every single computing problem can be transformed into a caching problem, or an indexing problem or a sorting problem, depending of the field of interest of the author.

This isn't necessarily bad, but you have to realise that this does not apply 1:1 to reality! Academics were clamoring the superiority of microkernels before, saying that the then-current monolithic kernels were a big mistake. The implementers of monolithic kernel claimed in return that microkernels had poor performance and wouldn't really be usable. They were both right! Nowaday, we have modular kernels, that have the performance of monolithic kernels, but with the adaptability and reconfigurability of microkernels. I remember older versions of Windows NT, when some academics were seeing confirmation of their ideas in it (it had a more or less pure microkernel), and their dismay when NT 4.0 moved graphic drivers into the kernel. It wasn't "pure" anymore, but it was now "usable", which has a bit more value in the real world, IMHO.

You see also the phenomenon at an ideological level. For example, RMS (Richard Stallman) and the free software and open source movements. He's quite extremist about his ideas, and they are good ideas, but he's horrible to work with, he's too "pure". But it gives others, more sensible people, some exposure to the idea, and it turns out to change the world in a good way. You probably wouldn't absolutely want RMS to have his full way, but you want some balance between the old oppression of closed source and the nirvana of free software.

Equilibrium, that's the key.

http://www.cs.nyu.edu/rgrimm/teaching/fa03-web/091603.pdf http://www.cs.nyu.edu/rgrimm/teaching/readings/events-threads.pdf

Timothy Downs - Re: Why Events Are a Bad Idea  blueArrow
1/27/2004; 12:32:44 AM (reads: 558, responses: 0)
Pierre Phaneuf, I really liked your post on Equilibrium; very sensible.

From a hobbyist or academic point of view however, it can be fun to run with a particular ideology or framework with as far as it can go. I think this is why Haskell, and other similar "pure" languages are not really popular in the industry, because they are not watered down enough for general consumption. But maybe I'm wrong.

James Hague - Re: Why Events Are a Bad Idea  blueArrow
1/27/2004; 7:03:23 AM (reads: 521, responses: 0)
"I'm a bit disappointed. The threads promoted are cooperative, which offer no additionnal concurrency compared to event-driven systems, and do introduce some of the tricky problems of threads."

Maybe not for webservers and similar highly concurrent applications, but I've come to love cooperative threads for other types of applications. In video games, for example, you have a lot of things going on at once, but you need them all to sync up, so cooperative multitasking is a spectacular win.

Pierre Phaneuf - Re: Why Events Are a Bad Idea  blueArrow
1/27/2004; 12:12:37 PM (reads: 490, responses: 0)
Timothy Downs: yes, I think you're right. That's what I mention in my last paragraph, you need some people to go right there at the far out limit and see what's there. It's just not where you want everyone to go.

James Hague: of course! I do a bit of video games myself (http://quadra.sourceforge.net for a particularly bad example), but when I hear "concurrent", I don't want to see "non-blocking", I want "concurrent".

I felt this clarification was important, because often, you'll see thread proponents saying that events are not concurrent, so if you have an SMP system, it cannot take advantage of it. But saying that, then saying that you can make everything just right by using cooperative threads like GNU Pth (that cannot take advantage of SMP system, precisely because they are not concurrent) is weaseling. Particularly as concurrency is what can make threads really difficult and why they picked cooperative threads.