+ All Categories
Home > Technology > Thread Blocking is Evil

Thread Blocking is Evil

Date post: 23-Jan-2015
Category:
Upload: -
View: 715 times
Download: 0 times
Share this document with a friend
Description:
This is the simple version of the orinial one addressed at Deview2011.
87
Thread Blocking Is Evil Game Network Component Team Kim, J.H.
Transcript
Page 1: Thread Blocking is Evil

Thread Blocking Is Evil

Game Network Component Team

Kim, J.H.

Page 2: Thread Blocking is Evil

CONCURRENCY

Page 3: Thread Blocking is Evil

Why is Thread Blocking

a Concern?

Page 4: Thread Blocking is Evil

“The Free Lunch is OVER.”

Herb SutterDecember 2004

Page 5: Thread Blocking is Evil

Clock speedExecution optimizationCache

Page 6: Thread Blocking is Evil

Cache

HyperthreadingMulticoreCache

Page 7: Thread Blocking is Evil

Write Multithreaded Application

Page 8: Thread Blocking is Evil

CONCURRENCY

Page 9: Thread Blocking is Evil

Waiting in Line

Page 10: Thread Blocking is Evil

Thread Blocking

Page 11: Thread Blocking is Evil

Starvation

Deadlock

Page 12: Thread Blocking is Evil

Starvation

Page 13: Thread Blocking is Evil

Task A Task B

1: Lock 1: Sleep 0 msec

2: Sleep 1msec

3: Unlock

Page 14: Thread Blocking is Evil

Pro

cess

ti

me(m

s)

# of Thread

1 2 3 4 5 6 7 8 9984986988990992994996998

1000

Task BTask A

Page 15: Thread Blocking is Evil

DeadlockHold & Wait

Page 16: Thread Blocking is Evil

Thread A Thread B

1A: Lock 1B: Lock

2A: Request 2B: Send Response

3A: Wait for Response 3B: Unlock

4A: Do something with Response

5A: Unlock

Page 17: Thread Blocking is Evil

Thread A Thread B

1A: Lock 1B: Lock

2A: Request 2B: Send Response

3A: Wait for Response 3B: Unlock

4A: Do something with Response

5A: Unlock

Page 18: Thread Blocking is Evil

My Prob-lem

Page 19: Thread Blocking is Evil

Framework for Lobby-Room style

Game

Page 20: Thread Blocking is Evil

Remote Procedure Call

Page 21: Thread Blocking is Evil

Easy to write code be-cause it’s all contained

inside one function.

Page 22: Thread Blocking is Evil

Game Client

LobbyRoom‘Enter Room’

RPC‘Authentication’

RPC

Page 23: Thread Blocking is Evil

Game Client

LobbyRoom

‘Enter Room’

RPC func-tion body

‘Authenti-cation RPC

‘Function

body

Call ‘Enter Room’ RPC

Call ‘Authentica-tion’ RPC

Return ‘Authentication’ RPC

Return ‘Enter Room’ RPC

Page 24: Thread Blocking is Evil

Game Client

LobbyRoom

Call ‘Enter Room’ RPC

Call ‘Authentica-tion’ RPC

Return ‘Authentication’ RPC

Return ‘Enter Room’ RPC

‘Enter Room’

RPC func-tion body

‘Authenti-cation RPC

‘Function

body

Page 25: Thread Blocking is Evil

Game Client

LobbyRoom

‘Enter Room’ RPC

function body

‘Authenti-cation RPC

‘Function

body

Call ‘Enter Room’ RPC

Call ‘Authentica-tion’ RPC

Return ‘Authentication’ RPC

Return ‘Enter Room’ RPC

Response DelayFrom Lobby Server

Page 26: Thread Blocking is Evil

Game Client

LobbyRoom

‘Enter Room’ RPC

function body

‘Authenti-cation RPC

‘Function

body

Call ‘Enter Room’ RPC

Call ‘Authentica-tion’ RPC

Return ‘Authentication’ RPC

Return ‘Enter Room’ RPC

