How to get Kernel time usage and User time usage of process?

21 07 2008


Every process spend its time in kernel space as well as in user space. You can watch it by using perfmon.exe. Have a look at the screenshot.

But how to get the Kernel time and user time of a particular process?


You can use the api – GetProcessTimes(). See the code snippet below,

FILETIME CreationTime = { 0 };
FILETIME ExitTime     = { 0 };
FILETIME KernelTime   = { 0 };
FILETIME UserTime     = { 0 };

// Get Process times.
GetProcessTimes( GetCurrentProcess(),
                 &CreationTime,
                 &ExitTime,
                 &KernelTime,
                 &UserTime );

// Format time to readable form.
SYSTEMTIME SystemTime = { 0 };
FileTimeToSystemTime( &KernelTime, & SystemTime );

// Kernel Time in HH:MM:SS:mmm.
CString csKernelTime;
csKernelTime.Format( _T("Kernel Time - %02d:%02d:%02d:%04d"),
                     SystemTime.wHour,
                     SystemTime.wMinute,
                     SystemTime.wSecond,
                     SystemTime.wMilliseconds );

// Format user time to readable form.
FileTimeToSystemTime( &UserTime, & SystemTime );

// Kernel Time in HH:MM:SS:mmm.
CString csUserTime;
csUserTime.Format( _T("User Time - %02d:%02d:%02d:%04d"),
                   SystemTime.wHour,
                   SystemTime.wMinute,
                   SystemTime.wSecond,
                   SystemTime.wMilliseconds );


You can also get the process creation time and process exit time by using the same api.


Targeted Audience – Beginners.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: