Esempio n. 1
0
        private IEnumerable <PdhCounterHandle> DoRemoveCounters(IEnumerable <IPerfCounter> counters)
        {
            PdhCounterHandle        hCounter        = null;
            List <PdhCounterHandle> removedCounters = new List <PdhCounterHandle>();

            foreach (var counter in counters)
            {
                if (_counters.TryGetValue(counter, out hCounter))
                {
                    _counters.Remove(counter);

                    removedCounters.Add(hCounter);
                }
            }

            return(removedCounters);
        }
Esempio n. 2
0
 public static extern UInt32 PdhGetRawCounterValue(
     PdhCounterHandle phCounter,
     IntPtr lpdwType,
     out PDH_FMT_COUNTERVALUE pValue);
Esempio n. 3
0
 public static extern UInt32 PdhGetFormattedCounterValue(
     PdhCounterHandle phCounter,
     PdhFormat dwFormat,
     IntPtr lpdwType,
     out PDH_FMT_COUNTERVALUE pValue);
Esempio n. 4
0
 public static extern UInt32 PdhAddEnglishCounterW(
     PdhQueryHandle hQuery,
     string szFullCounterPath,
     IntPtr dwUserData,
     out PdhCounterHandle phCounter);
Esempio n. 5
0
        public async Task DoRefresh()
        {
            if (!NeedsRefresh)
            {
                return;
            }

            PDH_FMT_COUNTERVALUE value = default(PDH_FMT_COUNTERVALUE);

            uint result = Pdh.PdhCollectQueryData(_query);

            if (result == Pdh.PDH_NO_DATA)
            {
                throw new MissingCountersException(_counters.Keys);
            }
            if (result != 0)
            {
                throw new Win32Exception((int)result);
            }

            bool multiSample = false;
            List <IPerfCounter> missingCounters = new List <IPerfCounter>();

            foreach (KeyValuePair <IPerfCounter, PdhCounterHandle> counterInstance in _counters)
            {
                IPerfCounter     counter = counterInstance.Key;
                PdhCounterHandle handle  = counterInstance.Value;

                result = Pdh.PdhGetFormattedCounterValue(handle, PdhFormat.PDH_FMT_LARGE, IntPtr.Zero, out value);

                if (result == Pdh.PDH_INVALID_DATA && value.CStatus == Pdh.PDH_CSTATUS_INVALID_DATA && !multiSample)
                {
                    multiSample = true;

                    result = Pdh.PdhCollectQueryData(_query);
                    if (result != 0)
                    {
                        throw new Win32Exception((int)result);
                    }

                    result = Pdh.PdhGetFormattedCounterValue(handle, PdhFormat.PDH_FMT_LARGE, IntPtr.Zero, out value);
                }

                if ((result != 0 && value.CStatus == Pdh.PDH_CSTATUS_NO_INSTANCE) ||
                    result == Pdh.PDH_INVALID_HANDLE ||
                    result == Pdh.PDH_CALC_NEGATIVE_VALUE ||
                    result == Pdh.PDH_CALC_NEGATIVE_DENOMINATOR ||
                    (result == Pdh.PDH_INVALID_DATA && value.CStatus == Pdh.PDH_INVALID_DATA))
                {
                    missingCounters.Add(counter);
                    continue;
                }
                else if (result != 0)
                {
                    if (!_counterFinder.CounterExists(counter))
                    {
                        missingCounters.Add(counter);
                        continue;
                    }

                    throw new Win32Exception((int)result);
                }
                else if (value.CStatus != 0)
                {
                    throw new Win32Exception((int)value.CStatus);
                }

                counter.Value = value.longLongValue;
            }

            if (missingCounters.Count > 0)
            {
                throw new MissingCountersException(missingCounters);
            }

            TimeSpan calculationDelta = DateTime.UtcNow - _lastCalculatedTime;

            _lastCalculatedTime = DateTime.UtcNow;
        }