Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
        private void PerformFirstRead()
        {
            long fileTimeStamp = 0;

            if (this._isPreVista)
            {
                Apis.PdhCollectQueryData(this._safeQueryHandle);
            }
            else
            {
                Apis.PdhCollectQueryDataWithTime(this._safeQueryHandle, ref fileTimeStamp);
            }
            //a sneaky way of not blocking until we actually call ReadNext
            this._backgroundTimer.Start();
        }