简体   繁体   English

性能计数器的读取访问速度非常慢-任务管理器如何做到这一点?

[英]Performance Counter read access very slow - How does Task Manager do it?

Im trying to implement a performance monitoring tool, I want to monitor basic things such as Memory and CPU. 我正在尝试实现性能监视工具,因此我想监视诸如内存和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);
            }
        }
    }

在此处输入图片说明

Has anyone got any suggestions on how to do this or how even Task Manager or Process Explorer is able to do this? 有没有人对如何执行此操作或任务管理器或Process Explorer如何执行此操作有任何建议?

How does Task Manager do it? 任务管理器如何执行?

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. 他使用了对ZwQuerySystemInformationZwQueryInformationProcessZwQueryInformationThread 。任务管理器维护活动进程的数据库,并通过调用ZwQuerySystemInformation(SystemProcessInformation,)定期更新此信息-退出时得到SYSTEM_PROCESS_INFORMATION数组。 add new entries if found new process, yet not in DB , remove entries for died processes, update info for lived SYSTEM_PROCESS_INFORMATION already containing a lot information of process. 如果找到新进程,则添加新条目,但不在DB ,删除死进程的条目,更新已经包含很多进程信息的有效SYSTEM_PROCESS_INFORMATION信息。 additional information can be get by open process and call ZwQueryInformationProcess with appropriate info class 其他信息可以通过打开过程获取,并使用适当的信息类调用ZwQueryInformationProcess

if you want implement a performance monitoring tool, without "quantum effect" (when the measurement affects the state itself) you need use this ntdll api. 如果要实现性能监视工具而没有“量子效应”(当测量影响状态本身时),则需要使用此ntdll api。 for definitions look at http://processhacker.sourceforge.net/doc/ntexapi_8h_source.html despite 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 有关定义的信息,请参见http://processhacker.sourceforge.net/doc/ntexapi_8h_source.html,尽管这没有记录,但现有的功能和结构并未改变win2000的最低要求(大约17年)-Windows新版本增加了很多新信息类,某些字段在旧版本中是备用/未使用的-可以使用,但旧(旧版)不会更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM