Effec%ve So*ware. Lecture 6: Non-blocking I/O, C10K, efficient networking. David Šišlák

Similar documents
Outline of Topics. UDP Socket Java Programming. Multicast in Java. Real-time protocol (RTP) XMPP and Jingle protocols. Java I/O and New IO (NIO)

Ch.11 Nonblocking I/O

Outline. Overview. Linux-specific, since kernel 2.6.0

Introduction to NIO: New I/O

Like select() and poll(), epoll can monitor multiple FDs epoll returns readiness information in similar manner to poll() Two main advantages:

Reading and Writing Files

OPERATING SYSTEMS & Network OVERVIEW. 1: OS & Network Overview

The Sun Certified Java Developer Exam with J2SE 1.4 MEHRAN HABIBI, JEREMY PATTERSON, AND TERRY CAMERLENGO

C18: Network Fundamentals and Reliable Sockets

[module lab 1.2] STRUCTURING PROGRAMS IN TASKS

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, part 4 Can't live with it. Can't live without it.

Project 0. Danyang Zhuo CSE sp Sec;on #2

Apple. Massive Scale Deployment / Connectivity. This is not a contribution

Improving Java Network Programming. Brian Runk, 25 Apr 2006

Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland

Parallel Programming Practice

Charlie Garrod Michael Hilton

SUMMARY FUTURES CALLABLES CONCURRENT PROGRAMMING THREAD S ADVANCED CONCEPTS

Introduction to Network Programming using Java

Light & NOS. Dan Li Tsinghua University

C19: User Datagram and Multicast

JAVA SOCKET PROGRAMMING

The ASP Virtual Networking (VNET) Component. 23 July Abstract

Parallel Programming Practice

From Processes to Threads

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Asynchronous Events on Linux

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

LINUX INTERNALS & NETWORKING Weekend Workshop

BENCHMARKING LIBEVENT AGAINST LIBEV

Networking in Java. Java Fundamentals. Dmitri Gabbasov Tartu 2016

Charlie Garrod Bogdan Vasilescu

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Outline. Network Applications: High-Performance Network Servers. Recap: Server Processing Steps. Worker. Recap: Main Thread. Recap: Progress So Far

Joseph Faber Wonderful Talking Machine (1845)

JAVA Network API. 2 - Connection-Oriented vs. Connectionless Communication

Chapter 2 Applications and

Lecture 8: Other IPC Mechanisms. CSC 469H1F Fall 2006 Angela Demke Brown

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

Topics. Lecture 8: Other IPC Mechanisms. Socket IPC. Unix Communication

Networking and Security

SPL - PS12. Reactor and Java New-IO (nio) classes

CSCD 330 Network Programming Spring 2018

Java NIO. Rein Raudjärv Nov 19th 2012 Java Fundamentals

Lecture 3: Socket Programming

Distributed Systems Operation System Support

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

CSCD 330 Network Programming Winter 2019

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

Network Applications: High-Performance Network Servers

May 1, Foundation for Research and Technology - Hellas (FORTH) Institute of Computer Science (ICS) A Sleep-based Communication Mechanism to

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Chapter 8: I/O functions & socket options

Networking: IPv6, UDP and TCP. Network Programming in Java UDP and TCP

Final Concurrency. Oleg October 27, 2014

CPSC 341 OS & Networks. Introduction. Dr. Yingwu Zhu

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

Operating Systems. 17. Sockets. Paul Krzyzanowski. Rutgers University. Spring /6/ Paul Krzyzanowski

CSE 461 Module 11. Connections

Concurrency in Object Oriented Programs 4. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

MegaPipe: A New Programming Interface for Scalable Network I/O

Mark Falco Oracle Coherence Development

Socket programming. Complement for the programming assignment INFO-0010

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

CISC2200 Threads Spring 2015

Principles of Software Construction: Objects, Design, and Concurrency. The Perils of Concurrency, Part 3 Can't live with it. Can't live without it.

Exception-Less System Calls for Event-Driven Servers

Construction d Applications Réparties / Master MIAGE

Protocols SPL/ SPL

Compilers and Assemblers

Interprocess Communication

Scalable IO in Java Doug Lea State University of New York at Oswego

