Event-Driven Programming Vivek Pai Dec 5, 2002 GedankenBits What does a raw bit cost? IDE 40GB: $100 120GB: $180 USB Pen: $38 FireWire: What cost? NAS 320GB: $3400 (Dell) 7.5TB: $300K (Zambeel) 32MB 60GB: $205 120GB: $280 USB: does a managed bit What does a person cost? $100K salary + $50K benefits/overhead 40GB: $140 120GB: $218 2 Extra Project We need to agree on it One-page proposal Idea, implementation, measurement Report (~10 pages) due on Dean’s Date Max boost of 2/3 of a letter grade 3 Project 6 Several goals Performance improvement via caching Dynamic adjustment to load Master/slave or symmetric programming Due on Dean’s Date Extra credit: 4 points (20 base) 4 Another Random Aside You may want to read Flash: An Efficient and Portable Web Server Available from my home page Caveat: far more complicated than Project 5 5 Official Goals Discuss the difference between standard programming styles and event-driven programming Show the difference between structuring servers using processes and application-level multiplexing Discuss the benefits and drawbacks of each approach 6 What Is An Event? Some kind of notification Interrupts Signals Polling (via poll/select/etc) Callback (via function pointer) Similarities? 7 “Reactive” Environments Windowing systems Network-aware programs Drivers of all sorts 8 Traditional Environments One thing going on at a time Finish that thing, go on to next Any step can block indefinitely Resumption from blocking is simple – OS provided 9 What Goes On In A Web Browser? Drawing the current page Inline images Translating machine names to IP addresses Launching connections Sending requests Getting piecemeal responses, drawing images User clicking to next link 10 Threads Versus Events One stack versus many stacks What happens on blocking operations Parallelism Shared variables State 11 Let’s Think Of States How many possible states are there? Take all pieces of information Decide valid range for all pieces Enumerate Can we reduce states? Some combinations invalid Still, lots of states 12 Really Reducing States Take all major pieces of program Add extra tags to state What do tags look like? Position Count # 13 State Examples If-then-else 3 For 1 states: start, then-clause, else-clause loop state + count Why do we care? Resuming at the right state 14 Remember This Diagram? Accept Conn Read Request Find File Send Header Read File Send Data end 15 Structure of Event-Driven Programs Lots of state machines Maintaining information about each one Some way of moving through states As few restrictions as possible on timing 16 The Real Structure While (1) Get event Dispatch event Or, while loop in library Event handlers in main program 17 Delays Are delays possible? Interrupt handlers – generally not Otherwise? Depends on event rate How to avoid delays? More events – asynchronous operations What happens if no async support? Fake it 18 Select( ) System Call int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); FD_SET(fd, &fdset); FD_CLR(fd, &fdset); FD_ISSET(fd, &fdset); FD_ZERO(&fdset); 19 20 Select Description DESCRIPTION Select() examines the I/O descriptor sets whose addresses are passed in readfds, writefds, and exceptfds to see if some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively. The only exceptional condition detectable is out-of-band data received on a socket. The first nfds descriptors are checked in each set; i.e., the descriptors from 0 through nfds-1 in the descriptor sets are examined. On return, select() replaces the given descriptor sets with subsets consisting of those descriptors that are ready for the requested operation. Select() returns the total number of ready descriptors in all the sets. The descriptor sets are stored as bit fields in arrays of integers. The following macros are provided for manipulating such descriptor sets: FD_ZERO(&fdset) initializes a descriptor set fdset to the null set. FD_SET(fd, &fdset) includes a particular descriptor fd in fdset. FD_CLR(fd, &fdset) removes fd from fdset. FD_ISSET(fd, &fdset) is non- zero if fd is a member of fdset, zero otherwise. The behavior of these macros is undefined if a descriptor value is less than zero or greater than or equal to FD_SETSIZE, which is normally at least equal to the maximum number of descriptors supported by the system.21 Blocking Steps Disk Blocking Accept Conn Read Request Find File Send Header Read File Send Data end Network Blocking 22 Overcoming Disk Blocking States Accept Conn Read Request Find File Helper Send Header Read File Send Data end Helper 23 New Architecture - AMPED Asymmetric Multiple Process Event Driven Accept Conn Read Request Find File Send Header Read File Send Data Event Dispatcher Helper 1 Helper 2 Helper N Helpers are threads or processes 24 Caches in Flash Web Server Accept Conn Read Request Find File Send Header Read File Send Data end Pathname Response Mapped Translation Header File Cache Cache Cache Helper Helper 25