/// <summary>
        ///    Converts 100NS elapsed time to fractional seconds
        /// </summary>
        /// <internalonly/>
        private static float GetElapsedTime(CounterSample oldSample, CounterSample newSample)
        {
            float eSeconds;
            float eDifference;

            if (newSample.RawValue == 0)
            {
                // no data [start time = 0] so return 0
                return(0.0f);
            }
            else
            {
                float eFreq;
                eFreq = (float)(ulong)oldSample.CounterFrequency;

                if (oldSample.UnsignedRawValue >= (ulong)newSample.CounterTimeStamp || eFreq <= 0.0f)
                {
                    return(0.0f);
                }

                // otherwise compute difference between current time and start time
                eDifference = (float)((ulong)newSample.CounterTimeStamp - oldSample.UnsignedRawValue);

                // convert to fractional seconds using object counter
                eSeconds = eDifference / eFreq;

                return(eSeconds);
            }
        }
Example #2
0
 public static Double Calculate(CounterSample oldSample, CounterSample newSample)
 {
     double difference = newSample.RawValue - oldSample.RawValue;
     double timeInterval = newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec;
     if (timeInterval != 0) return 100 * (1 - (difference / timeInterval));
     return 0;
 }
Example #3
0
 /// <summary>
 ///     Frees all the resources allocated by this counter
 /// </summary>
 public void Close()
 {
     _helpMsg       = null;
     _oldSample     = CounterSample.Empty;
     _sharedCounter = null;
     _initialized   = false;
     _counterType   = -1;
 }
Example #4
0
        public CounterData(CounterKey key, Color color)
        {
            Key = key;
            Color = color;
            _previousSample = CounterSample.Empty;

            Id = Interlocked.Increment(ref _globalId);
        }
Example #5
0
 public void Close()
 {
     this.helpMsg       = null;
     this.oldSample     = CounterSample.Empty;
     this.sharedCounter = null;
     this.initialized   = false;
     this.counterType   = -1;
 }
Example #6
0
        public float NextValue()
        {
            CounterSample nextCounterSample = this.NextSample();
            float         num = 0f;

            num            = CounterSample.Calculate(this.oldSample, nextCounterSample);
            this.oldSample = nextCounterSample;
            return(num);
        }
Example #7
0
        /// Returns this process's CPU utilization since the last call to ResetCounter().
        public float GetCpuUtilization()
        {
            if (_cnt == null)
                return float.NaN;

            CounterSample curr = _cnt.NextSample();
            float cpuutil = CounterSample.Calculate(_prevSample, curr);
            _prevSample = curr;
            return cpuutil;
        }
Example #8
0
 public static Double CalculateCpu(CounterSample oldSample, CounterSample newSample, int rounding)
 {
     double difference = newSample.RawValue - oldSample.RawValue;
     double timeInterval = newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec;
     if (timeInterval == 0) return 0;
     var division = (difference/timeInterval)-1;
     if (division > 1) return 100;
     if (division < 0) return 0;
     return Math.Round(100 * (1 - (difference / timeInterval)), rounding);
 }
        /// <summary>
        ///    Computes the calculated value given a raw counter sample.
        /// </summary>
        public static float ComputeCounterValue(CounterSample oldSample, CounterSample newSample)
        {
            int newCounterType = (int)newSample.CounterType;

            if (oldSample.SystemFrequency == 0)
            {
                if ((newCounterType != Interop.Kernel32.PerformanceCounterOptions.PERF_RAW_FRACTION) &&
                    (newCounterType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_RAWCOUNT) &&
                    (newCounterType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_RAWCOUNT_HEX) &&
                    (newCounterType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_LARGE_RAWCOUNT) &&
                    (newCounterType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_LARGE_RAWCOUNT_HEX) &&
                    (newCounterType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_BASE))
                {
                    // Since oldSample has a system frequency of 0, this means the newSample is the first sample
                    // on a two sample calculation.  Since we can't do anything with it, return 0.
                    return(0.0f);
                }
            }
            else if (oldSample.CounterType != newSample.CounterType)
            {
                throw new InvalidOperationException(SR.MismatchedCounterTypes);
            }

            if (newCounterType == Interop.Kernel32.PerformanceCounterOptions.PERF_ELAPSED_TIME)
            {
                return((float)GetElapsedTime(oldSample, newSample));
            }

            Interop.Kernel32.PerformanceCounterOptions.PDH_RAW_COUNTER newPdhValue = new Interop.Kernel32.PerformanceCounterOptions.PDH_RAW_COUNTER();
            Interop.Kernel32.PerformanceCounterOptions.PDH_RAW_COUNTER oldPdhValue = new Interop.Kernel32.PerformanceCounterOptions.PDH_RAW_COUNTER();

            FillInValues(oldSample, newSample, ref oldPdhValue, ref newPdhValue);

            LoadPerfCounterDll();

            Interop.Kernel32.PerformanceCounterOptions.PDH_FMT_COUNTERVALUE pdhFormattedValue = new Interop.Kernel32.PerformanceCounterOptions.PDH_FMT_COUNTERVALUE();
            long timeBase = newSample.SystemFrequency;
            int  result   = Interop.PerfCounter.FormatFromRawValue((uint)newCounterType, Interop.Kernel32.PerformanceCounterOptions.PDH_FMT_DOUBLE | Interop.Kernel32.PerformanceCounterOptions.PDH_FMT_NOSCALE | Interop.Kernel32.PerformanceCounterOptions.PDH_FMT_NOCAP100,
                                                                   ref timeBase, ref newPdhValue, ref oldPdhValue, ref pdhFormattedValue);

            if (result != Interop.Errors.ERROR_SUCCESS)
            {
                // If the numbers go negative, just return 0.  This better matches the old behavior.
                if (result == Interop.Kernel32.PerformanceCounterOptions.PDH_CALC_NEGATIVE_VALUE || result == Interop.Kernel32.PerformanceCounterOptions.PDH_CALC_NEGATIVE_DENOMINATOR || result == Interop.Kernel32.PerformanceCounterOptions.PDH_NO_DATA)
                {
                    return(0);
                }
                else
                {
                    throw new Win32Exception(result, SR.Format(SR.PerfCounterPdhError, result.ToString("x", CultureInfo.InvariantCulture)));
                }
            }

            return((float)pdhFormattedValue.data);
        }
 public PerformanceCounter()
 {
     this.counterType = -1;
     this.oldSample = CounterSample.Empty;
     this.machineName = ".";
     this.categoryName = string.Empty;
     this.counterName = string.Empty;
     this.instanceName = string.Empty;
     this.isReadOnly = true;
     GC.SuppressFinalize(this);
 }
Example #11
0
 public PerformanceCounter()
 {
     this.counterType  = -1;
     this.oldSample    = CounterSample.Empty;
     this.machineName  = ".";
     this.categoryName = string.Empty;
     this.counterName  = string.Empty;
     this.instanceName = string.Empty;
     this.isReadOnly   = true;
     GC.SuppressFinalize(this);
 }
