pyrfcon


The module for runtime performance monitoring.

About

Pyrfcon provides the ability to monitor performance counters data for process and its threads. You can use this module on Windows and Linux. In addition to standard set of counters for process and thread, you can observe two extra counters: cpuUsage (for process and thread) and memUsage (only for process).

Supported counters

Standard counters

For Windows:

Process: time, % Processor Time, % User Time, % Privileged Time, Virtual Bytes Peak, Virtual Bytes, Page Faults/sec, Working Set Peak, Working Set, Page File Bytes Peak, Page File Bytes, Private Bytes, Thread Count, Priority Base, Elapsed Time, ID Process, Creating Process ID, Pool Paged Bytes, Pool Nonpaged Bytes, Handle Count, IO Read Operations/sec, IO Write Operations/sec, IO Data Operations/sec, IO Other Operations/sec, IO Read Bytes/sec, IO Write Bytes/sec, IO Data Bytes/sec, IO Other Bytes/sec

Thread: time, % Processor Time, % User Time, % Privileged Time, Context Switches/sec, Elapsed Time, Priority Current, Priority Base, Start Address, Thread State, Thread Wait Reason, ID Process, ID Thread

For Linux:

Process: time, pid,comm, state, ppid, pgrp, session, tty_nr, tpgid, flags, minflt, cminflt, majflt, cmajflt, utime, stime, cutime, cstime, priority, nice, num_threads, itrealvalue, starttime, vsize, rss, rsslim, startcode, endcode, startstack, kstkesp, kstkeip, signal, blocked, sigignore, sigcatch, wchan, nswap, cnswap, exit_signal, processor, rt_priority, policy, delayacct_blkio_ticks, guest_time, cguest_time, size, resident, share, trs, lrs, drs, dt

Thread: time, pid,comm, state, ppid, pgrp, session, tty_nr, tpgid, flags, minflt, cminflt, majflt, cmajflt, utime, stime, cutime, cstime, priority, nice, num_threads, itrealvalue, starttime, vsize, rss, rsslim, startcode, endcode, startstack, kstkesp, kstkeip, signal, blocked, sigignore, sigcatch, wchan, nswap, cnswap, exit_signal, processor, rt_priority, policy, delayacct_blkio_ticks, guest_time, cguest_time

Extra counters

For Windows:

cpuUsage: % Processor Time is the percentage of elapsed time that all of process' threads used the processor to execution instructions. An instruction is the basic unit of execution in a computer, a thread is the object that executes instructions, and a process is the object created when a program is run. Code executed to handle some hardware interrupts and trap conditions are included in this count.

memUsage: Private Bytes is the current size, in bytes, of memory that this process has allocated that cannot be shared with other processes.

For Linux:

cpuUsage: 100 * (jiffies_delta / HZ) / (time_delta / 1e6)
time_delta = time(t) - time(t0)
jiffies_delta = (utime(t) + stime(t)) - (utime(t0) + stime(t0))
utime: the number of jiffies that this process has been scheduled in user mode
stime: the number of jiffies that this process has been scheduled in kernel mode
HZ: the number of ticks per second
t0,t: two serial time measurements
memUsage: vsize/fullmemsize
vsize: virtual memory size in bytes
fullmemsize: full memory size

Module usage

Module usage is pretty simple. You can find a working example in example.py. Here you'll find detailed step-by-step analysis of this example with comments on usage.

You can start new process for monitoring or attach to already running one.

To start new process: import subprocess process = subprocess.Popen(sys.argv[1:4]) pid = process.pid # Save process PID for later usage To attach to already running process: pid = 228 # PID of existing process

To start monitoring of a process and its threads, you must first create MonitorThread object. MonitorThread is inherited from the standard Thread class and is used to actually collect performance data in background. By default Monitor Thread saves the process info every 0.5 seconds, but you can set necessary update interval (in seconds): import pyrfcon updateInterval = 1 # 1 second update period thread = pyrfcon.MonitorThread(pid, updateInterval)

To start the thread’s activity, you must call start() function. This function arranges for the object’s run() method to be invoked in a separate thread of control: thread.start()

You can use getData() function to poll for performance counters updates or callbacks to get counters data as they change.

startCollectData() starts collecting data for getData() function: thread.startCollectData()

getData() returns collected data since startCollectData() call or last getData() call: thread.getData()

stopCollectData() stops collect data for getData() function: thread.stopCollectData()

setCallback(function) sets callback function: thread.setCallback(function)

clearCallback() clears callback: thread.clearCallback()

By default getData() returns standard set of counters as a tuple (processData, threadsData). processData: process data numpy array (dtype = typeProc) threadsData: threads data dictionary consists of thread data numpy arrays (dtype = typeThread). Threads IDs are used as a keys.

To limit number of monitored counters you can construct different queries from standard and extra (cpuUsage, memUsage) counters. procQuery = ['time', 'Working Set' , 'cpuUsage', 'memUsage'] threadsQuery= ['time', 'cpuUsage', 'memUsage'] You can use this queries for getData() to return only necessary set of counters.

Popen.wait() waits for child process to terminate. process.wait()

setProcess(process) starts or stops collect data. To start collecting data pass process = True, and to stop collecting data pass process = False: thread.setProcess(False) # Stop collecting data

join(timeout) waits until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs. thread.join()

Project Information

Labels:
python performance counter monitoring cpu memory usage windows linux