cpu占用率统计

命令行参数:此exe名 应用程序名(如:360se.exe)

//#include
#include
#include
#include
#include
#include
#include

#define MY_PROCESS_ERROR(Condition) do{ if (!(Condition))  goto Exit0; } while (false)

static DWORD g_sdwTickCountOld = 0;                // 上一次的tick计数
static LARGE_INTEGER g_slgProcessTimeOld;        // 保存进程上一次的时间占用
static DWORD g_sdwProcessorCoreNum = 0;            // 处理器核心数
static HANDLE g_shExitEvent = NULL;                // 线程退出控制

typedef struct _TARGET_PROCESS
{
    DWORD        dwProcessId;                    // 进程ID
}TARGET_PROCESS, *PTARGET_PROCESS;

 


HRESULT FindFirstProcessIdByName(const TCHAR* cpszExeFileName, DWORD &dwPID)
{
    HRESULT hr = E_FAIL;

    PROCESSENTRY32 pe = { 0 };
    HANDLE hSnapshot = NULL;

    if (NULL == cpszExeFileName)
    {
        hr = HRESULT_FROM_WIN32(ERROR_BAD_ARGUMENTS);
        goto Exit0;
    }

    pe.dwSize = sizeof(PROCESSENTRY32);
    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (INVALID_HANDLE_VALUE == hSnapshot)
    {
        hr = HRESULT_FROM_WIN32(GetLastError());
        goto Exit0;
   

    if (FALSE == Process32First(hSnapshot, &pe))
    {
        hr = HRESULT_FROM_WIN32(ERROR_NO_MORE_FILES);
        goto Exit0;
    }

    hr = S_FALSE;
    do
    {
        if (0 == _tcsicmp(cpszExeFileName, pe.szExeFile))
        {
            dwPID = pe.th32ProcessID;
            hr = S_OK;
            break;
        }
    }while(Process32Next(hSnapshot, &pe));

Exit0:
    if(hSnapshot)
    {
        CloseHandle(hSnapshot);
        hSnapshot = NULL;
    }

    return hr;
}


int GetProcessCpuPercent(const HANDLE hProcess, const DWORD dwElepsedTime)
{
    int nProcCpuPercent = 0;
    BOOL bRetCode = FALSE;

    FILETIME CreateTime, ExitTime, KernelTime,UserTime;
    LARGE_INTEGER lgKernelTime;
    LARGE_INTEGER lgUserTime;
    LARGE_INTEGER lgCurTime;

    bRetCode = GetProcessTimes(hProcess, &CreateTime, &ExitTime, &KernelTime, &UserTime);
    if (bRetCode)
    {
        lgKernelTime.HighPart = KernelTime.dwHighDateTime;
        lgKernelTime.LowPart = KernelTime.dwLowDateTime;

        lgUserTime.HighPart = UserTime.dwHighDateTime;
        lgUserTime.LowPart = UserTime.dwLowDateTime;

        lgCurTime.QuadPart = (lgKernelTime.QuadPart + lgUserTime.QuadPart) / 10000;
        nProcCpuPercent = (int)((lgCurTime.QuadPart - g_slgProcessTimeOld.QuadPart) * 100 / dwElepsedTime);
        g_slgProcessTimeOld = lgCurTime;
        nProcCpuPercent = nProcCpuPercent / g_sdwProcessorCoreNum;
    }
    else
    {
        nProcCpuPercent = -1;
    }

    return nProcCpuPercent;
}

unsigned __stdcall WorkerThread(void *pArg)
{
    HRESULT hr = E_FAIL;

    HANDLE hProcess = NULL;
    DWORD dwProcessId = 0;
    DWORD dwRetVal = 0;
    DWORD dwCurrentTickCount = 0;
    DWORD dwElapsedTime = 0;
    int nProcessCpuPercent = 0;
   
    TARGET_PROCESS *pTargetProcess = (TARGET_PROCESS *)pArg;

    dwProcessId = pTargetProcess->dwProcessId;
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION , FALSE, dwProcessId);
    MY_PROCESS_ERROR(hProcess);

 //#############################
 DWORD dwAverageCpuOwnerPercent = 0;
 DWORD dwTotalCpuOwnerPercent = 0;
 DWORD dwTimes = 0;
 DWORD dwMaxCpuOwnerPercent = 0;
 DWORD dwMinCpuOwnerPercent = 100;
 //############################
    do
    {
        dwRetVal = WaitForSingleObject(g_shExitEvent, 1000);
        if (WAIT_OBJECT_0 == dwRetVal ||
            WAIT_FAILED == dwRetVal
            )
        {
            break;
        }

        dwCurrentTickCount = GetTickCount();
        dwElapsedTime = dwCurrentTickCount - g_sdwTickCountOld;
        g_sdwTickCountOld = dwCurrentTickCount;
        nProcessCpuPercent = GetProcessCpuPercent(hProcess, dwElapsedTime);
  if(dwMaxCpuOwnerPercent < nProcessCpuPercent)
  {
   dwMaxCpuOwnerPercent = nProcessCpuPercent;
  }

  if(dwMinCpuOwnerPercent > nProcessCpuPercent)
  {
   dwMinCpuOwnerPercent = nProcessCpuPercent;
  }
  dwTotalCpuOwnerPercent += nProcessCpuPercent;
  dwTimes++;
  dwAverageCpuOwnerPercent = dwTotalCpuOwnerPercent / dwTimes;
  
        wprintf(L"cpu:- avg- max-\n", nProcessCpuPercent, dwAverageCpuOwnerPercent,dwMaxCpuOwnerPercent);
  //wprintf(L"cpu:%d\n", nProcessCpuPercent);
    } while (1);   
Exit0:
    if (hProcess)
    {
        CloseHandle(hProcess);
        hProcess = NULL;
    }
   
    return 0;
}

int main(int argc, _TCHAR* argv[])
{
    HRESULT hr = E_FAIL;
   
    HANDLE hThread = NULL;
    unsigned int uiTid = 0;
    SYSTEM_INFO sysInfo = { 0 };
    TARGET_PROCESS struTargetProcess;

    if (argc > 1)
    {
        hr = FindFirstProcessIdByName(argv[1], struTargetProcess.dwProcessId);
        if (S_OK != hr)
        {
            _tprintf(_T("Can't find process \'%s\' ret=0x%X\n"), argv[1], hr);
            goto Exit0;
        }

        GetSystemInfo(&sysInfo);
        g_sdwProcessorCoreNum = sysInfo.dwNumberOfProcessors;

        g_shExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
        MY_PROCESS_ERROR(g_shExitEvent);

        hThread = (HANDLE)_beginthreadex(NULL, 0, WorkerThread, &struTargetProcess, 0, &uiTid);
        MY_PROCESS_ERROR(hThread);

        _getch();
        SetEvent(g_shExitEvent);
        WaitForSingleObject(hThread, INFINITE);
    }
    else
    {
        _tprintf(_T("Please input a process name.\n"));
    }

Exit0:
    if (hThread)
    {
        CloseHandle(hThread);
        hThread = NULL;
    }

    if (g_shExitEvent)
    {
        CloseHandle(g_shExitEvent);
        g_shExitEvent = NULL;
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值