CSCI-1680 Network Programming II Rodrigo Fonseca.

Slides:



Advertisements
Similar presentations
Flash: An efficient and portable Web server Authors: Vivek S. Pai, Peter Druschel, Willy Zwaenepoel Presented at the Usenix Technical Conference, June.
Advertisements

CSCI-1680 Network Programming II Rodrigo Fonseca.
1 SEDA: An Architecture for Well- Conditioned, Scalable Internet Services Matt Welsh, David Culler, and Eric Brewer Computer Science Division University.
Fast Communication Firefly RPC Lightweight RPC  CS 614  Tuesday March 13, 2001  Jeff Hoy.
CS533 Concepts of Operating Systems Jonathan Walpole.
Operating Systems High Level View Chapter 1,2. Who is the User? End Users Application Programmers System Programmers Administrators.
SEDA: An Architecture for Well-Conditioned, Scalable Internet Services Matt Welsh, David Culler, and Eric Brewer Computer Science Division University of.
Capriccio: Scalable Threads for Internet Services ( by Behren, Condit, Zhou, Necula, Brewer ) Presented by Alex Sherman and Sarita Bafna.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
Capriccio: Scalable Threads for Internet Services Rob von Behren, Jeremy Condit, Feng Zhou, George Necula and Eric Brewer University of California at Berkeley.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Scripting Languages For Virtual Worlds. Outline Necessary Features Classes, Prototypes, and Mixins Static vs. Dynamic Typing Concurrency Versioning Distribution.
3.5 Interprocess Communication
Why Events Are a Bad Idea (for high-concurrency servers) Author: Rob von Behren, Jeremy Condit and Eric Brewer Presenter: Wenhao Xu Other References: [1]
Threads CSCI 444/544 Operating Systems Fall 2008.
Operational Analysis L. Grewe. Operational Analysis Relationships that do not require any assumptions about the distribution of service times or inter-arrival.
Concurrency, Threads, and Events Robbert van Renesse.
CS533 Concepts of Operating Systems Class 3 Integrated Task and Stack Management.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories
Why Events Are A Bad Idea (for high-concurrency servers) Rob von Behren, Jeremy Condit and Eric Brewer University of California at Berkeley
SEDA: An Architecture for Well-Conditioned, Scalable Internet Services
SEDA – Staged Event-Driven Architecture
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
Network Applications: Async Servers and Operational Analysis Y. Richard Yang 10/03/2013.
Flash An efficient and portable Web server. Today’s paper, FLASH Quite old (1999) Reading old papers gives us lessons We can see which solution among.
Servers: Concurrency and Performance Jeff Chase Duke University.
LiNK: An Operating System Architecture for Network Processors Steve Muir, Jonathan Smith Princeton University, University of Pennsylvania
Hardware Definitions –Port: Point of connection –Bus: Interface Daisy Chain (A=>B=>…=>X) Shared Direct Device Access –Controller: Device Electronics –Registers:
Node.js - What is Node.js? -
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
SEDA: An Architecture for Well-Conditioned, Scalable Internet Services Presented by Changdae Kim and Jaeung Han OFFENCE.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
Scalable Internet Services Cluster Lessons and Architecture Design for Scalable Services BR01, TACC, Porcupine, SEDA and Capriccio.
Background: I/O Concurrency Brad Karp UCL Computer Science CS GZ03 / M030 2 nd October, 2008.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
1 Combining Events and Threads for Scalable Network Services Peng Li and Steve Zdancewic University of Pennsylvania PLDI 2007, San Diego.
Chat Room App Logan Linn Network Application Design Fall 2010.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
LRPC Firefly RPC, Lightweight RPC, Winsock Direct and VIA.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
The Mach System Silberschatz et al Presented By Anjana Venkat.
CSE 60641: Operating Systems Next topic: CPU (Process/threads/scheduling, synchronization and deadlocks) –Why threads are a bad idea (for most purposes).
6.894: Distributed Operating System Engineering Lecturers: Frans Kaashoek Robert Morris
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
By: Rob von Behren, Jeremy Condit and Eric Brewer 2003 Presenter: Farnoosh MoshirFatemi Jan
SEDA An architecture for Well-Conditioned, scalable Internet Services Matt Welsh, David Culler, and Eric Brewer University of California, Berkeley Symposium.
Threads versus Events CSE451 Andrew Whitaker. This Class Threads vs. events is an ongoing debate  So, neat-and-tidy answers aren’t necessarily available.
1 Why Events Are A Bad Idea (for high-concurrency servers) By Rob von Behren, Jeremy Condit and Eric Brewer (May 2003) CS533 – Spring 2006 – DONG, QIN.
Node.Js 1. 2 Contents About Node.Js Web requirement latest trends Introduction Simple web server creation in Node.Js Dynamic Web pages Dynamic web page.
CS533 Concepts of Operating Systems Jonathan Walpole.
Background Computer System Architectures Computer System Software.
Network Applications: High-performance Server Design: Async Servers/Operational Analysis Y. Richard Yang 2/24/2016.
Introduction to Node.js® Jitendra Kumar Patel Saturday, January 31, 2015.
Node.Js Server Side Javascript
CSE 775 – Distributed Objects Submitted by: Arpit Kothari
Node.Js Server Side Javascript
2017, Fall Pusan National University Ki-Joune Li
Capriccio: Scalable Threads for Internet Services
Why Threads Are A Bad Idea (for most purposes)
Why Events Are a Bad Idea (for high concurrency servers)
Why Threads Are A Bad Idea (for most purposes)
Why Threads Are A Bad Idea (for most purposes)
Presentation transcript:

