0

启动server配置

$server->set(
            [
                'enable_static_handler' => true,
                'document_root'         => '/usr/share/nginx/xin_mount_dev/swoole30/demo/data/',
                'task_worker_num'       => 4,
                'worker_num'            => 2,
               
            ]

        );

查看启动进程

root@swoole_dev:/home/tb/mount_sh_all# ps -aux |grep ws_server
root      1826  0.0  3.5 390640 36572 pts/1    Sl+  23:03   0:00 php ws_server_oop.php
root      1827  0.0  0.8 316412  8368 pts/1    S+   23:03   0:00 php ws_server_oop.php
root      1829  0.0  0.8 306152  8824 pts/1    S+   23:03   0:00 php ws_server_oop.php
root      1830  0.0  0.8 306152  8824 pts/1    S+   23:03   0:00 php ws_server_oop.php
root      1831  0.0  0.8 306152  8824 pts/1    S+   23:03   0:00 php ws_server_oop.php
root      1832  0.0  0.8 306152  8824 pts/1    S+   23:03   0:00 php ws_server_oop.php
root      1833  0.0  0.8 308456  9080 pts/1    S+   23:03   0:00 php ws_server_oop.php
root      1834  0.0  0.8 308456  9080 pts/1    S+   23:03   0:00 php ws_server_oop.php
root      1837  0.0  0.1  12944  1032 pts/0    S+   23:04   0:00 grep --color=auto ws_server

其他信息

oot@swoole_dev:/proc/1828# ls -ail
total 0
22348 dr-xr-xr-x   9 root root 0 Jun  8 23:03 .
    1 dr-xr-xr-x 133 root root 0 Jun  8 22:29 ..
22611 dr-xr-xr-x   2 root root 0 Jun  8 23:08 attr
22593 -rw-r--r--   1 root root 0 Jun  8 23:08 autogroup
22588 -r--------   1 root root 0 Jun  8 23:08 auxv
22616 -r--r--r--   1 root root 0 Jun  8 23:08 cgroup
22608 --w-------   1 root root 0 Jun  8 23:08 clear_refs
22596 -r--r--r--   1 root root 0 Jun  8 23:08 cmdline
22594 -rw-r--r--   1 root root 0 Jun  8 23:08 comm
22622 -rw-r--r--   1 root root 0 Jun  8 23:08 coredump_filter
22615 -r--r--r--   1 root root 0 Jun  8 23:08 cpuset
22602 lrwxrwxrwx   1 root root 0 Jun  8 23:08 cwd -> /usr/share/nginx/xin_mount_dev/swoole30/demo/server
22587 -r--------   1 root root 0 Jun  8 23:08 environ
22604 lrwxrwxrwx   1 root root 0 Jun  8 23:08 exe -> /usr/bin/php7.0
22583 dr-x------   2 root root 0 Jun  8 23:08 fd
22585 dr-x------   2 root root 0 Jun  8 23:08 fdinfo
22625 -rw-r--r--   1 root root 0 Jun  8 23:08 gid_map
22623 -r--------   1 root root 0 Jun  8 23:08 io
22591 -r--r--r--   1 root root 0 Jun  8 23:08 limits
22620 -rw-r--r--   1 root root 0 Jun  8 23:08 loginuid
22584 dr-x------   2 root root 0 Jun  8 23:08 map_files
22599 -r--r--r--   1 root root 0 Jun  8 23:08 maps
22601 -rw-------   1 root root 0 Jun  8 23:08 mem
22606 -r--r--r--   1 root root 0 Jun  8 23:08 mountinfo
22605 -r--r--r--   1 root root 0 Jun  8 23:08 mounts
22607 -r--------   1 root root 0 Jun  8 23:08 mountstats
22586 dr-xr-xr-x   5 root root 0 Jun  8 23:08 net
22349 dr-x--x--x   2 root root 0 Jun  8 23:03 ns
22600 -r--r--r--   1 root root 0 Jun  8 23:08 numa_maps
22618 -rw-r--r--   1 root root 0 Jun  8 23:08 oom_adj
22617 -r--r--r--   1 root root 0 Jun  8 23:08 oom_score
22619 -rw-r--r--   1 root root 0 Jun  8 23:08 oom_score_adj
22610 -r--------   1 root root 0 Jun  8 23:08 pagemap
22590 -r--------   1 root root 0 Jun  8 23:08 personality
22626 -rw-r--r--   1 root root 0 Jun  8 23:08 projid_map
22603 lrwxrwxrwx   1 root root 0 Jun  8 23:08 root -> /
22592 -rw-r--r--   1 root root 0 Jun  8 23:08 sched
=========================================
oot@swoole_dev:/proc/1828# ps -aux |grep 1827 |grep -v grep
root      1827  0.0  0.8 316412  8368 pts/1    S+   23:03   0:00 php ws_server_oop.php
root@swoole_dev:/proc/1828# ps -aux |grep 1826 |grep -v grep
root      1826  0.0  3.5 390640 36572 pts/1    Sl+  23:03   0:00 php ws_server_oop.php
root@swoole_dev:/proc/1828# ps -aux |grep 1828 |grep -v grep
root@swoole_dev:/proc/1828# ps -aux |grep 1831 |grep -v grep
root      1831  0.0  0.8 306152  8824 pts/1    S+   23:03   0:00 php ws_server_oop.php

