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>()); }
public static void EnsureSuccess(this PdhStatus status, string method) { if (status != 0) { FailWithError(status, method); } }
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); } }
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); }
public static void EnsureStatus(this PdhStatus status, PdhStatus successfulStatus1, PdhStatus successfulStatus2, string method, string message = "") { if (status != successfulStatus1 && status != successfulStatus2) { FailWithError(status, method, message); } }
public static void CheckStatus(PdhStatus actualStatus, params PdhStatus[] expectedStatus) { for (int i = 0; i <= expectedStatus.GetUpperBound(0); i++) { if (actualStatus == expectedStatus[i]) { return; } } throw new Exception(actualStatus.ToString()); }
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); } }
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); }
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>(); } }
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(); }
public PdhException(string function, PdhStatus status, string message = "") : base( GetExceptionInfo(function, status, message)) => Status = status;
public static bool IsLargerBufferRequired(this PdhStatus status) => status == PdhStatus.PDH_MORE_DATA;
private static PdhException CreateException(PdhStatus error, string function, string message) => new PdhException(function, error, message);
private static InvalidOperationException CreateException(PdhStatus error, string function) => new InvalidOperationException(string.Format("Pdh function {0} failed with code {1:x8} ({2})", function, (int)error, Enum.GetName(typeof(PdhStatus), error)));
private static void FailWithError(PdhStatus error, string function) => throw CreateException(error, function);