Question

最近,偶然,会有人说,其docker容器中syslog-ng把cpu跑满,使用perf,mpstat,strace工具看到是syslog-ng在内核态cpu使用率很高,怀疑是某个系统调用进入了死循环。

一般在宿主机上有多个syslog-ng进程,其中有的容器的,使用mpstat发现,其中容器的sys cpu飙高到100%,但是,宿主机的sys cpu没有飙高,进入一个容器下,重启这个容器的syslog-ng,发现所有的容器的syslog-ng sys cpu从100%降低到正常位置;https://github.com/balabit/syslog-ng/issues/670发现了疑似的bug,但是这个bug在syslog-ng3.8版本中才修复,当前我们的版本,7u是3.5.6,6u是3.6.4。

解决办法:

我们希望通过cgroup的方法,将syslog-ng的资源圈住。如下,对sys cpu高的,cgroup圈住的方法,测试cgroup可以解决。

Troubleshooting

[root@e98j02430.st3 /home/ahao.mah]
#top
35142 root      20   0   98448   4556   3392 R 100.0  0.0   1517:59 syslog-ng
29452 root      20   0   98604   4688   3380 R 100.0  0.0   1518:08 syslog-ng
 3407 root      20   0   98604   3136   1816 R  43.8  0.0   1518:01 syslog-ng
[root@e98j02430.st3 /home/ahao.mah]
#ps auxwf | grep syslog
root      1377  0.0  0.0 100588  4460 ?        Ss   Aug29  10:28 /usr/sbin/syslog-ng -F -p /var/run/syslog-ng.pid
root     21265  0.0  0.0 112644   956 pts/6    S+   13:13   0:00                          \_ grep --color=auto syslog
root      1461  0.0  0.0   6540   244 ?        Ss   Aug29   0:00 /usr/sbin/mcelog --ignorenodev --daemon --syslog
root      3407  5.8  0.0  98604  3136 ?        Rs   Sep23 1519:10  |   \_ /usr/sbin/syslog-ng -F -p /var/run/syslog-ng.pid
root     29452  7.5  0.0  98604  4688 ?        Rs   Sep27 1519:15  |   \_ /usr/sbin/syslog-ng -F -p /var/run/syslog-ng.pid
root     35142 50.7  0.0  98448  4556 ?        Rs   Oct09 1519:07  |   \_ /usr/sbin/syslog-ng -F -p /var/run/syslog-ng.pid

后面我会进入一个容器,然后重启这个容器内的syslog-ng,本来以为,只有这个容器的syslog-ng会从cpu 100%降低,结果,三个都降低了:

[root@e98j02430.st3 /home/ahao.mah]
#ps auxwf | grep syslog
root      1377  0.0  0.0 100588  4460 ?        Ss   Aug29  10:29 /usr/sbin/syslog-ng -F -p /var/run/syslog-ng.pid
root     58578  0.0  0.0 112644   956 pts/6    S+   13:34   0:00  |                       \_ grep --color=auto syslog
root      1461  0.0  0.0   6540   244 ?        Ss   Aug29   0:00 /usr/sbin/mcelog --ignorenodev --daemon --syslog
root      3407  5.9  0.0  98604  3136 ?        Ss   Sep23 1524:31  |   \_ /usr/sbin/syslog-ng -F -p /var/run/syslog-ng.pid
root     29452  7.6  0.0  98604  4688 ?        Ss   Sep27 1524:37  |   \_ /usr/sbin/syslog-ng -F -p /var/run/syslog-ng.pid
root     29442  0.0  0.0  98448  4488 ?        Ss   13:18   0:00  |   \_ /usr/sbin/syslog-ng -F -p /var/run/syslog-ng.pid
[root@e98j02430.st3 /home/ahao.mah]
#lsof /dev/log
COMMAND     PID USER   FD   TYPE             DEVICE SIZE/OFF      NODE NAME
systemd       1 root   35u  unix 0xffff883f2387a400      0t0        58 /dev/log
systemd    3253 root   23u  unix 0xffff880c48341400      0t0 347521463 /dev/log
systemd-j  3311 root    5u  unix 0xffff880c48341400      0t0 347521463 /dev/log
systemd   29296 root   21u  unix 0xffff883f2537f800      0t0 377859569 /dev/log
systemd-j 29350 root    5u  unix 0xffff883f2537f800      0t0 377859569 /dev/log
systemd-j 30551 root    5u  unix 0xffff883f2387a400      0t0        58 /dev/log
systemd   34993 root   20u  unix 0xffff881e40aa2800      0t0 437312090 /dev/log
systemd-j 35050 root    5u  unix 0xffff881e40aa2800      0t0 437312090 /dev/log

