WINCE CPU memory usage calculation

Performance data collection process, it is often used in a performance indicator is the cpu occupancy of the process, its calculation method is given below and the sample code.
1, the definition of CPU usage
CPU utilization: process means a period of time consumed CPU time ratio of the period length.
 
2, CPU utilization rate calculation method of
the above definition, it is possible to obtain the process CPU utilization is calculated as follows:
kernel mode time process consumes CPU time = process consumes + user mode time process consumes, i.e. costTime = kernelTime + UserTime
CPU processes = occupancy process consumes CPU time / refresh period (total time)



void McuUsageThread :: RUN () {
    THREADENTRY32 TE32 = {0};
    the FILETIME ftTimeCreation = {0};
    the FILETIME ftTimeExit = {0};
    the FILETIME = {0 ftTimeKernel };
    the FILETIME ftTimeUser = {0};
    the FILETIME ftLastTimeKernel = {0};
    the FILETIME ftLastTimeUser = {0};
    HANDLE hSnap = NULL;
    the LARGE_INTEGER liPerfFreq = {0};
    The LARGE_INTEGER liPerfCountLast = {0};
    the LARGE_INTEGER liPerfCountCur = {0};
    The LONGLONG llPerfDelta = 0;
    The LONGLONG llUserCur = 0;
    The LONGLONG llKernelCur = 0;
    The LONGLONG llUserLast = 0;
    The LONGLONG llKernelLast = 0;
    The LONGLONG llKernelDelta = 0;
    The LONGLONG llUserDelta = 0;


    IF (! QueryPerformanceFrequency ((LARGE_INTEGER *) & liPerfFreq))
    { // type: Win32API
//原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
// function: frequency hardware support for high-precision return counter.
// Return Value: zero, precision hardware support counter; zero, the hardware does not support read failure
// liPerfFreq acquired clock frequency
        return;
    }


    CeSetThreadPriority (GetCurrentThread (), 100);
// priority thread handle GetCurrentThread () threads, the second parameter is the priority of the thread
    the while (. 1)
    {
        Sleep (m_u32SampleTime_ms);


        llKernelCur = 0;
        llUserCur = 0;


        hSnap = the CreateToolhelp32Snapshot (TH32CS_SNAPTHREAD, 0) ;
// create a snapshot, get the process list
1 // parameters for a specified object needs to return to the snapshot,
// Parameter 2, a process ID number, which is used to specify a process to get a snapshot of the system when the acquisition process list or to obtain a snapshot of the current process can be set to 0
// specific look blog post,


        IF (hSnap = INVALID_HANDLE_VALUE!)
        {
            Te32.dwSize = sizeof (TE32);
            IF (Thread32First (hSnap, & TE32)) // get a snapshot of the first thread information
            {
                do
                {
                    HANDLE hThread to OpenThread = (THREAD_ALL_ACCESS, FALSE, te32.th32ThreadID);
                    IF (hThread)
                    {
                        GetThreadTimes (hThread, & ftTimeCreation,
                                       & ftTimeExit, & ftTimeKernel, & ftTimeUser);
// get the thread time
// Parameter 1, thread handle; Parameter 2: Create a time; Parameter 3: logout time; Parameter 4: kernel time; Parameter 5: user time
                        llKernelCur + = * ((LONGLONG * ) (& ftTimeKernel)) - * ((LONGLONG * ) (& ftLastTimeKernel));
                        llUserCur + = * ((The LONGLONG *) (& ftTimeUser)) - * ((The LONGLONG *) (& ftLastTimeUser));
                        the CloseHandle (hThread);
                    }
                } the while (Thread32Next (hSnap, & TE32)); // take a snapshot of the next thread information
            }
            CloseToolhelp32Snapshot (hSnap); // Close snapshot
        }

        the QueryPerformanceCounter (& liPerfCountCur);
        llPerfDelta = liPerfCountCur.QuadPart - liPerfCountLast.QuadPart; // count
        llPerfDelta = 10000000 * llPerfDelta / liPerfFreq.QuadPart; // converted to time 10,000,000 femtosecond out unit conversion
Total time = clock number // / * frequency = clock number (a number of the clock 1 time / frequency);
        liPerfCountLast = liPerfCountCur;

        llKernelDelta = llKernelCur - llKernelLast; // time kernel consumed
        llUserDelta = llUserCur - llUserLast; // User time spent
        llKernelLast = llKernelCur;
        llUserLast = llUserCur;

        INT32 dCPUKernel; dCPUKernel MATH_MAX = (0, MATH_MIN (100.0, 100.0 * llKernelDelta / llPerfDelta));
// kernel percentage of time consumed
        INT32 dCPUsage; dCPUsage = MATH_MAX (dCPUKernel, MATH_MIN (100.0, 100.0 * (llUserDelta llKernelDelta +) / llPerfDelta));
// user consumes the core plus the percentage of time consuming


        MEMORYSTATUS memStatus;
        memStatus.dwLength = the sizeof (MEMORYSTATUS);
        the GlobalMemoryStatus (& memStatus); // get memory information

MEMORYSTATUS structure analysis //
/ *
typedef struct {_MEMORYSTATUS
    DWORD The dwLength; // length of this structure
    DWORD dwMemoryLoad; // percentage of used memory
    DWORD dwTotalPhys; total physical memory //
    DWORD dwAvailPhys; available physical memory //
    DWORD dwTotalPageFile; // swap file size of the total
    DWORD dwAvailPageFile; // size of the swap file empty section
    DWORD dwTotalVirtual; // address space available to a user
    DWORD dwAvailVirtual; // current address space free
} MEMORYSTATUS, LPMEMORYSTATUS *;


* /


        PRINTF_MSG ( "@@@@@@@@@@@ ======================== dCPUsage is% d , dCPUKernel is% d", dCPUsage, dCPUKernel);
        PRINTF_MSG("@@@@@@@@@@@======================== UsageMem is %s, TotalMem is %s  ", FormatMemory_toString(memStatus.dwTotalPhys - memStatus.dwAvailPhys).toUtf8().data(), FormatMemory_toString(memStatus.dwTotalPhys).toUtf8().data());
    }
}



QString McuUsageThread::FormatMemory_toString(quint32 f_u32BytesNum) {//打印需要的
    quint32 t_u32bt = f_u32BytesNum & 0x2FF;
    quint32 t_u32Kb = (f_u32BytesNum>>10) & 0x2FF;
    quint32 t_u32Mb = (f_u32BytesNum>>20);
    return QString::number(t_u32Mb) + "Mb " + QString::number(t_u32Kb) + "Kb " + QString::number(t_u32bt) + "byte";
}
/******************************************************************************************************/
class McuUsageThread: public QThread // Create a thread for acquiring a print memory cpu occupancy rate
{
public:
    quint32 m_u32SampleTime_ms;
    McuUsageThread () {
        m_u32SampleTime_ms = 3500;
    }
    QString FormatMemory_toString (quint32 f_u32BytesNum);


public:
    void RUN ();
};

/******************************************************************************************************/

Summary: The idea of ​​the entire code is:

1. Get the system clock frequency

2. Set priorities before a single thread (can not think, did not verify the correctness is not set)

3. Create a snapshot traverse thread,

4. The relevant time snapshot acquisition thread, and the thread of all time-consuming calculations.

The acquired timer value (timer precision hardware support).

6. The total time is calculated by obtaining the value of the timer and the system clock frequency.

7. The cores were calculated percentage consuming, time consuming, and the percentage of users.


Memory footprint is relatively simple calculation is not to say, look at the code can understand.



Guess you like

Origin blog.csdn.net/DreamSonGO/article/details/78469841