20110828 non blocking socket

38
Non Blocking Socket Hu Zi Ming [email protected] 201l-08-28 1 / 16 Non Blocking Socket N

Upload: linuxfb

Post on 12-May-2015

1.224 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 20110828 Non blocking socket

Non Blocking Socket

Hu Zi Ming

[email protected]

201l-08-28

1 / 16Non Blocking Socket

N

Page 2: 20110828 Non blocking socket

Outline

1 Normal ServerFunction FlowSimple Stream Server

2 The C10K Problem

3 High Performance Web Server

2 / 16Non Blocking Socket

N

Page 3: 20110828 Non blocking socket

Function Flow

S: Get available address

S: Create the socket file descriptor

S: Bind the descriptor to specified port

S: Listen on the port (address)

C: Create the socket

C: Connect to the server

S: Accept the connection

Data exchange

3 / 16Non Blocking Socket

N

Page 4: 20110828 Non blocking socket

Function Flow

S: Get available address

S: Create the socket file descriptor

S: Bind the descriptor to specified port

S: Listen on the port (address)

C: Create the socket

C: Connect to the server

S: Accept the connection

Data exchange

3 / 16Non Blocking Socket

N

Page 5: 20110828 Non blocking socket

Function Flow

S: Get available address

S: Create the socket file descriptor

S: Bind the descriptor to specified port

S: Listen on the port (address)

C: Create the socket

C: Connect to the server

S: Accept the connection

Data exchange

3 / 16Non Blocking Socket

N

Page 6: 20110828 Non blocking socket

Function Flow

S: Get available address

S: Create the socket file descriptor

S: Bind the descriptor to specified port

S: Listen on the port (address)

C: Create the socket

C: Connect to the server

S: Accept the connection

Data exchange

3 / 16Non Blocking Socket

N

Page 7: 20110828 Non blocking socket

Simple Stream Server

server.py

import socket

PORT = 8080HTTP_RESP = ’HTTP/1.1 200 OK\r\n’HTTP_RESP += ’Connection:close1\r\n’HTTP_RESP += ’Content-Type: text/html\r\n’HTTP_RESP += ’Content-Length: 21\r\n’HTTP_RESP += ’\r\n<h1>hello, world</h1>’

available_sock = socket.getaddrinfo(None, PORT, 0, socket.SOCK_STREAM)[0]s = socket.socket(available_sock[0], available_sock[1])s.bind((available_sock[3], PORT))print ’Binded at’, available_sock[4][0]s.listen(10)

while True:conn, addr = s.accept()print ’Connected by’, addrconn.send(HTTP_RESP)conn.close()

4 / 16Non Blocking Socket

N

Page 8: 20110828 Non blocking socket

Outline

1 Normal Server

2 The C10K ProblemThe C10K ProblemProgramming Model

3 High Performance Web Server

5 / 16Non Blocking Socket

N

Page 9: 20110828 Non blocking socket

Huston, We Have a Problem

As web developed in high speed

More servers need to handle 10 thousand clients simultaneously

Which names this problem: C10K

Classical server can not handle this scenario well

Here comes the new models

6 / 16Non Blocking Socket

N

Page 10: 20110828 Non blocking socket

Huston, We Have a Problem

As web developed in high speed

More servers need to handle 10 thousand clients simultaneously

Which names this problem: C10K

Classical server can not handle this scenario well

Here comes the new models

6 / 16Non Blocking Socket

N

Page 11: 20110828 Non blocking socket

Huston, We Have a Problem

As web developed in high speed

More servers need to handle 10 thousand clients simultaneously

Which names this problem: C10K

Classical server can not handle this scenario well

Here comes the new models

6 / 16Non Blocking Socket

N

Page 12: 20110828 Non blocking socket

Programming Model

One client with each server thread, synchronous blocking IO.

Classical model, used in Apache

Many clients with each thread

Asynchronous blocking IO and level-triggered readinessnotification

Many clients with each thread

Asynchronous blocking IO and edge-triggered readinessnotification

Many clients with each thread

Asynchronous non-blocking IO.

7 / 16Non Blocking Socket

N

