Swow is a high-performance pure coroutine network communication engine written in PHP and Cwhich is committed to using a minimal C core and most PHP code to support PHP high-performance web programming.

Project original intention

Although the PHP coroutine ecology now has a very diverse selection, all coroutine frameworks and libraries have a very fatal problem, that is, they all evolved from asynchronous frameworks, not bottom-up for coroutines. Due to some compatibility considerations, many capabilities that should be possessed are often sacrificed, or reasonable design changes cannot be made due to historical burdens.

Therefore, the Swow project is committed to creating a high-performance, highly controllable, and easily compatible engine designed for coroutines from the bottom up. Relying on the advantages of architecture design, it will fully release the true strength of coroutine technology. This is the original intention of Swow and its team members. We will continue to invest and work hard for this, and you are welcome to join us to participate in open source construction.


design concept

Swow’s minimum C core design determines that it is more about pursuing secondary development capabilities and debuggability in addition to ensuring key performance.

The seamless integration of Swow kernel and PHP code greatly improves the developer’s programming efficiency and flexibility.

Swow provides a variety of debugging mechanisms and powerful and low-threshold debugging capabilities to ensure that developers are free from bugs to the greatest extent. Ordinary developers can also have near-expert-level DEBUG capabilities through the blessing of tools.


thank you

Written before the introduction of capabilities: The development of PHP high-performance coroutine network communication engine has a long way to go. This is an open source work that is too high. I am very grateful to the following partners for their code contributions to the Swow project. Without everyone, there would be no current Swow ! Among them, we would like to thank @dixyes, the core member of the team, for his dedication, which has raised the capabilities and stability of Swow to a whole new level!

image-20230103052527965.png


Supported OS

operating systemswowOther asynchronous coroutine libraries
Win32โœ… IOCP native supportโŒ No native support
GNU/Linux (including musl)โœ… Epoll support + planned io_uring supportโš ๏ธ No io_uring plan support
macOSโœ… Kqueue support; support intel and m1 chipsโš ๏ธ Not Tier 1 support
FreeBSDโœ… Kqueue supportโš ๏ธ Lack of official support statement
other [^other-platforms]โš ๏ธ Second class support: AIX / IBM i / z / OS / Android / MinGW / SunOSโš ๏ธ not clear

[^other-platforms]: Other system support depends on libuv support and community needs


coroutine

C stack coroutine implementation

C stack coroutine implementationswowOther asynchronous coroutine libraries
boost-contextโœ… First class support [^boost-context-tier-1]โœ…
ucontextโœ… Better compatibilityโœ…
thread-contextโœ… You can observe the status of all C stack coroutines through gdbโš ๏ธ Only Swoole supports

[^boost-context-tier-1]: First-class support means that members of the Swow development team will submit code contributions to boost-context to better support the application scenarios required by Swow.

Coroutine model

Coroutine modelswowOther asynchronous coroutine libraries
main coroutineโœ… Wherever you go, there are coroutinesโŒ Not supported, not perfectly compatible with traditional PHP ecology
Cross-coroutine schedulingโœ… Support nested coroutine cross-level schedulingโŒ Not supported, unable to achieve cross-level interruption
independent environmentโœ… A single coroutine crash does not affect global stabilityโŒ Not supported, all crashed
Benchmarking Process Modelโœ… Imitate the process model, the design is reasonable to followโŒ self-made concept
Single Stack Lightning Switchingโœ… The scheduler or internal coroutine only needs a single-stack switchโŒ Not supported, always dual-stack switching

Coroutine control ability

Coroutine control abilityswowOther asynchronous coroutine libraries
forced wakeupโœ… Equivalent to ECANCELED or ETIMEDOUTโš ๏ธ Imperfect / no full scene support
abortโœ… Equivalent to SIGTERMโš ๏ธ Not fully supported
kill coroutineโœ… Equivalent to SIGKILLโŒ not supported