Example #12
0
 public bool Equals(CounterSample sample)
 {
     return((rawValue == sample.rawValue) &&
            (baseValue == sample.baseValue) &&
            (timeStamp == sample.timeStamp) &&
            (counterFrequency == sample.counterFrequency) &&
            (counterType == sample.counterType) &&
            (timeStamp100nSec == sample.timeStamp100nSec) &&
            (systemFrequency == sample.systemFrequency) &&
            (counterTimeStamp == sample.counterTimeStamp));
 }
Example #13
0
        /// <summary>
        ///     Obtains a counter sample and returns the calculated value for it.
        ///     NOTE: For counters whose calculated value depend upon 2 counter reads,
        ///           the very first read will return 0.0.
        /// </summary>
        public float NextValue()
        {
            //No need to initialize or Demand, since NextSample already does.
            CounterSample newSample = NextSample();
            float         retVal    = 0.0f;

            retVal     = CounterSample.Calculate(_oldSample, newSample);
            _oldSample = newSample;

            return(retVal);
        }
Example #14
0
 internal PerformanceCounter(string categoryName, string counterName, string instanceName, string machineName, bool skipInit)
 {
     this.counterType  = -1;
     this.oldSample    = CounterSample.Empty;
     this.MachineName  = machineName;
     this.CategoryName = categoryName;
     this.CounterName  = counterName;
     this.InstanceName = instanceName;
     this.isReadOnly   = true;
     this.initialized  = true;
     GC.SuppressFinalize(this);
 }
Example #15
0
 public PerformanceCounter(string categoryName, string counterName, string instanceName, bool readOnly)
 {
     this.counterType  = -1;
     this.oldSample    = CounterSample.Empty;
     this.MachineName  = ".";
     this.CategoryName = categoryName;
     this.CounterName  = counterName;
     this.InstanceName = instanceName;
     this.isReadOnly   = readOnly;
     this.Initialize();
     GC.SuppressFinalize(this);
 }
 public PerformanceCounter(string categoryName, string counterName, string instanceName, bool readOnly)
 {
     this.counterType = -1;
     this.oldSample = CounterSample.Empty;
     this.MachineName = ".";
     this.CategoryName = categoryName;
     this.CounterName = counterName;
     this.InstanceName = instanceName;
     this.isReadOnly = readOnly;
     this.Initialize();
     GC.SuppressFinalize(this);
 }
 internal PerformanceCounter(string categoryName, string counterName, string instanceName, string machineName, bool skipInit)
 {
     this.counterType = -1;
     this.oldSample = CounterSample.Empty;
     this.MachineName = machineName;
     this.CategoryName = categoryName;
     this.CounterName = counterName;
     this.InstanceName = instanceName;
     this.isReadOnly = true;
     this.initialized = true;
     GC.SuppressFinalize(this);
 }
		public static float ComputeCounterValue (CounterSample newSample)
		{
			switch (newSample.CounterType) {
			case PerformanceCounterType.RawFraction:
			case PerformanceCounterType.NumberOfItems32:
			case PerformanceCounterType.NumberOfItemsHEX32:
			case PerformanceCounterType.NumberOfItems64:
			case PerformanceCounterType.NumberOfItemsHEX64:
				return (float)newSample.RawValue;
			default:
				return 0;
			}
		}
Example #19
0
        // may throw InvalidOperationException, Win32Exception
        public CounterSample NextSample()
        {
            CounterSample sample;

            if (changed)
            {
                UpdateInfo();
            }
            GetSample(impl, false, out sample);
            valid_old  = true;
            old_sample = sample;
            return(sample);
        }
        /// <summary>
        /// Gets the elapsed time between two samples
        /// </summary>
        /// <param name="oldSample">The old sample.</param>
        /// <param name="newSample">The new sample.</param>
        /// <returns>Duration.</returns>
        public static Duration GetElapsedTime(CounterSample oldSample, CounterSample newSample)
        {
            // No data [start time = 0] so return 0 
            if (newSample.RawValue == 0)
                return Duration.Zero;

            float eFreq = (ulong)oldSample.CounterFrequency;
            ulong o = (ulong)oldSample.CounterTimeStamp;
            ulong n = (ulong)newSample.CounterTimeStamp;
            return o >= n ||
                   eFreq <= 0.0f
                ? Duration.Zero
                : Duration.FromSeconds((long)((n - o) / eFreq));
        }
        /// <include file='doc\CounterSampleCalculator.uex' path='docs/doc[@for="CounterSampleCalculator.CounterCounterCommon"]/*' />
        /// <devdoc>
        ///    Take the difference between the current and previous counts
        ///        then divide by the time interval
        /// </devdoc>
        /// <internalonly/>
        private static float CounterCounterCommon(CounterSample oldSample, CounterSample newSample)
        {
            float eTimeInterval;
            float eDifference;
            float eCount;
            bool  bValueDrop = false;

            if (!IsCounterBulk((int)oldSample.CounterType))
            {
                // check if it is too big to be a wrap-around case
                if (newSample.UnsignedRawValue < oldSample.UnsignedRawValue)
                {
                    if (newSample.UnsignedRawValue - oldSample.UnsignedRawValue > 0x00ffff0000)
                    {
                        return(0.0f);
                    }

                    bValueDrop = true;
                }
            }

            if (oldSample.UnsignedRawValue >= newSample.UnsignedRawValue)
            {
                return(0.0f);
            }

            eDifference = (float)(newSample.UnsignedRawValue - oldSample.UnsignedRawValue);


            eTimeInterval = GetTimeInterval((ulong)newSample.TimeStamp,
                                            (ulong)oldSample.TimeStamp,
                                            (ulong)newSample.SystemFrequency);

            if (eTimeInterval <= 0.0f)
            {
                return(0.0f);
            }
            else
            {
                eCount = eDifference / eTimeInterval;

                if (bValueDrop && (eCount > TOO_BIG))
                {
                    // ignore this bogus data since it is too big for
                    // the wrap-around case
                    eCount = 0.0f;
                }
                return(eCount);
            }
        }
