Exemplo n.º 1
0
        public static Dictionary <string, string> Parse(string dataSource)
        {
            SortedList <string, SortedSet <string> > allCounters = PdhUtils.Parse(dataSource);
            var generated = new Dictionary <string, string>();

            foreach (string counterSet in allCounters.Keys)
            {
                string setName = NameUtils.CreateIdentifier(counterSet);

                var sb = new StringBuilder("// This code was generated by EtwEventTypeGen");
                sb.AppendLine(setName);
                sb.Append("namespace Tx.Windows.Counters.");
                sb.AppendLine(setName);
                sb.AppendLine();
                sb.AppendLine("{");

                foreach (string counter in allCounters[counterSet])
                {
                    EmitCounter(counterSet, counter, ref sb);
                }

                sb.AppendLine("}");
                generated.Add(setName, sb.ToString());
            }

            return(generated);
        }
        public PerfMetadata(string logFileName)
        {
            List <string> machines     = PdhUtils.GetMachineList(logFileName);
            var           machineInfos = new List <MachineInfo>();

            foreach (string m in machines)
            {
                var objects     = PdhUtils.GetObjectList(logFileName, m);
                var objectInfos = new List <ObjectInfo>();

                foreach (string o in objects)
                {
                    var counters  = new List <string>();
                    var instances = new List <string>();

                    PdhUtils.GetCounterAndInstanceList(logFileName, m, o, out counters, out instances);
                    ObjectInfo info = new ObjectInfo(o, counters, instances);

                    objectInfos.Add(info);
                }

                machineInfos.Add(new MachineInfo(m, objectInfos));
            }

            _machines = machineInfos.AsReadOnly();
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 7
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();
        }