疑问 1828这个进程id代表啥呢?

root@swoole_dev:/home/tb/mount_sh_all# pstree -p 1826
php(1826)─┬─php(1827)─┬─php(1829)
          │           ├─php(1830)
          │           ├─php(1831)
          │           ├─php(1832)
          │           ├─php(1833)
          │           └─php(1834)
          └─{php}(1828)

可以看出MASTER ID IS :1826
MANAGER_PID IS: 1827,那么1828这个是?

2019-06-08 提问
2 个回答
0

看配置应该有8个进程。1个master 1manager 进程2worker 4task

0
#进程树关系
php(1672)─┬─php(1673)─┬─php(1676)
          │           ├─php(1677)
          │           ├─php(1678)
          │           ├─php(1679)
          │           ├─php(1680)
          │           └─php(1681)
          ├─{php}(1674)
          └─{php}(1675)
#追踪master进程,epoll_wait          
strace -p 1672
strace: Process 1672 attached
epoll_wait(19, [], 4096, 1000)          = 0
madvise(0x7ff8e800f000, 4096, MADV_DONTNEED) = 0
epoll_wait(19, [], 4096, 1000)          = 0
epoll_wait(19, [], 4096, 1000)          = 0
madvise(0x7ff8e800f000, 4096, MADV_DONTNEED) = 0
epoll_wait(19, [], 4096, 1000)          = 0
epoll_wait(19, ^Cstrace: Process 1672 detached
 <detached ...>
#追踪manager进程,wait
root@swoole_dev:/home/tb# strace -p 1673
strace: Process 1673 attached
wait4(-1, 
#追踪{线程},epoll_wait 
root@swoole_dev:/home/tb# strace -p 1674
strace: Process 1674 attached
epoll_wait(21, 

#追踪{线程},epoll_wait 
root@swoole_dev:/home/tb# strace -p 1675
strace: Process 1675 attached
epoll_wait(22, 

#追踪woker task进程
root@swoole_dev:/home/tb# strace -p 1676
strace: Process 1676 attached
read(11, 
#查看1672线程 
root@swoole_dev:/home/tb# ps -T -p 1672
  PID  SPID TTY          TIME CMD
 1672  1672 pts/0    00:00:00 php
 1672  1674 pts/0    00:00:00 php
 1672  1675 pts/0    00:00:00 php
#查看1673 manager进程
root@swoole_dev:/home/tb# ps -T -p 1673
  PID  SPID TTY          TIME CMD
 1673  1673 pts/0    00:00:00 php
#查看线程
root@swoole_dev:/home/tb# ps -T -p 1674
  PID  SPID TTY          TIME CMD
#查看线程
root@swoole_dev:/home/tb# ps -T -p 1675
  PID  SPID TTY          TIME CMD
#查看woker task进程
root@swoole_dev:/home/tb# ps -T -p 1676
  PID  SPID TTY          TIME CMD
 1676  1676 pts/0    00:00:00 php
#查看woker task进程
root@swoole_dev:/home/tb# ps -T -p 1677
  PID  SPID TTY          TIME CMD
 1677  1677 pts/0    00:00:00 php
root@swoole_dev:/home/tb# 

clipboard.png

Swoole的主进程是一个多线程的程序。其中有一组很重要的线程,称之为Reactor线程。它就是真正处理TCP连接,收发数据的线程。

Swoole的主线程在Accept新的连接后,会将这个连接分配给一个固定的Reactor线程,并由这个线程负责监听此socket。在socket可读时读取数据,并进行协议解析,将请求投递到Worker进程。在socket可写时将数据发送给TCP客户端。

Reactor线程数,reactor_num => 2,通过此参数来调节主进程内事件处理线程的数量,以充分利用多核。默认会启用CPU核数相同的数量。reactor_num建议设置为CPU核数的1-4倍

reactor_num最大不得超过SWOOLE_CPU_NUM 4Reactor线程是可以利用多核,如:机器有128核,那么底层会启动128线程。每个线程能都会维持一个EventLoop。线程之间是无锁的,指令可以被128核CPU并行执行。考虑到操作系统调度存在一定程度的性能损失,可以设置为CPU核数2,以便最大化利用CPU的每一个核。

撰写答案

推广链接