Skip to content

Commit

Permalink
Version 6.0: added multi thread supports (#5320)
Browse files Browse the repository at this point in the history
* Optimize code

* thread local

* swoole thread

* optimize argv

* optimize code, add more methods for thread

* Swoole\Thread\Map

* Swoole\Thread\ArrayList

* Optimize

* zend_array

* Optimize

* Optimize

* Optimize

* fix

* remove co redis/mysql/pgsql client

* Added Thread\Queue

* Fix Thread\Queue

* fix mem leak

* optimize, reduce memory copy

* Revert "optimize, reduce memory copy"

This reverts commit 449adf9.

* optimize code, remove 8.0 supports

* refactor atomic/lock, support thread

* clang-format

* fix tests

* [6.0] Server for thread mode (#5282)

* http server for thread mode

* fix compile error

* optimize code

* optimize code

* optimize code[2]

* optimize code[3]

* optimize code[4]

* refactor co-socket, support thread

* add signal example

---------

Co-authored-by: NathanFreeman <[email protected]>

* Update version

* Optimize header

* Fix

* refactor async-threads, support ZTS

* Optimize

* Optimize

* Optimize

* Optimize

* Optimize 5

* Optimize 6

* Optimize 7

* Optimize 8

* Optimize 10

* Optimize 11, add Server::get_worker_id()

* Optimize 12

* Optimize 13

* Optimize 14

* rename

* Refactor

* Refactor 2

* revert , format

* onPipeMessage/onTask/onFinish, fix message bus

* Update README.md

* Added pty support for proc_open function, fix #5275 (#5290)

* support pty, fix #5275

* fix

* fix 2

* fix

* fix BC

* fix tests

* Update boost asm (#5291)

* update boost asm

* Fix error

* fix tests

* fix tests

* fix tests

* remove hiredis

* fix tests [3]

* fix tests [4]

* fix tests [5], revert SwooleWG

* fix tests [6]

* fix tests [7]

* fix tests [8]

* optimize tests

* add thread test

* fix ci

* [test] fix ci 2

* [test] fix ci 3

* [test] fix ci 4

* [test] fix ci 5

* [test] fix ci 6

* [test] fix ci 7

* [test] fix ci 8

* [test] fix ci 9

* [test] fix ci 10

* [test] fix ci 11

* [test] remove swoole_timer_set, async settings can only be set in the main thread

* optimize create socket (#5293)

* optimize create socket

* fix error

* optimize code

* [test] fix tests

* [test] fix tests [3]

* [test] fix tests [4]

* sync plain_wrapper (#5296)

* [test] fix tests [5]

* No limit on the maximum buffer length of the read pipeline

* Refactor

* fix

* optimize tests

* fix tests

* code format

* fix tests

* fix tests

* fix tests [3]

* Fix transfer_t struct missing (#5303)

* Fix missing transfer_t

* Fix missing transfer_t

* Optimize thread context

* Fix tests[4]

* Refactor

* Refactor

* Refactor

* fix server shutdown

* fix thread atomic

* fix core tests

* Fix Sqlite pdo segmentfault (#5311)

* fix pdo

* fix sqlite error

* fix sqlite error

* fix sqlite error

* fix core tests

* Support passing streams between threads

* Support using stream as a thread argument

* optimize code

* optimize code

---------

Co-authored-by: NathanFreeman <[email protected]>
Co-authored-by: MARiA so cute <[email protected]>
  • Loading branch information
3 people authored May 9, 2024
1 parent dd6e0db commit ef7f7ed
Show file tree
Hide file tree
Showing 290 changed files with 6,648 additions and 18,680 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/conflict-exts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ on: [push, pull_request]

jobs:
tests:
if: "github.repository_owner == 'swoole' && !contains(github.event.head_commit.message, '[test]')"
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: ['8.0', '8.1', '8.2', '8.3']
php: ['8.1', '8.2', '8.3']

name: PHP ${{ matrix.php }} - Swoole

Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/framework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ on:
jobs:
linux:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[test]')"
if: 0
# if: "!contains(github.event.head_commit.message, '[test]')"
strategy:
fail-fast: false
matrix:
php-version: [ '8.0', '8.1', '8.2', '8.3' ]
php-version: [ '8.1', '8.2', '8.3' ]
framework: [ 'Laravel Octane', 'Hyperf', 'Simps', 'imi' ]
name: ${{ matrix.framework }} - PHP ${{ matrix.php-version }}
steps:
Expand Down Expand Up @@ -102,7 +103,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: [ '8.0', '8.1', '8.2', '8.3' ]
php-version: [ '8.1', '8.2', '8.3' ]
framework: [ 'Simps' ]
name: ${{ matrix.framework }} - PHP ${{ matrix.php-version }} - macOS
steps:
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ on: [push, pull_request]
jobs:
test-linux:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[zts]')"
strategy:
fail-fast: false
matrix:
php: ['8.0', '8.1', '8.2', '8.3']
php: ['8.1', '8.2', '8.3']
steps:
- uses: actions/checkout@v4
- name: Setup PHP
Expand All @@ -34,4 +35,6 @@ jobs:
- name: Run Swoole test
run: |
export SWOOLE_BRANCH=${GITHUB_REF##*/}
export SWOOLE_BUILD_DIR=$(realpath .)
export PHP_VERSION=${{ matrix.php }}
${{runner.workspace}}/swoole-src/scripts/route.sh
41 changes: 41 additions & 0 deletions .github/workflows/thread.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Thread Support Tests

on: [push, pull_request]

jobs:
test-linux:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[nts]')"
strategy:
fail-fast: false
matrix:
php: ['8.1-zts', '8.2-zts', '8.3-zts']
name: ${{ matrix.php }}
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
coverage: none
env:
phpts: ts
- name: Show machine information
run: |
date
env
uname -a
ulimit -a
php -v
php --ini
ls -al
pwd
echo "`git log -20 --pretty --oneline`"
echo "`git log -10 --stat --pretty --oneline`"
- name: Run Swoole test
run: |
export SWOOLE_BRANCH=${GITHUB_REF##*/}
export SWOOLE_THREAD=1
export SWOOLE_BUILD_DIR=$(realpath .)
export PHP_VERSION=${{ matrix.php }}
${SWOOLE_BUILD_DIR}/scripts/route.sh
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
PROJECT(libswoole)
cmake_minimum_required(VERSION 2.8.12)

ENABLE_LANGUAGE(ASM)
set(SWOOLE_VERSION 5.1.2)
set(SWOOLE_VERSION 6.0.0-dev)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
cmake_minimum_required(VERSION 2.8)

file(READ ./config.h SWOOLE_CONFIG_FILE)

Expand Down
28 changes: 17 additions & 11 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ PHP_ARG_ENABLE([thread-context],
[AS_HELP_STRING([--enable-thread-context],
[Use thread context])], [no], [no])

PHP_ARG_ENABLE([swoole-thread],
[whether to enable swoole thread support],
[AS_HELP_STRING([--enable-swoole-thread],
[Enable swoole thread support])], [no], [no])

PHP_ARG_ENABLE([swoole-coro-time],
[whether to enable coroutine execution time ],
[AS_HELP_STRING([--enable-swoole-coro-time],
Expand Down Expand Up @@ -877,6 +882,10 @@ EOF
AC_DEFINE(SW_LOG_TRACE_OPEN, 1, [enable trace log])
fi

if test "$PHP_SWOOLE_THREAD" != "no"; then
AC_DEFINE(SW_THREAD, 1, [enable swoole thread support])
fi

if test "$PHP_SOCKETS" = "yes"; then
AC_MSG_CHECKING([for php_sockets.h])

Expand Down Expand Up @@ -1025,13 +1034,6 @@ EOF
thirdparty/swoole_http_parser.c \
thirdparty/multipart_parser.c"

swoole_source_file="$swoole_source_file \
thirdparty/hiredis/hiredis.c \
thirdparty/hiredis/alloc.c \
thirdparty/hiredis/net.c \
thirdparty/hiredis/read.c \
thirdparty/hiredis/sds.c"

if test "$PHP_NGHTTP2_DIR" = "no"; then
PHP_ADD_INCLUDE([$ext_srcdir/thirdparty])
swoole_source_file="$swoole_source_file \
Expand Down Expand Up @@ -1092,6 +1094,7 @@ EOF
[mips64*], [SW_CPU="mips64"],
[mips*], [SW_CPU="mips32"],
[riscv64*], [SW_CPU="riscv64"],
[loongarch64*], [SW_CPU="loongarch64"],
[
SW_USE_ASM_CONTEXT="no"
]
Expand Down Expand Up @@ -1153,6 +1156,12 @@ EOF
else
SW_USE_ASM_CONTEXT="no"
fi
elif test "$SW_CPU" = "loongarch64"; then
if test "$SW_OS" = "LINUX"; then
SW_CONTEXT_ASM_FILE="loongarch64_sysv_elf_gas.S"
else
SW_USE_ASM_CONTEXT="no"
fi
else
SW_USE_ASM_CONTEXT="no"
fi
Expand All @@ -1177,7 +1186,6 @@ EOF
PHP_ADD_INCLUDE([$ext_srcdir/include])
PHP_ADD_INCLUDE([$ext_srcdir/ext-src])
PHP_ADD_INCLUDE([$ext_srcdir/thirdparty])
PHP_ADD_INCLUDE([$ext_srcdir/thirdparty/hiredis])

AC_MSG_CHECKING([swoole coverage])
if test "$PHP_SWOOLE_COVERAGE" != "no"; then
Expand All @@ -1192,8 +1200,7 @@ EOF
include/*.h \
stubs/*.h \
thirdparty/*.h \
thirdparty/nghttp2/*.h \
thirdparty/hiredis/*.h])
thirdparty/nghttp2/*.h])

PHP_REQUIRE_CXX()

Expand Down Expand Up @@ -1222,7 +1229,6 @@ EOF
PHP_ADD_BUILD_DIR($ext_builddir/src/wrapper)
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/boost)
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/boost/asm)
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/hiredis)
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/php/sockets)
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/php/standard)
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/php/curl)
Expand Down
8 changes: 4 additions & 4 deletions core-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8.1)
cmake_minimum_required(VERSION 2.8.12)

project(core_tests)

Expand All @@ -10,14 +10,14 @@ file(GLOB_RECURSE SOURCE_FILES FOLLOW_SYMLINKS src/*.cpp deps/llhttp/src/*.c)

add_definitions(-DHAVE_CONFIG_H)

set(core_tests_includes ./include/ ../thirdparty ../thirdparty/hiredis ./deps/llhttp/include)
set(core_tests_includes ./include/ ../thirdparty ../thirdparty/hiredis ./deps/llhttp/include /usr/local/include)
set(core_tests_libraries)
set(core_tests_link_directories /usr/local/lib)

list(APPEND core_tests_libraries pthread)
list(APPEND core_tests_libraries pthread gtest gtest_main)

# find GTest
find_package(GTest)
find_package(GTest REQUIRED)
if (!${GTEST_FOUND})
message(FATAL_ERROR "Not found GTest")
endif()
Expand Down
8 changes: 7 additions & 1 deletion core-tests/src/os/async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ TEST(async, schedule) {
count--;
if (count == 0) {
swoole_timer_del(timer);
ASSERT_EQ(SwooleTG.async_threads->get_worker_num(), 128);
ASSERT_GT(SwooleTG.async_threads->get_worker_num(), 16);
ASSERT_GT(SwooleTG.async_threads->get_queue_size(), 100);
ASSERT_GT(SwooleTG.async_threads->get_task_num(), 100);
break;
} else if (count == N - 1) {
ASSERT_EQ(SwooleTG.async_threads->get_worker_num(), 4);
ASSERT_EQ(SwooleTG.async_threads->get_queue_size(), 1);
ASSERT_EQ(SwooleTG.async_threads->get_task_num(), 1);
} else if (count < N / 2) {
ASSERT_GT(SwooleTG.async_threads->get_worker_num(), 4);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion core-tests/src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ TEST(server, task_worker4) {
serv->gs->task_workers.dispatch(&buf, &_dst_worker_id);
sleep(1);

EventData *task_result = &(serv->task_result[SwooleG.process_id]);
EventData *task_result = &(serv->task_result[swoole_get_process_id()]);
sw_memset_zero(task_result, sizeof(*task_result));
memset(&buf.info, 0, sizeof(buf.info));
buf.info.len = strlen(packet);
Expand Down
42 changes: 42 additions & 0 deletions examples/thread/aio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Swoole\Thread;
use Swoole\Thread\Queue;


$args = Thread::getArguments();
$c = 4;
$running = true;

if (empty($args)) {
$threads = [];
$atomic = new Swoole\Thread\Atomic();
for ($i = 0; $i < $c; $i++) {
$threads[] = Thread::exec(__FILE__, $i, $atomic);
}
for ($i = 0; $i < $c; $i++) {
$threads[$i]->join();
}
var_dump($atomic->get());
sleep(2);

Co\run(function () use($atomic) {
$n = 1024;
while ($n--) {
$atomic->add();
$rs = \Swoole\Coroutine\System::readFile(__FILE__);
var_dump(strlen($rs));
}
});
var_dump($atomic->get());
} else {
$atomic = $args[1];
Co\run(function () use($atomic) {
$n = 1024;
while ($n--) {
$atomic->add();
$rs = \Swoole\Coroutine\System::readFile(__FILE__);
var_dump(strlen($rs));
}
});
}
14 changes: 14 additions & 0 deletions examples/thread/argv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Swoole\Thread;

$args = Thread::getArguments();

if (empty($args)) {
var_dump($GLOBALS['argv']);
$thread = Thread::exec(__FILE__, 'thread-1', $argc, $argv);
$thread->join();
} else {
var_dump($args[0], $args[1], $args[2]);
sleep(1);
}
20 changes: 20 additions & 0 deletions examples/thread/array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

ini_set('memory_limit', '2G');
$dict = [];
const COUNT = 10000000;

$n = COUNT;
$s = microtime(true);
while ($n--) {
$dict['key-' . $n] = $n * 3;
}
echo 'array set: ' . round(microtime(true) - $s, 6) . ' seconds' . PHP_EOL;

$c = 0;
$n = COUNT;
$s = microtime(true);
while ($n--) {
$c += $dict['key-' . $n];
}
echo 'array get: ' . round(microtime(true) - $s, 6) . ' seconds' . PHP_EOL;
28 changes: 28 additions & 0 deletions examples/thread/atomic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Swoole\Thread;
use Swoole\Thread\Atomic;
use Swoole\Thread\Atomic\Long;

$args = Thread::getArguments();
$c = 4;
$n = 128;

if (empty($args)) {
$threads = [];
$a1 = new Atomic;
$a2 = new Long;
for ($i = 0; $i < $c; $i++) {
$threads[] = Thread::exec(__FILE__, $i, $a1, $a2);
}
for ($i = 0; $i < $c; $i++) {
$threads[$i]->join();
}
var_dump($a1->get(), $a2->get());
} else {
$a1 = $args[1];
$a2 = $args[2];

$a1->add(3);
$a2->add(7);
}
21 changes: 21 additions & 0 deletions examples/thread/benchmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
ini_set('memory_limit', '2G');
$args = Swoole\Thread::getArguments();

$dict = $args[1];
const COUNT = 10000000;

$n = COUNT;
$s = microtime(true);
while ($n--) {
$dict['key-' . $n] = $n * 3;
}
echo $args[0] . "\t" . 'array set: ' . round(microtime(true) - $s, 6) . ' seconds' . PHP_EOL;

$c = 0;
$n = COUNT;
$s = microtime(true);
while ($n--) {
$c += $dict['key-' . $n];
}
echo $args[0] . "\t" . 'array get: ' . round(microtime(true) - $s, 6) . ' seconds' . PHP_EOL;
Loading

0 comments on commit ef7f7ed

Please sign in to comment.