怎样得到一个线程的CPU使用率阿?

budded 2004-11-18 03:22:39
怎样得到一个线程的CPU使用率阿?
...全文
241 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
waterpub 2004-11-18
  • 打赏
  • 举报
回复
我先拿回去试试,如果可用的话,再来谢你!
曾牧暗鲨怎么跑到delphi版来了~
waterpub 2004-11-18
  • 打赏
  • 举报
回复
good
  • 打赏
  • 举报
回复
GetProcessUse(PROCESS_INFORMATION proc,_JOBOBJECT_BASIC_ACCOUNTING_INFORMAT Info)
{

Handle hJob=CreateJobOjbect(NULL,NULL);
AssignProcessToJobObject(hjob,proc.hProcess,NULL,0,0);
QueryInformationJobOjbect(hJob,Info)
closeHandle(hjob);
}

typedef struct _JOBOBJECT_BASIC_ACCOUNTING_INFORMATION
{
LARGE_INTEGER TotalUserTime;
LARGE_INTEGER TotalKernelTime;
LARGE_INTEGER ThisPeriodTotalUerTime
LARGE_INTEGER thisPeriodTotalKernelTime;
DWORD TotalPageFaultCount
DWORD TotalProcess
DWORD ActiveProcesses
DWORD TotalTerminatedProcesses;
}_JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,*_JOBOBJECT_BASIC_ACCOUNTING_INFORMATION
请提交FAQ
constantine 2004-11-18
  • 打赏
  • 举报
回复
我贴的第二个可以运行的,我试过
if(TotalDelta != 0)

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

"Memory = "+ IntToStr(dwWorkingSet / 1024) +" K";----》K前面刚才少了个+号
加个Timer1,Button2,就可以
InvidentXp 2004-11-18
  • 打赏
  • 举报
回复
.....虽然没试过管用不管用,但是Up.
Friecin 2004-11-18
  • 打赏
  • 举报
回复
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
NtQuerySystemInformation =(NTQSI)GetProcAddress(
GetModuleHandle("ntdll.dll"),
"NtQuerySystemInformation" );

if (!NtQuerySystemInformation)
{
// MessageBox("no this function");
return false;
}

// get num of processors
status = NtQuerySystemInformation(0,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
if (status != NO_ERROR)
{
// MessageBox("failed to query basic info(num of processors)");
return false;
}
return true;
}
double GetCPUUsage()
{
// get system time
status = NtQuerySystemInformation(3,&SysTimeInfo,sizeof(SysTimeInfo),0);
if (status!=NO_ERROR)
{
// MessageBox("failed to get system time!");
return ret_error;
}

// get cpu idle time
status = NtQuerySystemInformation(2,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR)
{
// MessageBox("failed to get cpu time");
return ret_error;
}
if (liOldIdleTime.QuadPart != 0)
{
// new cpu Time = NewTime - OldTime
dbCpuIdleTime =LI64ToDouble(SysPerfInfo.liIdleTime)-LI64ToDouble(liOldIdleTime);
dbSysTime =LI64ToDouble(SysTimeInfo.liKeSystemTime)-LI64ToDouble(liOldSysTime);

//get cpu usage
unCpuUsage=100 - 100*(dbCpuIdleTime/dbSysTime)/(double)SysBaseInfo.bKeNumberProcessors+0.5;
}

// store new cpu idle and system time
liOldIdleTime = SysPerfInfo.liIdleTime;
liOldSysTime = SysTimeInfo.liKeSystemTime;

return unCpuUsage;
}
constantine 2004-11-18
  • 打赏
  • 举报
回复
获得系统中某个进程的cpu使用率
本文作者: 不详
文章出处: csdn
阅读次数: 288
发布日期: 10/17/2003
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;



//每秒钟查询一次

void __fastcall TForm1::Timer1Timer(TObject *Sender)

{

Button2Click(NULL);

}

//---------------------------------------------------------------------------


void __fastcall TForm1::Button2Click(TObject *Sender)

{

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 == GetCurrentProcessId())

{

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)

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

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


LastTotalProcessCPUUsage = TotalProcessCPUUsage;

LastCurrentProcessCPUUsage = CurrentProcessCPUUsage;


delete[] pProcInfo;

}


这个估计比较清楚
Friecin 2004-11-18
  • 打赏
  • 举报
回复
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
NtQuerySystemInformation =(NTQSI)GetProcAddress(
GetModuleHandle("ntdll.dll"),
"NtQuerySystemInformation" );

if (!NtQuerySystemInformation)
{
// MessageBox("no this function");
return false;
}

// get num of processors
status = NtQuerySystemInformation(0,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
if (status != NO_ERROR)
{
// MessageBox("failed to query basic info(num of processors)");
return false;
}
return true;
}
double GetCPUUsage()
{
// get system time
status = NtQuerySystemInformation(3,&SysTimeInfo,sizeof(SysTimeInfo),0);
if (status!=NO_ERROR)
{
// MessageBox("failed to get system time!");
return ret_error;
}

// get cpu idle time
status = NtQuerySystemInformation(2,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR)
{
// MessageBox("failed to get cpu time");
return ret_error;
}
if (liOldIdleTime.QuadPart != 0)
{
// new cpu Time = NewTime - OldTime
dbCpuIdleTime =LI64ToDouble(SysPerfInfo.liIdleTime)-LI64ToDouble(liOldIdleTime);
dbSysTime =LI64ToDouble(SysTimeInfo.liKeSystemTime)-LI64ToDouble(liOldSysTime);

//get cpu usage
unCpuUsage=100 - 100*(dbCpuIdleTime/dbSysTime)/(double)SysBaseInfo.bKeNumberProcessors+0.5;
}

// store new cpu idle and system time
liOldIdleTime = SysPerfInfo.liIdleTime;
liOldSysTime = SysTimeInfo.liKeSystemTime;

return unCpuUsage;
}
constantine 2004-11-18
  • 打赏
  • 举报
回复
结构定义:

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;

}

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

以上代码运行非常正常

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

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