Room Serverthroughput Decline

Response DelayFrom Lobby Server

Page 27: Thread Blocking is Evil

What is theAxis of Evil?

Page 28: Thread Blocking is Evil

Thread Block-

ing

Game Client

LobbyRoom

Call ‘Enter Room’ RPC

Call ‘Authentica-tion’ RPC

Return ‘Authentication’ RPC

Return ‘Enter Room’ RPC

Page 29: Thread Blocking is Evil

Inherent Limitation of RPC

Page 30: Thread Blocking is Evil

Easy to write code be-cause it’s all contained

inside one function.

Page 31: Thread Blocking is Evil

Can not split code be-cause it’s all contained

inside one function.

Page 32: Thread Blocking is Evil

RPC::Result* JoinRoom(…){ … RPC::Result answer = RPC::SyncCall(“VerifyLobbyUserToken”, …); …. return result;}

Page 33: Thread Blocking is Evil

What We Want

Page 34: Thread Blocking is Evil

RPC Function Body

Non-Blocking

Page 35: Thread Blocking is Evil

What is the Essence of Problems?

Page 36: Thread Blocking is Evil

Request & Wait

Page 37: Thread Blocking is Evil

Solution

Page 38: Thread Blocking is Evil

Asynchronous

Programming

Page 39: Thread Blocking is Evil

Request(callback, …)

Page 40: Thread Blocking is Evil

Pros:Non-Blocking

Page 41: Thread Blocking is Evil

Cons:Can’t Write Sequential CodeCan’t Use Stack VariableCan’t Split Programming Construct…

Page 42: Thread Blocking is Evil

Too Difficult!!

Page 43: Thread Blocking is Evil

Coroutine

Page 44: Thread Blocking is Evil

Similar to Thread

Page 45: Thread Blocking is Evil

Line of Execution

Page 46: Thread Blocking is Evil

Stack&

Local Variable

Page 47: Thread Blocking is Evil

But

Page 48: Thread Blocking is Evil

Non-preemptive

Page 49: Thread Blocking is Evil

Instruction:YieldYield BreakResume

Page 50: Thread Blocking is Evil

Who does provide Coroutine?

Page 51: Thread Blocking is Evil

C#ErlangHaskell

JavaScript(since 1.7)LuaPerl

Python(since 2.5)Ruby

Page 52: Thread Blocking is Evil

What about C++?

Page 53: Thread Blocking is Evil
Page 54: Thread Blocking is Evil

On Your Own!

Page 55: Thread Blocking is Evil

Windows: Fiber

Linux: getcontext/setcontextmakecontextswapcontext

Page 56: Thread Blocking is Evil

Asynchronous Pro-gramming

+Coroutine

Page 57: Thread Blocking is Evil

Non-Blocking&

Writing Sequential Code

Page 58: Thread Blocking is Evil

Solution for Starva-

tion

Page 59: Thread Blocking is Evil

Task A Task B

1: Lock 1: Sleep 0msec

2: Sleep 2msec

3: Unlock

Before

Page 60: Thread Blocking is Evil

Task A Task B

Thread A 1: Sleep 0msec

1: Create & Resume Coroutine

2: Request Lock

3: Yield

Thread A’

4: Resume

5: Sleep 2msec

6: Yield Break

7: Unlock

Non-Blocking

Sequential Code

After

Page 61: Thread Blocking is Evil

BeforePro

cess

ti

me(m

s)

# of Thread

1 2 3 4 5 6 7 8 9984986988990992994996998

1000

Task BTask A

Page 62: Thread Blocking is Evil

AfterPro

cess

ti

me(m

s)

1 2 3 4 5 6 7 8 90

200

400

600

800

1000

1200

Task BTask A

# of Thread

Page 63: Thread Blocking is Evil

Solution for

DeadlockHold & Wait

Page 64: Thread Blocking is Evil

Thread A Thread B

1A: Lock 1B: Lock

2A: Request 2B: Send Response

3A: Wait for Response 3B: Unlock

4A: Do something with Response