JAVA BYTE IPC: PART 1 DERIVING NIO FROM I/O. Instructor: Prasun Dewan (FB 150,

Advanced Java Programming. Networking

Input / Output. Kevin Webb Swarthmore College April 12, 2018

OpenOnload. Dave Parry VP of Engineering Steve Pope CTO Dave Riddoch Chief Software Architect

Eduardo

A Scalable Event Dispatching Library for Linux Network Servers

Info 408 Distributed Applications programming 2 nd semester of Credits: 5 Lecturer: Dr. Antoun Yaacoub

Advanced Computer Networks. End Host Optimization

CS533 Concepts of Operating Systems. Jonathan Walpole

Networking Basics. network communication.

Outline. Introduction. Survey of Device Driver Management in Real-Time Operating Systems

CSci 4061 Introduction to Operating Systems. (Thread-Basics)

CSCD 330 Network Programming Spring 2018

Collaboration of Tasks

Light: A Scalable, High-performance and Fully-compatible User-level TCP Stack. Dan Li ( 李丹 ) Tsinghua University

A RESTful Java Framework for Asynchronous High-Speed Ingest

31 Network Programming

SDC 2015 Santa Clara

Speeding up Linux TCP/IP with a Fast Packet I/O Framework

The Java ExecutorService (Part 1)

CS377P Programming for Performance Operating System Performance

Much Faster Networking

Kea Messages Manual. Kea Messages Manual

Overview. This Lecture. Interrupts and exceptions Source: ULK ch 4, ELDD ch1, ch2 & ch4. COSC440 Lecture 3: Interrupts 1

Processes and Threads

AUTOBEST: A United AUTOSAR-OS And ARINC 653 Kernel. Alexander Züpke, Marc Bommert, Daniel Lohmann

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Applied Networks & Security

Transcription:

Effec%ve So*ware Lecture 6: Non-blocking I/O, C10K, efficient networking David Šišlák david.sislak@fel.cvut.cz

Network Communica%on OSI Model 10 th April 2017 ESW Lecture 6 2

Network Communica%on HTTP Example 10th April 2017 ESW Lecture 6 3

Network Communica%on Introduc%on» ports 16-bit number» IPv4 32-bit address» IPv6 128-bit address 48-bit or more roumng prefix, 16-bit or less subnet id, 64-bit interface hqp://[1fff:0:a88:85a3::ac1f]:8080/index.html» TCP/UDP connecmon idenmficamon quad src IP, src port, dst IP, dst port 10 th April 2017 ESW Lecture 6 4

C10k Problem» handling a large number of clients (10 000s) at the same Mme (late 90s) concurrent connec%ons at one server requiring efficient scheduling not related to requests per second» somemme known as C1M or C10M problem (nowadays)» approach don t use threading servers like Apache each connecmon handled by own thread/process (pooled but limited) connecmon operamons usually use blocking operamons thread scheduling doesn t scale (+cost for thread context switching) thread scheduling used as packet scheduling use event-driven I/O servers like Nginx do packet scheduling yourself single/mulm-threaded event loop using non-blocking (asynchronous) operamons with event interceptors mul%-core scalability with controlled number of worker threads reuse thread-based data structures, avoid locks (atomics, non-blocking) 10 th April 2017 ESW Lecture 6 5

Threads» processes vs. threads both support concurrent execumon one process has one or mulmple threads threads share the same address space (data and code) context switching between threads is usually less expensive thread inter-communicamon is relamvely efficient using shared memory» JVM a thread executes sequence of code with own stack with frames own local variables own method parameters» thread creamon by subclass of java.lang.thread t.getstacktrace() implementamon of java.lang.runnable 10 th April 2017 ESW Lecture 6 6

JAVA Thread Pool - ExecutorService» concept of thread pooling since 1.5» suitable for execumon of large number of asynchronous tasks e.g. processing of requests in server» reduce overhead with Thread creamon for each task, context switching» interface - java.uml.concurrent.executorservice shutdown(), shutdownnow(), awaitterminamon execute(runnable r) Future<?> submit(runnable r), Future<T> submit(callable<t> c)» java.uml.concurrent.future<t> boolean cancel(boolean mayinterruptifrunning) iscancelled(), isdone() V get(), V get(long Mmeout, TimeUnit unit)» java.uml.concurrent.executors (opmonally with ThreadFactory) newsinglethreadexecutor() newfixedthreadpool(nthreads) newcachedthreadpool() default 60 seconds keep-alive 10 th April 2017 ESW Lecture 6 7

Non-Blocking I/O Approach» polling looping to regularly check status (readiness for I/O) wastes CPU cycles» signals OS generated signals on I/O readiness might leave other processes inconsistent» callbacks pointer to handler funcmon stack deepening issue (callback issuing I/O)» interrupts hardware interrupts in kernel mode» event-based see the next slide 10 th April 2017 ESW Lecture 6 8

Event-Based I/O on Descriptor at OS level» all following methods are system calls allowing waimng for mulmple events» select defined in POSIX (Portable OperaMng System Interface) originally used for blocking I/O passed lists of descriptors cannot be reused in subsequent calls as they are modified by the system call not scalable limited descriptors + iterate over to find the event» poll polled descriptors not limited descriptors can be reused beqer but you smll need iterate over descriptors to find events» epoll Linux only (e.g. Windows has IOCP IO CompleMon Ports) scalable monitored events can be modified while polling (via syscall) returns triggered events directly 10 th April 2017 ESW Lecture 6 9

Epoll» API epoll_create & epoll_create1 inimalize epoll instance epoll_ctl add/modify/remove descriptors to epoll instance epoll_wait wait for events up to Mmeout» modes level triggered wait always returns if event is available event triggered (EPOLLET) readiness returned upon incoming event only (you have to process all pending events before next wait!)» events EPOLLIN, EPOLLOUT, EPOLLPRI EPOLLRDHUP, EPOLLHUP EPOLLERR 10 th April 2017 ESW Lecture 6 10

Epoll Usage epoll structure: inimalizamon: 10 th April 2017 ESW Lecture 6 11

Epoll Event Loop 10 th April 2017 ESW Lecture 6 12

JAVA Networking» java.net package» addressing InetAddress, InetSocketAddress» UDP DatagramPacket DatagramSocket MulMcastSocket» TCP Socket ServerSocket URL URLConnecMon, HQpURLConnecMon 10 th April 2017 ESW Lecture 6 13

JAVA Networking» InetAddress get by name - InetAddress InetAddress.getByName( google.com ) get by address - InetAddress InetAddress.getByAddress(byte ip[]) get special - InetAddress InetAddress.getLocalHost()» InetSocketAddress IP with port complete address new InetSocketAddress(ia, port) InetSocketAddress.createUnresolved( www.google.com, 80) nonspecified address, automamc port new InetSocketAddress(0)» NetworkInterface NetworkInterface.getAll(), NetworkInterface.getByName( eth0 ) methods getdisplayname(), gethardwareaddress(), getinetaddresses() 10 th April 2017 ESW Lecture 6 14

JAVA Networking TCP Client» Socket client end-point of network TCP/IP connecmon is bound to parmcular desmnamon IP and port each TCP/IP connecmon is uniquely idenmfied by its two end-points provides input/output streams 10 th April 2017 ESW Lecture 6 15

JAVA Networking TCP Server» ServerSocket special socket represenmng listening TCP/IP end-point within constructor you specify the port, and opmonally IP where it has to be bound wait for establishing connecmon using method Socket accept() 10 th April 2017 ESW Lecture 6 16

JAVA Networking TCP Server - Example threading server example each handler runs in own thread with blocking I/O 10 th April 2017 ESW Lecture 6 17

JAVA Networking - UDP» DatagramPacket independent, self-contained message sent over the network like packet InetAddress address, int port desmnamon byte data[], int length, int offset SocketAddress sa sender» DatagramSocket sending or receiving point for a packet delivery service can be bound to any available port (using default constructor) connect(inetaddress,int) can sent or receive packets only specified host, if not set in DatagramPacket automamcally fill send(datagrampacket p), receive(datagrampacket p) blocking IO» Mul%castSocket addimonal capabilimes for joining/leaving mulmcast groups, loopback mulmcast IP (IGMP Internet Group Management Protocol) 224.0.0.0 239.255.255.255 10 th April 2017 ESW Lecture 6 18

JAVA Networking - NIO» scalable I/O asynchronous I/O requests and polling» high-speed block-oriented binary and character I/O working including mapping files to the memory, using channels and selectors» Channel is like a block device working with Buffers 10 th April 2017 ESW Lecture 6 19

JAVA NIO - Buffer» java.nio.buffer linear, finite sequence of elements of a specific primimve type ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer, MappedByteBuffer {FileChannel.map( )} not thread safe, mul% mode for the same buffer (both read & write) key propermes 0 <= mark <= posimon <= limit <= capacity capacity numbers of elements, never changing! limit index of the first element that should not be read or wriqen posimon index of the next element to be read or wriqen mark index to which its posimon is set ayer reset() clear() posimon=0, limit=capacity => ready for channel read (put) flip() limit=posimon, posimon=0 => ready for channel write (get) rewind() limit unchanged, posimon=0 => ready for re-reading mark() mark = posimon reset() posimon=mark 10 th April 2017 ESW Lecture 6 20

JAVA NIO - Buffer» write mode channel.read(buf); buf.put( );» read mode channel.write(buf); buf.get(); 10 th April 2017 ESW Lecture 6 21

JAVA NIO - Buffer» java.nio.buffer isreadonly() can be read-only hasarray() is backed by an accessible array (array()) equals(), compareto() compare remainder sequence can be allocated to na%ve memory (see next slide) typical usage Note: compact() bytes between posimon and limit are copied to the beginning of the buffer and put prepare for wrimng again 10 th April 2017 ESW Lecture 6 22

JVM Memory Layout Na%ve Memory JNI native NIO buffers native memory 10 th April 2017 ESW Lecture 6 23

JVM NIO - Direct Buffers» ByteBuffer.allocateDirect( )» stored out of JAVA heap in na%ve memory» allow namve code and Java code to share data without copying useful for file and socket the same memory is passed to kernel during calls» mulmple buffers can share namve memory slice()/duplicate() independent posimon, limit, mark, shared content asreadonlybuffer() read only view of shared content» tuning/tracking -XX:MaxDirectMemorySize=N (default unlimited) -XX:NaMveMemoryTracking=off summary detail -XX:+PrintNMTStaMsMcs Note: usage of heap buffers implies content copy out/in Java heap space due to possible relocamons by GC 10 th April 2017 ESW Lecture 6 24

JAVA Networking - NIO Channel, Selector» one thread works with mul%ple channels at the same %me epoll-based if OS support epoll» Channel cover UDP+TCP network IO, file IO FileChannel from Input/OutputStream or RandomAccessFile DatagramChannel MulMcastChannel (since 1.7) SocketChannel ServerSocketChannel 10 th April 2017 ESW Lecture 6 25

JAVA NIO Channel» Channel read/write at the same Mme (streams are only one-way) always read/write from/to a buffer» FileChannel only blocking support direct buffers, mapped files, locking bulk transfers between channels no copy at all, direct transfer e.g. to socket transferfrom(sourcechannel, int pos, int count) transferto(int pos, int count, dstchannel) 10 th April 2017 ESW Lecture 6 26

JAVA NIO Channel» SocketChannel client end-point of TCP/IP can be configured as non-blocking before connecmng SocketChannel socket.getchannel(); SocketChannel SocketChannel.open(); sch.connect( ) write( ) and read( ) may return without having wriqen/read anything for non-blocking channel» ServerSocketChannel server end-point of TCP/IP can be configured as non-blocking can be created directly using open() or from ServerSocket accept() returns SocketChannel in the same mode 10 th April 2017 ESW Lecture 6 27

JAVA NIO Selector» Selector Selector Selector.open(); only channels in non-blocking mode can be registered channel.configureblocking(false); SelecMonKey channel.register(selector, SelecMonKey.OP_READ); FileChannel doesn t support non-blocking mode» Selec%onKey events you can listen for (can be combined together) OP_CONNECT OP_ACCEPT OP_READ OP_WRITE» events are filled by channel which is ready with operamon 10 th April 2017 ESW Lecture 6 28

JAVA NIO Selector» Selec%onKey returned from register method interest set your configured ops ready set which ops are ready, sk.isreadable(), sk.iswritable(), channel selector opmonal aqached object sk.aqach(object obj); Object sk.aqachment() SelecMonKey channel.register(selector, ops, aqachmentobj); 10 th April 2017 ESW Lecture 6 29

JAVA NIO Selector» Selector with registered one or more channels int select() blocks unml at least one channel is ready int select(long Mmeout) with Mmeout milliseconds int selectnow() doesn t block at all, returns immediately return the number of channels which are ready from the last call Set<SelecMonKey> selector.selectedkeys(); wakeup() different thread can wake up thread blocked in select() close() invalidates selector, channels are not closed 10 th April 2017 ESW Lecture 6 30

JAVA NIO Server Using Mul%ple Reactors Client Client NIOReactors NIOAcceptor Handler Client receive NIOClientHandlers parse send Queued parse tasks ThreadPool Worker thread Worker thread 10 th April 2017 ESW Lecture 6 31

JAVA NIOServer Example 10 th April 2017 ESW Lecture 6 32

JAVA NIOReactor Example 10 th April 2017 ESW Lecture 6 33

JAVA NIOHandler Example 10 th April 2017 ESW Lecture 6 34

JAVA NIOAcceptorHandler Example 10 th April 2017 ESW Lecture 6 35

JAVA NIOClientHandler Example 10 th April 2017 ESW Lecture 6 36

JAVA NIOClientHandler Example 10 th April 2017 ESW Lecture 6 37

JAVA - NIOClientHandler Example 10 th April 2017 ESW Lecture 6 38