Get resource usage of process

AUTHORS:

  • William Stein (2006-03-04): initial version
sage.misc.getusage.VmB(VmKey)
Function used internally by this module.
sage.misc.getusage.get_memory_usage(t=None)

Return memory usage.

INPUT:

  • t - a float (default: None); output of an earlier call

OUTPUT:

  • Linux - Returns float number (in megabytes)
  • OS X - Returns float number (in megabytes) that matches VSIZE column of ‘top’
  • Solaris or OpenSolaris - Returns float number (in megabytes) that matches RSS column of ‘prstat’. Depending on the memory usage, ‘prstat’ will output the data in KB, MB or GB. In each case, the value returned by this function will always be in MB.
  • other - not implemented for any other operating systems

EXAMPLES:

We test that memory usage doesn’t change instantly:

sage: t = get_memory_usage()
sage: get_memory_usage(t)          # amount of memory more than when we defined t.
0.0

NOTES:

  • Currently, get_memory_usage() calls ‘prstat’ on Solaris and OpenSolaris to get the data it requires. In the long term, a better solution would be to use Solaris system calls.
  • In some instances, ‘top’ may be used on OS X. This may break if the memory usage is greater than 9999 MB. However, normally ‘top’ is not used on OS X.
sage.misc.getusage.linux_memory_usage()
Return memory usage in megabytes.
sage.misc.getusage.top()

Return the ‘top’ or ‘prstat’ line that contains this running Sage process.

OUTPUT:

  • a string

EXAMPLES:

sage: top()              # random output
'72373 python       0.0%  0:01.36   1    14+  1197   39M+   34M+   55M+  130M+'

NOTES:

The external command ‘top’ (http://www.unixtop.org/) is called on Linux, and most other operating systems. The output format of ‘top’ is not consistent across all platforms and all versions of ‘top’. If the top() function does not work in Sage, you may need to install ‘top’.

The external command ‘prstat’ is called on the Solaris and OpenSolaris systems. That is part of Solaris, and will not need to be installed. The columns used in the ‘prstat’ output are:

PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP

Previous topic

A tool for inspecting Python pickles

Next topic

Multidimensional enumeration

This Page