CSCI-1680 Network Programming II Rodrigo Fonseca

Today Network programming –Programming Paradigms –Programming libraries Final project

Low-level Sockets Address Family AF_PACKET –Socket type: SOCK_RAW See link-layer (Ethernet) headers. Can send broadcast on a LAN. Can get/create non-IP packets –Socket type: SOCK_DGRAM See IP headers. Can get protocols other than TCP/UDP: ICMP, SCTP, DCCP, your own… Can cook your own IP packets –Must have root privileges to play with these

Building High Performance Servers

The need for concurrency How to improve throughput? –Decrease latency (throughput α 1/latency) –Hard to do! Optimize code (this you should try!) Faster processor (no luck here, recently) Speed of light isn’t changing anytime soon… Disks have to deal with things like inertia! –Do multiple things at once Concurrency –Allows overlapping of computation and I/O –Allows use of multiple cores, machines

High-performance Servers Common Patterns Figures from Pai, et al., 1999 “Flash: An efficient and portable Web server” Multiple processes Multiple Threads Single Process Event Driven Single Process Event Driven with Helpers

Threads Usual model for achieving concurrency Uniform abstraction for single and multiple cores Concurrency with locks/mutexes –Threads may block, hold locks for long time Easy to reason about –Each thread has own stack Strong support from OS, libraries, debuggers Traditionally, problems with more than a few 100 threads –Memory overhead, O(n) operations

Performance, Thread-based server From Welsh, et al., SOSP 2001 “SEDA: An Architecture for Well-Conditioned, Scalable Internet Services

Events Small number of threads, one per CPU Threads do one thing: while(1) { get event from queue Handle event to completion } Events are network, I/O readiness and completion, timers, signals –Remember select()? Assume event handlers never block –Helper threads handle blocking calls, like disk I/O

Events Many works in the early 2000’s claimed that events are needed for high performance servers –E.g., Flash, thttpd, Zeus, JAWS web servers Indeed, many of today’s fastest servers are event-driven –E.g., OKCupid, lighttpd, nginx, tornado Lighttpd: “Its event-driven architecture is optimized for a large number of parallel connections” Tornado: “Because it is non-blocking and uses epoll, it can handle thousands of simultaneous standing connections”epoll Tornado: “Because it is non-blocking and uses epoll, it can handle thousands of simultaneous standing connections”epoll

