我如何通过 PID 计算每个进程的百分比 CPU

How can i count percentage CPU for each process by PID

我正在尝试编写任务管理器代码,但我坚持为每个进程 dy PID 使用 %CPU。 我写了类似的东西:

  static float CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks)
{
    static unsigned long long _previousTotalTicks = 0;
    static unsigned long long _previousIdleTicks = 0;

    unsigned long long totalTicksSinceLastTime = totalTicks - _previousTotalTicks;
    unsigned long long idleTicksSinceLastTime = idleTicks - _previousIdleTicks;

    float ret = 1.0f - ((totalTicksSinceLastTime > 0) ? ((float)idleTicksSinceLastTime) / totalTicksSinceLastTime : 0);

    _previousTotalTicks = totalTicks;
    _previousIdleTicks = idleTicks;
    return ret;
}

static unsigned long long FileTimeToInt64(const FILETIME& ft) { return (((unsigned long long)(ft.dwHighDateTime)) << 32) | ((unsigned long long)ft.dwLowDateTime); }

并且像这样使用它:

 hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE)
    {
        printError("Failed to create Process Snap");
        return FALSE;
    }
    pe.dwSize = sizeof(PROCESSENTRY32);

    if (!Process32First(hProcessSnap, &pe))
    {
        printError("Failed to move along process snap");
        CloseHandle(hProcessSnap);
        return FALSE;
    }

    do
    {
        printf("\n\n=====================================================");
        _tprintf(TEXT("\n PROCESS NAME: %s"), pe.szExeFile);
        printf("\n-----------------------------------------------------");
        dwPriorityClass = 0;
        hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
        if (hProcess == NULL)
        {
            printError("Failed to open process");
        }
        else
        {
            for (int i = 0; i < 2; i++)
            {
            GetProcessTimes(hProcess, &exist, &exit, &lastKernel, &lastUser);
            GetSystemTimes(&lastIdle, 0, 0);
            GetCPULoad(lastIdle, lastKernel, lastUser);
            Sleep(2500);
            }
        std::cout << GetCPULoad(lastIdle, lastKernel, lastUser) << "\n";
        CloseHandle(hProcess);
        }
    } while (Process32Next(hProcessSnap, &pe));
    CloseHandle(hProcessSnap);
    return (TRUE);
}

我知道在这里使用 sleep() 不是一个好主意,但我暂时还没有想出更好的办法。

如果可以的话,请帮我写一些代码示例。

我也想知道我说的对不对:

CPU% for process= (1- (IdleSystemTimeDelta/TotalProcessTimeDelta))*100%

这就是我 get_CPU 的百分比。

我使用 hash_map 是为了让 PID 时间连接静态来更新它。

第一次使用get_cpu_usage(int pid) returns每次都为零,但随着每次使用它会越来越准确(我使用它的时间为0.5秒)。

static int get_processor_number()
    {
        SYSTEM_INFO info;
        GetSystemInfo(&info);
        return (int)info.dwNumberOfProcessors;
    }
    static __int64 file_time_2_utc(const FILETIME* ftime)
    {
        LARGE_INTEGER li;
    
        li.LowPart = ftime->dwLowDateTime;
        li.HighPart = ftime->dwHighDateTime;
        return li.QuadPart;
    }
    static int get_cpu_usage(int pid)
    {
        static int processor_count_ = -1;
        static std::unordered_map<int, __int64> last_time_;
        static std::unordered_map<int, __int64> last_system_time_;
    
        FILETIME now;
        FILETIME creation_time;
        FILETIME exit_time;
        FILETIME kernel_time;
        FILETIME user_time;
        __int64 system_time;
        __int64 time;
        __int64 system_time_delta;
        __int64 time_delta;
    
        int cpu = -1;
    
        if (processor_count_ == -1)
        {
            processor_count_ = get_processor_number();
        }
    
        GetSystemTimeAsFileTime(&now);
    
        HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
        if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))
        {
            std::cout << "Unable to getProcessTime\n";
            return -1;
        }
        system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time)) / processor_count_;
        time = file_time_2_utc(&now);
    
        if ((last_system_time_[pid] == 0) || (last_time_[pid] == 0))
        {
            last_system_time_[pid] = system_time;
            last_time_[pid] = time;
            return 0;
        }
    
        system_time_delta = system_time - last_system_time_[pid];
        time_delta = time - last_time_[pid];
    
        if (time_delta == 0)
        {
            std::cout << "timedelta=0";
            return -1;
        }
        cpu = int((system_time_delta * 100 + time_delta / 2) / time_delta);
        last_system_time_[pid] = system_time;
        last_time_[pid] = time;
        return cpu;
    }