1.1 CPU占用率

zz/2024/6/13 21:44:04

下面是一些了解当前线程/进程/系统效能的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

 


http://www.ngui.cc/zz/2732118.html

相关文章

VC 2005 coding shortcuts

coding 了这么多年,有些快捷键没用了老忘,有空把它整出来(备忘) 一些还不怎么熟悉使用的快捷键://debug: AltF10 :Apply codes changes CtrlF10 :Run to cursor ShiftF11 :跳出当前函数CtrlShiftF10 &a…

[转]Cygwin使用

最近需要用到bash,perl等脚本,找到cygwin来学学... 很多用windows的朋友不习惯于用linux的开发环境。虽然很乐意尝试一下,但是往往怕linux系统打乱了自己的正常生活:1〉装linux系统把windows系统给搞坏了,导致自己无法正常生活&a…

ExtJs之grid使用详解(转帖)

Ext2.0是一个javascript框架,它的Grid控件和其它可以显示数据的控件,能够支持多种数据类型,如二维数组、Json数据和XML数据,甚至包括我们自定义的数据类型。Ext为我们提供了一个桥梁Ext.data.Store,通过它我们可以把任…

Oracle存储过程总结 (内容转载)

1、创建存储过程 create or replace procedure test(var_name_1 in type,var_name_2 out type) as --声明变量(变量名 变量类型) begin --存储过程的执行体 end test;-- E.g: 打印出输入的时间信息create or replace procedure test(workDate in Date) is begin dbms_output.…

Java的抽象类和接口示例

最近在学习java,在抽象类和接口上有这样一个示例。 代码如下: 下面展示一些 内联代码片。 package TaskFive; //打印机抽象类 //抽象类中的抽象方法,方法的具体实现由子类确定 abstract class Printer {abstract void pri(String s);//打印a…

Servlet补充学习——《JSP Web开发应用》

Servlet是java服务器端的小程序,是Java环境下实现动态网页的基本技术。Servlet程序能够调用Javabean、JDBC、其他servlet、RML等程序完成指定的功能。在应用中servlet起到了中间层的作用,将客户端和后台的资源隔离开来。 servlet有支持servlet的服务器&a…

Python的For+While循环的课堂练习(附上文连接)

>> Python每日笔记—Day04 << # 课堂小作业1-Star&#xff1a; # 通过while循环嵌套 实现如下图形 # ****...*** # * * # * * # * * # ****...***row int(input("请输入行数")) col int(input("请输入列数")) i 0 j…

Python使用Tkiner进行MD5加密

MD5加密 from tkinter import * import hashlib import timeLOG_LINE_NUM 0 # 日志信息内容class mu_gui():def __init__(self, init_window_name):self.init_window_name init_window_name# 窗口布局的方法def set_init_window(self):self.init_window_name.titleMD5加密…

Python每日笔记13(线程)

本次内容&#xff1a;线程 >> Python每日笔记—目录 << 线程 多线程的使用 Thread() group:线程组&#xff0c;目前只能使用None target:执行的目标任务名 *args:以元组的方式给执行任务进行传参 **kwargs:以字典的方式进行传参 name:线程名称&#xff0c;一般…

爬虫网络库(5.requests网络库)

这里写目录标题 Request网络库介绍5.1 request使用案例15.2 get方法5.3 设置请求头headers5.4 get_binary方法5.5 使用post方法发送post请求5.6 response请求响应结果5.7 uploadfile上传文件5.8 设置Cookie5.9 使用Session对象5.10 SSL签名证书5.11 proxy设置HTTP/HTTPS的代理5…