宿主机上你会发现:

[root@e98j02430.st3 /home/ahao.mah]
#lsof /dev/kmsg
COMMAND     PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
syslog-ng  1377  root    7r   CHR   1,11      0t0 1034 /dev/kmsg
systemd-j 30551  root    6w   CHR   1,11      0t0 1034 /dev/kmsg
systemd-j 30551  root    9u   CHR   1,11      0t0 1034 /dev/kmsg
dmesg     31994 admin    3r   CHR   1,11      0t0 1034 /dev/kmsg

容器上你会发现:

进入容器:

[root@e98j02430.st3 /]
#ps aux | grep syslog-ng
root        77  0.0  0.0  98448  4556 ?        Rs   Oct09 1521:58 /usr/sbin/syslog-ng -F -p /var/run/syslog-ng.pid
root     49004  0.0  0.0 112644   952 ?        S+   13:15   0:00 grep --color=auto syslog-ng
[root@e98j02430.st3 /]
#strace -p 77
epoll_ctl(4, EPOLL_CTL_ADD, 7, {EPOLLIN, {u32=30089056, u64=30089056}}) = 0
epoll_wait(4, {{EPOLLIN, {u32=30089056, u64=30089056}}}, 9, 19591) = 1
epoll_ctl(4, EPOLL_CTL_DEL, 7, {0, {u32=30089056, u64=30089056}}) = 0
fcntl(7, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fcntl(7, F_GETFL)                       = 0x8800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE)
setsockopt(7, SOL_SOCKET, SO_OOBINLINE, [1], 4) = -1 ENOTSOCK (Socket operation on non-socket)
epoll_ctl(4, EPOLL_CTL_ADD, 7, {EPOLLIN, {u32=30089056, u64=30089056}}) = 0
epoll_wait(4, {{EPOLLIN, {u32=30089056, u64=30089056}}}, 9, 19591) = 1
epoll_ctl(4, EPOLL_CTL_DEL, 7, {0, {u32=30089056, u64=30089056}}) = 0
fcntl(7, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fcntl(7, F_GETFL)                       = 0x8800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE)
setsockopt(7, SOL_SOCKET, SO_OOBINLINE, [1], 4) = -1 ENOTSOCK (Socket operation on non-socket)
epoll_ctl(4, EPOLL_CTL_ADD, 7, {EPOLLIN, {u32=30089056, u64=30089056}}) = 0
epoll_wait(4, {{EPOLLIN, {u32=30089056, u64=30089056}}}, 9, 19590) = 1
epoll_ctl(4, EPOLL_CTL_DEL, 7, {0, {u32=30089056, u64=30089056}}) = 0
fcntl(7, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fcntl(7, F_GETFL)                       = 0x8800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE)
setsockopt(7, SOL_SOCKET, SO_OOBINLINE, [1], 4) = -1 ENOTSOCK (Socket operation on non-socket)
epoll_ctl(4, EPOLL_CTL_ADD, 7, {EPOLLIN, {u32=30089056, u64=30089056}}) = 0
epoll_wait(4, {{EPOLLIN, {u32=30089056, u64=30089056}}}, 9, 19590) = 1
epoll_ctl(4, EPOLL_CTL_DEL, 7, {0, {u32=30089056, u64=30089056}}) = 0
fcntl(7, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fcntl(7, F_GETFL)                       = 0x8800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE)
setsockopt(7, SOL_SOCKET, SO_OOBINLINE, [1], 4) = -1 ENOTSOCK (Socket operation on non-socket)
epoll_ctl(4, EPOLL_CTL_ADD, 7, {EPOLLIN, {u32=30089056, u64=30089056}}) = 0
epoll_wait(4, {{EPOLLIN, {u32=30089056, u64=30089056}}}, 9, 19590) = 1
epoll_ctl(4, EPOLL_CTL_DEL, 7, {0, {u32=30089056, u64=30089056}}) = 0
fcntl(7, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fcntl(7, F_GETFL)                       = 0x8800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE)
setsockopt(7, SOL_SOCKET, SO_OOBINLINE, [1], 4) = -1 ENOTSOCK (Socket operation on non-socket)
epoll_ctl(4, EPOLL_CTL_ADD, 7, {EPOLLIN, {u32=30089056, u64=30089056}}) = 0
epoll_wait(4, {{EPOLLIN, {u32=30089056, u64=30089056}}}, 9, 19590) = 1
epoll_ctl(4, EPOLL_CTL_DEL, 7, {0, {u32=30089056, u64=30089056}}) = 0
fcntl(7, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fcntl(7, F_GETFL)                       = 0x8800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE)
[root@e98j02430.st3 /]
#lsof /dev/log
lsof: WARNING: can't stat() ext4 file system /tmp/.group (deleted)
      Output information may be incomplete.
