Расчет нагрузки процессора указанным процессом

Эксперт
Статус
Оффлайн
Регистрация
12 Июн 2014
Сообщения
999
Реакции[?]
1,209
Поинты[?]
3K
старый кусок кода, определяющий общую нагрузку приложения на процессор.

Код:
static int proc_count = 0;

class IInfo
{
public:
    IInfo(HANDLE        m_Handle)
    {
        this->m_Handle = m_Handle;
    }
    int   NumberProcessors()
    {
        SYSTEM_INFO info;
        GetSystemInfo(&info);
        return static_cast<int>(info.dwNumberOfProcessors);
    }

    uint64_t   QuadPartFt(const FILETIME* ft)
    {
        LARGE_INTEGER li;
        if (NULL == ft)
            return -1;
        li.LowPart = ft->dwLowDateTime;
        li.HighPart = ft->dwHighDateTime;
        return li.QuadPart;
    }

    int  cpu()
    {
        proc_count = -1;

        
        static int64_t last_time_ = 0;
        static int64_t last_system_time_ = 0;

        FILETIME now;
        FILETIME creation_time;
        FILETIME exit_time;
        FILETIME kernel_time;
        FILETIME user_time;
        int64_t system_time;
        int64_t time;
        int64_t system_time_delta;
        int64_t time_delta;

        int cpu = -1;

        if (proc_count == -1)
            proc_count = NumberProcessors();


        GetSystemTimeAsFileTime(&now);

        if (!GetProcessTimes(GetHandle(), &creation_time, &exit_time, &kernel_time, &user_time))
            return -1;

        system_time = (QuadPartFt(&kernel_time) + QuadPartFt(&user_time)) / proc_count;
        time = QuadPartFt(&now);

        if ((last_system_time_ == 0) || (last_time_ == 0))
        {
            last_system_time_ = system_time;
            last_time_ = time;
            return -1;
        }

        system_time_delta = system_time - last_system_time_;
        time_delta = time - last_time_;

        //assert(time_delta != 0);

        if (time_delta == 0)
            return -1;

        
        cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta);
        last_system_time_ = system_time;
        last_time_ = time;

        return cpu;
    }
    bool  CheckHandle()
    {
        return NULL != m_Handle;
    }

    ~IInfo()
    {
        if (NULL != m_Handle)
        {
            CloseHandle(m_Handle);
            m_Handle = NULL;
        }
    }
    HANDLE GetHandle() const { return m_Handle; }
private:
    HANDLE        m_Handle;
};

использование:
Код:
IInfo  *m_pInfo = new IInfo(GetCurrentProcess());
    if (m_pInfo)
    {
        while (m_pInfo->CheckHandle())
        {
            int cpu = m_pInfo->cpu();
            printf_s("\rCPU: %10u", cpu);
            Sleep(1000);
        }
     }

    delete m_pInfo;

для теста:
Код:
#include <process.h>
#include <time.h>
void workload(void *)
{
   while (1)
   {
     clock_t t = clock() + 50;
     while (clock() < t) {}
     Sleep(50);
   }
}



         //эмитируем загрузку процессора
        int work;
        for (int i = 0; i < 4; i++)
            _beginthread(workload, 0, &work);
 
Сверху Снизу