【问题标题】:Why a call to PerformanceCounter is slow?为什么调用 PerformanceCounter 很慢?
【发布时间】:2014-02-24 14:11:30
【问题描述】:

我正在使用从 PerformanceCounters 读取“% CPU”和“可用内存”的服务器。 它在我的开发机器上运行良好。但是在实际的服务器上,从这两个 PerformanceCounter 中读取确实很慢。至少在前两次读取操作中。
执行以下代码最多可能需要 4-6 分钟:

        Stopwatch watch = new Stopwatch();

        Log.Instance.Debug("Going to initialize Diagnostic's PerformanceCounters");

        watch.Start();

        m_CPUPerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
        m_MemoryPerformanceCounter = new PerformanceCounter("Memory", "Available MBytes", true);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        //Ensure an updated value...
        System.Threading.Thread.Sleep(1000);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        watch.Stop();

        Log.Instance.Debug("Finished initializing Diagnosticss PerformanceCounters. Time elapsed: {0}", watch.Elapsed);  

当我在我的开发机器上运行这段代码时,它会在不到 2 秒的时间内完成(有时甚至更短)。但是在我们产品的客户应该使用的实际服务器上,需要很长时间。见下文:

Finished initializing Diagnosticss PerformanceCounters. Time elapsed: 00:03:59.6706860

重要的是这些读取操作(以及后来的“读取”操作)执行得非常快。甚至在一开始。

我的开发机器是 Windows 7,64 位,8 GB RAM。
客户端服务器是 Windows server 2008 R2 Enterprise,64 位,4 GB RAM。

我用谷歌搜索(并搜索)它,但找不到任何答案。 为什么会这样?我该如何解决?

【问题讨论】:

  • 我不知道问题出在哪里,但是://Ensure an updated value... System.Threading.Thread.Sleep(1000); 肯定不能确保值得到更新。
  • 为什么将计数器指定为只读模式访问?作为测试尝试 new PerformanceCounter("Processor", "% Processor Time", "_Total"); - 你确定你没有在这里隐藏异常吗?例如未授权访问异常
  • @Tarec 你是对的。但如果需要,操作系统有足够的时间更新这些 PerformanceCounters。
  • @RBarryYoung 经过这么长时间的初始化,每个读取操作都会执行得非常快。每次服务(我构建的,而不是操作系统)重新启动时,缓慢的操作都会重现。重启虚拟机没关系。
  • 网络问题可能来自内部尝试联系 PDC/BDC 并授权您的服务访问性能信息。

标签: c# .net windows performance performancecounter


【解决方案1】:

问题很可能出在受监控系统上安装的其他软件提供的计数器上。

在这种情况下,您应该对系统注册表中注册的所有性能计数器进行性能计数器初始化扫描,这可以使用 ProcMon 完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 2017-06-07
    • 2019-10-02
    • 2010-10-30
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多