Ubuntu 18+ 上的 php 7.3 mmap munmap 太多调用\时间

Ubuntu 18+ 上的 php 7.3 mmap munmap 太多调用\时间

今天是个好日子。在将高负载后端 php 服务器迁移到新服务器后,我遇到了问题。 ( 投入的 ) 。为此我开始比较 ubuntu 16 和 20(18 相同)。

我购买了 2 个类似的 m5.large aws 实例并简单地检查:

简单的脚本strace -c php -r " exit(1);"

这给了我

ubuntu@ip-172-31-40-209:~$ sudo uname -srm
Linux 5.4.0-1009-aws x86_64
ubuntu@ip-172-31-40-209:~$ sudo php --version
PHP 7.3.17-1+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Apr 28 2020 14:49:03) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.17, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.17-1+ubuntu20.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
ubuntu@ip-172-31-40-209:~$ sudo sysctl -a | grep vm.nr_hugepages
vm.nr_hugepages = 0
vm.nr_hugepages_mempolicy = 0
ubuntu@ip-172-31-40-209:~$ cat /sys/kernel/mm/transparent_hugepage/enabled
always [madvise] never
ubuntu@ip-172-31-40-209:~$ cat /sys/kernel/mm/transparent_hugepage/defrag
always defer defer+madvise [madvise] never


% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 31.94    0.002967          18       157           munmap
 27.64    0.002567           4       549         1 mmap
 12.12    0.001126           6       173           mprotect
  9.32    0.000866           4       182         5 openat
  4.86    0.000451           3       135         1 read
  4.35    0.000404           2       182           close
  3.63    0.000337           1       181           fstat
  1.48    0.000137           6        20           futex
  1.02    0.000095           3        26           brk
  0.74    0.000069          69         1           poll
  0.60    0.000056           0        85           rt_sigaction
  0.54    0.000050           4        11           fcntl
  0.36    0.000033           8         4           write
  0.25    0.000023          11         2           writev
  0.18    0.000017           2         8           getpid
  0.17    0.000016           4         4           uname
  0.12    0.000011          11         1           sysinfo
  0.12    0.000011           3         3           getrandom
  0.11    0.000010          10         1           connect
  0.10    0.000009           0        40         1 stat
  0.06    0.000006           6         1           unlink
  0.06    0.000006           6         1           fchmod
  0.05    0.000005           2         2           socket
  0.04    0.000004           2         2           gettid
  0.03    0.000003           1         2           rt_sigprocmask
  0.03    0.000003           1         3         2 access
  0.02    0.000002           2         1           getegid
  0.01    0.000001           1         1           getuid
  0.01    0.000001           1         1           getgid
  0.01    0.000001           1         1           geteuid
  0.01    0.000001           1         1           getppid
  0.00    0.000000           0         8         3 lstat
  0.00    0.000000           0        43         3 lseek
  0.00    0.000000           0        39        39 ioctl
  0.00    0.000000           0         8           pread64
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         1           getcwd
  0.00    0.000000           0         2           readlink
  0.00    0.000000           0         3           getrusage
  0.00    0.000000           0         2         1 arch_prctl
  0.00    0.000000           0         2           getdents64
  0.00    0.000000           0         1           set_tid_address
  0.00    0.000000           0         1           set_robust_list
  0.00    0.000000           0         2           prlimit64
------ ----------- ----------- --------- --------- ----------------
100.00    0.009288                  1894        56 total

https://gist.github.com/futureisrise/46cc3d0bb55e612038fb7a95bc5efbfa完整的 strace 调用。

相同的脚本在 ubuntu 16 上运行。

ubuntu@ip-172-31-44-252:~$ sudo uname -srm
Linux 4.4.0-1105-aws x86_64
ubuntu@ip-172-31-44-252:~$ sudo php -v
PHP 7.3.17-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Apr 19 2020 07:44:05) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.17, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.17-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

ubuntu@ip-172-31-44-252:~$sudo sysctl -a | grep vm.nr_hugepages
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.ens5.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
vm.nr_hugepages = 0
vm.nr_hugepages_mempolicy = 0
ubuntu@ip-172-31-44-252:~$ cat /sys/kernel/mm/transparent_hugepage/enabled
always [madvise] never
ubuntu@ip-172-31-44-252:~$ cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never


% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  0.00    0.000000           0        46           read
  0.00    0.000000           0        69         3 open
  0.00    0.000000           0        69           close
  0.00    0.000000           0        20           stat
  0.00    0.000000           0        73           fstat
  0.00    0.000000           0         8         3 lstat
  0.00    0.000000           0        24         3 lseek
  0.00    0.000000           0       130           mmap
  0.00    0.000000           0        81           mprotect
  0.00    0.000000           0        52           munmap
  0.00    0.000000           0        12           brk
  0.00    0.000000           0        80           rt_sigaction
  0.00    0.000000           0         2           rt_sigprocmask
  0.00    0.000000           0        20        20 ioctl
  0.00    0.000000           0        24        23 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         2           getdents
  0.00    0.000000           0         1           getcwd
  0.00    0.000000           0         2           readlink
  0.00    0.000000           0         2           getrlimit
  0.00    0.000000           0         1           sysinfo
  0.00    0.000000           0         1           arch_prctl
  0.00    0.000000           0        19           futex
  0.00    0.000000           0         1           set_tid_address
  0.00    0.000000           0         1           clock_getres
  0.00    0.000000           0         1           set_robust_list
  0.00    0.000000           0         2           getrandom
------ ----------- ----------- --------- --------- ----------------
100.00    0.000000                   744        52 total

详细信息在这里https://gist.github.com/futureisrise/de0a54d28e794c4d9bff714a1cbd21e1

正如我们所看到的,ubuntu 16 速度快了很多倍。还通过 blackfire io、newrelic 等进行了测试。当出现 DDOS 攻击时 - 服务器 CPU 负载非常高。

为了让这个测试清楚 - 刚刚安装

add-apt-repository ppa:ondrej/php
apt update
apt install strace php7.3 php7.3-cli

相关内容