V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
ioiioi
V2EX  ›  Linux

linux 下的 top 和 htop 不尽相同,该相信谁?

  •  
  •   ioiioi · 2015-07-21 00:51:38 +08:00 · 8850 次点击
    这是一个创建于 3173 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我跑了一个很费资源的程序,开始用top看的时候cpu利用率达到9x%,随后用htop看了以下,每核cpu的利用率倒是没超过50%,我该相信谁?

    1  [||||||                                  6.8%]     Tasks: 33, 19 thr; 2 running
    2  [||||||||||                             13.0%]     Load average: 1.01 0.66 0.28
    3  [||||||||||||||||||||||||||||           36.4%]     Uptime: 00:04:34
    4  [|||||||||||||||                        20.1%]
    5  [|||||||||||||||||||                    24.8%]
    6  [||||||||||||||||||                     23.8%]
    7  [||||||||||||||                         17.7%]
    8  [||||||                                  8.2%]
    Mem[||||||                            432/7870MB]
    Swp[                                       0/0MB]
    
      PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
     1715 root       20   0  995M   99M  2676 S 93.0  1.3  4:05.50 /usr/local/unba/bin/probe
     1811 root       20   0  995M   99M  2676 S 33.0  1.3  1:16.48 /usr/local/unba/bin/probe
     1813 root       20   0  995M   99M  2676 S 29.0  1.3  0:59.18 /usr/local/unba/bin/probe
     1809 root       20   0  995M   99M  2676 S 13.0  1.3  0:46.68 /usr/local/unba/bin/probe
     1805 root       20   0  995M   99M  2676 S 13.0  1.3  0:45.78 /usr/local/unba/bin/probe
     1899 chenrui    20   0  156M  5044  1308 S  2.0  0.1  0:00.72 mosh-server new -s -c 0
     1560 root       20   0  249M  3776  1092 S  2.0  0.0  0:06.95 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
     1807 root       20   0  995M   99M  2676 S  2.0  1.3  0:05.11 /usr/local/unba/bin/probe
    ...
    

    使用了tmux分隔,所以下面的top信息跟htop是同屏显示的,时间是同步的。

    top - 00:41:29 up 4 min,  2 users,  load average: 1.01, 0.66, 0.28
    Tasks: 175 total,   2 running, 173 sleeping,   0 stopped,   0 zombie
    Cpu(s):  3.1%us,  2.0%sy,  0.0%ni, 81.6%id,  0.2%wa,  0.1%hi, 12.9%si,  0.2%st
    Mem:   8058904k total,   570824k used,  7488080k free,    55012k buffers
    Swap:        0k total,        0k used,        0k free,    72820k cached
    
      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
     1715 root      20   0  995m  99m 2676 S 87.9  1.3   4:04.45 probe
       33 root      20   0     0    0    0 S 44.9  0.0   0:32.22 ksoftirqd/7
       13 root      20   0     0    0    0 R 37.1  0.0   0:28.72 ksoftirqd/2
       17 root      20   0     0    0    0 S  7.8  0.0   0:33.92 ksoftirqd/3
     1899 chenrui   20   0  156m 4916 1308 S  5.9  0.1   0:00.70 mosh-server
       31 root      RT   0     0    0    0 S  2.0  0.0   0:00.01 migration/7
       42 root      20   0     0    0    0 S  2.0  0.0   0:00.13 events/7
     1560 root      20   0  249m 3776 1092 S  2.0  0.0   0:06.91 rsyslogd
     1922 chenrui   20   0 25992 1604 1072 S  2.0  0.0   0:00.04 tmux
     1961 root      20   0  110m 2300 1240 S  2.0  0.0   0:00.31 htop
     1979 chenrui   20   0 15028 1224  868 R  2.0  0.0   0:00.01 top
        1 root      20   0 19232 1484 1192 S  0.0  0.0   0:01.13 init
        2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
        3 root      RT   0     0    0    0 S  0.0  0.0   0:00.01 migration/0
        4 root      20   0     0    0    0 S  0.0  0.0   0:29.03 ksoftirqd/0
        ...
    
    15 条回复    2015-08-03 07:48:17 +08:00
    Andy1999
        1
    Andy1999  
       2015-07-21 00:59:36 +08:00 via iPhone
    我用探针看的
    nine
        2
    nine  
       2015-07-21 01:14:41 +08:00
    都准,未必是同步的
    AstroProfundis
        3
    AstroProfundis  
       2015-07-21 01:15:09 +08:00
    top 是所有 cpu 的总和,htop 是每个 cpu 分开的
    你把 htop 里面单个 cpu 的值加起来应该和 top 里面的差不多
    ffffwh
        4
    ffffwh  
       2015-07-21 01:15:58 +08:00
    应该都是对的。。
    问题是怎么解读。。。
    超线程算到谁头上?
    redsonic
        5
    redsonic  
       2015-07-21 02:51:41 +08:00   ❤️ 3
    LZ误解了,htop里面8个cpu平均18。和top里面的us+sy+si+hi差不多,所以不矛盾的。

    此外,对于用户态的东西,htop一行对应一个线程,top一行对应一个进程,所以top里面你看到同样的多线程应用,top的比htop的高,甚至超过100,但所有线程加一起就和top里的一样多。

    如果想了解硬件的真实负载可以安装powertop,他的数据有一部分来自cpu硬件内部的统计而不是操作系统。
    msg7086
        6
    msg7086  
       2015-07-21 06:13:15 +08:00
    Cpu(s): 3.1%us, 2.0%sy, 0.0%ni, 81.6%id, 0.2%wa, 0.1%hi, 12.9%si, 0.2%st

    不是写着 81.6% idle 么,负载哪里超过一半了。

    要是超过一半的话,怎么也不可能87.9%吧,至少400%了吧?
    lujiajing1126
        7
    lujiajing1126  
       2015-07-21 08:30:20 +08:00 via Android
    @msg7086
    负载和占用率不是一个概念啊
    maxbon
        8
    maxbon  
       2015-07-21 08:31:43 +08:00   ❤️ 1
    top的时候你按一下数字键1,你就知道了
    xdeng
        9
    xdeng  
       2015-07-21 08:48:35 +08:00
    采样时间点不一样
    Ranler
        10
    Ranler  
       2015-07-21 09:02:48 +08:00
    都是从procfs中读出的数据,所以原始数据是一样的,只是数据读出时间和数据整合方式不同
    lijianying10
        11
    lijianying10  
       2015-07-21 09:53:38 +08:00
    如果楼主看完这篇:
    http://server.51cto.com/sCollege-188250.htm
    可以发现,CPU使用率仅仅是一个统计而已。
    通过包含了每一种cpu使用情况时间的统计。
    因此从不同时刻考虑cpu使用率有不同的统计结果。

    最后希望能帮到楼主。
    ioiioi
        12
    ioiioi  
    OP
       2015-07-21 14:56:25 +08:00
    看来我得先理解一下linux的工作机制再过来提问,我现在还搞不清top中的那一行

    ```
    Cpu(s): 3.1%us, 2.0%sy, 0.0%ni, 81.6%id, 0.2%wa, 0.1%hi, 12.9%si, 0.2%st
    ```

    到底啥意思。
    JamesRuan
        13
    JamesRuan  
       2015-07-21 20:34:06 +08:00
    @ioiioi
    us, user : time running un-niced user processes
    sy, system : time running kernel processes
    ni, nice : time running niced user processes
    id, idle : time spent in the kernel idle handler
    wa, IO-wait : time waiting for I/O completion
    hi : time spent servicing hardware interrupts
    si : time spent servicing software interrupts
    st : time stolen from this vm by the hypervisor
    ioiioi
        14
    ioiioi  
    OP
       2015-07-22 22:56:11 +08:00
    @JamesRuan
    谢谢,我想了解的不仅仅是字面的意思,我更想知道在什么样的情况下是cpu遇到了瓶颈,我估计除了top,还需要配合其它工具才能进行性能的调优。
    jedihy
        15
    jedihy  
       2015-08-03 07:48:17 +08:00
    多核平均和单核的区别。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5067 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:45 · PVG 17:45 · LAX 02:45 · JFK 05:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.