Example #1
0
        private void AddCounters(IEnumerable <string> validPaths)
        {
            uint resultCode = (uint)PdhResults.PDH_CSTATUS_VALID_DATA;

            foreach (string validPath in validPaths)
            {
                IntPtr counterPointer;
                resultCode = Apis.PdhAddCounter(this._safeQueryHandle, validPath, IntPtr.Zero, out counterPointer);
                if (resultCode == PdhResults.PDH_CSTATUS_VALID_DATA)
                {
                    CounterHandleNInstance instance = new CounterHandleNInstance()
                    {
                        CounterHandle = counterPointer, InstanceName = null
                    };
                    PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = PdhHelper.ParsePath(validPath);

                    if (pCounterPathElements.InstanceName != null)
                    {
                        instance.InstanceName = pCounterPathElements.InstanceName.ToLower(CultureInfo.InvariantCulture);
                    }
                    if (!this._consumerPathToHandleAndInstanceMap.ContainsKey(validPath.ToLower(CultureInfo.InvariantCulture)))
                    {
                        this._consumerPathToHandleAndInstanceMap.Add(validPath.ToLower(CultureInfo.InvariantCulture), instance);
                    }
                }
            }
        }
Example #2
0
        public PerformanceCounterSampleSet ReadNextSet()
        {
            this._firstRead.Wait();
            long fileTimeStamp = 0;
            uint returnCode    = this._isPreVista ? Apis.PdhCollectQueryData(this._safeQueryHandle)
                                : Apis.PdhCollectQueryDataWithTime(this._safeQueryHandle, ref fileTimeStamp);

            if (this._isLastSampleBad)
            {
                return(null);
            }
            if (returnCode != PdhResults.PDH_CSTATUS_VALID_DATA)
            {
                //this makes sure next call to ReadNextSet doesn't examine the data, and just returns null
                this._isLastSampleBad = true;
                return(null);
            }

            DateTime now = (_isPreVista || returnCode == PdhResults.PDH_NO_DATA) ? DateTime.Now :
                           new DateTime(DateTime.FromFileTimeUtc(fileTimeStamp).Ticks, DateTimeKind.Local);

            PerformanceCounterSample[] counterSamples = new PerformanceCounterSample[this._consumerPathToHandleAndInstanceMap.Count];
            int samplesRead = 0;

            foreach (string key in this._consumerPathToHandleAndInstanceMap.Keys)
            {
                IntPtr      counterHandle = this._consumerPathToHandleAndInstanceMap[key].CounterHandle;
                CounterInfo info          = PdhHelper.GetCounterInfo(counterHandle);

                var sample            = GetRawCounterSample(counterHandle, key, info, now);
                var performanceSample = sample.PerformanceCounterSample ?? GetFormattedCounterSample(counterHandle, key, info, sample.RawCounter);

                if (!this._ignoreBadStatusCodes && (performanceSample.Status != 0))
                {
                    throw BuildException(performanceSample.Status);
                }

                if (performanceSample.Status != 0)
                {
                    _log.Info(() => string.Format("Status {0:x} ignored for counter {1}", performanceSample.Status, key));
                    continue;
                }
                counterSamples[samplesRead++] = performanceSample;
            }

            this._isLastSampleBad = false;
            //in the event we skipped bad data
            if (samplesRead < counterSamples.Length)
            {
                Array.Resize(ref counterSamples, samplesRead);
            }
            return(new PerformanceCounterSampleSet(this._isPreVista ? counterSamples[samplesRead].Timestamp : now, counterSamples));
        }
Example #3
0
        public static string TranslateLocalCounterPath(string englishPath)
        {
            PDH_COUNTER_PATH_ELEMENTS counterPathElements = PdhHelper.ParsePath(englishPath);

            string counterName = counterPathElements.CounterName.ToLower(CultureInfo.InvariantCulture),
                   objectName  = counterPathElements.ObjectName.ToLower(CultureInfo.InvariantCulture);

            string[] counterNames = (string[])Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009").GetValue("Counter");

            int counterIndex = -1, objectIndex = -1;

            for (int i = 1; i < counterNames.Length; i++)
            {
                string currentCounter = counterNames[i];
                if (currentCounter.ToLower(CultureInfo.InvariantCulture) == counterName)
                {
                    counterIndex = Convert.ToInt32(counterNames[i - 1], CultureInfo.InvariantCulture);
                    if ((counterIndex != -1) && (objectIndex != -1))
                    {
                        break;
                    }

                    continue;
                }
                if (currentCounter.ToLower(CultureInfo.InvariantCulture) == objectName)
                {
                    objectIndex = Convert.ToInt32(counterNames[i - 1], CultureInfo.InvariantCulture);
                }

                if ((counterIndex != -1) && (objectIndex != -1))
                {
                    break;
                }
            }

            counterPathElements.ObjectName  = PdhHelper.LookupPerfNameByIndex(counterPathElements.MachineName, (uint)objectIndex);
            counterPathElements.CounterName = PdhHelper.LookupPerfNameByIndex(counterPathElements.MachineName, (uint)counterIndex);

            return(PdhHelper.MakePath(counterPathElements, false));
        }
Example #4
0
        private string[] ExpandWildCardPath(string path)
        {
            IntPtr pathListLength = new IntPtr(0);
            uint   resultCode     = Apis.PdhExpandWildCardPathH(this._safeDataSourceHandle, path, IntPtr.Zero, ref pathListLength, PdhWildcardPathFlags.None);

            if (resultCode == PdhResults.PDH_MORE_DATA)
            {
                IntPtr expandedPathList = Marshal.AllocHGlobal(pathListLength.ToInt32() * 2);
                try
                {
                    resultCode = Apis.PdhExpandWildCardPathH(this._safeDataSourceHandle, path, expandedPathList, ref pathListLength, PdhWildcardPathFlags.None);
                    if (resultCode == PdhResults.PDH_CSTATUS_VALID_DATA)
                    {
                        return(PdhHelper.ParsePdhMultiStringBuffer(ref expandedPathList, pathListLength.ToInt32()));
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(expandedPathList);
                }
            }
            return(null);
        }
        private IEnumerable<PerformanceCounterSampleSet> ProcessGetCounter(IEnumerable<string> counters, TimeSpan sampleInterval, int maxSamples, CancellationToken token)
        {
            using (PdhHelper helper = new PdhHelper(this._computerNames, counters, this._ignoreBadStatusCodes))
            {
                int samplesRead = 0;

                do
                {
                    PerformanceCounterSampleSet set = helper.ReadNextSet();
                    if (null != set)
                    {
                        yield return set;
                    }
                    samplesRead++;
                }
                while (((maxSamples == INFINITIY) || (samplesRead < maxSamples)) && !token.WaitHandle.WaitOne(sampleInterval, true));
            }
        }