• 取得某个进程CPU的占用率


    /* percent = (user_time_diff + kernel_time_diff) * 100 / (cpu_num * system_time_diff) */
    #include<windows.h>
    #include<stdio.h>
    #include <pdh.h>
    #include <Tlhelp32.h>
    int main()
    {
        // Find the specified Process
        HANDLE hProcess = NULL;
        PROCESSENTRY32 pe = {sizeof(pe)};
        HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        BOOL suc = Process32First(hSnapShot, &pe);
    
        while(suc)
        {
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, false, pe.th32ProcessID);
            if(hProcess != NULL)
            {
                if(strcmp(pe.szExeFile, "cpu_test.exe") == 0)
                {
                    break;
                }
            }
            CloseHandle(hProcess);
            suc = Process32Next(hSnapShot, &pe);
        }
        // Got it
        if(suc)
        {
            ULARGE_INTEGER ulLastSysTime, ulCurSysTime;
            ULARGE_INTEGER ulStart, ulEnd, 
                ulLastProcKernelTime, ulLastProcUserTime,
                ulCurProcKernelTime, ulCurProcUserTime;
            ULARGE_INTEGER ulKernelTimeUsed ,ulUserTimeUsed, ulSysTimePast;
            SYSTEM_INFO si = {0};
            int iCpuUsage = 0;
    
            GetSystemInfo(&si);
            GetSystemTimeAsFileTime((PFILETIME)&ulLastSysTime);
            GetProcessTimes(hProcess, (PFILETIME)&ulStart, (PFILETIME)&ulEnd, 
                (PFILETIME)&ulLastProcKernelTime, (PFILETIME)&ulLastProcUserTime);
    
            while(1)
            {
    
                Sleep(1000);
    
                GetProcessTimes(hProcess, (PFILETIME)&ulStart, (PFILETIME)&ulEnd, 
                    (PFILETIME)&ulCurProcKernelTime, (PFILETIME)&ulCurProcUserTime);
                GetSystemTimeAsFileTime((PFILETIME)&ulCurSysTime);
    
                ulKernelTimeUsed.QuadPart = ulCurProcKernelTime.QuadPart - ulLastProcKernelTime.QuadPart;
                ulUserTimeUsed.QuadPart = ulCurProcUserTime.QuadPart - ulLastProcUserTime.QuadPart;
                ulSysTimePast.QuadPart = ulCurSysTime.QuadPart - ulLastSysTime.QuadPart;
    
                iCpuUsage = (
                    (ulKernelTimeUsed.QuadPart + ulUserTimeUsed.QuadPart)*100 /  
                                    (float)(si.dwNumberOfProcessors*ulSysTimePast.QuadPart)) + 0.5f
                    ;
    
                printf("\rThe Process use %3d%%", iCpuUsage);
    
                ulLastSysTime = ulCurSysTime;
                ulLastProcKernelTime = ulCurProcKernelTime;
                ulLastProcUserTime = ulCurProcUserTime;
            }
        }
        return 0;
    }
  • 相关阅读:
    关于TextField
    判断一个显示对象是否移除
    不争气的Discuz!NT 3.6和MVC3整合,主要实现同步登录和注册,登出。
    我的博客是英文的
    TFS不提供 Team Foundation 服务的解决办法。
    四 为提高entity framework 性能,要注意哪些事情.
    三 EF 和ado.net 的性能对比.
    一 关于大项目的经验总结
    在.net 中,ajax 如何调用本页数据源
    关于有序guid 的使用
  • 原文地址:https://www.cnblogs.com/zengqh/p/2479813.html
Copyright © 2020-2023  润新知