如何在内核中查看进程的占用内存及CPU

218 views
Skip to first unread message

. dyron

unread,
Apr 26, 2010, 10:23:19 PM4/26/10
to linux-kernel
大家好:

最近项目所需,需要在内核中查看指定进程所占用内存大小及占用CPU的使用比率,和OOP信息,
当前进程使用的内存堆栈可以从task_struct->mm_struct中获得,但是cpu的使用比率和OOP信息从什么地方可以
获得呢,或者通过什么方法可以计算出来,请有指教。 谢谢


dyron
_______________________________________________
Linux 内核开发中文邮件列表
Linux-...@zh-kernel.org
http://zh-kernel.org/mailman/listinfo/linux-kernel
Linux 内核开发中文社区: http://zh-kernel.org

--
这是zh-kernel邮件列表内容的只读归档,如果想参与讨论请访问http://zh-kernel.org进行订阅。

Subscription settings: http://groups.google.com/group/zh-kernel/subscribe?hl=zh-CN

. dyron

unread,
Apr 27, 2010, 1:03:12 AM4/27/10
to linux-kernel
---------- 已转发邮件 ----------
发件人: . dyron <dyron...@gmail.com>
日期: 2010年4月27日 下午1:02
主题: Re: 如何在内核中查看进程的占用内存及CPU
收件人: Wenyi Liu <qings...@gmail.com>


嗯,谢谢,不过我想在内核态下去查看,不过你给我提供了思路,我去查看一下这些系统调用的实现。


在 2010年4月27日 下午12:49,Wenyi Liu <qings...@gmail.com>写道:

top命令似乎满足你的要求。
>
> oops看dmesg

. dyron

unread,
Apr 27, 2010, 11:22:27 PM4/27/10
to Tao Yinqing, linux-kernel
Tao yingqing, 谢谢你的回复,从你的回复中我查看内核代码,找到task_struct->mm_struct->total_vm中,可以得到
总的虚拟内存占用量,但是还不能获得实际的内存占用量,以及CPU占用率等。

我想在内核中获取这一切,并非用户态。谢谢大家的帮助!

在 2010年4月27日 下午6:59,Tao Yinqing <liuqin...@gmail.com>写道:

> /proc/<PID>/smaps
> /proc/<PID>/maps

> Tao Yinqing(陶寅清)

. dyron

unread,
Apr 28, 2010, 5:59:31 AM4/28/10
to Tao Yinqing, linux-kernel
已经通过get_mm_rss接口获得物理内存使用大小了,谢谢大家了!

Amerigo Wang

unread,
Apr 28, 2010, 6:53:29 AM4/28/10
to . dyron, linux-kernel
On Tue, Apr 27, 2010 at 10:23:19AM +0800, . dyron wrote:
> 最近项目所需,需要在内核中查看指定进程所占用内存大小及占用CPU的使用比率,和OOP信息,
> 当前进程使用的内存堆栈可以从task_struct->mm_struct中获得,但是cpu的使用比率和OOP信息从什么地方可以
> 获得呢,或者通过什么方法可以计算出来,请有指教。 谢谢

cpu使用率你可以自己算。

task_struct里有utime和stime,然后每一个cpu有自己的user time和system time。

对于oops信息,没必要在内核来读,这是用户空间的事儿,比如syslogd。

. dyron

unread,
Apr 28, 2010, 10:06:35 AM4/28/10
to Amerigo Wang, linux-kernel
Amerigo Wang: 愿意听你详细的说明一下计算cpu使用率的方法,我用了一些方法计算,发现不正确,
如用:
oldtime = task_sturct->utime + task_struct->stime;
delay_100_jiffies();
cpu_usage = task_struct->utime + task_struct->stime - oldtime;

请问正确的计算方法,谢谢。

yanan_111

unread,
Apr 28, 2010, 10:13:04 AM4/28/10
to linux-kernel


HTTP是用TCP来实现数据传输的,但是我想知道的是HTTP服务器端岂不是要维护多条TCP连接,HTTP服务器端是不是这样实现的呀!

