Example #1
0
        public static List <string> GetObjectList(string logFilename, string machineName)
        {
            uint objectBufferLength = 0;

            PdhStatus pdhStatus = PdhNativeMethods.PdhEnumObjects(
                logFilename,
                machineName,
                null,
                ref objectBufferLength,
                PdhDetailLevel.PERF_DETAIL_WIZARD,
                0);

            CheckStatus(pdhStatus, PdhStatus.PDH_MORE_DATA, PdhStatus.PDH_CSTATUS_VALID_DATA);
            if (pdhStatus == PdhStatus.PDH_MORE_DATA)
            {
                var objectListBuffer = new char[objectBufferLength];

                pdhStatus = PdhNativeMethods.PdhEnumObjects(
                    logFilename,
                    machineName,
                    objectListBuffer,
                    ref objectBufferLength,
                    PdhDetailLevel.PERF_DETAIL_WIZARD,
                    0);

                CheckStatus(pdhStatus, PdhStatus.PDH_CSTATUS_VALID_DATA);

                return(MultiSzToStringList(objectListBuffer));
            }

            return(new List <string>());
        }
Example #2
0
        public void OnTimer(object state)
        {
            try
            {
                if (_firstMove)
                {
                    // some counters need two samples to calculate their value
                    // so skip a sample to make sure there are no further complications
                    PdhNativeMethods.PdhCollectQueryData(_query);
                    _firstMove = false;
                    return;
                }

                long      time;
                PdhStatus status = PdhNativeMethods.PdhCollectQueryDataWithTime(_query, out time);
                PdhUtils.CheckStatus(status, PdhStatus.PDH_CSTATUS_VALID_DATA);
                DateTime timestamp = TimeUtil.FromFileTime(time);

                foreach (PerfCounterInfo counterInfo in _counters)
                {
                    ProduceCounterSamples(counterInfo, timestamp);
                }
            }
            catch (Exception ex)
            {
                _observer.OnError(ex);
            }
        }
Example #3
0
        public static List <string> GetMachineList(string logFileName)
        {
            List <string> machines;
            uint          objectBufferLength = 0;

            PdhStatus pdhStatus = PdhNativeMethods.PdhEnumMachines(
                logFileName,
                null,
                ref objectBufferLength);

            CheckStatus(pdhStatus, PdhStatus.PDH_MORE_DATA);

            var objectListBuffer = new char[objectBufferLength];

            pdhStatus = PdhNativeMethods.PdhEnumMachines(
                logFileName,
                objectListBuffer,
                ref objectBufferLength);

            CheckStatus(pdhStatus, PdhStatus.PDH_CSTATUS_VALID_DATA);

            machines = MultiSzToStringList(objectListBuffer);

            return(machines);
        }
Example #4
0
        internal void ProduceCounterSamples(PerfCounterInfo counterInfo, DateTime timestamp)
        {
            uint bufferSize = 0;
            uint bufferCount;

            PdhStatus status = PdhNativeMethods.PdhGetFormattedCounterArray(
                counterInfo.Handle,
                PdhFormat.PDH_FMT_DOUBLE,
                ref bufferSize,
                out bufferCount,
                IntPtr.Zero);

            PdhUtils.CheckStatus(status, PdhStatus.PDH_MORE_DATA);

            var buffer = new byte[bufferSize];

            unsafe
            {
                fixed(byte *pb = buffer)
                {
                    status = PdhNativeMethods.PdhGetFormattedCounterArray(
                        counterInfo.Handle,
                        PdhFormat.PDH_FMT_DOUBLE,
                        ref bufferSize,
                        out bufferCount,
                        (IntPtr)pb);
                    if (status == PdhStatus.PDH_INVALID_DATA ||
                        status == PdhStatus.PDH_NO_DATA ||
                        status == PdhStatus.PDH_CALC_NEGATIVE_VALUE ||
                        status == PdhStatus.PDH_CALC_NEGATIVE_DENOMINATOR ||
                        status == PdhStatus.PDH_CALC_NEGATIVE_TIMEBASE)
                    {
                        var sample = new PerformanceSample(counterInfo, counterInfo.Instance, timestamp, double.NaN);
                        _observer.OnNext(sample);
                        return;
                    }

                    PdhUtils.CheckStatus(status, PdhStatus.PDH_CSTATUS_VALID_DATA);

                    var items = (PDH_FMT_COUNTERVALUE_ITEM *)pb;

                    for (int i = 0; i < bufferCount; i++)
                    {
                        PDH_FMT_COUNTERVALUE_ITEM *item = items + i;
                        var instanceName = new string((char *)item->szName);
                        var sample       = new PerformanceSample(counterInfo, instanceName, timestamp, item->FmtValue.doubleValue);

                        _observer.OnNext(sample);
                    }
                }
            }
        }
        public void Read()
        {
            try
            {
                if (_firstMove)
                {
                    // some counters need two samples to calculate their value
                    // so skip a sample to make sure there are no further complications
                    PdhNativeMethods.PdhCollectQueryData(_query);
                    _firstMove = false;
                }

                while (true)
                {
                    long      time;
                    PdhStatus status = PdhNativeMethods.PdhCollectQueryDataWithTime(_query, out time);
                    if (status == PdhStatus.PDH_NO_MORE_DATA)
                    {
                        break;
                    }

                    if (status == PdhStatus.PDH_NO_DATA)
                    {
                        if (_binaryLog)
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }

                    PdhUtils.CheckStatus(status, PdhStatus.PDH_CSTATUS_VALID_DATA);
                    //DateTime timestamp = TimeUtil.FromFileTime(time);
                    DateTime timestamp = DateTime.FromFileTimeUtc(time);

                    foreach (PerfCounterInfo counterInfo in _counters)
                    {
                        ProduceCounterSamples(counterInfo, timestamp);
                    }
                }

                _observer.OnCompleted();
            }
            catch (Exception ex)
            {
                _observer.OnError(ex);
            }
        }
