PerformanceCounter“每个会话的用户输入延迟”消失

Posted

技术标签:

【中文标题】PerformanceCounter“每个会话的用户输入延迟”消失【英文标题】:PerformanceCounter "User Input Delay per Session" disappears 【发布时间】:2021-02-23 12:57:13 【问题描述】:

Win2019 Server 提供了新的性能计数器,可以帮助解决带有 RDS(=远程桌面服务)的主机上的性能问题。 使用前必须使用注册表项激活它们:https://docs.microsoft.com/en-us/windows-server/remote/remote-desktop-services/rds-rdsh-performance-counters 系统重新启动后,它们就可用了。

在 perfmon.msc 中它们是可见的并且可以使用。 但是当我开始使用 C# 代码来读取这些计数器时。

PerfCounters 在 C# 中也可见:

PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories();
foreach(var category in categories)

   Console.WriteLine(category.CategoryName);

然后我开始读取性能计数器:

PerformanceCounter performanceCounter = null;
bool continueRunning = true;
try

   performanceCounter = new PerformanceCounter("User Input Delay per Session", "Max Input Delay", "Max");
   Console.WriteLine("PerfCounter created");

catch(Exception ex)

   Console.WriteLine(string.Format("ERROR: Could not create PerfCounter = '0'", ex.Message));
   continueRunning = false;
          
while (continueRunning)

   try
   
     float perfValue = performanceCounter.NextValue();
     Console.WriteLine(string.Format("NextValue = '0'", perfValue));
   
   catch(Exception ex)
   
      Console.WriteLine(string.Format("ERROR: Could not read NextValue '0'", ex.Message));
      continueRunning = false;
   
   System.Threading.Thread.Sleep(2000);

现在,我收到程序错误和两个事件日志条目: 程序输出:

PerfCounter created
ERROR: Coud not read NextValue 'The Counter layout for the Category specified is invalid, a counter of the type:  AverageCount64, AverageTimer32, CounterMultiTimer, CounterMultiTimerInverse, CounterMultiTimer100Ns, CounterMultiTimer100NsInverse, RawFraction, or SampleFraction has to be immediately followed by any of the base counter types: AverageBase, CounterMultiBase, RawBase or SampleBase.'
Program terminated

事件日志条目:

    事件 ID 1003:“LSM”服务的可扩展计数器 DLL“C:\Windows\System32\perfts.dll”返回了不正确的对象长度。返回的 1 个对象长度的总和与返回的缓冲区大小不匹配。 事件 ID 1017:已禁用从“LSM”服务收集性能计数器数据,因为该服务的性能计数器库已生成一个或多个错误。强制执行此操作的错误已写入应用程序事件日志。在启用此服务的性能计数器之前更正错误。

从现在开始,性能计数器已经消失(也在 perfmon.msc 中)并且不再可见/不可用。 此外,(对我而言)它们无法重新激活。 perfmon.msc 中的性能计数器也消失了。 必须做些什么才能在 C# 中读取这些性能计数器而不会在操作系统中“丢失”它们? 也很重要 如何使丢失的性能计数器再次可见/可用?

【问题讨论】:

你确定这个查询在调用 NextValue() 之前不需要某种 First() 或 Seek() 吗?错误消息表明请求无效。也许 NextValue() 引用的日志行未正确初始化.. Microsoft 的示例仅使用 NextValue(),但我找不到 First() 方法。对我来说这有点令人沮丧,因为我无法重新激活丢失的性能计数器(重新安装操作系统不是要走的路) 【参考方案1】:

您可以使用 Microsoft 工具 lodctr 保存和恢复性能计数器。 因此,如果您可能已经“丢失”了这些计数器,请尝试“lodctr /r”。 他们可能会回来。

perfts.dll 需要 8 字节对齐,这并不总是能够满足,尤其是在 .NET 程序中。 解决此问题的解决方法: 在 VisualStudio 201 中禁用“首选 32 位”复选框。/ProjectSettings/Build/General

【讨论】:

【参考方案2】:

您只能以管理员权限阅读它们...

【讨论】:

以上是关于PerformanceCounter“每个会话的用户输入延迟”消失的主要内容,如果未能解决你的问题,请参考以下文章

需要使用 CounterDelta32 PerformanceCounter 的示例

如何以编程方式记录 PerformanceCounter

PerformanceCounter 在连接远程服务器时非常慢

到 PerformanceCounter 还是编写自定义监控?

PerformanceCounter“每个会话的用户输入延迟”消失

使用 PerformanceCounter 跟踪每个进程的内存和 CPU 使用情况?