Threads versus Events CSE451 Andrew Whitaker. This Class Threads vs. events is an ongoing debate  So, neat-and-tidy answers aren’t necessarily available.

Slides:



Advertisements
Similar presentations
Categories of I/O Devices
Advertisements

I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
Flash: An efficient and portable Web server Authors: Vivek S. Pai, Peter Druschel, Willy Zwaenepoel Presented at the Usenix Technical Conference, June.
1 SEDA: An Architecture for Well- Conditioned, Scalable Internet Services Matt Welsh, David Culler, and Eric Brewer Computer Science Division University.
CS533 Concepts of Operating Systems Jonathan Walpole.
SEDA: An Architecture for Well- Conditioned, Scalable Internet Services Matt Welsh, David Culler, and Eric Brewer Computer Science Division University.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Why Events Are A Bad Idea (for high-concurrency servers) By Rob von Behren, Jeremy Condit and Eric Brewer.
Capriccio: Scalable Threads for Internet Services ( by Behren, Condit, Zhou, Necula, Brewer ) Presented by Alex Sherman and Sarita Bafna.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
Capriccio: Scalable Threads for Internet Services (von Behren) Kenneth Chiu.
Event-Driven Programming Vivek Pai Dec 5, GedankenBits  What does a raw bit cost?  IDE  40GB: $100  120GB: $180  32MB USB Pen: $38  FireWire:
1 Data Communications and Networking Socket Programming Part II: Design of Server Software Reference: Internetworking with TCP/IP, Volume III Client-Server.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
“Why Events are a Bad Idea (For high-concurrency servers)” Paper by Rob von Behren, Jeremy Condit and Eric Brewer, May 2003 Presentation by Loren Davis,
Concurrency, Threads, and Events Robbert van Renesse.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
SEDA: An Architecture for Well-Conditioned, Scalable Internet Services
Apache Architecture. How do we measure performance? Benchmarks –Requests per Second –Bandwidth –Latency –Concurrency (Scalability)
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
SEDA: An Architecture for Well-Conditioned, Scalable Internet Services
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Rapid Development of High Performance Servers Khaled ElMeleegy Alan Cox Willy Zwaenepoel.
Socket Models Different ways to manage your connections.
Background: I/O Concurrency Brad Karp UCL Computer Science CS GZ03 / M030 2 nd October, 2008.
Background: Operating Systems Brad Karp UCL Computer Science CS GZ03 / M th November, 2008.
1 COMP/ELEC 429/556 Introduction to Computer Networks Creating a Network Application Some slides used with permissions from Edward W. Knightly, T. S. Eugene.
CS333 Intro to Operating Systems Jonathan Walpole.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
CSE 451: Operating Systems Winter 2015 Module 5 1 / 2 User-Level Threads & Scheduler Activations Mark Zbikowski 476 Allen Center.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
CSE 60641: Operating Systems Next topic: CPU (Process/threads/scheduling, synchronization and deadlocks) –Why threads are a bad idea (for most purposes).
1 Cooperative Task Management without Manual Stack Management or Event-driven Programming is not the Opposite of Threaded Programming Atul Adya, Jon Howell,
LAIO: Lazy Asynchronous I/O For Event Driven Servers Khaled Elmeleegy Alan L. Cox.
Project 2 Overview (Threads in Practice) CSE451 Andrew Whitaker.
By: Rob von Behren, Jeremy Condit and Eric Brewer 2003 Presenter: Farnoosh MoshirFatemi Jan
CS533 Concepts of Operating Systems Jonathan Walpole.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Operating System Concepts
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Multithreading vs. Event Driven in Code Development of High Performance Servers.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Java Thread Programming
Tutorial 2: Homework 1 and Project 1
Introduction to threads
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Chapter 4: Threads.
Node.Js Server Side Javascript
Scheduler activations
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Multithreaded Programming
Lecture 21 Concurrency Introduction
Node.Js Server Side Javascript
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Why Threads Are A Bad Idea (for most purposes)
CS510 Operating System Foundations
CSE 153 Design of Operating Systems Winter 2019
Why Threads Are A Bad Idea (for most purposes)
Why Threads Are A Bad Idea (for most purposes)
CS703 – Advanced Operating Systems
Presentation transcript:

Threads versus Events CSE451 Andrew Whitaker

This Class Threads vs. events is an ongoing debate  So, neat-and-tidy answers aren’t necessarily available Our goals:  Understand the debate  Analyze arguments  Form our own opinion