Page 13: 20110828 Non blocking socket

Programming Model

One client with each server thread, synchronous blocking IO.

Classical model, used in Apache

Many clients with each thread

Asynchronous blocking IO and level-triggered readinessnotification

Many clients with each thread

Asynchronous blocking IO and edge-triggered readinessnotification

Many clients with each thread

Asynchronous non-blocking IO.

7 / 16Non Blocking Socket

N

Page 14: 20110828 Non blocking socket

Programming Model

One client with each server thread, synchronous blocking IO.

Classical model, used in Apache

Many clients with each thread

Asynchronous blocking IO and level-triggered readinessnotification

Many clients with each thread

Asynchronous blocking IO and edge-triggered readinessnotification

Many clients with each thread

Asynchronous non-blocking IO.

7 / 16Non Blocking Socket

N

Page 15: 20110828 Non blocking socket

Programming Model

One client with each server thread, synchronous blocking IO.

Classical model, used in Apache

Many clients with each thread

Asynchronous blocking IO and level-triggered readinessnotification

Many clients with each thread

Asynchronous blocking IO and edge-triggered readinessnotification

Many clients with each thread

Asynchronous non-blocking IO.

7 / 16Non Blocking Socket

N

Page 16: 20110828 Non blocking socket

Outline

1 Normal Server

2 The C10K Problem

3 High Performance Web ServerSynchronous Blocking IO with Multi Thread/ProcessLevel-triggered (LT) Readiness NotificationEdge-triggered (ET) Readiness NotificationAsynchronous Non-Blocking IO

8 / 16Non Blocking Socket

N

Page 17: 20110828 Non blocking socket

Synchronous Blocking IO w/MultiThread/Process

Easy for programming

Can be used for complex logic

Good for interactive and persistent connection

Consume too much memory

9 / 16Non Blocking Socket

N

Page 18: 20110828 Non blocking socket

Synchronous Blocking IO w/MultiThread/Process

Easy for programming

Can be used for complex logic

Good for interactive and persistent connection

Consume too much memory

9 / 16Non Blocking Socket

N

Page 19: 20110828 Non blocking socket

Level-triggered (LT) Readiness Notification

Triggered by state

Notifications will be constantly sent if state not change

select(2) and poll(2) are working in this mode

epoll(4) can be configured to work in this mode

Support both blocking and non-blocking socket

10 / 16Non Blocking Socket

N

Page 20: 20110828 Non blocking socket

Level-triggered (LT) Readiness Notification

Triggered by state

Notifications will be constantly sent if state not change

select(2) and poll(2) are working in this mode

epoll(4) can be configured to work in this mode

Support both blocking and non-blocking socket

10 / 16Non Blocking Socket

N

Page 21: 20110828 Non blocking socket

select() & poll()

Both are working in LT mode

Max acceptable fd num of select() is FD SETSIZE

Which is defined in the kernel

User defined array is used in poll() to break this limitation

The whole fd set will be scanned, and. . .

The whole fd set will be copied between user and kernel mode

So, the performance will be reduced as set increased

11 / 16Non Blocking Socket

N

Page 22: 20110828 Non blocking socket

select() & poll()

Both are working in LT mode

Max acceptable fd num of select() is FD SETSIZE

Which is defined in the kernel

User defined array is used in poll() to break this limitation

The whole fd set will be scanned, and. . .

The whole fd set will be copied between user and kernel mode

So, the performance will be reduced as set increased

11 / 16Non Blocking Socket

N

Page 23: 20110828 Non blocking socket

select() & poll()

Both are working in LT mode

Max acceptable fd num of select() is FD SETSIZE

Which is defined in the kernel

User defined array is used in poll() to break this limitation

The whole fd set will be scanned, and. . .

The whole fd set will be copied between user and kernel mode

So, the performance will be reduced as set increased

11 / 16Non Blocking Socket

N

Page 24: 20110828 Non blocking socket

select() & poll()

Both are working in LT mode

Max acceptable fd num of select() is FD SETSIZE

Which is defined in the kernel

User defined array is used in poll() to break this limitation

The whole fd set will be scanned, and. . .

The whole fd set will be copied between user and kernel mode

