如何获得线程占用的CPU?

GDI_GTK 2008-11-26 07:40:37
在进程外或者进程内获得都可以。

谢谢!
...全文
49 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
thunk123 2008-11-26
得到一个线程的CPU使用率
---------------------------------------------------------------

结构定义:

typedef struct _THREAD_INFO

{

LARGE_INTEGER CreateTime;

DWORD dwUnknown1;

DWORD dwStartAddress;

DWORD StartEIP;

DWORD dwOwnerPID;

DWORD dwThreadId;

DWORD dwCurrentPriority;

DWORD dwBasePriority;

DWORD dwContextSwitches;

DWORD Unknown;

DWORD WaitReason;

}THREADINFO, *PTHREADINFO;

typedef struct _UNICODE_STRING

{

USHORT Length;

USHORT MaxLength;

PWSTR Buffer;

} UNICODE_STRING;

typedef struct _PROCESS_INFO

{

DWORD dwOffset;

DWORD dwThreadsCount;

DWORD dwUnused1[6];

LARGE_INTEGER CreateTime;

LARGE_INTEGER UserTime;

LARGE_INTEGER KernelTime;

UNICODE_STRING ProcessName;

DWORD dwBasePriority;

DWORD dwProcessID;

DWORD dwParentProcessId;

DWORD dwHandleCount;

DWORD dwUnused3[2];

DWORD dwVirtualBytesPeak;

DWORD dwVirtualBytes;

ULONG dwPageFaults;

DWORD dwWorkingSetPeak;

DWORD dwWorkingSet;

DWORD dwQuotaPeakPagedPoolUsage;

DWORD dwQuotaPagedPoolUsage;

DWORD dwQuotaPeakNonPagedPoolUsage;

DWORD dwQuotaNonPagedPoolUsage;

DWORD dwPageFileUsage;

DWORD dwPageFileUsagePeak;

DWORD dCommitCharge;

THREADINFO ThreadSysInfo[1];

} PROCESSINFO, *PPROCESSINFO;

函数定义:

int Button2Click(int id);//参数是进程ID

函数实体:

int Button2Click(int id)

{

int cpuusage;

PVOID pProcInfo = NULL;

DWORD dwInfoSize = 0x20000;

PPROCESSINFO pProcessInfo;

DWORD dwWorkingSet;

long ( __stdcall *NtQuerySystemInformation )( DWORD, PVOID, DWORD, DWORD );

static __int64 LastTotalProcessCPUUsage = 0;

static __int64 LastCurrentProcessCPUUsage = 0;

int CurrentDelta;

int TotalDelta;

__int64 TotalProcessCPUUsage = 0;

__int64 CurrentProcessCPUUsage = 0;

/////////////////////////////////

pProcInfo = (PVOID)(new byte[dwInfoSize]);

NtQuerySystemInformation = (long(__stdcall*)(DWORD,PVOID,DWORD,DWORD))

GetProcAddress( GetModuleHandle( "ntdll.dll" ),"NtQuerySystemInformation" );

NtQuerySystemInformation(5,pProcInfo,dwInfoSize,0);

pProcessInfo = (PPROCESSINFO)pProcInfo;

do

{

TotalProcessCPUUsage += (__int64)pProcessInfo->KernelTime.QuadPart + (__int64)pProcessInfo->UserTime.QuadPart;

if(pProcessInfo->dwProcessID == id)

{

dwWorkingSet = pProcessInfo->dwWorkingSet;

CurrentProcessCPUUsage += (__int64)pProcessInfo->KernelTime.QuadPart + (__int64)pProcessInfo->UserTime.QuadPart;

}

/////////

if(pProcessInfo->dwOffset == 0)

{

break;

}

pProcessInfo = (PPROCESSINFO)((byte*)pProcessInfo + pProcessInfo->dwOffset);

}

while(true);

TotalDelta = TotalProcessCPUUsage - LastTotalProcessCPUUsage;

CurrentDelta = CurrentProcessCPUUsage - LastCurrentProcessCPUUsage;

if(TotalDelta != 0)

cpuusage = 100 * CurrentDelta / TotalDelta;

//this->Caption = "CPU = " + IntToStr(100 * CurrentDelta / TotalDelta) +

//"Memory = "+ IntToStr(dwWorkingSet / 1024) " K";

LastTotalProcessCPUUsage = TotalProcessCPUUsage;

LastCurrentProcessCPUUsage = CurrentProcessCPUUsage;

delete[] pProcInfo;

return cpuusage;

}

控制台调用:

int main(void)

{

while(true)

{

int s = Button2Click(0);//在此把进程ID作为参数传入

printf("%d\n",s);

Sleep(1000);

}

return 0;

}

///////////////////////////////

以上代码运行非常正常

///////////////////////////////

---------------------------------------------------------------

bool CanGetCPUUsage()
{
//os version information structure
OSVERSIONINFO OsVersionInfo;
OsVersionInfo.dwOSVersionInfoSize= sizeof(OSVERSIONINFO);
//get os version
GetVersionEx(&OsVersionInfo);
//only run in win2000/nt OS
if(OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
{
// MessageBox("Only run in Win2K/NT,\\nYour OS is not Win2000/NT!","prompt:");
return false;
}

//Get NtQuerySystemInformation Address from ntdll.dll
回复
chexlong 2008-11-26
BOOL GetThreadTimes(
HANDLE hThread,
LPFILETIME lpCreationTime,
LPFILETIME lpExitTime,
LPFILETIME lpKernelTime,
LPFILETIME lpUserTime
);

和总时间比较一下
回复
cnzdgs 2008-11-26
GetThreadTimes
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2008-11-26 07:40
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……