5A: Unlock

Before

Page 65: Thread Blocking is Evil

Thread A Thread B

1A: Lock 1B: Lock

2A: Create & Resume Coroutine 2B: Send Response

3A: Request 3B: Unlock

4A: Yield

5A: Unlock

Thread A’ (invoked by response)

6A’: Lock

7A’: Resume

8A’: Do something with response

9A’: Yield Break

10A’: Unlock

Non-Blocking

Sequential Code

After

Page 66: Thread Blocking is Evil

Solutionfor

My Problem

Page 67: Thread Blocking is Evil

Thread Block-

ing

Game Client

LobbyRoom

Call ‘Enter Room’ RPC

Call ‘Authentica-tion’ RPC

Return ‘Authentication’ RPC

Return ‘Enter Room’ RPC

Page 68: Thread Blocking is Evil

Room Thread Lobby Service

1R: Call ‘Authentication’ RPC function 1L: Process the request

2R: Wait for the response 2L: Send back the response

3R: Process

Before

Page 69: Thread Blocking is Evil

Room Thread Lobby Service

1R: Create & Resume Coroutine 1L: Process the request

2R: Request 2L: Send back the response

3R: Yield

Room Thread’(invoked by response)

4R’: Resume

5R’: Process

6R’: Yield Break

Non-Blocking

Sequential Code

After

Page 70: Thread Blocking is Evil

Request & Wait

Request & Yield

Page 71: Thread Blocking is Evil

Non-Blocking&

Writing Sequential Code

Page 72: Thread Blocking is Evil

Coroutine(Fiber)

Thread

CPU CPU CPU CPU

User-mode SchedulingKernel-mode Scheduling

Page 73: Thread Blocking is Evil

Corou-tine Pool

Network I/O Thread

Pool

RPC Task Queue

RPC Exe-cution

Thread Pool

Page 74: Thread Blocking is Evil

RPC Task Queue

RPC Execu-tion Thread

Network I/O Thread

enqueuetask

dequeuetask

acquire corou-tine

enqueue

re-sumetask

dequeue

re-sumetask

resume

yield break

release corou-tine

Network Event

Network Event

Coroutine Pool

pool-ing

corou-tine

RPC Execu-tion

yield return

start

Page 75: Thread Blocking is Evil

RPC Task Queue

RPC Execu-tion Thread

1

Coroutine Pool

RPC Execu-tion

enqueuetask

dequeuetask

acquire corou-tine corou-

tine

yield return

enqueue

re-sumetask

dequeue

re-sumetask

release corou-tine

Network Event

Network Event

pool-ing

RPC Execu-tion Thread

2

resume

yield break

resume

Network I/O Thread

Page 76: Thread Blocking is Evil

RPC::Result* JoinRoom(…){ … RPC::Result answer = RPC::SyncCall(“VerifyLobbyUserToken”, …); …. return result;}

Page 77: Thread Blocking is Evil

RPC::Result* JoinRoom(…){ … RPC::Result answer = RPC::SyncCallYield(“VerifyLobbyUserToken”, …); …. return result;}

Page 78: Thread Blocking is Evil

TPS

# of Thread

Blocking

Yield

Page 79: Thread Blocking is Evil

Wrap-up

Page 80: Thread Blocking is Evil

“The free lunch is OVER.”Write Multithreaded

Application

CONCURRENCY

Page 81: Thread Blocking is Evil

Thread Blocking is Evil

Starvation Deadlock

Page 82: Thread Blocking is Evil

Asynchronous Programming

+Coroutine

Page 83: Thread Blocking is Evil

Non-Blocking+

Writing Sequen-tial Code

Page 84: Thread Blocking is Evil

Request & Wait

Request & Yield

Page 85: Thread Blocking is Evil

Inspired by Jeffrey Richter

‘Simplified APM With The Asyn-cEnumerator’

Page 86: Thread Blocking is Evil
Page 87: Thread Blocking is Evil

Pictures fromwww.Istockphoto.comwww.Flickr.com


Recommended