Beispiel #1
0
        private void DoAddCounters(IEnumerable <IPerfCounter> counters)
        {
            uint result;

            if (_query == null)
            {
                PdhQueryHandle query;

                result = Pdh.PdhOpenQueryW(null, IntPtr.Zero, out query);
                if (result != 0)
                {
                    throw new Win32Exception((int)result);
                }

                _query = query;
            }

            List <IPerfCounter> missingCounters = new List <IPerfCounter>();

            foreach (var counter in counters)
            {
                PdhCounterHandle hCounter;

                result = Pdh.PdhAddEnglishCounterW(_query, counter.Path, IntPtr.Zero, out hCounter);
                if (result == Pdh.PDH_CSTATUS_NO_OBJECT ||
                    result == Pdh.PDH_CSTATUS_NO_COUNTER)
                {
                    missingCounters.Add(counter);
                    continue;
                }
                if (result != 0)
                {
                    throw new Win32Exception((int)result);
                }

                _counters[counter] = hCounter;
            }

            if (missingCounters.Count > 0)
            {
                throw new MissingCountersException(missingCounters);
            }
        }
Beispiel #2
0
        private string LookupName(uint index)
        {
            uint bufSize = 0;

            uint result = Pdh.PdhLookupPerfNameByIndexW(null, index, null, ref bufSize);

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

            StringBuilder buffer = new StringBuilder((int)bufSize);

            result = Pdh.PdhLookupPerfNameByIndexW(null, index, buffer, ref bufSize);

            if (result != 0)
            {
                throw new Win32Exception((int)result);
            }

            return(buffer.ToString());
        }
Beispiel #3
0
 protected override bool ReleaseHandle()
 {
     return(Pdh.PdhRemoveCounter(handle) == 0);
 }
Beispiel #4
0
 protected override bool ReleaseHandle()
 {
     return(Pdh.PdhCloseQuery(handle) == 0);
 }
Beispiel #5
0
 protected override bool ReleaseHandle()
 {
     return(Pdh.PdhCloseLog(handle, Pdh.PDH_FLAGS_CLOSE_QUERY) == 0);
 }
Beispiel #6
0
        private List <string> ExpandCounterPath(string searchPattern, PdhExpansionFlags flags)
        {
            long cchPathListLength = 0;
            uint result            = 0;

            byte[] buffer = null;

            //
            // Do - while, the buffer can grow after being told the necessary size
            do
            {
                IntPtr mszExpandedPathList = IntPtr.Zero;

                if (cchPathListLength > 0)
                {
                    //
                    // If we received a buffer size allocate one
                    // Unicode size is 2 bytes
                    mszExpandedPathList = Marshal.AllocHGlobal(2 * (int)cchPathListLength);
                }

                try {
                    result = Pdh.PdhExpandWildCardPathW(null, searchPattern, mszExpandedPathList, ref cchPathListLength, flags);

                    if (result == Pdh.PDH_MORE_DATA)
                    {
                        continue;
                    }

                    if (result == Pdh.PDH_CSTATUS_NO_OBJECT || result == Pdh.PDH_CSTATUS_NO_INSTANCE)
                    {
                        return(new List <string>());
                    }

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

                    buffer = new byte[cchPathListLength * 2];
                    Marshal.Copy(mszExpandedPathList, buffer, 0, buffer.Length);
                }
                finally {
                    //
                    // Always clean up allocated buffer
                    if (mszExpandedPathList != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(mszExpandedPathList);
                    }
                }
            } while (result == Pdh.PDH_MORE_DATA);

            if (buffer == null)
            {
                throw new Win32Exception((int)result);
            }

            //
            // Parse paths from filled buffer

            List <string> strings = new List <string>();
            int           start   = 0;
            int           end     = 0;

            var chars = Encoding.Unicode.GetChars(buffer);

            do
            {
                do
                {
                    end++;
                }while (end < chars.Length && chars[end] != 0);

                strings.Add(new string(chars, start, end - start));
                start = end;
            } while (start < chars.Length - 1 && chars[start + 1] != 0);

            return(strings);
        }
Beispiel #7
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;
        }