Example #22
0
        public static float ComputeCounterValue(CounterSample newSample)
        {
            switch (newSample.CounterType)
            {
            case PerformanceCounterType.RawFraction:
            case PerformanceCounterType.NumberOfItems32:
            case PerformanceCounterType.NumberOfItemsHEX32:
            case PerformanceCounterType.NumberOfItems64:
            case PerformanceCounterType.NumberOfItemsHEX64:
                return((float)newSample.RawValue);

            default:
                return(0);
            }
        }
		public static float ComputeCounterValue (CounterSample oldSample,
			CounterSample newSample)
		{
			if (newSample.CounterType != oldSample.CounterType)
				throw new Exception ("The counter samples must be of the same type");
			switch (newSample.CounterType) {
			case PerformanceCounterType.RawFraction:
			case PerformanceCounterType.NumberOfItems32:
			case PerformanceCounterType.NumberOfItemsHEX32:
			case PerformanceCounterType.NumberOfItems64:
			case PerformanceCounterType.NumberOfItemsHEX64:
				return (float)newSample.RawValue;
			case PerformanceCounterType.AverageCount64:
				return (float)(newSample.RawValue - oldSample.RawValue)/(float)(newSample.BaseValue - oldSample.BaseValue);
			case PerformanceCounterType.AverageTimer32:
				return (((float)(newSample.RawValue - oldSample.RawValue))/newSample.SystemFrequency)/(float)(newSample.BaseValue - oldSample.BaseValue);
			case PerformanceCounterType.CounterDelta32:
			case PerformanceCounterType.CounterDelta64:
				return (float)(newSample.RawValue - oldSample.RawValue);
			case PerformanceCounterType.CounterMultiTimer:
				return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp) * 100.0f/newSample.BaseValue;
			case PerformanceCounterType.CounterMultiTimer100Ns:
				return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec) * 100.0f/newSample.BaseValue;
			case PerformanceCounterType.CounterMultiTimerInverse:
				return (newSample.BaseValue - ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp)) * 100.0f;
			case PerformanceCounterType.CounterMultiTimer100NsInverse:
				return (newSample.BaseValue - ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec)) * 100.0f;
			case PerformanceCounterType.CounterTimer:
			case PerformanceCounterType.CountPerTimeInterval32:
			case PerformanceCounterType.CountPerTimeInterval64:
				return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp);
			case PerformanceCounterType.CounterTimerInverse:
				return (1.0f - ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec)) * 100.0f;
			case PerformanceCounterType.ElapsedTime:
				// FIXME
				return 0;
			case PerformanceCounterType.Timer100Ns:
				return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp) * 100.0f;
			case PerformanceCounterType.Timer100NsInverse:
				return (1f - ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp)) * 100.0f;
			case PerformanceCounterType.RateOfCountsPerSecond32:
			case PerformanceCounterType.RateOfCountsPerSecond64:
				return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp) * 10000000;
			default:
				Console.WriteLine ("Counter type {0} not handled", newSample.CounterType);
				return 0;
			}
		}
        private static float GetElapsedTime(CounterSample oldSample, CounterSample newSample)
        {
            if (newSample.RawValue == 0L)
            {
                return(0f);
            }
            float counterFrequency = oldSample.CounterFrequency;

            if ((oldSample.UnsignedRawValue >= newSample.CounterTimeStamp) || (counterFrequency <= 0f))
            {
                return(0f);
            }
            float num2 = ((ulong)newSample.CounterTimeStamp) - oldSample.UnsignedRawValue;

            return(num2 / counterFrequency);
        }
        /// <devdoc>
        ///    Computes the calculated value given a raw counter sample.
        /// </devdoc>
        public static float ComputeCounterValue(CounterSample oldSample, CounterSample newSample) {
            int newCounterType = (int) newSample.CounterType;
            if (oldSample.SystemFrequency == 0) {
                if ((newCounterType != NativeMethods.PERF_RAW_FRACTION) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_RAWCOUNT) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_RAWCOUNT_HEX) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_LARGE_RAWCOUNT) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_LARGE_RAWCOUNT_HEX) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_MULTI_BASE)) {

                    // Since oldSample has a system frequency of 0, this means the newSample is the first sample
                    // on a two sample calculation.  Since we can't do anything with it, return 0.
                    return 0.0f;
                }
            }
            else if (oldSample.CounterType != newSample.CounterType) {
                throw new InvalidOperationException(SR.GetString(SR.MismatchedCounterTypes));
            }

            if (newCounterType == NativeMethods.PERF_ELAPSED_TIME) 
                return (float)GetElapsedTime(oldSample, newSample);
            
            NativeMethods.PDH_RAW_COUNTER newPdhValue = new NativeMethods.PDH_RAW_COUNTER();
            NativeMethods.PDH_RAW_COUNTER oldPdhValue = new NativeMethods.PDH_RAW_COUNTER();

            FillInValues(oldSample, newSample, oldPdhValue, newPdhValue);

            LoadPerfCounterDll();

            NativeMethods.PDH_FMT_COUNTERVALUE pdhFormattedValue= new NativeMethods.PDH_FMT_COUNTERVALUE();
            long timeBase = newSample.SystemFrequency;
            int result = SafeNativeMethods.FormatFromRawValue((uint) newCounterType, NativeMethods.PDH_FMT_DOUBLE | NativeMethods.PDH_FMT_NOSCALE | NativeMethods.PDH_FMT_NOCAP100, 
                                                          ref timeBase, newPdhValue, oldPdhValue, pdhFormattedValue);
            
            if (result != NativeMethods.ERROR_SUCCESS) {
                // If the numbers go negative, just return 0.  This better matches the old behavior. 
                if (result == NativeMethods.PDH_CALC_NEGATIVE_VALUE || result == NativeMethods.PDH_CALC_NEGATIVE_DENOMINATOR || result == NativeMethods.PDH_NO_DATA)
                    return 0;
                else
                    throw new Win32Exception(result, SR.GetString(SR.PerfCounterPdhError, result.ToString("x", CultureInfo.InvariantCulture)));
            }
            
            return (float) pdhFormattedValue.data;
            
        }
        /// <include file='doc\CounterSampleCalculator.uex' path='docs/doc[@for="CounterSampleCalculator.CounterRawFraction"]/*' />
        /// <devdoc>
        ///    Evaluate a raw fraction (no time, just two values: Numerator and
        ///        Denominator) and multiply by 100 (to make a percentage;
        /// </devdoc>
        /// <internalonly/>
        private static float CounterRawFraction(CounterSample newSample)
        {
            float eCount;
            float eNumerator;

            if (newSample.RawValue == 0 ||
                newSample.BaseValue == 0)
            {
                // invalid value
                return(0.0f);
            }
            else
            {
                eNumerator = (float)newSample.UnsignedRawValue * 100;
                eCount     = eNumerator /
                             (float)newSample.UnsignedBaseValue;
                return(eCount);
            }
        }
 internal InstanceDataCollection ReadInstanceData(string counterName)
 {
     InstanceDataCollection datas = new InstanceDataCollection(counterName);
     string[] array = new string[this.categorySample.InstanceNameTable.Count];
     this.categorySample.InstanceNameTable.Keys.CopyTo(array, 0);
     int[] numArray = new int[this.categorySample.InstanceNameTable.Count];
     this.categorySample.InstanceNameTable.Values.CopyTo(numArray, 0);
     for (int i = 0; i < array.Length; i++)
     {
         long baseValue = 0L;
         if (this.BaseCounterDefinitionSample != null)
         {
             int index = (int) this.BaseCounterDefinitionSample.categorySample.InstanceNameTable[array[i]];
             baseValue = this.BaseCounterDefinitionSample.instanceValues[index];
         }
         CounterSample sample = new CounterSample(this.instanceValues[numArray[i]], baseValue, this.categorySample.CounterFrequency, this.categorySample.SystemFrequency, this.categorySample.TimeStamp, this.categorySample.TimeStamp100nSec, (PerformanceCounterType) this.CounterType, this.categorySample.CounterTimeStamp);
         datas.Add(array[i], new InstanceData(array[i], sample));
     }
     return datas;
 }
