Click here to Skip to main content
12,072,809 members (78,147 online)
Click here to Skip to main content

Stats

71.1K views
1.7K downloads
169 bookmarked
Posted

ProSysLib: Dissecting the Process

, 22 Nov 2010 CPOL
Access detailed information about the current process the easiest way.
ProcessInfo32_bin.zip
ProcessInfo.exe
ProSysLib.dll
Interop.ProSysLib.dll
ProcessInfo64_bin.zip
Interop.ProSysLib.dll
ProcessInfo.exe
ProSysLib.dll
ProcessInfo_bin.zip
PSL64v0.9.dll
Interop.ProSysLib.dll
ProcessInfo.exe
PSL32v0.9.dll
ProSysLib_v0.9.zip
ProSysLib_v0.9
Bin
PSL32v0.9.dll
PSL64v0.9.dll
Help
Samples
C#2008
ProcessInfo
Properties
Settings.settings
ProcessViewer
Properties
Settings.settings
VB6
CPUInfo.exe
CPUInfo.frm
CPUInfo.frx
CPUInfo.vbp
CPUInfo.vbw
VC2008
Deployment
Src
mssccprj.scc
ProSysLib.def
ProSysLib.rgs
ProSysLib.vcproj.vspscc
ProSysLib.vssscc
PSLSystem.rgs
PSLv0.5Setup.zip
PSLv0.5Setup.exe
PSLv0.7Setup.zip
PSLv0.7Setup.exe
#include "StdAfx.h"
#include "ProcessorUsage.h"

DWORD CProcessorUsage::s_TickMark = 0;
__int64  CProcessorUsage::s_time = 0;
__int64 CProcessorUsage::s_idleTime = 0;
__int64 CProcessorUsage::s_kernelTime = 0;
__int64 CProcessorUsage::s_userTime = 0;
__int64 CProcessorUsage::s_kernelTimeProcess = 0;
__int64 CProcessorUsage::s_userTimeProcess = 0;
int CProcessorUsage::s_count = 0;
int CProcessorUsage::s_index = 0;
int CProcessorUsage::s_lastCpu = 0;
int CProcessorUsage::s_cpu[5] = {0, 0, 0, 0, 0};
int CProcessorUsage::s_cpuProcess[5] = {0, 0, 0, 0, 0};

CProcessorUsage::CProcessorUsage()
{
	::InitializeCriticalSection(&m_cs);

	s_pfnNtQuerySystemInformation = NULL;
	s_pfnGetSystemTimes = NULL;

	m_pInfo = NULL;
	m_uInfoLength = sizeof(PROC_PERF_INFO);

	HMODULE hModule  = ::GetModuleHandle(_T("kernel32.dll"));
	if(hModule)
		s_pfnGetSystemTimes = (pfnGetSystemTimes)::GetProcAddress(hModule, "GetSystemTimes");

	if(!s_pfnGetSystemTimes)
	{
		hModule = ::GetModuleHandle(_T("ntdll.dll"));
		if(hModule)
		{
			s_pfnNtQuerySystemInformation = (pfnNtQuerySystemInformation)::GetProcAddress(hModule, "NtQuerySystemInformation");
			if(s_pfnNtQuerySystemInformation)
			{
				PROC_PERF_INFO tmp;
				s_pfnNtQuerySystemInformation(8, &tmp, m_uInfoLength, &m_uInfoLength);
				m_pInfo = new PROC_PERF_INFO[m_uInfoLength / sizeof(PROC_PERF_INFO)];
			}
		}
	}
	s_TickMark = ::GetTickCount();
}

CProcessorUsage::~CProcessorUsage()
{
	if(m_pInfo)
		delete m_pInfo;

    ::DeleteCriticalSection(&m_cs);
}

