一、GetProcessTimes函數(shù)簡介(微軟MSDN)
微軟提供了一個(gè)非常有用的API函數(shù)GetProcessTimes
用來獲取進(jìn)程創(chuàng)建時(shí)間、銷毀時(shí)間、用戶態(tài)時(shí)間、內(nèi)核態(tài)時(shí)間,msdn連接為:GetProcessTimes 函數(shù) (processthreadsapi.h)
其函數(shù)原型為:
BOOL GetProcessTimes(
[in] HANDLE hProcess,
[out] LPFILETIME lpCreationTime,
[out] LPFILETIME lpExitTime,
[out] LPFILETIME lpKernelTime,
[out] LPFILETIME lpUserTime
);
其參數(shù)如下:
其返回值和函數(shù)說明如下:
二、示例程序
相關(guān)示例程序如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <Psapi.h>
#include <winnt.h>
#include <winternl.h>
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
void test_GetProcessTimes()
{
HANDLE processHandle = GetCurrentProcess();
DWORD currentProcessId = GetProcessId(processHandle);
FILETIME createTime, exitTime, kernelTime, userTime;
// 獲取當(dāng)前進(jìn)程的PID
DWORD pid = GetCurrentProcessId();
printf("pid: %d\t currentProcessId: %d\n", pid, currentProcessId);
GetProcessTimes(processHandle, &createTime, &exitTime, &kernelTime, &userTime);
printf("processHandle: %lu\t currentProcessId: %d\n", HandleToULong(processHandle), currentProcessId);
printf("Create time: %lu\t %lu\nExit Time: %lu\t %lu\nKernel time: %lu\t %lu\nUser time: %lu\t %lu\n",
createTime.dwLowDateTime, createTime.dwHighDateTime,
exitTime.dwLowDateTime, exitTime.dwHighDateTime,
kernelTime.dwLowDateTime, kernelTime.dwHighDateTime,
userTime.dwLowDateTime, userTime.dwHighDateTime);
::CloseHandle(processHandle);
}
// 返回進(jìn)程pid創(chuàng)建時(shí)間到現(xiàn)在時(shí)間經(jīng)過的秒數(shù)
double get_uptime_sec(DWORD pid)
{
double r{ 0 };
HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (hProcess)
{
FILETIME creationTime, exitTime, kernelTime, userTime;
if (::GetProcessTimes(
hProcess, &creationTime, &exitTime, &kernelTime, &userTime)) {
LARGE_INTEGER tCreate;
tCreate.LowPart = creationTime.dwLowDateTime;
tCreate.HighPart = creationTime.dwHighDateTime;
std::cout << "tCreate: " << tCreate.QuadPart << std::endl;
int64_t tt = (static_cast<int64_t>(creationTime.dwHighDateTime) << 32) | creationTime.dwLowDateTime;
std::cout << "tt: " << tt << std::endl;
SYSTEMTIME stCreate;
FileTimeToSystemTime(&creationTime, &stCreate);
r = (double)stCreate.wHour * 3600.0 +
(double)stCreate.wMinute * 60.0 +
(double)stCreate.wSecond +
(double)stCreate.wMilliseconds / 1000.0;
std::cout << "r: " << r << std::endl;
}
::CloseHandle(hProcess);
}
return r;
}
三、進(jìn)一步擴(kuò)展:獲取每個(gè)進(jìn)程一段時(shí)間內(nèi)的CPU使用率
由于GetProcessTimes 函數(shù)可以獲取某個(gè)進(jìn)程的在內(nèi)核模式下執(zhí)行的時(shí)間量和用戶模式下執(zhí)行的時(shí)間量(以100納秒為單位)。我們可以先使用NtQuerySystemInformation
函數(shù)獲取每個(gè)CPU核心的總的用戶態(tài)、內(nèi)核態(tài)、空閑時(shí)間總時(shí)間量sysTotalTime,然后遍歷枚舉當(dāng)前系統(tǒng)所有運(yùn)行進(jìn)程,再用GetProcessTimes
去獲取每個(gè)進(jìn)程的在內(nèi)核模式下執(zhí)行的時(shí)間量和用戶模式下執(zhí)行的時(shí)間量,除以sysTotalTime即為該進(jìn)程的CPU使用率。開一個(gè)線程每隔一段時(shí)間,比如說250毫秒、500毫秒、1秒、2秒等定時(shí)輪詢獲取。
參考ProcessHacker
的源代碼,它里面也大體是這個(gè)思路。
四、參考資料
-
getProcessTimes 函數(shù) (processthreadsapi.h)
-
How to retrieve the running-time of a process文章來源:http://www.zghlxwxcb.cn/news/detail-759005.html
-
How to get Process Uptime in Windows文章來源地址http://www.zghlxwxcb.cn/news/detail-759005.html
到了這里,關(guān)于VC++使用GetProcessTimes獲取進(jìn)程創(chuàng)建時(shí)間、銷毀時(shí)間、用戶態(tài)時(shí)間、內(nèi)核態(tài)時(shí)間的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!