Example #28
0
        internal InstanceDataCollection ReadInstanceData(string counterName)
        {
            InstanceDataCollection datas = new InstanceDataCollection(counterName);

            string[] array = new string[this.categorySample.InstanceNameTable.Count];
            this.categorySample.InstanceNameTable.Keys.CopyTo(array, 0);
            int[] numArray = new int[this.categorySample.InstanceNameTable.Count];
            this.categorySample.InstanceNameTable.Values.CopyTo(numArray, 0);
            for (int i = 0; i < array.Length; i++)
            {
                long baseValue = 0L;
                if (this.BaseCounterDefinitionSample != null)
                {
                    int index = (int)this.BaseCounterDefinitionSample.categorySample.InstanceNameTable[array[i]];
                    baseValue = this.BaseCounterDefinitionSample.instanceValues[index];
                }
                CounterSample sample = new CounterSample(this.instanceValues[numArray[i]], baseValue, this.categorySample.CounterFrequency, this.categorySample.SystemFrequency, this.categorySample.TimeStamp, this.categorySample.TimeStamp100nSec, (PerformanceCounterType)this.CounterType, this.categorySample.CounterTimeStamp);
                datas.Add(array[i], new InstanceData(array[i], sample));
            }
            return(datas);
        }
        public static float ComputeCounterValue(CounterSample oldSample, CounterSample newSample)
        {
            int counterType = (int)newSample.CounterType;

            if (oldSample.SystemFrequency == 0L)
            {
                if ((((counterType != 0x20020400) && (counterType != 0x10000)) && ((counterType != 0) && (counterType != 0x10100))) && ((counterType != 0x100) && (counterType != 0x42030500)))
                {
                    return(0f);
                }
            }
            else if (oldSample.CounterType != newSample.CounterType)
            {
                throw new InvalidOperationException(SR.GetString("MismatchedCounterTypes"));
            }
            if (counterType == 0x30240500)
            {
                return(GetElapsedTime(oldSample, newSample));
            }
            Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER newPdhValue = new Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER();
            Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER oldPdhValue = new Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER();
            FillInValues(oldSample, newSample, oldPdhValue, newPdhValue);
            LoadPerfCounterDll();
            Microsoft.Win32.NativeMethods.PDH_FMT_COUNTERVALUE pFmtValue = new Microsoft.Win32.NativeMethods.PDH_FMT_COUNTERVALUE();
            long systemFrequency = newSample.SystemFrequency;
            int  error           = Microsoft.Win32.SafeNativeMethods.FormatFromRawValue((uint)counterType, 0x9200, ref systemFrequency, newPdhValue, oldPdhValue, pFmtValue);

            switch (error)
            {
            case 0:
                return((float)pFmtValue.data);

            case -2147481640:
            case -2147481642:
            case -2147481643:
                return(0f);
            }
            throw new Win32Exception(error, SR.GetString("PerfCounterPdhError", new object[] { error.ToString("x", CultureInfo.InvariantCulture) }));
        }
Example #30
0
        // may throw InvalidOperationException, Win32Exception
        public float NextValue()
        {
            CounterSample sample;

            if (changed)
            {
                UpdateInfo();
            }
            GetSample(impl, false, out sample);
            float val;

            if (valid_old)
            {
                val = CounterSampleCalculator.ComputeCounterValue(old_sample, sample);
            }
            else
            {
                val = CounterSampleCalculator.ComputeCounterValue(sample);
            }
            valid_old  = true;
            old_sample = sample;
            return(val);
        }
        public static float ComputeCounterValue(CounterSample oldSample, CounterSample newSample)
        {
            int counterType = (int) newSample.CounterType;
            if (oldSample.SystemFrequency == 0L)
            {
                if ((((counterType != 0x20020400) && (counterType != 0x10000)) && ((counterType != 0) && (counterType != 0x10100))) && ((counterType != 0x100) && (counterType != 0x42030500)))
                {
                    return 0f;
                }
            }
            else if (oldSample.CounterType != newSample.CounterType)
            {
                throw new InvalidOperationException(SR.GetString("MismatchedCounterTypes"));
            }
            if (counterType == 0x30240500)
            {
                return GetElapsedTime(oldSample, newSample);
            }
            Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER newPdhValue = new Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER();
            Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER oldPdhValue = new Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER();
            FillInValues(oldSample, newSample, oldPdhValue, newPdhValue);
            LoadPerfCounterDll();
            Microsoft.Win32.NativeMethods.PDH_FMT_COUNTERVALUE pFmtValue = new Microsoft.Win32.NativeMethods.PDH_FMT_COUNTERVALUE();
            long systemFrequency = newSample.SystemFrequency;
            int error = Microsoft.Win32.SafeNativeMethods.FormatFromRawValue((uint) counterType, 0x9200, ref systemFrequency, newPdhValue, oldPdhValue, pFmtValue);
            switch (error)
            {
                case 0:
                    return (float) pFmtValue.data;

                case -2147481640:
                case -2147481642:
                case -2147481643:
                    return 0f;
            }
            throw new Win32Exception(error, SR.GetString("PerfCounterPdhError", new object[] { error.ToString("x", CultureInfo.InvariantCulture) }));
        }
        /// <devdoc>
        ///    Converts 100NS elapsed time to fractional seconds
        /// </devdoc>
        /// <internalonly/>
        private static float GetElapsedTime(CounterSample oldSample, CounterSample newSample) {
            float eSeconds;
            float eDifference;

            if (newSample.RawValue == 0) {
                // no data [start time = 0] so return 0
                return 0.0f;
            } 
            else {
                float eFreq;
                eFreq = (float)(ulong)oldSample.CounterFrequency;

                if (oldSample.UnsignedRawValue >= (ulong)newSample.CounterTimeStamp || eFreq <= 0.0f)
                    return 0.0f;
                    
                // otherwise compute difference between current time and start time
                eDifference = (float)((ulong)newSample.CounterTimeStamp - oldSample.UnsignedRawValue);
            
                // convert to fractional seconds using object counter
                eSeconds = eDifference / eFreq;

                return eSeconds;
            }           
        }