Performance, Event-Driven Web server From Welsh, et al., SOSP 2001 “SEDA: An Architecture for Well-Conditioned, Scalable Internet Services

Flash Web Server Pai, Drushel, Zwaenepoel, 1999 Influential work Compared four architectures –Multi-process servers –Multi-threaded servers –Single-process event-driven –Asymmetric Multi-process event driven AMPED was the fastest

Events (cont) Highly efficient code –Little or no switching overhead –Easy concurrency control Common complaint: hard to program and reason about –For people and tools Main reason: stack ripping

Events criticism: control flow Accept Conn. Write Response Read File Read Request Pin Cache Web Server Exit ThreadsEvents thread_main(int sock) { struct session s; accept_conn(sock, &s); read_request(&s); pin_cache(&s); write_response(&s); unpin(&s); } pin_cache(struct session *s) { pin(&s); if( !in_cache(&s) ) read_file(&s); } CacheHandler(struct session *s) { pin(s); if( !in_cache(s) ) ReadFileHandler.enqueue(s); else ResponseHandler.enqueue(s); } RequestHandler(struct session *s) { …; CacheHandler.enqueue(s); }... ExitHandlerr(struct session *s) { …; unpin(&s); free_session(s); } AcceptHandler(event e) { struct session *s = new_session(e); RequestHandler.enqueue(s); } Events obscure control flow –For programmers and tools

Events criticism: Exceptions Exceptions complicate control flow –Harder to understand program flow –Cause bugs in cleanup code Accept Conn. Write Response Read File Read Request Pin Cache Web Server Exit ThreadsEvents thread_main(int sock) { struct session s; accept_conn(sock, &s); if( !read_request(&s) ) return; pin_cache(&s); write_response(&s); unpin(&s); } pin_cache(struct session *s) { pin(&s); if( !in_cache(&s) ) read_file(&s); } CacheHandler(struct session *s) { pin(s); if( !in_cache(s) ) ReadFileHandler.enqueue(s); else ResponseHandler.enqueue(s); } RequestHandler(struct session *s) { …; if( error ) return; CacheHandler.enqueue(s); }... ExitHandlerr(struct session *s) { …; unpin(&s); free_session(s); } AcceptHandler(event e) { struct session *s = new_session(e); RequestHandler.enqueue(s); }

Events criticism: State Management ThreadsEvents thread_main(int sock) { struct session s; accept_conn(sock, &s); if( !read_request(&s) ) return; pin_cache(&s); write_response(&s); unpin(&s); } pin_cache(struct session *s) { pin(&s); if( !in_cache(&s) ) read_file(&s); } CacheHandler(struct session *s) { pin(s); if( !in_cache(s) ) ReadFileHandler.enqueue(s); else ResponseHandler.enqueue(s); } RequestHandler(struct session *s) { …; if( error ) return; CacheHandler.enqueue(s); }... ExitHandlerr(struct session *s) { …; unpin(&s); free_session(s); } AcceptHandler(event e) { struct session *s = new_session(e); RequestHandler.enqueue(s); } Accept Conn. Write Response Read File Read Request Pin Cache Web Server Exit Events require manual state management Hard to know when to free –Use GC or risk bugs

Usual Arguments Events: –Hard to program (stack ripping) –Easy to deal with concurrency (cooperative task management) Shared state is more explicit –High performance (low overhead, no switching, no blocking) Threads –Easy to reason about flow, state (automatic stack management) –Hard to deal with concurrency (preemptive task management) Everything is shared –Lower performance (thread switching cost, memory overhead)

Capriccio (2003) Showed threads can perform as well as events –Avoid O(n) operations –Cooperative lightweight user-level threads (still one kernel thread per core) –Asynchronous I/O Handled by the library –Variable-length stacks –The thread library runs an event-based system underneath!

