1.1 CPU占用率

下面是一些了解当前线程/进程/系统效能的API,所有描述来自MSDN文档,具体如下:

注意每个函数对应的头文件和相应的lib.



DWORD GetTickCount(void);
/*
Header: Winbase.h, include Windows.h
Link Library: Kernel32.lib
DLL: Dernel32.dll
Details:
The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.
Remarks:
The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.


Example Code:
The following example demonstrates how to use a this function to wait for a time interval to pass. Due to the nature of unsigned arithmetic, this code works correctly if the return value wraps one time. If the difference between the two calls to GetTickCount is more than 49.7 days, the return value could wrap more than one time and this code will not work.


DWORD dwStart = GetTickCount();
if( GetTickCount() - dwStart >= TIMELIMIT )
    Cancel();
*/

void Sleep(DWORD dwMilliseconds);
/*
Header: Winbase.h, include Windows.h
Library: Kernel32.lib
DLL: Kernel32.dll

Details:
This function suspends the execution of the current thread for a specified interval.
Parameters:
Specifies the time, in milliseconds, for which to suspend execution.
A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If no other threads of equal priority are ready to run, the function returns immediately, and the thread continues execution.
A value of INFINITE causes an infinite delay.


Remarks:
You have to be careful when using Sleep and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. If you have a thread that uses Sleep with infinite delay, the system will deadlock. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than Sleep.
*/

DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);
/*
Header: Winbase.h, include Windows.h
Library: Kernel32.lib
DLL: Kernel32.dll

Details:
The WaitForSingleObject function returns when the specified object is in the signaled state or the time-out interval elapses.


Parameters:
hHandle:
Handle to the object. If this handle is closed while the wait is still pending, the function's behavior is undefined.
dwMilliseconds:
Time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.

Return Values:
If the function succeeds, the return value indicates the event that caused the function to return. It can be one of the following values.
WAIT_ABANDONED:当hHandle为mutex时,如果拥有mutex的线程在结束时没有释放核心对象会引发此返回值。
WAIT_OBJECT_0:核心对象已被激活
WAIT_TIMEOUT:等待超时
WAIT_FAILED:出现错误,可通过GetLastError得到错误代码

Remarks:
The WaitForSingleObject function checks the current state of the specified object. If the object's state is nonsignaled, the calling thread enters the wait state. It uses no processor time while waiting for the object state to become signaled or the time-out interval to elapse.
The function modifies the state of some types of synchronization objects. Modification occurs only for the object whose signaled state caused the function to return. For example, the count of a semaphore object is decreased by one.
*/

HRESULT GetProcessorInfo(WORD* pwProcessorArchitectureOut, WORD* pwProcessorLevelOut);
/*
DLL: Clcfgsrv.dll
Details:
The GetProcessorInfo method retrieves the processor information for the node.

Parameters:
pwProcessorArchitectureOut: Processor architecture information.
pwProcessorLevelOut: Processor level information.
*/

DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask);
/*
Header: Winbase.h, include Windows.h
DLL: Kernel32.dll
Library: Kernel32.lib

Details:
The SetThreadAffinityMask function sets a processor affinity mask for the specified thread.

Return Values:
If the function succeeds, the return value is the thread's previous affinity mask. Windows Me/98/95: The return value is 1. To succeed, hThread must be valid and dwThreadAffinityMask must be 1.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks:
A thread affinity mask is a bit vector in which each bit represents the processors that a thread is allowed to run on.
A thread affinity mask must be a proper subset of the process affinity mask for the containing process of a thread. A thread is only allowed to run on the processors its process is allowed to run on.
*/

DWORD timeGetTime(VOID);
/*
Header: Mmsystem.h, include Windows.h
Library: Winmm.lib

Details:
The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows was started.

Remarks:
The only difference between this function and the timeGetSystemTime function is that timeGetSystemTime uses the MMTIME structure to return the system time. The timeGetTime function has less overhead than timeGetSystemTime.
Note that the value returned by the timeGetTime function is a DWORD value. The return value wraps around to 0 every 2^32 milliseconds, which is about 49.71 days. This can cause problems in code that directly uses the timeGetTime return value in computations, particularly where the value is used to control code execution. You should always use the difference between two timeGetTime return values in computations.
*/

MMRESULT timeGetSystemTime(LPMMTIME pmmt, UINT cbmmt);
/*
Header: Mmsystem.h, include Windows.h
Library: Winmm.lib

Details:
The timeGetSystemTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows was started. This function works very much like the timeGetTime function. See timeGetTime for details of these functions' operation.

Parameters:
pmmt:  Pointer to an MMTIME structure.
cbmmt: Size, in bytes, of the MMTIME structure.

Return Values:
Returns TIMERR_NOERROR. The system time is returned in the ms member of the MMTIME structure.
*/

在使用Windows.h中定义:
typedef unsigned long  DWORD;
typedef unsigned char  BYTE;
typedef unsigned short WORD;
typedef unsigned int UINT;

 

basetsd.h

 

typedef signed __int64  INT64

typedef unsigned __int64  UINT64

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值