Example #33
0
        public static float ComputeCounterValue(CounterSample oldSample,
                                                CounterSample newSample)
        {
            if (newSample.CounterType != oldSample.CounterType)
            {
                throw new Exception("The counter samples must be of the same type");
            }
            switch (newSample.CounterType)
            {
            case PerformanceCounterType.RawFraction:
            case PerformanceCounterType.NumberOfItems32:
            case PerformanceCounterType.NumberOfItemsHEX32:
            case PerformanceCounterType.NumberOfItems64:
            case PerformanceCounterType.NumberOfItemsHEX64:
                return((float)newSample.RawValue);

            case PerformanceCounterType.AverageCount64:
                return((float)(newSample.RawValue - oldSample.RawValue) / (float)(newSample.BaseValue - oldSample.BaseValue));

            case PerformanceCounterType.AverageTimer32:
                return((((float)(newSample.RawValue - oldSample.RawValue)) / newSample.SystemFrequency) / (float)(newSample.BaseValue - oldSample.BaseValue));

            case PerformanceCounterType.CounterDelta32:
            case PerformanceCounterType.CounterDelta64:
                return((float)(newSample.RawValue - oldSample.RawValue));

            case PerformanceCounterType.CounterMultiTimer:
                return(((float)(newSample.RawValue - oldSample.RawValue)) / (float)(newSample.TimeStamp - oldSample.TimeStamp) * 100.0f / newSample.BaseValue);

            case PerformanceCounterType.CounterMultiTimer100Ns:
                return(((float)(newSample.RawValue - oldSample.RawValue)) / (float)(newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec) * 100.0f / newSample.BaseValue);

            case PerformanceCounterType.CounterMultiTimerInverse:
                return((newSample.BaseValue - ((float)(newSample.RawValue - oldSample.RawValue)) / (float)(newSample.TimeStamp - oldSample.TimeStamp)) * 100.0f);

            case PerformanceCounterType.CounterMultiTimer100NsInverse:
                return((newSample.BaseValue - ((float)(newSample.RawValue - oldSample.RawValue)) / (float)(newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec)) * 100.0f);

            case PerformanceCounterType.CounterTimer:
            case PerformanceCounterType.CountPerTimeInterval32:
            case PerformanceCounterType.CountPerTimeInterval64:
                return(((float)(newSample.RawValue - oldSample.RawValue)) / (float)(newSample.TimeStamp - oldSample.TimeStamp));

            case PerformanceCounterType.CounterTimerInverse:
                return((1.0f - ((float)(newSample.RawValue - oldSample.RawValue)) / (float)(newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec)) * 100.0f);

            case PerformanceCounterType.ElapsedTime:
                // FIXME
                return(0);

            case PerformanceCounterType.Timer100Ns:
                return(((float)(newSample.RawValue - oldSample.RawValue)) / (float)(newSample.TimeStamp - oldSample.TimeStamp) * 100.0f);

            case PerformanceCounterType.Timer100NsInverse:
                return((1f - ((float)(newSample.RawValue - oldSample.RawValue)) / (float)(newSample.TimeStamp - oldSample.TimeStamp)) * 100.0f);

            case PerformanceCounterType.RateOfCountsPerSecond32:
            case PerformanceCounterType.RateOfCountsPerSecond64:
                return(((float)(newSample.RawValue - oldSample.RawValue)) / (float)(newSample.TimeStamp - oldSample.TimeStamp) * 10000000);

            default:
                Console.WriteLine("Counter type {0} not handled", newSample.CounterType);
                return(0);
            }
        }
 /// <devdoc>
 ///    Static functions to calculate the performance value off the samples
 /// </devdoc>
 public static float Calculate(CounterSample counterSample, CounterSample nextCounterSample) { 
     return CounterSampleCalculator.ComputeCounterValue(counterSample, nextCounterSample);
 }
 /// <summary>
 ///    Computes the calculated value given a raw counter sample.
 /// </summary>
 public static float ComputeCounterValue(CounterSample newSample)
 {
     return(ComputeCounterValue(CounterSample.Empty, newSample));
 }
 public static float ComputeCounterValue(CounterSample newSample)
 {
     return(default(float));
 }
        // This method figures out which values are supposed to go into which structures so that PDH can do the 
        // calculation for us.  This was ported from Window's cutils.c
        private static void FillInValues(CounterSample oldSample, CounterSample newSample, NativeMethods.PDH_RAW_COUNTER oldPdhValue, NativeMethods.PDH_RAW_COUNTER newPdhValue) {
            int newCounterType = (int) newSample.CounterType;

            switch (newCounterType) {
                case NativeMethods.PERF_COUNTER_COUNTER:
                case NativeMethods.PERF_COUNTER_QUEUELEN_TYPE:
                case NativeMethods.PERF_SAMPLE_COUNTER:
                case NativeMethods.PERF_OBJ_TIME_TIMER:
                case NativeMethods.PERF_COUNTER_OBJ_TIME_QUEUELEN_TYPE:
                    newPdhValue.FirstValue  = newSample.RawValue;
                    newPdhValue.SecondValue = newSample.TimeStamp;

                    oldPdhValue.FirstValue  = oldSample.RawValue;
                    oldPdhValue.SecondValue = oldSample.TimeStamp;
                    break;
                
                case NativeMethods.PERF_COUNTER_100NS_QUEUELEN_TYPE:
                    newPdhValue.FirstValue  = newSample.RawValue;
                    newPdhValue.SecondValue = newSample.TimeStamp100nSec;

                    oldPdhValue.FirstValue  = oldSample.RawValue;
                    oldPdhValue.SecondValue = oldSample.TimeStamp100nSec;
                    break;
                
                case NativeMethods.PERF_COUNTER_TIMER:
                case NativeMethods.PERF_COUNTER_TIMER_INV:
                case NativeMethods.PERF_COUNTER_BULK_COUNT:
                case NativeMethods.PERF_COUNTER_LARGE_QUEUELEN_TYPE:
                case NativeMethods.PERF_COUNTER_MULTI_TIMER:
                case NativeMethods.PERF_COUNTER_MULTI_TIMER_INV:
                    newPdhValue.FirstValue  = newSample.RawValue;
                    newPdhValue.SecondValue = newSample.TimeStamp;

                    oldPdhValue.FirstValue  = oldSample.RawValue;
                    oldPdhValue.SecondValue = oldSample.TimeStamp;
                    if (newCounterType == NativeMethods.PERF_COUNTER_MULTI_TIMER || newCounterType == NativeMethods.PERF_COUNTER_MULTI_TIMER_INV) {
                        //  this is to make PDH work like PERFMON for
                        //  this counter type
                        newPdhValue.FirstValue *= (uint) newSample.CounterFrequency;
                        if (oldSample.CounterFrequency != 0) {
                            oldPdhValue.FirstValue *= (uint) oldSample.CounterFrequency;
                        }
                    }

                    if ((newCounterType & NativeMethods.PERF_MULTI_COUNTER) == NativeMethods.PERF_MULTI_COUNTER) {
                            newPdhValue.MultiCount = (int) newSample.BaseValue;
                            oldPdhValue.MultiCount = (int) oldSample.BaseValue;
                    }
                    
                        
                    break;
                //
                //  These counters do not use any time reference
                //
                case NativeMethods.PERF_COUNTER_RAWCOUNT:
                case NativeMethods.PERF_COUNTER_RAWCOUNT_HEX:
                case NativeMethods.PERF_COUNTER_DELTA:
                case NativeMethods.PERF_COUNTER_LARGE_RAWCOUNT:
                case NativeMethods.PERF_COUNTER_LARGE_RAWCOUNT_HEX:
                case NativeMethods.PERF_COUNTER_LARGE_DELTA:
                    newPdhValue.FirstValue  = newSample.RawValue;
                    newPdhValue.SecondValue = 0;

                    oldPdhValue.FirstValue  = oldSample.RawValue;
                    oldPdhValue.SecondValue = 0;
                    break;
                //
                //  These counters use the 100 Ns time base in thier calculation
                //
                case NativeMethods.PERF_100NSEC_TIMER:
                case NativeMethods.PERF_100NSEC_TIMER_INV:
                case NativeMethods.PERF_100NSEC_MULTI_TIMER:
                case NativeMethods.PERF_100NSEC_MULTI_TIMER_INV:
                    newPdhValue.FirstValue  = newSample.RawValue;
                    newPdhValue.SecondValue = newSample.TimeStamp100nSec;

                    oldPdhValue.FirstValue  = oldSample.RawValue;
                    oldPdhValue.SecondValue = oldSample.TimeStamp100nSec;
                    if ((newCounterType & NativeMethods.PERF_MULTI_COUNTER) == NativeMethods.PERF_MULTI_COUNTER) {
                        newPdhValue.MultiCount = (int) newSample.BaseValue;
                        oldPdhValue.MultiCount = (int) oldSample.BaseValue;
                    }
                    break;
                //
                //  These counters use two data points
                //
                case NativeMethods.PERF_SAMPLE_FRACTION:
                case NativeMethods.PERF_RAW_FRACTION:
                case NativeMethods.PERF_LARGE_RAW_FRACTION:
                case NativeMethods.PERF_PRECISION_SYSTEM_TIMER:
                case NativeMethods.PERF_PRECISION_100NS_TIMER:
                case NativeMethods.PERF_PRECISION_OBJECT_TIMER:
                case NativeMethods.PERF_AVERAGE_TIMER:
                case NativeMethods.PERF_AVERAGE_BULK:
                    newPdhValue.FirstValue = newSample.RawValue;
                    newPdhValue.SecondValue = newSample.BaseValue;

                    oldPdhValue.FirstValue = oldSample.RawValue;
                    oldPdhValue.SecondValue = oldSample.BaseValue;
                    break;
                
                default:
                    // an unidentified counter was returned so
                    newPdhValue.FirstValue  = 0;
                    newPdhValue.SecondValue = 0;

                    oldPdhValue.FirstValue  = 0;
                    oldPdhValue.SecondValue = 0;
                    break;
            }
        }