王志忠

unread,
Apr 28, 2010, 8:35:58 PM4/28/10
to yanan_111, linux-kernel
对是这样的。
------------------------------------------
yanan_111 wrote:
> HTTP是用TCP来实现数据传输的,但是我想知道的是HTTP服务器端岂不是要维护多条TCP连接,HTTP服务器端是不是这样实现的呀!
> _______________________________________________
> Linux 内核开发中文邮件列表
> Linux-...@zh-kernel.org
> http://zh-kernel.org/mailman/listinfo/linux-kernel
> Linux 内核开发中文社区: http://zh-kernel.org
> _______________________________________________
> Linux 内核开发中文邮件列表
> Linux-...@zh-kernel.org
> http://zh-kernel.org/mailman/listinfo/linux-kernel
> Linux 内核开发中文社区: http://zh-kernel.org

---------------------------------------------------------------------------------------------------
Confidentiality Notice: The information contained in this e-mail and any accompanying attachment(s)
is intended only for the use of the intended recipient and may be confidential and/or privileged of
Neusoft Corporation, its subsidiaries and/or its affiliates. If any reader of this communication is
not the intended recipient, unauthorized use, forwarding, printing, storing, disclosure or copying
is strictly prohibited, and may be unlawful.If you have received this communication in error,please
immediately notify the sender by return e-mail, and delete the original message and all copies from
your system. Thank you.
---------------------------------------------------------------------------------------------------

Amerigo Wang

unread,
Apr 29, 2010, 2:34:08 AM4/29/10
to . dyron, linux-kernel
On Wed, Apr 28, 2010 at 10:06:35PM +0800, . dyron wrote:
>Amerigo Wang: 愿意听你详细的说明一下计算cpu使用率的方法,我用了一些方法计算,发现不正确,
>如用:
> oldtime = task_sturct->utime + task_struct->stime;
> delay_100_jiffies();
> cpu_usage = task_struct->utime + task_struct->stime - oldtime;
>
>请问正确的计算方法,谢谢。
>

old_ptime = p->utime + p->stime
old_sys_time = utime + stime
//do something
new_ptime = p->utime + p->stime
new_sys_time = utime + stime
cpu_usage = (new_ptime - old_ptime) / (new_sys_time - old_sys_time)

上面的系统utime和stime来自 kstat_cpu(i).cpustat.{user,system}。
还有,最后一个除是整除,你或许需要反过来。

ChenLong

unread,
Apr 28, 2010, 8:34:27 PM4/28/10
to yanan_111, linux-kernel
Apache的结构是:前端维护了多个Socket用于同用户通信,然后后端是一个线程池来处理用户请求。
同web应用所需要的资源相比,维护几千个Socket的资源就算不了什么了(现在我做接触的服务器也就几千并发连接,不知道有没有同学用过大型机开发银行系统等等,希望能请教下这些系统的服务器端是如何实现的?)

-----邮件原件-----
发件人: linux-kern...@zh-kernel.org [mailto:linux-kern...@zh-kernel.org] 代表 yanan_111
发送时间: 2010年4月28日 22:13
收件人: linux-kernel
主题: Re: Fwd: 如何在内核中查看进程的占用内存及CPU

Juan Chung

unread,
Apr 30, 2010, 11:29:18 AM4/30/10
to linux-...@zh-kernel.org
On 2010-4-29 8:34, ChenLong wrote:
> Apache的结构是:前端维护了多个Socket用于同用户通信,然后后端是一个线程池来处理用户请求。
>
维护多个Socket?这我不是很理解。一般一个Socket监听一个端口,一个端口也只
有一个进程占用;假设Apache监听的是80端口,那么按上面的推理只会有一个
Socket在监听。
怎么会有多个呢?如果有多个,怎么处理监听端口的冲突问题呢?( Sorry, 刚才
回给个人了 )。
Reply all
Reply to author
Forward
0 new messages