So, the performance will be reduced as set increased

11 / 16Non Blocking Socket

N

Page 25: 20110828 Non blocking socket

select() & poll()

Both are working in LT mode

Max acceptable fd num of select() is FD SETSIZE

Which is defined in the kernel

User defined array is used in poll() to break this limitation

The whole fd set will be scanned, and. . .

The whole fd set will be copied between user and kernel mode

So, the performance will be reduced as set increased

11 / 16Non Blocking Socket

N

Page 26: 20110828 Non blocking socket

Edge-triggered (ET) Readiness Notification

Triggered by state change

Notification will only be sent out when state change

Recommended mode for epoll(4)

Support only non-blocking socket

12 / 16Non Blocking Socket

N

Page 27: 20110828 Non blocking socket

Edge-triggered (ET) Readiness Notification

Triggered by state change

Notification will only be sent out when state change

Recommended mode for epoll(4)

Support only non-blocking socket

12 / 16Non Blocking Socket

N

Page 28: 20110828 Non blocking socket

epoll()

Default mode is LT, can be configured to ET

Maximum acceptable fd num is the up limit of opened file

Only handle activate connections

So performance is better in normal scenario

Return fd id while trigged by event

So no need to traverse the whole fd set

Shared memory is used to avoid copying

13 / 16Non Blocking Socket

N

Page 29: 20110828 Non blocking socket

epoll()

Default mode is LT, can be configured to ET

Maximum acceptable fd num is the up limit of opened file

Only handle activate connections

So performance is better in normal scenario

Return fd id while trigged by event

So no need to traverse the whole fd set

Shared memory is used to avoid copying

13 / 16Non Blocking Socket

N

Page 30: 20110828 Non blocking socket

epoll()

Default mode is LT, can be configured to ET

Maximum acceptable fd num is the up limit of opened file

Only handle activate connections

So performance is better in normal scenario

Return fd id while trigged by event

So no need to traverse the whole fd set

Shared memory is used to avoid copying

13 / 16Non Blocking Socket

N

Page 31: 20110828 Non blocking socket

epoll()

Default mode is LT, can be configured to ET

Maximum acceptable fd num is the up limit of opened file

Only handle activate connections

So performance is better in normal scenario

Return fd id while trigged by event

So no need to traverse the whole fd set

Shared memory is used to avoid copying

13 / 16Non Blocking Socket

N

Page 32: 20110828 Non blocking socket

Asynchronous Non-Blocking IO

Not yet become popular in Unix

The programming model is different with classical

Almost the ONLY and BEST solution on Windowz machine

But only file system IO is supported on Linux (?)

14 / 16Non Blocking Socket

N

Page 33: 20110828 Non blocking socket

Asynchronous Non-Blocking IO

Not yet become popular in Unix

The programming model is different with classical

Almost the ONLY and BEST solution on Windowz machine

But only file system IO is supported on Linux (?)

14 / 16Non Blocking Socket

N

Page 34: 20110828 Non blocking socket

AIO

Overlapping process with IO

The READ request return immediately

Which indicate the request has been initiated successfully

The operation will be performed in background

After READ response ready

A signal or thread-based callback will be generated

15 / 16Non Blocking Socket

N

Page 35: 20110828 Non blocking socket

AIO

Overlapping process with IO

The READ request return immediately

Which indicate the request has been initiated successfully

The operation will be performed in background

After READ response ready

A signal or thread-based callback will be generated

15 / 16Non Blocking Socket

N

Page 36: 20110828 Non blocking socket

AIO

Overlapping process with IO

The READ request return immediately

Which indicate the request has been initiated successfully

The operation will be performed in background

After READ response ready

A signal or thread-based callback will be generated

15 / 16Non Blocking Socket

N

Page 37: 20110828 Non blocking socket

AIO

Overlapping process with IO

The READ request return immediately

Which indicate the request has been initiated successfully

The operation will be performed in background

After READ response ready

A signal or thread-based callback will be generated

15 / 16Non Blocking Socket

N

Page 38: 20110828 Non blocking socket

Q AND A

16 / 16Non Blocking Socket

N