Example #38
0
 /// <devdoc>
 ///    Static functions to calculate the performance value off the samples
 /// </devdoc>
 public static float Calculate(CounterSample counterSample, CounterSample nextCounterSample)
 {
     return(CounterSampleCalculator.ComputeCounterValue(counterSample, nextCounterSample));
 }
 public void Close()
 {
     this.helpMsg = null;
     this.oldSample = CounterSample.Empty;
     this.sharedCounter = null;
     this.initialized = false;
     this.counterType = -1;
 }
		static bool GetSample (IntPtr impl, bool only_value, out CounterSample sample)
		{
			throw new System.NotImplementedException();
		}
		// may throw InvalidOperationException, Win32Exception
		public float NextValue ()
		{
			CounterSample sample;
			if (changed)
				UpdateInfo ();
			GetSample (impl, false, out sample);
			float val;
			if (valid_old)
				val = CounterSampleCalculator.ComputeCounterValue (old_sample, sample);
			else
				val = CounterSampleCalculator.ComputeCounterValue (sample);
			valid_old = true;
			old_sample = sample;
			return val;
		}
        /// <include file='doc\CounterSampleCalculator.uex' path='docs/doc[@for="CounterSampleCalculator.ComputeCounterValue1"]/*' />
        /// <devdoc>
        ///    Computes the calculated value given a raw counter sample.
        /// </devdoc>
        public static float ComputeCounterValue(CounterSample oldSample, CounterSample newSample)
        {
            int newCounterType = (int)newSample.CounterType;

            if (oldSample.SystemFrequency == 0)
            {
                if ((newCounterType != NativeMethods.PERF_RAW_FRACTION) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_RAWCOUNT) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_RAWCOUNT_HEX) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_LARGE_RAWCOUNT) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_LARGE_RAWCOUNT_HEX) &&
                    (newCounterType != NativeMethods.PERF_COUNTER_MULTI_BASE))
                {
                    // Since oldSample has a system frequency of 0, this means the newSample is the first sample
                    // on a two sample calculation.  Since we can't do anything with it, return 0.
                    return(0.0f);
                }
            }
            else if (oldSample.CounterType != newSample.CounterType)
            {
                throw new InvalidOperationException(SR.GetString(SR.MismatchedCounterTypes));
            }


            switch (newCounterType)
            {
            case NativeMethods.PERF_COUNTER_COUNTER:
            case NativeMethods.PERF_COUNTER_BULK_COUNT:
            case NativeMethods.PERF_SAMPLE_COUNTER:
                return(CounterCounterCommon(oldSample, newSample));

            case NativeMethods.PERF_AVERAGE_TIMER:
                return(CounterAverageTimer(oldSample, newSample));

            case NativeMethods.PERF_COUNTER_QUEUELEN_TYPE:
            case NativeMethods.PERF_COUNTER_LARGE_QUEUELEN_TYPE:
            case NativeMethods.PERF_AVERAGE_BULK:
                return(CounterAverageBulk(oldSample, newSample));

            case NativeMethods.PERF_COUNTER_TIMER:
            case NativeMethods.PERF_COUNTER_TIMER_INV:
            case NativeMethods.PERF_100NSEC_TIMER:
            case NativeMethods.PERF_100NSEC_TIMER_INV:
            case NativeMethods.PERF_COUNTER_MULTI_TIMER:
            case NativeMethods.PERF_COUNTER_MULTI_TIMER_INV:
            case NativeMethods.PERF_100NSEC_MULTI_TIMER:
            case NativeMethods.PERF_100NSEC_MULTI_TIMER_INV:
                return(CounterTimerCommon(oldSample, newSample));

            case NativeMethods.PERF_RAW_FRACTION:
                return(CounterRawFraction(newSample));

            case NativeMethods.PERF_SAMPLE_FRACTION:
                return(SampleCommon(oldSample, newSample));

            case NativeMethods.PERF_COUNTER_RAWCOUNT:
            case NativeMethods.PERF_COUNTER_RAWCOUNT_HEX:
                return((float)newSample.RawValue);

            case NativeMethods.PERF_COUNTER_LARGE_RAWCOUNT:
            case NativeMethods.PERF_COUNTER_LARGE_RAWCOUNT_HEX:
                return((float)newSample.RawValue);

            case NativeMethods.PERF_COUNTER_MULTI_BASE:
                return(0.0f);

            case NativeMethods.PERF_ELAPSED_TIME:
                return((float)GetElapsedTime(oldSample, newSample));

            case NativeMethods.PERF_COUNTER_DELTA:
            case NativeMethods.PERF_COUNTER_LARGE_DELTA:
                if (oldSample.UnsignedRawValue >= newSample.UnsignedRawValue)
                {
                    return(-(float)(oldSample.UnsignedRawValue - newSample.UnsignedRawValue));
                }
                else
                {
                    return((float)(newSample.UnsignedRawValue - oldSample.UnsignedRawValue));
                }

            default:
                return(0.0f);
            }
        }
Example #43
0
 static bool GetSample(IntPtr impl, bool only_value, out CounterSample sample)
 {
     throw new System.NotImplementedException();
 }