lsof: WARNING: can't stat() ext4 file system /tmp/.passwd (deleted)
      Output information may be incomplete.
lsof: WARNING: can't stat() ext4 file system /tmp/bootstrap.sh (deleted)
      Output information may be incomplete.
lsof: WARNING: can't stat() tmpfs file system /run/docker.sock
      Output information may be incomplete.
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF      NODE NAME
systemd     1 root   20u  unix 0xffff881e40aa2800      0t0 437312090 /dev/log
systemd-j  52 root    5u  unix 0xffff881e40aa2800      0t0 437312090 /dev/log
[root@e98j02430.st3 /]
#pstack 77
#0  0x00007fefb2e3d80a in epoll_ctl () from /lib64/libc.so.6
#1  0x00007fefb332bfdb in __iv_fd_epoll_flush_one.isra.0 () from /lib64/libivykis.so.0
#2  0x00007fefb332c0cc in iv_fd_epoll_poll () from /lib64/libivykis.so.0
#3  0x00007fefb33298a5 in iv_fd_poll_and_run () from /lib64/libivykis.so.0
#4  0x00007fefb332a954 in iv_main () from /lib64/libivykis.so.0
#5  0x00007fefb4938ca7 in main_loop_run () from /lib64/libsyslog-ng-3.5.6.so
#6  0x0000000000401627 in main ()

我在一个容器中重启了syslog-ng,本来以为,在宿主机上只会有一个syslog-ng 100%下降,但是,我看发现,三个100%全部消失了;

