Get resource usage of process

AUTHORS:

  • William Stein (2006-03-04): initial version
  • Jeroen Demeyer (2016-11-14): implement as thin wrapper over psutil package
sage.misc.getusage.VmB(VmKey)

Function used internally by this module.

sage.misc.getusage.get_memory_usage(t=None)

Return the memory usage of the current process in megabytes.

INPUT:

  • t – a float (default: None); output of an earlier call. If this is given, return the current usage minus t.

OUTPUT: a float representing the number of megabytes used.

EXAMPLES:

sage: t = get_memory_usage(); t  # random
873.98046875
sage: type(t)
<type 'float'>
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. For FreeBSD, return the line containing this running Sage process from ‘ps -axwww -o pid,user,vsz,rss,state,pri,nice,time,cpu,comm’.

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
sage.misc.getusage.virtual_memory_limit()

Return the upper limit for virtual memory usage.

This is the value set by ulimit -v at the command line or a practical limit if no limit is set. In any case, the value is bounded by sys.maxsize.

OUTPUT:

Integer. The virtual memory limit in bytes.

EXAMPLES:

sage: from sage.misc.getusage import virtual_memory_limit
sage: virtual_memory_limit() > 0
True
sage: virtual_memory_limit() <= sys.maxsize
True