coroutine debugger

coroutine debuggerswowOther asynchronous coroutine libraries
SDBโœ… Customized for the coroutine modelโŒ Lack of Debugger fully oriented to coroutine design
Xdebugโœ… Supported from PHP-8.1โœ… Supported from PHP-8.1

Coroutine debugger capability

Coroutine debugger capabilitiesswowOther asynchronous coroutine libraries
PHP writing & secondary developmentโœ… Inheritable extensionsโŒ Depends on other extension capabilities
Production environment supportโœ… No performance impactโš ๏ธ Not recommended to enable in production environment
list of coroutinesโœ… Equivalent to the ps command to view the processโš ๏ธ Not fully supported, libraries that rely on traditional PHP debuggers have no support for coroutine granularity
View all coroutine informationโœ… You can view the status of the coroutine, the information of each stack frame of the full call stack, the content of the source code executed, the variables on the stack, etc.โš ๏ธ Not fully supported, libraries that rely on traditional PHP debuggers have no support for coroutine granularity
Enter a coroutine and track debuggingโœ… Equivalent to gdb attachโŒ not supported
Breakpoints, single-step debuggingโœ… Only affects debugged coroutinesโš ๏ธ Rely on tools such as Xdebug, and will block the entire process
Dynamically insert PHP instructions while debuggingโœ… When using the debugger for breakpoint debugging to the BUG, โ€‹โ€‹you can dynamically execute PHP instructions to debug the BUG [^gdb-print]โŒ not supported
Scanning zombie coroutinesโœ… Find coroutines that have been inactive for a long timeโŒ not supported
kill coroutineโœ… Equivalent to kill commandโŒ not supported
kill all coroutines and exitโœ… Equivalent to graceful shutdownโŒ not supported

[^gdb-print]: Equivalent to the print command of GDB, dynamically execute the expression given by the debugger, and print its return value.


aisle

The core effective code is less than 300 lines, and the unit test coverage rate is 100%.

channel capabilityswowOther asynchronous coroutine libraries
zero-buffered channelโœ… New Channel(0)โŒ not supported
buffered channelโœ… new Channel(n)โœ…
selectโœ… Selector | CallbackSelectorโŒ not supported
circular reference GCโœ… Can handle circular references in the queueโŒ not supported
read after closeโœ… The remaining data in the queue can still be read after the channel is closedโŒ not supported

Synchronize

other abilitiesswow
WaitGroupโœ… Built-in high performance implementation
WaitReferenceโœ… Implemented by reference counting mechanism, coding is easier

event

coroutine libraryevent driven
swowโœ… Self-maintained based on libcat [^libcat]libcat is a derivative project of libuv [^libuv]
Swooleโœ… Self-developed, but no Windows native support
Other asynchronous coroutine librariesโŒ Rely on event-driven extensions maintained by third parties, and some extensions are no longer maintained

[^libcat]: libcat is the coroutine version of libuv, which is independently maintained by the Swow team and can be used by all C-based projects. For example, you can use libcat to write a C++ coroutine project, or provide C stack coroutine capabilities for Python and Node.js Wait.

[^libuv]: The asynchronous event driver of libuv provides mainstream full-platform support, has passed extensive and rigorous production verification in languages โ€‹โ€‹such as Node.js and Julia, and has reached industrial-grade standards. The Swow team will submit code contributions to libuv to make it better support the application scenarios required by Swow.

Signal

signal abilityswowOther asynchronous coroutine ecology
Windows signal simulationโœ… Use Linux-like signal mechanism under Windowsโš ๏ธ Lack of Windows native support

network programming

