问题描述
我正在尝试实现性能监视工具,因此我想监视诸如内存和CPU之类的基本内容.
Im trying to implement a performance monitoring tool, I want to monitor basic things such as Memory and CPU.
我正在尝试通过使用性能计数器来执行此操作,因为我相信这也是任务管理器也在后台使用的功能.我不知道任务管理器如何执行此操作,但是对我而言,使用这种方法似乎很费时的时间来检索过程数据:
I am attempting to do so by using Performance Counters as I believe this is what Task Manager is using behind the scenes too. I have no idea how Task Manager is able to do this however as to me it seems to take a VERY long time to retrieve process data using this method:
class Program
{
static void Main(string[] args)
{
while (true)
{
var pcs = Process.GetProcesses()
.Select(p => new PerformanceCounter("Process", "Working Set - Private", p.ProcessName));
var sw = Stopwatch.StartNew();
foreach (var pc in pcs)
pc.NextValue();
Console.WriteLine($"Time taken to read {pcs.Count()} performance counters: {sw.ElapsedMilliseconds}ms");
Thread.Sleep(1000);
}
}
}
有人对如何执行此操作或任务管理器或Process Explorer如何执行此操作有任何建议吗?
Has anyone got any suggestions on how to do this or how even Task Manager or Process Explorer is able to do this?
推荐答案
他使用了对ZwQuerySystemInformation
,ZwQueryInformationProcess
,ZwQueryInformationThread
的调用.任务管理器维护活动进程的数据库,并通过调用ZwQuerySystemInformation(SystemProcessInformation,)
定期更新此信息-因此在退出时获得了SYSTEM_PROCESS_INFORMATION
数组.添加新条目(如果找到新进程,但未在DB
中),删除死进程的条目,更新已运行进程的信息SYSTEM_PROCESS_INFORMATION
已经包含很多过程信息.其他信息可以通过打开过程获取,并使用适当的信息类致电ZwQueryInformationProcess
he used calls to ZwQuerySystemInformation
, ZwQueryInformationProcess
, ZwQueryInformationThread
..Task Manager maintain database of active processes and periodically update this info by calling ZwQuerySystemInformation(SystemProcessInformation,)
- so got array of SYSTEM_PROCESS_INFORMATION
on exit.add new entries if found new process, yet not in DB
, remove entries for died processes, update info for livedSYSTEM_PROCESS_INFORMATION
already containing a lot information of process. additional information can be get by open process and call ZwQueryInformationProcess
with appropriate info class
如果要实现性能监视工具而没有量子效应"(当测量影响状态本身时),则需要使用此ntdll api.有关定义,请参见 http://processhacker.sourceforge.net/doc/ntexapi_8h_source.html尽管没有记录,但现有功能和结构并未改变,从win2000(大约17年)起的最低要求-Windows的新版本添加了许多新的信息类,可以使用一些旧版本中未使用/未使用的字段-但旧版本(旧版)未更改
if you want implement a performance monitoring tool, without "quantum effect" (when the measurement affects the state itself) you need use this ntdll api. for definitions look at http://processhacker.sourceforge.net/doc/ntexapi_8h_source.htmldespite this is undocumented, existing functions and structures not changed how minimum from win2000 (so ~17 years) - new version of windows add a lot new info classes, some fields which was spare/unused in old version - can become used, but old(legacy) not changed
这篇关于性能计数器的读取访问速度非常慢-任务管理器如何做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!