[root@e98j02430.st3 /]
#strace -p 49092
Process 49092 attached
epoll_wait(4,

{}, 9, 3863)              = 0
epoll_wait(4, {}, 9, 0)                 = 0
epoll_wait(4, {}, 9, 7700)              = 0
epoll_wait(4, {}, 9, 0)                 = 0
epoll_wait(4, {}, 9, 22293)             = 0
epoll_wait(4, {}, 9, 0)                 = 0
epoll_wait(4, {}, 9, 7685)              = 0
close(15)                               = 0
epoll_wait(4, {}, 9, 0)                 = 0
epoll_wait(4, {{EPOLLIN, {u32=28761216, u64=28761216}}}, 9, 22308) = 1
epoll_ctl(4, EPOLL_CTL_DEL, 3, {0, {u32=28761216, u64=28761216}}) = 0
recvfrom(3, "<30>Oct 11 13:22:01 systemd[1]: "..., 8192, 0, {sa_family=AF_LOCAL, sun_path="/dev/log"}, [11]) = 68
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=388, ...}) = 0
write(5, "\1\0\0\0\0\0\0\0", 8)         = 8
recvfrom(3, "<30>Oct 11 13:22:01 systemd[1]: "..., 8192, 0, {sa_family=AF_LOCAL, sun_path="/dev/log"}, [11]) = 69
recvfrom(3, 0x1b73f00, 8192, 0, 0x7ffc68128550, 0x7ffc6812854c) = -1 EAGAIN (Resource temporarily unavailable)
fcntl(3, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fcntl(3, F_GETFL)                       = 0x802 (flags O_RDWR|O_NONBLOCK)
setsockopt(3, SOL_SOCKET, SO_OOBINLINE, [1], 4) = 0
epoll_ctl(4, EPOLL_CTL_ADD, 3, {EPOLLIN, {u32=28761216, u64=28761216}}) = 0
epoll_wait(4, {{EPOLLIN, {u32=28710024, u64=28710024}}}, 9, 22269) = 1
read(5, "\1\0\0\0\0\0\0\0", 8)          = 8
writev(14, [{"Oct 11 13:22:01 e98j02430 system"..., 75}], 1) = 75
writev(14, [{"Oct 11 13:22:01 e98j02430 system"..., 76}], 1) = 76
epoll_ctl(4, EPOLL_CTL_MOD, 8, {EPOLLOUT, {u32=28767704, u64=28767704}}) = 0
epoll_wait(4, {{EPOLLIN, {u32=28761216, u64=28761216}}, {EPOLLOUT, {u32=28767704, u64=28767704}}}, 9, 22269) = 2
epoll_ctl(4, EPOLL_CTL_DEL, 3, {0, {u32=28761216, u64=28761216}}) = 0
recvfrom(3, "<78>Oct 11 13:22:01 CROND[49192]"..., 8192, 0, {sa_family=AF_LOCAL, sun_path="/dev/log"}, [11]) = 84
capget({_LINUX_CAPABILITY_VERSION_3, 0}, NULL) = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, {0, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_RAW, 0}) = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, NULL) = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, {0, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_RAW, 0}) = 0
capset({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_DAC_OVERRIDE, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_RAW, 0}) = 0
stat("/var/log", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/var/log/cron", {st_mode=S_IFREG|0640, st_size=320772, ...}) = 0
open("/var/log/cron", O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_NONBLOCK, 0640) = 15
fcntl(15, F_GETFD)                      = 0
fcntl(15, F_SETFD, FD_CLOEXEC)          = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, NULL) = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_DAC_OVERRIDE, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_RAW, 0}) = 0
capset({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_CHOWN|CAP_DAC_OVERRIDE, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_RAW, 0}) = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, NULL) = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_CHOWN|CAP_DAC_OVERRIDE, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_RAW, 0}) = 0
capset({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_FOWNER, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_RAW, 0}) = 0
fchown(15, 0, 4294967295)               = 0
fchown(15, 4294967295, 4)               = 0
fchmod(15, 0640)                        = 0
capset({_LINUX_CAPABILITY_VERSION_3, 0}, {0, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_RAW, 0}) = 0
fstat(15, {st_mode=S_IFREG|0640, st_size=320772, ...}) = 0
write(5, "\1\0\0\0\0\0\0\0", 8)         = 8
recvfrom(3, 0x1b73f00, 8192, 0, 0x7ffc68128550, 0x7ffc6812854c) = -1 EAGAIN (Resource temporarily unavailable)
fcntl(3, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fcntl(3, F_GETFL)                       = 0x802 (flags O_RDWR|O_NONBLOCK)
setsockopt(3, SOL_SOCKET, SO_OOBINLINE, [1], 4) = 0
epoll_ctl(4, EPOLL_CTL_DEL, 8, {0, {u32=28767704, u64=28767704}}) = 0
sendto(8, "<30>Oct 11 13:22:01 e98j02430 sy"..., 79, 0, NULL, 0) = 79
sendto(8, "<30>Oct 11 13:22:01 e98j02430 sy"..., 80, 0, NULL, 0) = 80
sendto(8, "<78>Oct 11 13:22:01 e98j02430 CR"..., 95, 0, NULL, 0) = 95
fcntl(8, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fcntl(8, F_GETFL)                       = 0x802 (flags O_RDWR|O_NONBLOCK)
setsockopt(8, SOL_SOCKET, SO_OOBINLINE, [1], 4) = 0
epoll_ctl(4, EPOLL_CTL_ADD, 3, {EPOLLIN, {u32=28761216, u64=28761216}}) = 0
epoll_ctl(4, EPOLL_CTL_ADD, 8, {0, {u32=28767704, u64=28767704}}) = 0
epoll_wait(4, {{EPOLLIN, {u32=28710024, u64=28710024}}}, 9, 22269) = 1
read(5, "\1\0\0\0\0\0\0\0", 8)          = 8
writev(15, [{"Oct 11 13:22:01 e98j02430 CROND["..., 91}], 1) = 91
writev(14, [{"Oct 11 13:22:01 e98j02430 CROND["..., 91}], 1) = 91
[root@e98j02430.st3 /]
#pstack 49092
#0  0x00007f0e752ab843 in __epoll_wait_nocancel () from /lib64/libc.so.6
#1  0x00007f0e7579a12f in iv_fd_epoll_poll () from /lib64/libivykis.so.0
#2  0x00007f0e757978a5 in iv_fd_poll_and_run () from /lib64/libivykis.so.0
#3  0x00007f0e75798954 in iv_main () from /lib64/libivykis.so.0
#4  0x00007f0e76da6ca7 in main_loop_run () from /lib64/libsyslog-ng-3.5.6.so
#5  0x0000000000401627 in main ()

cpu在内核空间cpu飙高(sys高)

[root@jiangyi01.sqa.zmf /home/ahao.mah/muahao_git/c]
#cat cpu_sys_high.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i = 0;
    for(;;) chdir("/");
    return 0;
}

测试

[root@jiangyi01.sqa.zmf /home/ahao.mah/muahao_git/c]
#systemctl cat cc
# /etc/systemd/system/cc.service
[Unit]
Description=cc
ConditionFileIsExecutable=/usr/libexec/cc.py

[Service]
Type=simple
#ExecStart=/usr/libexec/cc.py
ExecStart=/home/ahao.mah/muahao_git/c/cpu_sys_high
Slice=jiangyi.slice
CPUAccounting=yes
#CPUQuota=40%
MemoryAccounting=yes
TasksAccounting=yes
BlockIOAccounting=yes

[Install]
WantedBy=multi-user.target
[root@jiangyi01.sqa.zmf /sys/fs/cgroup/cpu]
#mpstat -P ALL 1 112
Linux 3.10.0-327.ali2000.alios7.x86_64 (jiangyi01.sqa.zmf)  10/12/2016  _x86_64_    (24 CPU)

04:24:07 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
04:24:08 PM  all    1.79    0.00   10.91    0.08    0.00    0.00    0.00    0.00    0.00   87.21
04:24:08 PM    0    0.00    0.00    0.00    2.02    0.00    0.00    0.00    0.00    0.00   97.98
04:24:08 PM    1    3.00    0.00   97.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
04:24:08 PM    2    1.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.00
04:24:08 PM    3    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM    4    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM    5    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM    6    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM    7   35.00    0.00   65.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
04:24:08 PM    8    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM    9    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   10    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   11    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   12    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   13    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   14    0.00    0.00    0.99    0.00    0.00    0.00    0.00    0.00    0.00   99.01
04:24:08 PM   15    3.00    0.00   97.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
04:24:08 PM   16    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   17    0.00    0.00    0.99    0.00    0.00    0.00    0.00    0.00    0.00   99.01
04:24:08 PM   18    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   19    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   20    0.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00    0.00   99.00
04:24:08 PM   21    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   22    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:24:08 PM   23    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

结果

当开启CPUQuota=40%如下,cpu的占用率会降低到40%

[root@jiangyi01.sqa.zmf /sys/fs/cgroup/cpu]
#mpstat -P ALL 1 112
Linux 3.10.0-327.ali2000.alios7.x86_64 (jiangyi01.sqa.zmf)  10/12/2016  _x86_64_    (24 CPU)

04:30:59 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
04:31:00 PM  all    1.71    0.00    4.79    0.00    0.00    0.00    0.00    0.00    0.00   93.50
04:31:00 PM    0    1.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.00
04:31:00 PM    1    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM    2    1.01    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.99
04:31:00 PM    3    0.99    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.01
04:31:00 PM    4    0.00    0.00    0.99    0.00    0.00    0.00    0.00    0.00    0.00   99.01
04:31:00 PM    5    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM    6    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM    7   34.34    0.00   65.66    0.00    0.00    0.00    0.00    0.00    0.00    0.00
04:31:00 PM    8    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM    9    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   10    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   11    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   12    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   13    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   14    0.00    0.00    0.99    0.00    0.00    0.00    0.00    0.00    0.00   99.01
04:31:00 PM   15    3.96    0.00   46.53    0.00    0.00    0.00    0.00    0.00    0.00   49.50
04:31:00 PM   16    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   17    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   18    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   19    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   20    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   21    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   22    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
04:31:00 PM   23    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

REF

为什么写这篇博客

Linux的top或者ps都可以查看进程的cpu利用率,那为什么还需要了解这个细节呢。编写这篇文章呢有如下三个原因:

希望在脚本中,能够以过”非阻塞”的方式获取进程cpu利用率

ps无法获得进程当前时刻的CPU利用率;top则需要至少1秒才能获得进程当前的利用率

好奇

http://blog.sae.sina.com.cn/archives/2829

Linux平台Cpu使用率的计算

http://www.blogjava.net/fjzag/articles/317773.html

在linux的系统维护中,可能需要经常查看cpu使用率,分析系统整体的运行情况。而监控CPU的性能一般包括以下3点:运行队列、CPU使用率和上下文切换。

对于每一个CPU来说运行队列最好不要超过3,例如,如果是双核CPU就不要超过6。如果队列长期保持在3以上,说明任何一个进程运行时都不能马上得到cpu的响应,这时可能需要考虑升级cpu。另外满负荷运行cpu的使用率最好是user空间保持在65%~70%,system空间保持在30%,空闲保持在0%~5% 。

下面总结下查看CPU使用率常用的几个命令。

http://ju.outofmemory.cn/entry/193618

BUG:

https://github.com/balabit/syslog-ng/issues/670

syslog-ng-3.5.6把容器的单核cpu跑满的更多相关文章

  1. Docker: 限制容器可用的 CPU

    默认情况下容器可以使用的主机 CPU 资源是不受限制的.和内存资源的使用一样,如果不对容器可以使用的 CPU 资源进行限制,一旦发生容器内程序异常使用 CPU 的情况,很可能把整个主机的 CPU 资源 ...

  2. docker容器内存和CPU使用限制

    docker容器内存和CPU使用限制 示例如下 sudo docker run --name seckill0 -p 8080:8080 -m 1024M --cpus=0.2 -d seckill: ...

  3. docker容器资源限制:限制容器对内存/CPU的访问

    目录 一.系统环境 二.前言 三.docker对于CPU和内存的限制 3.1 限制容器对内存的访问 3.2 限制容器对CPU的访问 一.系统环境 服务器版本 docker软件版本 CPU架构 Cent ...

  4. docker19.03限制容器使用的cpu资源

    一,用--cpus限制可用的cpu个数 例子: [root@localhost liuhongdi]# docker run -idt --name kafka1 --hostname kafka1 ...

  5. kubernetes基础知识:限制POD和容器运行的CPU、内存

    限制运行内存 https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/ 先看一个pod的yaml ...

  6. docker容器安装及使用技巧

    关于docker前言 A)首先是关于虚拟化 虚拟化我们可以简单的理解为一种资源管理方式.有如下几种虚拟化的方式: 1.完全虚拟化:对底层硬件实现完全的虚拟.例如:Vmware Workstation ...

  7. tasksetCPU亲和力&docke容器资源限制

    [taskset详解] taskset设置cpu亲和力,taskset能够将一个或者多个进程绑定到一个或者多个处理器上运行 参数: 选项: -a, --all-tasks 在给定 pid 的所有任务( ...

  8. docker容器日志收集方案汇总评价总结

    docker日志收集方案有太多,下面截图罗列docker官方给的日志收集方案(详细请转docker官方文档).很多方案都不适合我们下面的系列文章没有说. 经过以下5篇博客的叙述简单说下docker容器 ...

  9. docker——容器(container)

    容器相关命令一览表: docker create docker run docker start/stop/restart docker attach/exec docker rm docker ex ...

随机推荐

  1. 有趣的JavaScript

    让你的js代码从一行开始,另一行结束: var s /*:String*/ = "Test \ multi \ line."; 看到没,只需要在你换行的字符串后面加上反斜线(bac ...

  2. C++中的重载、覆盖、隐藏

    前几天面试时被问及C++中的覆盖.隐藏,概念基本答不上来,只答了怎么用指针实现多态,也还有遗漏.最终不欢而散.回来后在网上查找学习了一番,做了这个总结.其中部分文字借用了别人的博客,望不要见怪.引用的 ...

  3. mysql 获取当前时间戳

      mysql 获取当前时间为select now() 运行结果: 2012-09-05 17:24:15 mysql 获取当前时间戳为select unix_timestamp(now()) 运行结 ...

  4. Direct3D 11的流水线

    流水线 流水线(Pipeline)是理解D3D必须要掌握的概念. 整个流水线有很多步骤,有的步骤是固定功能,不用怎么配置,有的步骤是要写代码的,也就是所谓的着色器程序(Shader). 一般来说,将流 ...

  5. JSON解析---初识

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 全然独立于语言的文本格式 易于人阅读和编写 易于解析和生成 (网络传输速度快) JSON语法规则 数据在 ...

  6. 使用HashMap对象传递url參数有用工具类

    代码例如以下: package com.yanek.util; import java.util.ArrayList; import java.util.Collections; import jav ...

  7. oracle SQL语句练习MERGE、模糊查询、排序、

    Oracle支持的SQL指令可分为数据操作语言语句.数据定义语言语句.事务控制语句.会话控制语句等几种类型:1.数据操作语言语句数据操作语言语句(Data manipulation language, ...

  8. (转)javascript深入理解js闭包

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...

  9. 光盘自动运行HTML页,Autorun文件写法

    1.把你的网页放在一个根目录下面,起名为index.html 2.在目录新建一个autorun.inf的文件,打开后编辑为以下内容: 代码如下: [autorun]icon=***.ico(加图标) ...

  10. 第一次写博客,关于前端开发deMVC在js中的应用

    对前端MVC MVC分别是model.view.controller的缩写,模型.视图.控制器.这些更加偏向于后台,在以前MVC是只属于后台的.当然随着技术的进步,前端的大牛们将后台的一些东西应用于前 ...