Example #44
0
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public InstanceData(string instanceName, CounterSample sample) {
     this.instanceName = instanceName;
     this.sample = sample;
 }
        /// <include file='doc\CounterSampleCalculator.uex' path='docs/doc[@for="CounterSampleCalculator.CounterTimerCommon"]/*' />
        /// <devdoc>
        ///    Take the difference between the current and previous counts,
        ///      Normalize the count (counts per interval)
        ///      divide by the time interval (count = % of interval)
        ///      if (invert)
        ///        subtract from 1 (the normalized size of an interval)
        ///      multiply by 100 (convert to a percentage)
        ///      this value from 100.
        /// </devdoc>
        /// <internalonly/>
        private static float CounterTimerCommon(CounterSample oldSample, CounterSample newSample)
        {
            float eTimeInterval;
            float eDifference;
            float eFreq;
            float eFraction;
            float eMultiBase;
            float eCount;

            // Get the amount of time that has passed since the last sample
            int oldCounterType = (int)oldSample.CounterType;

            if (oldCounterType == NativeMethods.PERF_100NSEC_TIMER ||
                oldCounterType == NativeMethods.PERF_100NSEC_TIMER_INV ||
                oldCounterType == NativeMethods.PERF_100NSEC_MULTI_TIMER ||
                oldCounterType == NativeMethods.PERF_100NSEC_MULTI_TIMER_INV)
            {
                if (oldSample.TimeStamp100nSec >= newSample.TimeStamp100nSec)
                {
                    return(0.0f);
                }

                eTimeInterval = (float)((ulong)newSample.TimeStamp100nSec - (ulong)oldSample.TimeStamp100nSec);
            }
            else
            {
                eTimeInterval = GetTimeInterval((ulong)newSample.TimeStamp, (ulong)oldSample.TimeStamp, (ulong)newSample.SystemFrequency);

                if (eTimeInterval <= 0.0f)
                {
                    return(0.0f);
                }
            }


            // Get the current and previous counts.
            if (oldSample.UnsignedRawValue >= newSample.UnsignedRawValue)
            {
                eDifference = -(float)(oldSample.UnsignedRawValue - newSample.UnsignedRawValue);
            }
            else
            {
                eDifference = (float)(newSample.UnsignedRawValue - oldSample.UnsignedRawValue);
            }

            // Get the number of counts in this time interval.
            // (1, 2, 3 or any number of seconds could have gone by since
            // the last sample)
            if (oldCounterType == 0 || oldCounterType == NativeMethods.PERF_COUNTER_TIMER_INV)
            {
                // Get the counts per interval (second)
                eFreq = (float)(ulong)newSample.SystemFrequency;
                if (eFreq <= 0.0f)
                {
                    return((float)0.0f);
                }

                // Calculate the fraction of the counts that are used by whatever
                // we are measuring
                eFraction = eDifference / eFreq;
            }
            else
            {
                eFraction = eDifference;
            }

            // Calculate the fraction of time used by what were measuring.

            eCount = eFraction / eTimeInterval;

            // If this is  an inverted count take care of the inversion.

            if (oldCounterType == NativeMethods.PERF_COUNTER_TIMER_INV ||
                oldCounterType == NativeMethods.PERF_100NSEC_TIMER_INV)
            {
                eCount = (float)1.0f - eCount;
            }

            // If this is  an inverted multi count take care of the inversion.
            if (oldCounterType == NativeMethods.PERF_COUNTER_MULTI_TIMER_INV ||
                oldCounterType == NativeMethods.PERF_100NSEC_MULTI_TIMER_INV)
            {
                eMultiBase = (float)newSample.UnsignedBaseValue;
                eCount     = eMultiBase - eCount;
            }

            // adjust eCount for non-inverted multi count

            if (oldCounterType == NativeMethods.PERF_COUNTER_MULTI_TIMER ||
                oldCounterType == NativeMethods.PERF_100NSEC_MULTI_TIMER)
            {
                eMultiBase = (float)newSample.UnsignedBaseValue;
                eCount     = eCount / eMultiBase;
            }

            // Scale the value to up to 100.
            eCount *= 100.0f;

            if (eCount < 0.0f)
            {
                eCount = 0.0f;
            }

            if (eCount > 100.0f &&
                oldCounterType != NativeMethods.PERF_100NSEC_MULTI_TIMER &&
                oldCounterType != NativeMethods.PERF_100NSEC_MULTI_TIMER_INV &&
                oldCounterType != NativeMethods.PERF_COUNTER_MULTI_TIMER &&
                oldCounterType != NativeMethods.PERF_COUNTER_MULTI_TIMER_INV)
            {
                eCount = 100.0f;
            }

            return(eCount);
        }
        /// <summary>
        /// 获取性能计数器下一个采样
        /// </summary>
        /// <returns></returns>
        public CounterSample NextSample()
        {
            CounterSample sample = new CounterSample();

            if (this.counter != null)
                sample = this.counter.NextSample();

            return sample;
        }
        internal InstanceDataCollection ReadInstanceData(string counterName) {
#pragma warning disable 618
            InstanceDataCollection data = new InstanceDataCollection(counterName);
#pragma warning restore 618

            string[] keys = new string[categorySample.InstanceNameTable.Count];
            categorySample.InstanceNameTable.Keys.CopyTo(keys, 0);
            int[] indexes = new int[categorySample.InstanceNameTable.Count];
            categorySample.InstanceNameTable.Values.CopyTo(indexes, 0);
            for (int index = 0; index < keys.Length; ++ index) {
                long baseValue = 0;
                if (this.BaseCounterDefinitionSample != null) {
                    CategorySample baseCategorySample = this.BaseCounterDefinitionSample.categorySample;
                    int baseIndex = (int)baseCategorySample.InstanceNameTable[keys[index]];
                    baseValue = this.BaseCounterDefinitionSample.instanceValues[baseIndex];
                }

                CounterSample sample = new CounterSample(this.instanceValues[indexes[index]],
                                                        baseValue,
                                                        categorySample.CounterFrequency,
                                                        categorySample.SystemFrequency,
                                                        categorySample.TimeStamp,
                                                        categorySample.TimeStamp100nSec,
                                                        (PerformanceCounterType)this.CounterType,
                                                        categorySample.CounterTimeStamp);

                data.Add(keys[index], new InstanceData(keys[index], sample));
            }

            return data;
        }
 /// <devdoc>
 ///    Computes the calculated value given a raw counter sample.
 /// </devdoc>
 public static float ComputeCounterValue(CounterSample newSample) {
     return ComputeCounterValue(CounterSample.Empty, newSample);
 }
Example #49
0
 static extern bool GetSample(IntPtr impl, bool only_value, out CounterSample sample);