Example #6
0
        public PerfCounterRealTimeProbe(IObserver <PerformanceSample> observer, TimeSpan samplingRate, params string[] counterPaths)
            : base(observer)
        {
            PdhStatus status = PdhNativeMethods.PdhOpenQuery(null, IntPtr.Zero, out _query);

            PdhUtils.CheckStatus(status, PdhStatus.PDH_CSTATUS_VALID_DATA);

            for (int i = 0; i < counterPaths.Length; i++)
            {
                AddCounter(counterPaths[i], i);
            }

            _timer = new Timer(OnTimer, null, TimeSpan.Zero, samplingRate);
        }
Example #7
0
        public static void GetCounterAndInstanceList(
            string logFilename,
            string machineName,
            string objectName,
            out List <string> counterList,
            out List <string> instanceList)
        {
            uint counterBufferLength  = 0;
            uint instanceBufferLength = 0;

            PdhStatus pdhStatus = PdhNativeMethods.PdhEnumObjectItems(
                logFilename,
                machineName,
                objectName,
                null,
                ref counterBufferLength,
                null,
                ref instanceBufferLength,
                PdhDetailLevel.PERF_DETAIL_WIZARD,
                0);

            CheckStatus(pdhStatus, PdhStatus.PDH_MORE_DATA, PdhStatus.PDH_CSTATUS_VALID_DATA);
            if (pdhStatus == PdhStatus.PDH_MORE_DATA)
            {
                var counterListBuffer  = new char[counterBufferLength];
                var instanceListBuffer = new char[instanceBufferLength];

                pdhStatus = PdhNativeMethods.PdhEnumObjectItems(
                    logFilename,
                    machineName,
                    objectName,
                    counterListBuffer,
                    ref counterBufferLength,
                    instanceListBuffer,
                    ref instanceBufferLength,
                    PdhDetailLevel.PERF_DETAIL_WIZARD,
                    0);

                CheckStatus(pdhStatus, PdhStatus.PDH_CSTATUS_VALID_DATA);

                counterList  = MultiSzToStringList(counterListBuffer);
                instanceList = MultiSzToStringList(instanceListBuffer);
            }
            else
            {
                counterList  = new List <string>();
                instanceList = new List <string>();
            }
        }
Example #8
0
        protected void AddCounter(string counterPath, int index)
        {
            PdhCounterHandle counter;
            PdhStatus        status = PdhNativeMethods.PdhAddCounter(_query, counterPath, IntPtr.Zero, out counter);

            if (status == PdhStatus.PDH_ENTRY_NOT_IN_LOG_FILE || status == PdhStatus.PDH_CSTATUS_NO_INSTANCE)
            {
                return;
            }

            PdhUtils.CheckStatus(status, PdhStatus.PDH_CSTATUS_VALID_DATA);

            var counterInfo = new PerfCounterInfo(counterPath, counter, index);

            _counters.Add(counterInfo);
        }
        public PerfCounterFileReader(IObserver <PerformanceSample> observer, string file)
            : base(observer)
        {
            string extension = Path.GetExtension(file);

            if (extension != null)
            {
                _binaryLog = extension.ToLowerInvariant() == ".blg";
            }

            string[] counterPaths = PdhUtils.GetCounterPaths(file);

            PdhStatus status = PdhNativeMethods.PdhOpenQuery(file, IntPtr.Zero, out _query);

            PdhUtils.CheckStatus(status, PdhStatus.PDH_CSTATUS_VALID_DATA);

            for (int i = 0; i < counterPaths.Length; i++)
            {
                AddCounter(counterPaths[i], i);
            }

            Read();
        }
 protected override bool ReleaseHandle()
 {
     return(PdhNativeMethods.PdhRemoveCounter(handle) == 0);
 }
 protected override bool ReleaseHandle()
 {
     return(PdhNativeMethods.PdhCloseQuery(handle) == 0);
 }
 protected override bool ReleaseHandle()
 {
     return(PdhNativeMethods.PdhCloseLog(handle, PdhNativeMethods.PDH_FLAGS_CLOSE_QUERY) == 0);
 }