Network programming abilityswowOther asynchronous coroutine ecology
Bufferโœ… Accurately control the memory usage of each byte to achieve high-performance data processingโŒ Coarse-grained memory management
Buffer (copy-on-write)โœ… Copy-on-write mechanism + memory mapping mechanismโŒ php_stream needs to create an internal memory copy
Socketโœ… Can be linked with Buffer to achieve data transmission and reception accurate to every byteโŒ Coarse-grained memory management
Socket (writev support)โœ… Single system call zero copy to send multiple blocks of memory dataโŒ unavoidable memory copy or multiple system calls
IpAddressโœ… Perfect high-performance IP address resolution library to help network programmingโš ๏ธ Lack of relevant built-in support
HttpParserโœ… Can be controlled by PHP code to complete high-performance HTTP protocol analysisโŒ Can only receive underlying processed HTTP objects
WebSocketโœ… Can be controlled by PHP code to complete high-performance WebSocket protocol analysisโŒ Can only receive the underlying processed WebSocket object

With the help of the minimal capability provided by the underlying core of Swow, a high-performance API gateway can be realized by writing PHP code. The specific project practice content and production environment verification sharing, please look forward to the follow-up Twitter update of the official Swow team!


synchronous blocking coroutine

synchronous blocking coroutineswowSwooleOther asynchronous coroutine ecology
PHP stream (blocking mode)โœ…โœ…โŒ
PHP stream (non-blocking mode)โœ… BIO modeโœ… SSL_read/write modeโŒ
PHP stream (file IO)โœ…โœ…โŒ
PHP stream (file lock)โœ… Full scene coverageโœ…โŒ
pdo_mysqlโœ…โœ…โŒ
pdo_pgsqlโš ๏ธ Not supported yet [^not-supported-yet]โš ๏ธ Not supported yet [^not-supported-yet]โŒ
mysqliโœ…โœ…โŒ
phpredisโœ…โœ…โŒ
curlโœ…โœ…โŒ
proc_openโœ…โœ…โŒ
sockets extensionโš ๏ธ Not supported yet [^not-supported-yet]โœ…โŒ
sleep, usleep, nanosleep…โœ…โœ…โŒ
shell_exec, exec, passthru…โœ…โœ…โŒ
Gethostbyname and other DNS related functionsโœ…โœ…โŒ
stream_selectโœ…โœ…โŒ
stream_poll_oneโœ… Workerman special offerโœ… Swoole\\EventโŒ

[^not-supported-yet]: Not supported at the moment means that it will be supported in the future, or it is already in the implementation plan

Benefits of this technology:

Advantages of ecological reuseswowSwooleOther asynchronous coroutine ecology
Open source libraries based on file_get_contents(), cURL (such as: Guzzle, etc.)โœ…โœ…โŒ Ecological fragmentation
The cURL-based SDK provided by the cloud platform (such as the SDK of Alibaba Cloud, Tencent Cloud, AWS and other platforms)โœ…โœ…โŒ Ecological fragmentation
Open source library based on PDO, mysqli, phpredisโœ…โœ…โŒ Ecological fragmentation
Based on stream_select() or other event-driven asynchronous open source libraries (such as: workerman, reactphp, amphp)โœ…โœ…โŒ cannot work simultaneously
FileSystem related open source libraries (eg: symfony/filesystem)โœ…โœ…โŒ Unable to solve, block directly
Print directly to standard output or log libraries that need to read and write log files (such as: symfony/console, monolog)โœ…โœ…โŒ Unable to solve, block directly

CPU scheduling

Watchdog provides CPU scheduling capabilities for PHP. The core principle is that the Watchdog thread will periodically check the activity of the coroutines in other threads. If it is found that the coroutines in the worker threads are no longer active, they will be checked through the interrupt mechanism of ZendVM. Confirm that if the VM interrupt fails, it indicates that the worker thread is blocked by a system call and triggers an alarm; if the VM interrupt succeeds, it indicates that the worker thread is caught in CPU-intensive operations or an infinite loop, and immediately triggers the scheduling rules set by the user for scheduling.

