登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Code@Pig Home

喜欢背着一袋Code傻笑的Pig .. 忧美.欢笑.记忆.忘却 .之. 角落

 
 
 

日志

 
 

[FreeBSD] usr.bin/top  

2009-10-02 15:12:55|  分类: os_FreeBSD |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
感谢水龙同学贡献此文,我就不客气,收藏了。
top指令输出的内容比较多,首先说一下偶认为比较有用的过滤选项:

-Uxxx:只显示用户名为xxx的进程
-S:切换是否显示系统进程
-I:切换是否显示空闲的进程
-b:进入非交互模式
-s:设置刷新时间
-m:设置显示的数据,有cpu以及io两种

top的输出分为2个部分,第一部分为系统信息,比较重要的是CPU以及MEM两项:
CPU:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle
其中的nice表示被nice命令改变优先级的任务占用的CPU时间百分比

Mem: 2210M Active, 629M Inact, 235M Wired, 118M Cache, 112M Buf, 63M Free
获取方式为:
GETSYSCTL("vfs.bufspace", bufspace);
GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]);        /* number of pages active */
GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]);        /* number of pages inactive */
GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]);            /* number of pages active */    
GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]);        /* number of pages on buffer cache queue */
GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]);            /* number of pages free */    
GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin);                /* swap pager pages paged in */
GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout);            /* swap pager pages paged out */    

具体各种内存的含义
http://www.hudong.com/wiki/FreeBSD%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D

active是当前系统中在一定的时间内被持续使用的页面,系统中有一个后台核心进程专门扫描active队列,当有发现好久没有使用某个页面时,内核就会把该页面移动到inactive队列。

inactive队列,个人推测,即使使用其的进程已经退出,也不会把进程对应的Inact的内容放到Free里面。直到 Free 不够用了,才会动用Inact里面的内存。
当Inact里面的值很大时,可以通过下面的小程序,不停分配内存,同时看到Inact的内容慢慢减少。
----------------
#include <stdio.h>
#include <unistd.h>

int main()
{
        while (1)
        {
                void *p = malloc(1024*1024*50);
                memset(p, 0, 1024*1024*50);
                usleep(1000*1000*1);
                printf("vvv\n");
        }

        return 0;
}
----------------

cache队列中的页面是有些子系统释放某些页面,但是页面中的数据依然有效,将来还有可能再次使用其中的数据的页面,只是这些页面已经是“干净的”,不用再存盘,典型的是文件系统使用这种技术,用户关闭文件,但是文件系统有自己的cache管理,不一定就立即关闭文件,而将数据留在内存中,以便下次使用时能迅速获得数据而不需要读盘。

free的意思很明了,其中不包含数据也没有谁再与其有联系。无论上述是哪种页面,内核都可以随时那来作其他用途,只是使用的优先级别不一样,在内核需要一个新页时,free的页面被最先考虑,但是当数值小于一定量时,就会从cache类型的队列中找,cache中的页面被取出时,需要与原来的vm_object交互,或许要脱钩,当然没有申请free的页面来得快,但使用cache还是很快的,如果cache队列中没有了页面,就会去找inactive队列中的页面, inactive中的页面有可能是“Dirty”,在拿做其他用途时,不但要与原来的vm_object脱钩,而且还要因为是“Dirty”而要引起写盘,万不得已时active队列中的页面就会被拿作他用,这是系统会非常繁忙了。

wired页面, 意思是被挂起的,这些页面是不在上面讲的队列中的,因此它不能被交换到硬盘,这些页面多了会导致系统使用内存紧张,需要交换时可以活动的余地就会减少,导致系统性能下降。通常内核中的数据结构都使用wired页面,避免在内核使用自己的数据时发生缺页而导致不可预料的结果,通常是无法恢复故障而崩溃,负载较重的系统,wired页面会多一些。而除了内核使用的内存是wired以外,用户进程则几 乎全部使用上述四种队列种的页面,全是可以被丢弃而再重新恢复的那种类型。wired页面还可以由用户进程调用mlock系统调用锁定页面而引起的。


top输出的第二部分为各个进程的信息:
  PID    USERNAME    THR PRI NICE   SIZE       RES       STATE  C   TIME   WCPU  COMMAND
10368 mysql                12       20    0      56868K 25940K   kserel    0    3:25     0.00%  mysqld

PRI,为进程优先级
SIZE,为虚存大小
RES,为实存大小
C,为该进程运行的cpu编号
CPU,CPU占用时间
WCPU,权重CPU占用时间

CPU与WCPU的关系为:
#define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \
        ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu))))
其中pct为CPU/100, (pp)->ki_swtime 为当前进程swap in/out的时间,lopcpu为根据kern.ccpu计算的一个常数
详细的解释还可参考:http://lopsa.org/node/1495
按方向键“右”可以切换CPU/WCPU,不过按上面文章说,其实WCPU是为早期的bsd设计的,现代操作系统调度算法已经很先进了,WCPU其实反倒没啥意义。是否如此,有待考证。



STATE为进程当前的状态
http://www.webhostingtalk.com/showthread.php?t=583672

select
Process is blocked in the select(2) syscall. Usually means it's waiting for external input (network, terminal, file updated, things like that). select only supports waiting for something related to a file descriptor (e.g., socket, tty, vnode).

nanslp
Process is blocked in the nanosleep(2) syscall, which is an explicit request to pause for a certain amount of time. Hard to generalize this one. It should be infrequent, or it might be used by something implementing its own polling.

kserel
Process is blocked waiting for an event to trigger a KSE upcall. This isn't easy to explain in a few words, but you can read kse(2) for most of the relevant details. Usually means the process is waiting for external input (similar to select).
kse 对应到程序就是 pthread 啦。

RUN
Process is ready to run but has been suspended while another process is running.

pause
Process is blocked in the pause(2) or sigsuspend(2) syscall. It might be waiting for a signal, or you might see this if another thread is running and the main thread is waiting for all other threads to end.

lockf
Process is blocked waiting for a file lock to be released. Could be an flock(2) lock or an fcntl(2)/F_SETLK record lock.

kqread
Process is blocked in the kqueue(2) syscall. This is similar to select(2)--waiting for external input. kqueue is more efficient than select and supports several other events that can be waited on.

sbwait
Process is waiting for a socket buffer to be filled or emptied.
比如 read 一个 block-fd,此时没有数据,就 block 住啦。

pipered
Process is waiting for data to arrive on a pipe ("rd" is short for read).
  评论这张
 
阅读(1431)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018