WMI, negative CPU usage value and Timestamp_Sys100NS in past

后端 未结 3 939
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 09:10

I am monitoring some machines using WMI, using .NET\'s System.Management stuff. The query I am using is this:

SELECT Timestamp_Sys100NS, PercentProc         


        
3条回答
  •  既然无缘
    2021-02-05 09:37

    The formula you are specifically talking about is PERF_100NSEC_TIMER_INV http://msdn.microsoft.com/en-us/library/ms803963.aspx

    I have not personally dealt with this problem because I have never seen values below zero.

    This is all I have been doing:

    /// 
    /// PERF_100NSEC_TIMER_INV algorithm.
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public static int CalculatePerf100NsecTimerInv(long n2, UInt64 d2,
                                                   long n1, UInt64 d1)
    {
        int usage = 0;
        try
        {
            double dataDiff = (n2 - n1);
            double timeDiff = (d2 - d1);
            double dUsage = (1 - (dataDiff / timeDiff)) * 100;
    
            // Evaluate
            usage = (dUsage >= 0.5) ? Convert.ToInt32(Math.Ceiling(dUsage)) : 0;
        }
        catch { }
    
        // Return
        return usage;
    }
    

    Usage:

    // Calculate
    int cpuTime =
        MSPerformanceAlgorithms.CalculatePerf100NsecTimerInv(
            current.PercentProcessorTime, current.TimestampSys100Ns,
            previous.PercentProcessorTime, previous.TimestampSys100Ns);
    

    If I watch the CPU usage on the task manager for the 2003 box, it matches fine. I think you can disregard anything less than zero as long as you are calculating with these values.

提交回复
热议问题