// // WriteSampleSetObject() helper. // In addition to writing the PerformanceCounterSampleSet object, // it writes a single error if one of the samples has an invalid (non-zero) status. // private void WriteSampleSetObject(PerformanceCounterSampleSet set) { foreach (PerformanceCounterSample sample in set.CounterSamples) { if (sample.Status != 0) { string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterSampleDataInvalid")); Exception exc = new Exception(msg); WriteError(new ErrorRecord(exc, "CounterApiError", ErrorCategory.InvalidResult, null)); break; } } WriteObject(set); }
public long ReadNextSetPreVista(out PerformanceCounterSampleSet nextSet, bool bSkipReading) { long num = 0; nextSet = null; num = PdhCollectQueryData(this.m_hQuery); if (bSkipReading) { return num; } if ((num != 0) && (num != 0x800007d5L)) { return num; } PerformanceCounterSample[] counterSamples = new PerformanceCounterSample[this.m_ConsumerPathToHandleAndInstanceMap.Count]; long num2 = 0; long num3 = 0; long num4 = 0; DateTime now = DateTime.Now; foreach (string str in this.m_ConsumerPathToHandleAndInstanceMap.Keys) { PDH_RAW_COUNTER pdh_raw_counter; IntPtr lpdwType = new IntPtr(0); long counterType = 0x40030403; long defaultScale = 0; ulong timeBase = 0L; IntPtr hCounter = this.m_ConsumerPathToHandleAndInstanceMap[str].hCounter; this.GetCounterInfoPlus(hCounter, out counterType, out defaultScale, out timeBase); num = PdhGetRawCounterValue(hCounter, out lpdwType, out pdh_raw_counter); switch (num) { case 0xc0000bc6L: case 0x800007d5L: counterSamples[num2++] = new PerformanceCounterSample(str, this.m_ConsumerPathToHandleAndInstanceMap[str].InstanceName, 0.0, 0L, 0L, 0, PerformanceCounterType.RawBase, defaultScale, timeBase, DateTime.Now, (ulong) DateTime.Now.ToFileTime(), pdh_raw_counter.CStatus); num3++; num4 = num; break; default: { PDH_FMT_COUNTERVALUE_DOUBLE pdh_fmt_countervalue_double; if (num != 0) { return num; } long fileTime = (pdh_raw_counter.TimeStamp.dwHighDateTime << 0x20) + ((long) ((ulong) pdh_raw_counter.TimeStamp.dwLowDateTime)); now = new DateTime(DateTime.FromFileTimeUtc(fileTime).Ticks, DateTimeKind.Local); num = PdhGetFormattedCounterValue(hCounter, 0x8200, out lpdwType, out pdh_fmt_countervalue_double); switch (num) { case 0xc0000bc6L: case 0x800007d5L: { counterSamples[num2++] = new PerformanceCounterSample(str, this.m_ConsumerPathToHandleAndInstanceMap[str].InstanceName, 0.0, (ulong) pdh_raw_counter.FirstValue, (ulong) pdh_raw_counter.SecondValue, pdh_raw_counter.MultiCount, (PerformanceCounterType) counterType, defaultScale, timeBase, now, (ulong) fileTime, pdh_fmt_countervalue_double.CStatus); num3++; num4 = num; continue; } } if (num != 0) { return num; } counterSamples[num2++] = new PerformanceCounterSample(str, this.m_ConsumerPathToHandleAndInstanceMap[str].InstanceName, pdh_fmt_countervalue_double.doubleValue, (ulong) pdh_raw_counter.FirstValue, (ulong) pdh_raw_counter.SecondValue, pdh_raw_counter.MultiCount, (PerformanceCounterType) lpdwType.ToInt32(), defaultScale, timeBase, now, (ulong) fileTime, pdh_fmt_countervalue_double.CStatus); break; } } } nextSet = new PerformanceCounterSampleSet(now, counterSamples, this.m_firstReading); this.m_firstReading = false; if (num3 == counterSamples.Length) { return num4; } return 0; }
private void WriteSampleSetObject(PerformanceCounterSampleSet set, bool firstSet) { if (!firstSet) { foreach (PerformanceCounterSample sample in set.CounterSamples) { if (sample.Status != 0) { Exception exception = new Exception(string.Format(CultureInfo.InvariantCulture, this._resourceMgr.GetString("CounterSampleDataInvalid"), new object[0])); base.WriteError(new ErrorRecord(exception, "CounterApiError", ErrorCategory.InvalidResult, null)); break; } } } base.WriteObject(set); }
public long AddRelogCounters(PerformanceCounterSampleSet sampleSet) { long num = 0; Dictionary<string, List<PerformanceCounterSample>> dictionary = new Dictionary<string, List<PerformanceCounterSample>>(); foreach (PerformanceCounterSample sample in sampleSet.CounterSamples) { PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS(); num = this.ParsePath(sample.Path, ref pCounterPathElements); if (num == 0) { List<PerformanceCounterSample> list; string str = pCounterPathElements.MachineName.ToLower(CultureInfo.InvariantCulture); string str2 = pCounterPathElements.ObjectName.ToLower(CultureInfo.InvariantCulture); string str3 = pCounterPathElements.CounterName.ToLower(CultureInfo.InvariantCulture); string key = @"\\" + str + @"\" + str2 + @"\" + str3; if (dictionary.TryGetValue(key, out list)) { dictionary[key].Add(sample); } else { List<PerformanceCounterSample> list2 = new List<PerformanceCounterSample> { sample }; dictionary.Add(key, list2); } } } foreach (string str5 in dictionary.Keys) { IntPtr ptr; string path = dictionary[str5][0].Path; if (dictionary[str5].Count > 1) { num = this.MakeAllInstancePath(dictionary[str5][0].Path, out path); if (num != 0) { continue; } } num = PdhAddRelogCounter(this.m_hQuery, path, (long) dictionary[str5][0].CounterType, dictionary[str5][0].DefaultScale, dictionary[str5][0].TimeBase, out ptr); if (num == 0) { foreach (PerformanceCounterSample sample2 in dictionary[str5]) { PDH_COUNTER_PATH_ELEMENTS pdh_counter_path_elements2 = new PDH_COUNTER_PATH_ELEMENTS(); num = this.ParsePath(sample2.Path, ref pdh_counter_path_elements2); if (num == 0) { CounterHandleNInstance instance = new CounterHandleNInstance { hCounter = ptr }; if (pdh_counter_path_elements2.InstanceName != null) { instance.InstanceName = pdh_counter_path_elements2.InstanceName.ToLower(CultureInfo.InvariantCulture); } if (!this.m_ReloggerPathToHandleAndInstanceMap.ContainsKey(sample2.Path.ToLower(CultureInfo.InvariantCulture))) { this.m_ReloggerPathToHandleAndInstanceMap.Add(sample2.Path.ToLower(CultureInfo.InvariantCulture), instance); } } } } } if (this.m_ReloggerPathToHandleAndInstanceMap.Keys.Count <= 0) { return num; } return 0; }
public long AddRelogCountersPreservingPaths(PerformanceCounterSampleSet sampleSet) { long num = 0; foreach (PerformanceCounterSample sample in sampleSet.CounterSamples) { PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS(); num = this.ParsePath(sample.Path, ref pCounterPathElements); if (num == 0) { IntPtr ptr; num = PdhAddRelogCounter(this.m_hQuery, sample.Path, (long) sample.CounterType, sample.DefaultScale, sample.TimeBase, out ptr); if (num == 0) { CounterHandleNInstance instance = new CounterHandleNInstance { hCounter = ptr }; if (pCounterPathElements.InstanceName != null) { instance.InstanceName = pCounterPathElements.InstanceName.ToLower(CultureInfo.InvariantCulture); } if (!this.m_ReloggerPathToHandleAndInstanceMap.ContainsKey(sample.Path.ToLower(CultureInfo.InvariantCulture))) { this.m_ReloggerPathToHandleAndInstanceMap.Add(sample.Path.ToLower(CultureInfo.InvariantCulture), instance); } } } } if (this.m_ReloggerPathToHandleAndInstanceMap.Keys.Count <= 0) { return num; } return 0; }
public uint ReadNextSet(out PerformanceCounterSampleSet nextSet, bool bSkipReading) { Debug.Assert(_hQuery != null && !_hQuery.IsInvalid); if (_isPreVista) { return ReadNextSetPreVista(out nextSet, bSkipReading); } uint res = 0; nextSet = null; Int64 batchTimeStampFT = 0; res = PdhCollectQueryDataWithTime(_hQuery, ref batchTimeStampFT); if (bSkipReading) { return res; } if (res != 0 && res != PdhResults.PDH_NO_DATA) { return res; } // // NOTE: PDH returns the filetime as local time, therefore // we need to call FromFileTimUtc() to avoid .NET applying the timezone adjustment. // However, that would result in the DateTime object having Kind.Utc. // We have to copy it once more to correct that (Kind is a read-only property). // DateTime batchStamp = DateTime.Now; if (res != PdhResults.PDH_NO_DATA) { batchStamp = new DateTime(DateTime.FromFileTimeUtc(batchTimeStampFT).Ticks, DateTimeKind.Local); } PerformanceCounterSample[] samplesArr = new PerformanceCounterSample[_consumerPathToHandleAndInstanceMap.Count]; uint sampleIndex = 0; uint numInvalidDataSamples = 0; uint lastErr = 0; foreach (string path in _consumerPathToHandleAndInstanceMap.Keys) { IntPtr counterTypePtr = new IntPtr(0); UInt32 counterType = (UInt32)PerformanceCounterType.RawBase; UInt32 defaultScale = 0; UInt64 timeBase = 0; IntPtr hCounter = _consumerPathToHandleAndInstanceMap[path].hCounter; Debug.Assert(hCounter != null); res = GetCounterInfoPlus(hCounter, out counterType, out defaultScale, out timeBase); if (res != 0) { //Console.WriteLine ("GetCounterInfoPlus for " + path + " failed with " + res); } PDH_RAW_COUNTER rawValue; res = PdhGetRawCounterValue(hCounter, out counterTypePtr, out rawValue); if (res != 0) { samplesArr[sampleIndex++] = new PerformanceCounterSample(path, _consumerPathToHandleAndInstanceMap[path].InstanceName, 0, (ulong)0, (ulong)0, 0, PerformanceCounterType.RawBase, defaultScale, timeBase, batchStamp, (UInt64)batchStamp.ToFileTime(), (rawValue.CStatus == 0) ? res : rawValue.CStatus); numInvalidDataSamples++; lastErr = res; continue; } long dtFT = (((long)rawValue.TimeStamp.dwHighDateTime) << 32) + (uint)rawValue.TimeStamp.dwLowDateTime; DateTime dt = new DateTime(DateTime.FromFileTimeUtc(dtFT).Ticks, DateTimeKind.Local); PDH_FMT_COUNTERVALUE_DOUBLE fmtValueDouble; res = PdhGetFormattedCounterValue(hCounter, PdhFormat.PDH_FMT_DOUBLE | PdhFormat.PDH_FMT_NOCAP100, out counterTypePtr, out fmtValueDouble); if (res != 0) { samplesArr[sampleIndex++] = new PerformanceCounterSample(path, _consumerPathToHandleAndInstanceMap[path].InstanceName, 0, (ulong)rawValue.FirstValue, (ulong)rawValue.SecondValue, rawValue.MultiCount, (PerformanceCounterType)counterType, defaultScale, timeBase, dt, (UInt64)dtFT, (fmtValueDouble.CStatus == 0) ? res : rawValue.CStatus); numInvalidDataSamples++; lastErr = res; continue; } samplesArr[sampleIndex++] = new PerformanceCounterSample(path, _consumerPathToHandleAndInstanceMap[path].InstanceName, fmtValueDouble.doubleValue, (ulong)rawValue.FirstValue, (ulong)rawValue.SecondValue, rawValue.MultiCount, (PerformanceCounterType)counterTypePtr.ToInt32(), defaultScale, timeBase, dt, (UInt64)dtFT, fmtValueDouble.CStatus); } nextSet = new PerformanceCounterSampleSet(batchStamp, samplesArr, _firstReading); _firstReading = false; if (numInvalidDataSamples == samplesArr.Length) { res = lastErr; } else { // // Reset the error - any errors are saved per sample in PerformanceCounterSample.Status for kvetching later // res = 0; } return res; }
// // AddRelogCountersPreservingPaths preserves all paths and adds as relog counters to m_hQuery. // The counter handles and full paths are added to m_ReloggerPathToHandleAndInstanceMap // public uint AddRelogCountersPreservingPaths(PerformanceCounterSampleSet sampleSet) { Debug.Assert(_hQuery != null && !_hQuery.IsInvalid); uint res = 0; // // Go through all the samples one, constructing prefixInstanceMap and adding new counters as needed // foreach (PerformanceCounterSample sample in sampleSet.CounterSamples) { PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS(); res = ParsePath(sample.Path, ref pathElts); if (res != 0) { // Skipping for now, but should be a non-terminating error continue; } IntPtr counterHandle; res = PdhAddRelogCounter(_hQuery, sample.Path, (uint)sample.CounterType, sample.DefaultScale, sample.TimeBase, out counterHandle); if (res != 0) { // Skipping for now, but should be a non-terminating error continue; } CounterHandleNInstance chi = new CounterHandleNInstance(); chi.hCounter = counterHandle; if (pathElts.InstanceName != null) { chi.InstanceName = pathElts.InstanceName.ToLower(CultureInfo.InvariantCulture); } if (!_reloggerPathToHandleAndInstanceMap.ContainsKey(sample.Path.ToLower(CultureInfo.InvariantCulture))) { _reloggerPathToHandleAndInstanceMap.Add(sample.Path.ToLower(CultureInfo.InvariantCulture), chi); } } return (_reloggerPathToHandleAndInstanceMap.Keys.Count > 0) ? 0 : res; }
// // AddRelogCounters combines instances and adds counters to m_hQuery. // The counter handles and full paths // public uint AddRelogCounters(PerformanceCounterSampleSet sampleSet) { Debug.Assert(_hQuery != null && !_hQuery.IsInvalid); uint res = 0; Dictionary<string, List<PerformanceCounterSample>> prefixInstanceMap = new Dictionary<string, List<PerformanceCounterSample>>(); // // Go through all the samples one, constructing prefixInstanceMap and adding new counters as needed // foreach (PerformanceCounterSample sample in sampleSet.CounterSamples) { PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS(); res = ParsePath(sample.Path, ref pathElts); if (res != 0) { // Skipping for now, but should be a non-terminating error continue; } string lowerCaseMachine = pathElts.MachineName.ToLower(CultureInfo.InvariantCulture); string lowerCaseObject = pathElts.ObjectName.ToLower(CultureInfo.InvariantCulture); string lowerCaseCounter = pathElts.CounterName.ToLower(CultureInfo.InvariantCulture); string lcPathMinusInstance = @"\\" + lowerCaseMachine + @"\" + lowerCaseObject + @"\" + lowerCaseCounter; List<PerformanceCounterSample> sampleList; if (prefixInstanceMap.TryGetValue(lcPathMinusInstance, out sampleList)) { prefixInstanceMap[lcPathMinusInstance].Add(sample); } else { List<PerformanceCounterSample> newList = new List<PerformanceCounterSample>(); newList.Add(sample); prefixInstanceMap.Add(lcPathMinusInstance, newList); } //Console.WriteLine ("Added path " + sample.Path + " to the 1ist map with prefix " + lcPathMinusInstance); } // // Add counters to the query, consolidating multi-instance with a wildcard path, // and construct m_ReloggerPathToHandleAndInstanceMap where each full path would be pointing to its counter handle // and an instance name (might be empty for no-instance counter types). // You can have multiple full paths inside m_ReloggerPathToHandleAndInstanceMap pointing to the same handle. // foreach (string prefix in prefixInstanceMap.Keys) { IntPtr counterHandle; string unifiedPath = prefixInstanceMap[prefix][0].Path; if (prefixInstanceMap[prefix].Count > 1) { res = MakeAllInstancePath(prefixInstanceMap[prefix][0].Path, out unifiedPath); if (res != 0) { // Skipping for now, but should be a non-terminating error continue; } } res = PdhAddRelogCounter(_hQuery, unifiedPath, (UInt32)prefixInstanceMap[prefix][0].CounterType, prefixInstanceMap[prefix][0].DefaultScale, prefixInstanceMap[prefix][0].TimeBase, out counterHandle); if (res != 0) { // Skipping for now, but should be a non-terminating error // Console.WriteLine ("PdhAddCounter returned " + res + " for counter path " + unifiedPath); continue; } //Console.WriteLine ("added pdh query path:" + unifiedPath ); //now, add all actual paths to m_ReloggerPathToHandleAndInstanceMap foreach (PerformanceCounterSample sample in prefixInstanceMap[prefix]) { PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS(); res = ParsePath(sample.Path, ref pathElts); if (res != 0) { // Skipping for now, but should be a non-terminating error continue; } CounterHandleNInstance chi = new CounterHandleNInstance(); chi.hCounter = counterHandle; if (pathElts.InstanceName != null) { chi.InstanceName = pathElts.InstanceName.ToLower(CultureInfo.InvariantCulture); } if (!_reloggerPathToHandleAndInstanceMap.ContainsKey(sample.Path.ToLower(CultureInfo.InvariantCulture))) { _reloggerPathToHandleAndInstanceMap.Add(sample.Path.ToLower(CultureInfo.InvariantCulture), chi); //Console.WriteLine ("added map path:" + sample.Path ); } } } //TODO: verify that all counters are in the map return (_reloggerPathToHandleAndInstanceMap.Keys.Count > 0) ? 0 : res; }