Artificial Dichotomy! Old debate! Lauer and Needham, 78 –Duality between process-based and message- passing –Updated by the Capriccio folks, 2003 Performance should be similar –No inherent reason for threads to be worse –Implementation is key ThreadsEvents Monitors Exported functions Call/return and fork/join Wait on condition variable Event handler & queue Events accepted Send message / await reply Wait for new messages

Artificial Dichotomy Threads –Preemptive multitasking –Automatic stack management Events –Cooperative multitasking –Manual stack management (stack ripping) Adya, 2002: you can choose your features! –They show that you can have cooperative multitasking with automatic stack managment Adya, A. et al., “Cooperative Task Management without Manual Stack Managementor, Event-driven Programming is Not the Opposite of Threaded Programming

Threads vs. Events Today you still have to mostly choose either style (complete packages) –Thread-based servers very dependent on OS, threading libraries Some promising directions! –TAME allows you to write sequential C++ code (with some annotations), converts it into event- based –Scala (oo/functional language that runs on the JVM) makes threaded and event-based code look almost identical

Popular Event-Based Frameworks libevent libasync (SFS, SFS-light) Javascript –All browser code –Node.js at the server side GUI programming

Some available libraries With material from Igor Ganichev

Python Rich standard library –url/http/ftp/pop/imap/smtp/telnet –SocketServer, HTTPServer, DocXMLRPCServer, etc Twisted –Very popular –Has a lot of stuff, but quite modular –Event-driven, many design patterns. Steep learning curve… –Well maintained and documented

Java Mature RPC library: RMI River: RMI + service discovery, mobile code Java.NIO –High-level wrapping of OS primitives Select -> Selector. Socket -> Channel –Good, efficient buffer abstraction Jetty –Extensible, event-driven framework –High-performance –Avoid unnecessary copies –Other side doesn’t have to be in Java

C Sockets! Direct access to what the OS provides Libevent –Simple, somewhat portable abstraction of select() with uniform access to events: I/O, timers, signals –Supports /dev/poll, kqueue(2), event ports, select(2), poll(2) and epoll(4). –Well maintained, actively developed –Behind many very high-performance servers Memcached

C++ Boost.ASIO –Clean, lightweight, portable abstraction of sockets and other features –Not a lot of higher-level protocol support –Has support for both synchronous and asynchronous operations, threads (from other parts of Boost) Others: ACE, POCO

ICE Cross-language middleware + framework –Think twisted + protocol buffers Open source but owned by a company SSL, sync/async, threads, resource allocation, firewall traversal, event distribution, fault tolerance Supports many languages –C++, Java,.NET-languages (such as C# or Visual Basic), Objective-C, Python, PHP, and Ruby

Other “cool” approaches Erlang, Scala, Objective C –Support the Actor model: program is a bunch of actors sending messages to each other –Naturally extends to multi-core and multiple machines, as sending messages is the same Go –Built for concurrency, uses ‘Goroutines’, no shared state –“Don’t share memory to communicate, communicate to share memory”

Node.js Javascript server framework Leverages highly efficient Chrome V8 Javascript JIT runtime Completely event-based Many high-level libraries var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8124, " "); console.log('Server running at

Final Assignment

Final Project Tethering IP over 3G Problem: Laptop in need of internet, no Wi-Fi available. On hand: Smartphone with 3G connection. Native applications don’t always allow custom network programming. –iOS App Store guidelines.

Custom Tethering Solution Websockets to the rescue! –Implemented in browsers. –Bi-directional, full-duplex connection over a single TCP socket. –Modern smartphone browsers have implemented websockets.

Implementation

Some questions How to connect phone to laptop? How to encode data? Virtual interfaces: TUN or TAP? Client: setting up routes Server: what to do with the packets you receive?

Some Resources TUN/TAP Interfaces –TunTap package for Mac OSX Websocket Server –Twisted NAT –Scapy Base64 Encoding –