A Brief History of a Debate 1995: Why Threads are a Bad Idea (for most purposes)  John Ousterhout, (UC Berkeley, Sun Labs) 2001: SEDA: An Architecture for Well- Conditioned, Scalable Internet Services  Staged, Event-driven Architecture  M. Welsh, D. Culler, and Eric Brewer (UC Berkeley) 2003: Why Events are a Bad Idea (for high- concurrency servers)  R. van Behren, J. Condit, Eric Brewer (UC Berkeley)

Background Problem: How do scale up servers to handle many simultaneous requests Server workloads have a lot of I/O, a little CPU Send HTTP request over network Read and parse HTTP request Read file from disk Invoke write syscall Send response over network time HTTP processing

Quick Example Q: Suppose it takes 10 seconds for a web server to transfer a file to the client. Of this time, 10 milliseconds is dedicated to CPU processing. How many simultaneous requests do we need to keep the CPU fully utilized? A: 1000

Concurrency Strategy #1: Thread- per-Request Run each web request in its own thread  Assuming kernel threads, blocking I/O operations only stall one thread

Review: Web Server Worker Thread 1.Read from socket to extract request URI (e.g., index.html) 2.Read desired file into a buffer 3.Write file over the socket 4.Close the socket while (true) { } These are blocking calls

Concurrency Strategy #2: Event- driven Execution Use a single thread for all requests Use non-blocking I/O  Replace blocking I/O with calls that return immediately  Program is notified about interesting I/O events This is philosophically similar to hardware interrupts  “Tell me when something interesting happens”

Event Loop Pseudocode while (true) { // Ask the OS for Sockets with active I/O; // this blocks if no socket is active Socket sock = getActiveSocket(); if (sock.isReadable()) handleReadEvent(sock); if (sock.isWriteable()) handleWriteEvent(sock); } In a pure event system, this is the only thread

Details: UNIX Select System Call Select takes three arrays of file descriptors  Reads, writes, and exceptional events  Note that file descriptors may represent an open file or a socket Select returns the subset of each array that is ready for reading, writing, or has an exceptional condition  Or, blocks if no FDs are active int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

Event Handler Pseudocode private Hashtable perSocketState; // this code *never* blocks public void handleRead(Socket sock) { // lookup state about this socket SocketState state = perSocketState.get(sock); state.bytesRead += sock.nonBlockingRead(state.buffer,state.bytesRead); // if we’ve read the entire request, send a response if (state.bytesRead >= REQUEST_LENGTH) { sendResponse(sock); } else {} // do nothing; wait for another event }

Can You Think of a System that Works This Way? GUI frameworks (like Swing)  Single event-handling thread  User programs install “event listeners” to receive callbacks Event Loop Event Handlers

Why Events? A potentially simpler programming model  No concurrency, locks, conditional variables, etc. Less memory footprint  A single stack (at most, a stack per CPU) More scalable?

Cut to Ousterhout

Critiquing Ousterhout Events don’t handle true CPU concurrency  We can fix this with an event-loop per processor But, then we have multiple threads… Programming model becomes complex if event handlers do not run to completion  This requires “stack ripping” to maintain state

State Transition Diagram for the Web Server Reading request Reading File Writing File In a threaded system, state transitions are maintained by the stack In an event system, we must manually maintain the state of each request  “Stack ripping”

Andrew’s Opinion: Code Understanding Event systems are difficult to read / understand  Which events happen in which order? while (true) { // Ask the OS for Sockets with active I/O; // this blocks if no socket is active Socket sock = getActiveSocket(); if (sock.isReadable()) handleReadEvent(sock); if (sock.isWriteable()) handleWriteEvent(sock); }

Threads vs Events in Practice Both are used in production systems  Apache is multi-threaded / multi-process  Lighttpd is event-driven Which is better? Why?

The Devil is in the (Implementation) Details Many systems did not support kernel threads  Making events more attractive Support for non-blocking I/O is uneven  e.g., UNIX/Linux do not support non-blocking file I/O The UNIX select command has poor scalability (Banga et al, 1998)  But, more recent alternatives to select are much more scalable (Linux epoll) Thread packages often do not scale (Welsh, 2001)  But, optimizations can vastly improve thread scalability Van Behren 2003, Linux 2.6 and NPTL

So, What Can We Say For Sure? Event-based systems are harder to program  Requires “stack ripping”  Harder to track control flow  Possible exception: run-to-completion event handlers (e.g., Java Swing GUIs) Event systems use fewer resources  At most a stack per processor Event systems require less synchronization  But, they still require some on multi-processor systems (which is every system!)  Open question: is this is any easier to program?

Software Engineering Issues Usually, choice of concurrency model is made for “historical reasons” Unfortunately, changing concurrency models is very difficult Use threads Use events Begin project