CPU scheduling capabilityswowSwooleOther asynchronous coroutine ecology
CPU blocking alarmโœ… Contains system call blocking warningsโš ๏ธ Silent scheduling, no alarmโŒ No alarm, direct downtime
CPU blocking slowdownโœ… Dynamically adjustableโœ… Fixed scheduling timeโŒ Unable to slow down, directly down
CPU blocking throttlingโœ… Support killing coroutines stuck in an infinite loopโš ๏ธ Only timed transfer is supported to reduce negative impactโŒ Unable to cope, direct downtime
Dynamic Switch Schedulerโœ… API switch schedulerโš ๏ธ Only support settings via iniโŒ No CPU scheduling capability
Dynamic Programming Scheduling Behaviorโœ… Programmable, dynamically determine the scheduling method when blockedโš ๏ธ Fixed scheduling rulesโŒ No CPU scheduling capability

modernization

modernization capabilitiesswowOther asynchronous coroutine libraries
object orientedโœ… The kernel and PHP code are seamlessly integrated, all objects can be extended through inheritance, and secondary development can be easily completedโš ๏ธ Not fully object-oriented
exception mechanismโœ… Fully embrace the exception mechanism, and chain calls are easy to codeโš ๏ธ Incomplete use of exception mechanism
Composerโœ… Support the whole process to install and deploy through Composer, and support All in one or sub-library pull methodsโš ๏ธ The extension needs to be installed by other means
IDE assistantโœ… Composer built-in IDE assistant code in the package after installationโš ๏ธ Need to pull additional IDE assistant

Other debugging capabilities

Other debugging capabilitiesswowOther asynchronous coroutine libraries
class strace logโœ… In Debug mode, it supports printing the underlying coroutine scheduling, network sending and receiving, and system call information in strace format (supports multiple formats of time, module information, and PHP source code location display), and SSL network sending and receiving data supports plain text display, eliminating the need to capture packets Tool and certificate configuration troubles.โŒ No related support
debugInfo supportโœ… Print internal objects through var_dump(), and you can also see rich debugging informationโš ๏ธ Need to manually print the required information

build system

build capacityswowSwooleOther asynchronous coroutine ecology
Builderโœ… The project can be pulled through Composer, and the extension can be installed with one click using the built-in script, supporting independent extended version of each projectโš ๏ธ Only manual compilation or PECL installation is supportedโŒ You need to install third-party event-driven extensions yourself
PECLโŒ No planned support, PECL is in disrepair, lack of maintenanceโœ… First class supportโš ๏ธ Event-driven extensions are provided by a third party, and reliability cannot be guaranteed independently
Windows DLLsโœ… Can be downloaded directly from the release pageโŒ Does not support WindowsโŒ No Windows native support
CLI issueโœ… Based on lwmbs capability, it can be downloaded directlyโœ… swoole-cliโŒ Need to compile it yourself or install it through the system package manager
One-click packaging of micro into binaryโœ… Based on lwmbs capability, Swow is optional for PHP project binary packaging and release, once packaged and run in multiple places [^hyperf-box]โš ๏ธ Not supported yet [^not-supported-yet]โŒ Not supported, but can be achieved with the help of PHP packaging capabilities provided by the Swow team

[^hyperf-box]: Hyperf Box provides out-of-the-box one-click packaging binary tools for the Swow project, and more open source community partners are welcome to re-develop more out-of-the-box packaging tools based on micro technology to benefit the community.


More

The above capability enumeration is just the beginning of Swow. The Swow core also contains a lot of detailed design and some experimental functions that have not been officially disclosed.

In general, the future of Swow is full of imagination. It provides PHP with possibilities far beyond the past. We can use Swow’s ability to think and do more things that have never been thought about.

In the future, we will share with you the detailed description and best practices of each capability item in detail by “publishing a large number of short articles”, and disclose a large number of internal implementation details and production applications, so stay tuned!

#Swow #v100 #released #News Fast Delivery

Leave a Comment

Your email address will not be published. Required fields are marked *