Example #50
0
		/// Resets the internal counter. All subsequent calls to GetCpuUtilization() will 
		/// be relative to the point in time when you called ResetCounter(). This 
		/// method can be call as often as necessary to get a new baseline for 
		/// CPU utilization measurements.
		public void ResetCounter()
		{
			_startSample = _cnt.NextSample();
		}
		static extern bool GetSample (IntPtr impl, bool only_value, out CounterSample sample);
        private static void FillInValues(CounterSample oldSample, CounterSample newSample, Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER oldPdhValue, Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER newPdhValue)
        {
            int counterType = (int)newSample.CounterType;

            switch (counterType)
            {
            case 0:
            case 0x100:
            case 0x10000:
            case 0x400500:
            case 0x10100:
            case 0x400400:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = 0L;
                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = 0L;
                return;

            case 0x410400:
            case 0x650500:
            case 0x450400:
            case 0x10410400:
            case 0x20610500:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.TimeStamp;
                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.TimeStamp;
                return;

            case 0x550500:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.TimeStamp100nSec;
                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.TimeStamp100nSec;
                return;

            case 0x450500:
            case 0x10410500:
            case 0x20410500:
            case 0x22410500:
            case 0x21410500:
            case 0x23410500:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.TimeStamp;
                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.TimeStamp;
                switch (counterType)
                {
                case 0x22410500:
                case 0x23410500:
                    newPdhValue.FirstValue *= (uint)newSample.CounterFrequency;
                    if (oldSample.CounterFrequency != 0L)
                    {
                        oldPdhValue.FirstValue *= (uint)oldSample.CounterFrequency;
                    }
                    break;
                }
                if ((counterType & 0x2000000) == 0x2000000)
                {
                    newPdhValue.MultiCount = (int)newSample.BaseValue;
                    oldPdhValue.MultiCount = (int)oldSample.BaseValue;
                }
                return;

            case 0x20020400:
            case 0x20020500:
            case 0x20470500:
            case 0x20670500:
            case 0x20c20400:
            case 0x20570500:
            case 0x30020400:
            case 0x40020500:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.BaseValue;
                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.BaseValue;
                return;

            case 0x20510500:
            case 0x22510500:
            case 0x21510500:
            case 0x23510500:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.TimeStamp100nSec;
                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.TimeStamp100nSec;
                if ((counterType & 0x2000000) == 0x2000000)
                {
                    newPdhValue.MultiCount = (int)newSample.BaseValue;
                    oldPdhValue.MultiCount = (int)oldSample.BaseValue;
                }
                return;
            }
            newPdhValue.FirstValue  = 0L;
            newPdhValue.SecondValue = 0L;
            oldPdhValue.FirstValue  = 0L;
            oldPdhValue.SecondValue = 0L;
        }
 public InstanceData(string instanceName, CounterSample sample)
 {
 }
 public static float ComputeCounterValue(CounterSample newSample)
 {
   return default(float);
 }
 public InstanceData(string instanceName, CounterSample sample)
 {
     this.instanceName = instanceName;
     this.sample       = sample;
 }
        // This method figures out which values are supposed to go into which structures so that PDH can do the
        // calculation for us.  This was ported from Window's cutils.c
        private static void FillInValues(CounterSample oldSample, CounterSample newSample, ref Interop.Kernel32.PerformanceCounterOptions.PDH_RAW_COUNTER oldPdhValue, ref Interop.Kernel32.PerformanceCounterOptions.PDH_RAW_COUNTER newPdhValue)
        {
            int newCounterType = (int)newSample.CounterType;

            switch (newCounterType)
            {
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_COUNTER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_QUEUELEN_TYPE:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_SAMPLE_COUNTER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_OBJ_TIME_TIMER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_OBJ_TIME_QUEUELEN_TYPE:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.TimeStamp;

                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.TimeStamp;
                break;

            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_100NS_QUEUELEN_TYPE:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.TimeStamp100nSec;

                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.TimeStamp100nSec;
                break;

            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_TIMER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_TIMER_INV:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_BULK_COUNT:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_LARGE_QUEUELEN_TYPE:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER_INV:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.TimeStamp;

                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.TimeStamp;
                if (newCounterType == Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER || newCounterType == Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER_INV)
                {
                    //  this is to make PDH work like PERFMON for
                    //  this counter type
                    newPdhValue.FirstValue *= (uint)newSample.CounterFrequency;
                    if (oldSample.CounterFrequency != 0)
                    {
                        oldPdhValue.FirstValue *= (uint)oldSample.CounterFrequency;
                    }
                }

                if ((newCounterType & Interop.Kernel32.PerformanceCounterOptions.PERF_MULTI_COUNTER) == Interop.Kernel32.PerformanceCounterOptions.PERF_MULTI_COUNTER)
                {
                    newPdhValue.MultiCount = (int)newSample.BaseValue;
                    oldPdhValue.MultiCount = (int)oldSample.BaseValue;
                }

                break;

            //
            //  These counters do not use any time reference
            //
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_RAWCOUNT:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_RAWCOUNT_HEX:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_DELTA:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_LARGE_RAWCOUNT:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_LARGE_RAWCOUNT_HEX:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_LARGE_DELTA:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = 0;

                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = 0;
                break;

            //
            //  These counters use the 100 Ns time base in thier calculation
            //
            case Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_TIMER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_TIMER_INV:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER_INV:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.TimeStamp100nSec;

                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.TimeStamp100nSec;
                if ((newCounterType & Interop.Kernel32.PerformanceCounterOptions.PERF_MULTI_COUNTER) == Interop.Kernel32.PerformanceCounterOptions.PERF_MULTI_COUNTER)
                {
                    newPdhValue.MultiCount = (int)newSample.BaseValue;
                    oldPdhValue.MultiCount = (int)oldSample.BaseValue;
                }
                break;

            //
            //  These counters use two data points
            //
            case Interop.Kernel32.PerformanceCounterOptions.PERF_SAMPLE_FRACTION:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_RAW_FRACTION:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_LARGE_RAW_FRACTION:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_PRECISION_SYSTEM_TIMER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_PRECISION_100NS_TIMER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_PRECISION_OBJECT_TIMER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_TIMER:
            case Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_BULK:
                newPdhValue.FirstValue  = newSample.RawValue;
                newPdhValue.SecondValue = newSample.BaseValue;

                oldPdhValue.FirstValue  = oldSample.RawValue;
                oldPdhValue.SecondValue = oldSample.BaseValue;
                break;

            default:
                // an unidentified counter was returned so
                newPdhValue.FirstValue  = 0;
                newPdhValue.SecondValue = 0;

                oldPdhValue.FirstValue  = 0;
                oldPdhValue.SecondValue = 0;
                break;
            }
        }
		// may throw InvalidOperationException, Win32Exception
		public CounterSample NextSample ()
		{
			CounterSample sample;
			if (changed)
				UpdateInfo ();
			GetSample (impl, false, out sample);
			valid_old = true;
			old_sample = sample;
			return sample;
		}
 public bool Equals(CounterSample sample) {
     return (rawValue == sample.rawValue) && 
                (baseValue == sample.baseValue) && 
                (timeStamp == sample.timeStamp) && 
                (counterFrequency == sample.counterFrequency) &&
                (counterType == sample.counterType) &&
                (timeStamp100nSec == sample.timeStamp100nSec) && 
                (systemFrequency == sample.systemFrequency) &&
                (counterTimeStamp == sample.counterTimeStamp);                       
 }
 public InstanceData(string instanceName, CounterSample sample)
 {
 }
 public float NextValue()
 {
     CounterSample nextCounterSample = this.NextSample();
     float num = 0f;
     num = CounterSample.Calculate(this.oldSample, nextCounterSample);
     this.oldSample = nextCounterSample;
     return num;
 }