Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Preemptive multitasking using coroutines in PHP Swoole

Bruce Dou
June 06, 2019
89

Preemptive multitasking using coroutines in PHP Swoole

PHP Swoole enables PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, WebSocket services in PHP programming language.

Compared with other async programming frameworks or software such as Nginx, Tornado, Node.js, Swoole has the built-in async, coroutines, multiple threads I/O modules.

Preemptive scheduling is the key to building a high performance, low latency, near the real-time system.

Compared with other programming languages or frameworks, PHP Swoole is one of the very few supports preemptive multitasking using coroutines.

This talk we like to introduce the coroutines in PHP Swoole and the implementation of a multitasking coroutines scheduler in PHP Swoole.

Bruce Dou

June 06, 2019
Tweet

Transcript

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

    View Slide

  2. 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

    View Slide

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

    View Slide

  4. Security
    Performance
    Regulation compliance
    Cost control
    About Transfon

    View Slide

  5. Evolution of web server or services

    View Slide

  6. View Slide

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

    View Slide

  8. I/O bound vs CPU bound

    View Slide

  9. Decoupling I/O and Computation

    View Slide

  10. Async and non-blocking I/O

    View Slide

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

    View Slide

  12. 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.

    View Slide

  13. Reactor pattern

    View Slide

  14. PHP ?

    View Slide

  15. 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

    View Slide

  16. View Slide

  17. 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

    View Slide

  18. 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

    View Slide

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

    View Slide

  20. 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

    View Slide

  21. How it looks like

    View Slide

  22. How fast

    View Slide

  23. 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

    View Slide

  24. 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

    View Slide

  25. Callback hell

    View Slide

  26. Callback hell
    Swoole 2.x.x

    View Slide

  27. I/O based automatically
    scheduling in PHP Swoole

    View Slide

  28. I/O based automatically scheduling

    View Slide

  29. Coroutines in PHP Swoole

    View Slide

  30. Coroutines in PHP Swoole

    View Slide

  31. Problem with CPU heavy business logics

    View Slide

  32. Preemptive scheduling

    View Slide

  33. PHP Swoole Server

    View Slide

  34. HTTP server with PHP Swoole

    View Slide

  35. View Slide

  36. HTTP server structure

    View Slide

  37. Preemptive scheduling in PHP Swoole Server

    View Slide

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

    View Slide

  39. Preemptive scheduling in PHP Swoole Server

    View Slide

  40. HTTP server structure single process

    View Slide

  41. 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

    View Slide

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

    View Slide

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

    View Slide

  44. Real world meaningof preemptive scheduling

    View Slide

  45. Real-world meaning of preemptive scheduling

    View Slide

  46. 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

    View Slide