Win/linux using Psutil to get process Cpu/memory/io occupancy information

Source: Internet
Author: User

PSUTIL-A cross-platform process and System utilities module for Python

1. Installation

PIP can be installed.

Need to install vs2008 under Windows, otherwise error: unable to find Vcvarsall.bat

If you have already installed vs2010/vs2012, you need to set the environment variable, Vs90comntools point to the existing VS variable.

The VS2010 settings are as follows:

Vs90comntools =%vs100comntools%
2. Get specific Process objects
    • Create a Process object based on the process ID
    • Get all Process objects, filter out the target process
#-*-coding:utf-8-*-ImportPsutildefget_proc_by_id (PID):returnPsutil. Process (PID)defGet_proc_by_name (pname):"""get process by name return the first process if there is more than one"""     forProcinchpsutil.process_iter ():Try:   (). lower () = =pname.lower ():returnProc#return if found one        exceptPsutil. AccessDenied:Pass        exceptPsutil. Nosuchprocess:Pass    returnNoneif '__main__'==__name__:    PrintGet_proc_by_name ("Chrome.exe")    PrintGET_PROC_BY_ID (4364)

3. Obtaining process Information 3.1 requires special attention to exception protection, especiallyPsutil. AccessDenied

Different processes, permissions and other information may be different, while traversing all process fetching information, it is necessary to protect each process separate process exception.

3.2 Getting All Processes

Most of the demo code uses psutil.get_process_list , but the method is already marked as obsolete in the source.

The new recommended is the psutil.process_iter iterator.

According to the following source code to realize the principle: get all the process ID, and then create a process object based on the ID.

_pmap = {}defprocess_iter ():"""Return a generator yielding a Process class instance for all running processes on the local machine.  Every new Process instance is only created once and then cached to an internal table which are updated every time this    is used.  Cached process instances is checked for identity so the you ' re safe in case a PID have been reused by another Process,    In which case, the cached instance is updated.    The sorting order in which processes was yielded is based on their PIDs. """    defAdd (PID): Proc=Process (PID) _pmap[]=procreturnprocdefRemove (PID): _pmap.pop (PID, None) a=Set (Get_pid_list ()) b=Set (_pmap.keys ()) New_pids= A-b gone_pids= B-a forPidinchgone_pids:remove (PID) forPID, ProcinchSorted (List (_pmap.items ()) +list (Dict.fromkeys (new_pids). Items ())):Try:            ifProc isNone:#New Process                yieldAdd (PID)Else:                #Use is_running () to check whether PID have been reused by                #another process in which case yield a new process instance                ifproc.is_running ():yieldprocElse:                    yieldAdd (PID)exceptnosuchprocess:remove (PID)exceptAccessDenied:#Process creation time can ' t be determined hence there ' s            #No-to-tell whether the PID of the cached process            #Has been reused. Just return the cached version.            yieldproc@_deprecated ()defget_process_list ():"""Return A list of Process class instances for all running processes in the local machine (deprecated). """    returnList (Process_iter ())
3.3 Process Memory Information--Vss/rss/pss/uss

VSS is the remaining accessible memory.

Process-consuming memory consists of 2 parts, self + shared libraries. Different algorithms produce 3 different memory metrics, namely: Rss/pss/uss.

In general, memory footprint has the following rules: VSS >= RSS >= PSS >= USS

The Demo code is as follows

proc = Psutil. Process (4364== = proc.memory_percent ()print"RSS:%s Byte , VSS:%s Byte" % (RSS, VSS)print"total:%.2f (M)" % ( Float (total)/1024/1024/1024)print"percent:%.2f%%, Calc:%.2f%%" % (Percent, 100*float (RSS)/total)


Native Memory information

Detailed Description:

  • VSS (Reported as VSZ from PS) are the total accessible address space of a process.
    This is the size also includes memory that May is resident in RAM like Mallocs that has been allocated but not written to.
    VSS is of very little use for determing real memory usage of a process.
  • RSS is the total memory actually held in RAM for a process.
    RSS can be misleading, because it reports the total all of the the the the the process uses,
    Even though a shared library is only loaded into memory once regardless of what many processes use it.
    RSS isn't an accurate representation of the memory usage for a single process.
  • pss  differs from RSS in so it reports the proportional size of its shared libraries,
    i.e. If three processes all use a shared libraries that have pages,
    that library would only contribute pages to the PSS t Hat is reported for each of the three processes.
    PSS is a very useful number because if the PSS for all processes in the system be summed together,
    that's a G Ood representation for the total memory usage in the system.
    When a process was killed, the shared libraries that contributed to its PSS
    would be proportionally distributed to The PSS totals for the remaining processes still using that library.
    in the this-slightly misleading, because
    when a-process is killed, PSS does not accurately represent t He memory returned to the overall system.
  • uss  is the Total private memory for a process,
    i.e. that's memory that's completely uniq UE to that process.
    USS is an extremely useful number because it indicates the true incremental cost of running a particular process.
    When a process was killed, the USS is the total memory, which is actually returned to the system.
    USS is the best number to watch when initially suspicious ofmemory leaksin a process.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.