void CProcessorUsage::GetSysTimes(__int64 & idleTime, __int64 & kernelTime, __int64 & userTime)
{
	if(s_pfnGetSystemTimes)
		s_pfnGetSystemTimes((LPFILETIME)&idleTime, (LPFILETIME)&kernelTime, (LPFILETIME)&userTime);
	else
	{
		idleTime = 0;
		kernelTime = 0;
		userTime = 0;
		if(s_pfnNtQuerySystemInformation && m_uInfoLength && !s_pfnNtQuerySystemInformation(0x08, m_pInfo, m_uInfoLength, &m_uInfoLength))
		{
			// NtQuerySystemInformation returns information for all
			// CPU cores in the system, so we take the average here:
			int nCores = m_uInfoLength / sizeof(PROC_PERF_INFO);
			for(int i = 0;i < nCores;i ++)
			{
				idleTime += m_pInfo[i].IdleTime.QuadPart;
				kernelTime += m_pInfo[i].KernelTime.QuadPart;
				userTime += m_pInfo[i].UserTime.QuadPart;
			}
			idleTime /= nCores;
			kernelTime /= nCores;
			userTime /= nCores;
		}
	}
}

short CProcessorUsage::GetUsage()
{
    __int64 sTime;
    int sLastCpu;

	CCritSecLock cs(m_cs);
	sTime = s_time;
	sLastCpu = s_lastCpu;

    if(((::GetTickCount() - s_TickMark) & 0x7FFFFFFF) <= 200)
		return sLastCpu;

	__int64 time;
	__int64 idleTime;
	__int64 kernelTime;
	__int64 userTime;
	__int64 kernelTimeProcess;
	__int64 userTimeProcess;

	::GetSystemTimeAsFileTime((LPFILETIME)&time);

    if(!sTime)
    {
		GetSysTimes(idleTime, kernelTime, userTime);
		FILETIME createTime;
		FILETIME exitTime;
		::GetProcessTimes(::GetCurrentProcess(), &createTime, &exitTime, (LPFILETIME)&kernelTimeProcess, (LPFILETIME)&userTimeProcess);

		s_time = time;
		s_idleTime = idleTime;
		s_kernelTime = kernelTime;
		s_userTime = userTime;
		s_kernelTimeProcess = kernelTimeProcess;
		s_userTimeProcess = userTimeProcess;
		s_lastCpu = 0;
		s_TickMark = ::GetTickCount();
		return 0;
	}

    __int64 div = (time - sTime);

	GetSysTimes(idleTime, kernelTime, userTime);

	FILETIME createTime;
	FILETIME exitTime;
	::GetProcessTimes(::GetCurrentProcess(), &createTime, &exitTime, (LPFILETIME)&kernelTimeProcess, (LPFILETIME)&userTimeProcess);

    int cpu;
    int cpuProcess;

     __int64 usr = userTime   - s_userTime;
     __int64 ker = kernelTime - s_kernelTime;
     __int64 idl = idleTime   - s_idleTime;
     __int64 sys = (usr + ker);

	if(sys)
		cpu = int((sys - idl) * 100 / sys); // System Idle take 100 % of cpu;
	else
		cpu = 0;

	cpuProcess = int((((userTimeProcess - s_userTimeProcess) + (kernelTimeProcess - s_kernelTimeProcess)) * 100 ) / div);

	s_time = time;
	s_idleTime = idleTime;
	s_kernelTime = kernelTime;
	s_userTime = userTime;
	s_kernelTimeProcess = kernelTimeProcess;
	s_userTimeProcess = userTimeProcess;
	s_cpu[(s_index ++) %5] = cpu;
	s_cpuProcess[(s_index++) %5] = cpuProcess;
	s_count ++;

	if(s_count > 5)
		s_count = 5;

	cpu = 0;
	int i;
	for(i = 0; i < s_count; i++ )
		cpu += s_cpu[i];
     
	cpuProcess = 0;
	for(i = 0; i < s_count; i++ )
		cpuProcess += s_cpuProcess[i];

	cpu /= s_count;
	cpuProcess /= s_count;   
	s_lastCpu = cpu;
	sLastCpu = s_lastCpu;
	s_TickMark = ::GetTickCount();
    return sLastCpu;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vitaly Tomilov
Software Developer (Senior) Sibedge IT
Ireland Ireland
My online CV: cv.vitalytomilov.com

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160208.1 | Last Updated 22 Nov 2010
Article Copyright 2008 by Vitaly Tomilov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid