+ -
当前位置:首页 → 问答吧 → 通过系统调用获取特定进程占有的内存量,CPU占有率

通过系统调用获取特定进程占有的内存量,CPU占有率

时间:2007-03-06

来源:互联网

我想通过系统调用获取特定进程占有的内存量,还有CPU占有率.

#include   <sys/types.h>
#include   <sys/time.h>
#include   <sys/resource.h>
 
#define       RUSAGE_SELF                     0
#define       RUSAGE_CHILDREN           -1
   
int     getrusage(int   who,   struct   rusage   *rusage);

函数返回后,在rusage所指结构中有一个成员long     ru_ixrss;   /*   integral   shared   memory   size   */(这是man   page   中的注释)

我把整个结构也给出来:
struct       rusage   {
            struct   timeval   ru_utime;       /*   user   time   used   */
            struct   timeval   ru_stime;       /*   system   time   used   */
            long     ru_maxrss;                       /*   max   resident   set   size   */
            long     ru_ixrss;                         /*   integral   shared   memory   size   */
            long     ru_idrss;                         /*   integral   unshared   data   */
            long     ru_isrss;                         /*   integral   unshared   stack   */
            long     ru_minflt;                       /*   page   reclaims   */
            long     ru_majflt;                       /*   page   faults   */
            long     ru_nswap;                         /*   swaps   */
            long     ru_inblock;                     /*   block   input   operations   */
            long     ru_oublock;                     /*   block   output   operations   */
            long     ru_msgsnd;                       /*   messages   sent   */
            long     ru_msgrcv;                       /*   messages   received   */
            long     ru_nsignals;                   /*   signals   received   */
            long     ru_nvcsw;                         /*   voluntary   context   switches   */
            long     ru_nivcsw;                       /*   involuntary   "   */
    };

打印出来后,发现ru_ixrss是个很大的整数.应该不大可能是正常的内存量.这些成员的具体指的什么意思?有没有熟悉的朋友,可以讨论一下吧!
或者有什么好方法得到内存和cpu占有率,系统调用的形式.
另外这个函数在APUE中没有提到,是不是不提倡用?

作者: levinee   发布时间: 2007-03-06

man   getrusage
...
  ru_ixrss           an   ``integral ' '   value   indicating   the   amount   of   memory   used
                                    by   the   text   segment   that   was   also   shared   among   other   pro-
                                    cesses.     This   value   is   expressed   in   units   of   kilobytes   *
                                    ticks-of-execution.     Ticks   are   statistics   clock   ticks.     The
                                    statistics   clock   has   a   frequency   of   sysconf(_SC_CLK_TCK)
                                    ticks   per   second.
....
这个值是共享的正文段内存大小和系统嘀嗒数的乘积,大一些也正常了。

作者: iisbsd   发布时间: 2007-03-12

那这个线程占用了多少内存,是那个字段呢? 或者说,这个结构体里面没有?通过其他方式获取的,ps?

作者: bluefish625   发布时间: 2011-12-06

http://blog.csdn.net/crazyguang/article/details/2407263
http://topic.csdn.net/u/20091224/14/5f84b381-1e43-474d-a69c-3e28cb83aae1.html

作者: axengine   发布时间: 2011-12-06