Slide 1

Slide 1 text

Preemptive multitasking using coroutines in PHP Swoole Bruce Dou | @doubaokun [email protected] 6th Jun 2019

Slide 2

Slide 2 text

1. Evolution of web server or services 2. The problems to solve for PHP 3. Coroutinesand preemptive schedulingfor PHP 4. Show case preemptivescheduling in PHP Swoole Agenda

Slide 3

Slide 3 text

About me • Software Architect & Head of Development • Founderat Transfon • Twitter:@doubaokun • Email:[email protected] • Linkedin:linkedin.com/in/brucedou • Github: github.com/doubaokun

Slide 4

Slide 4 text

Security Performance Regulation compliance Cost control About Transfon

Slide 5

Slide 5 text

Evolution of web server or services

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

https://unixism.net/2019/04/linux-applications-performance-introduction/

Slide 8

Slide 8 text

I/O bound vs CPU bound

Slide 9

Slide 9 text

Decoupling I/O and Computation

Slide 10

Slide 10 text

Async and non-blocking I/O

Slide 11

Slide 11 text

Why non-blocking I/O matters Multiple cores and fast CPU vs slow Network I/O

Slide 12

Slide 12 text

Linux I/O multiplexing, non-blocking • select() monitors the sockets’ status flag and return the status of all sockets, manually iterate through all the fds (C10K problem) • poll() similar to select() but allocate an array of pollfd, can handle more than 1024 sockets •epoll() monitors sockets’ status and trigger related events, and only return triggered events array.

Slide 13

Slide 13 text

Reactor pattern

Slide 14

Slide 14 text

PHP ?

Slide 15

Slide 15 text

The problems to solve for PHP • High concurrency,large numberof connections • Long-liveconnections,real-timeservices • Long-liveconnectionsto DB, Cache, RPC etc • Supportmoreprotocols • Server programming,not just scripts • Milliseconds scheduler • Async tasks of consuming3rd party services • Manage and reuse the statusin memory • Performance

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

1.Receive the request 2.Load and compile PHP files and codes 3.Initialize the context objects and variables 4.Execute the functions 5.Send the response 6.Recycle the resources The life cycle of HTTP request in PHP-FPM

Slide 18

Slide 18 text

1.Load and compile files and codes 2.Initialize the context objects and variables 3.Receive the request 4.Execute the functions 5.Send the response 6.Recycle the resources The life cycle of HTTP request in PHP Swoole

Slide 19

Slide 19 text

PHP Swoole https://www.swoole.co.uk/ https://github.com/swoole/swoole-src @php_swoole Coroutine-based concurrency library for PHP

Slide 20

Slide 20 text

Get started git clone [email protected]:doubaokun/phplondon201906.git docker build -f Dockerfile -t swoole-phplondon . docker run --rm -p 9501:9501 -v $(pwd):/app -w /app \ swoole-phplondon server.php

Slide 21

Slide 21 text

How it looks like

Slide 22

Slide 22 text

How fast

Slide 23

Slide 23 text

Open Source(Apache2 license) PHP Extension C/C++ / Boost.Context Callbackstyle & Coroutine Event-basednon-blockingI/O HTTP/HTTP2/Websocket/TCP/UDPetc Milliseconds task scheduler Process manager Preemptivescheduling PHP Swoole

Slide 24

Slide 24 text

The problems we like to solve further • Callback hell • Use Coroutinestyle (sync) concurrency • Automaticallycontextswitchbasedon I/O • Soft real-time • Long tail latency • One bad guy stopthe world

Slide 25

Slide 25 text

Callback hell

Slide 26

Slide 26 text

Callback hell Swoole 2.x.x

Slide 27

Slide 27 text

I/O based automatically scheduling in PHP Swoole

Slide 28

Slide 28 text

I/O based automatically scheduling

Slide 29

Slide 29 text

Coroutines in PHP Swoole

Slide 30

Slide 30 text

Coroutines in PHP Swoole

Slide 31

Slide 31 text

Problem with CPU heavy business logics

Slide 32

Slide 32 text

Preemptive scheduling

Slide 33

Slide 33 text

PHP Swoole Server

Slide 34

Slide 34 text

HTTP server with PHP Swoole

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

HTTP server structure

Slide 37

Slide 37 text

Preemptive scheduling in PHP Swoole Server

Slide 38

Slide 38 text

Scheduling thread ZendVM:EG(vm_interrupt)= 1 Zend_interrupt_function Yield current coroutineif it runs longer than 5ms How preemptive scheduling works

Slide 39

Slide 39 text

Preemptive scheduling in PHP Swoole Server

Slide 40

Slide 40 text

HTTP server structure single process

Slide 41

Slide 41 text

Preemptive scheduling in PHP Swoole Server time curl -I http://127.0.0.1:9501/\?p\=1 (CPU heavy logics) time curl -I http://127.0.0.1:9501/\?p\=0 (light weight logics) docker run --rm -p 9501:9501 -v $(pwd):/app -w /app \ swoole-phplondon server_preemptive.php time curl -I http://127.0.0.1:9501/\?p\=1 & sleep 1 && \ time curl -I http://127.0.0.1:9501/\?p\=0 & wait

Slide 42

Slide 42 text

Preemptive scheduling in PHP Swoole Server ini_set("swoole.enable_preemptive_scheduler", "0");

Slide 43

Slide 43 text

Preemptive scheduling in PHP Swoole Server ini_set("swoole.enable_preemptive_scheduler", "1");

Slide 44

Slide 44 text

Real world meaningof preemptive scheduling

Slide 45

Slide 45 text

Real-world meaning of preemptive scheduling

Slide 46

Slide 46 text

https://github.com/doubaokun/phplondon201906 https://www.swoole.co.uk/ https://github.com/swoole/swoole-src https://swoole.slack.com https://twitter.com/php_swoole Thank you