On Tue, 2010-10-05 at 09:49 +1100, Cameron Simpson wrote: 
> On 04Oct2010 09:02, Adam Tauno Williams <awill...@whitemice.org> wrote:
> | I'm using a call to the resource module's getrusage method.  On openSUSE
> | this works, on CentOS [python26-2.6.5-3.el5] it 'works' but just returns
> | zeros for the memory utilization values.
> | resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
> | openSUSE: returns 5512
> | CentOS:   returns 0
> | Anyone know what condition causes this?  Or is there a better /
> | more-reliable way to check memory utilization of the current process?
> Long standing Linux shortcoming. "man getrusage" on a handy Gentoo box
> says:
>   The structure definition shown at the start of this page was taken from
>   4.3BSD Reno.  Not all fields are meaningful under Linux.  In Linux  2.4
>   only  the fields ru_utime, ru_stime, ru_minflt, and ru_majflt are
>   maintained.  Since Linux 2.6, ru_nvcsw and ru_nivcsw are  also maintained. 
>   Since Linux 2.6.22, ru_inblock and ru_oublock are also maintained.
> I ran across this deficiency a few years back hoping to get some memory
> stats for some software engineeers in a former life.
> Also look in /proc - the info may be available there - I expect that's
> where a Linux ps gets a lot of info.

Yep, I went that route.

def get_rss_size():
  rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss * 1000
  if (rss == 0):
    # NOTE: This means RSS via getrusage is not working on this system
    #       So we try our fallback method or reading proc/{pid}/statm
    try:
      handle = open('/proc/{0}/statm'.format(os.getpid()), 'rb')
      data = handle.read(512)
      handle.close()
      rss = int(data.split(' ')[1]) * 4000
    except Exception, e:
      rss = 0
  return rss

Both the /proc and getrusage produce the same value, and that value
corresponds to what is seen in ps, top, or gnome-system-monitor.
-- 
Adam Tauno Williams <awill...@whitemice.org> LPIC-1, Novell CLA
<http://www.whitemiceconsulting.com>
OpenGroupware, Cyrus IMAPd, Postfix, OpenLDAP, Samba

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to