// // ProcessGetCounter() // Does the work to process GetCounterSet parameter set. // private void ProcessGetCounter() { // 1. Combine machine names with paths, if needed, to construct full paths // 2. Translate default paths into current locale // 3. Expand wildcards and validate the paths and write errors for any invalid paths // 4. OpenQuery/ AddCounters // 5. Skip the first reading CultureInfo culture = GetCurrentCulture(); List <Tuple <char, char> > characterReplacementList = null; List <string> paths = CombineMachinesAndCounterPaths(); uint res = 0; if (!_defaultCounters) { _cultureAndSpecialCharacterMap.TryGetValue(culture.Name, out characterReplacementList); } StringCollection allExpandedPaths = new StringCollection(); foreach (string path in paths) { string localizedPath = path; if (_defaultCounters) { res = _pdhHelper.TranslateLocalCounterPath(path, out localizedPath); if (res != 0) { string msg = string.Format(CultureInfo.CurrentCulture, _resourceMgr.GetString("CounterPathTranslationFailed"), res); Exception exc = new Exception(msg); WriteError(new ErrorRecord(exc, "CounterPathTranslationFailed", ErrorCategory.InvalidResult, null)); localizedPath = path; } } else if (characterReplacementList != null) { foreach (Tuple <char, char> pair in characterReplacementList) { localizedPath = localizedPath.Replace(pair.Item1, pair.Item2); } } StringCollection expandedPaths; res = _pdhHelper.ExpandWildCardPath(localizedPath, out expandedPaths); if (res != 0) { WriteDebug("Could not expand path " + localizedPath); ReportPdhError(res, false); continue; } foreach (string expandedPath in expandedPaths) { if (!_pdhHelper.IsPathValid(expandedPath)) { string msg = string.Format(CultureInfo.CurrentCulture, _resourceMgr.GetString("CounterPathIsInvalid"), localizedPath); Exception exc = new Exception(msg); WriteError(new ErrorRecord(exc, "CounterPathIsInvalid", ErrorCategory.InvalidResult, null)); continue; } allExpandedPaths.Add(expandedPath); } } if (allExpandedPaths.Count == 0) { return; } res = _pdhHelper.OpenQuery(); if (res != 0) { ReportPdhError(res, false); } res = _pdhHelper.AddCounters(ref allExpandedPaths, true); if (res != 0) { ReportPdhError(res, true); return; } PerformanceCounterSampleSet nextSet; bool bSkip = true; uint sampleReads = 0; if (Continuous.IsPresent) { _maxSamples = KEEP_ON_SAMPLING; } while (true) { // read the first set just to get the initial values res = _pdhHelper.ReadNextSet(out nextSet, bSkip); if (res == 0) { // Display valid data if (!bSkip) { WriteSampleSetObject(nextSet); sampleReads++; } // Don't need to skip anymore bSkip = false; } else if (res == PdhResults.PDH_NO_DATA || res == PdhResults.PDH_INVALID_DATA) { // The provider may not be running. // We should keep on trying - but skip the next valid reading. ReportPdhError(res, false); bSkip = true; // Count this failed attempt as a sample: sampleReads++; } else { // Unexpected error, return ReportPdhError(res, true); return; } if (_maxSamples != KEEP_ON_SAMPLING && sampleReads >= _maxSamples) { break; } #if CORECLR // CoreCLR has no overload of WaitOne with (interval, exitContext) bool cancelled = _cancelEventArrived.WaitOne((int)_sampleInterval * 1000); #else bool cancelled = _cancelEventArrived.WaitOne((int)_sampleInterval * 1000, true); #endif if (cancelled) { break; } } }
// // ProcessGetCounter() // Does the work to process GetCounterSet parameter set. // private void ProcessGetCounter() { // Validate StartTime-EndTime, if present if (_startTime != DateTime.MinValue || _endTime != DateTime.MaxValue) { if (_startTime >= _endTime) { string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterInvalidDateRange")); Exception exc = new Exception(msg); ThrowTerminatingError(new ErrorRecord(exc, "CounterInvalidDateRange", ErrorCategory.InvalidArgument, null)); return; } } uint res = _pdhHelper.ConnectToDataSource(_resolvedPaths); if (res != 0) { ReportPdhError(res, true); return; } StringCollection validPaths = new StringCollection(); if (_counter.Length > 0) { foreach (string path in _counter) { StringCollection expandedPaths; res = _pdhHelper.ExpandWildCardPath(path, out expandedPaths); if (res != 0) { WriteDebug(path); ReportPdhError(res, false); continue; } foreach (string expandedPath in expandedPaths) { if (!_pdhHelper.IsPathValid(expandedPath)) { string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterPathIsInvalid"), path); Exception exc = new Exception(msg); WriteError(new ErrorRecord(exc, "CounterPathIsInvalid", ErrorCategory.InvalidResult, null)); continue; } validPaths.Add(expandedPath); } } if (validPaths.Count == 0) { return; } } else { res = _pdhHelper.GetValidPathsFromFiles(ref validPaths); if (res != 0) { ReportPdhError(res, false); } } if (validPaths.Count == 0) { string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterPathsInFilesInvalid")); Exception exc = new Exception(msg); ThrowTerminatingError(new ErrorRecord(exc, "CounterPathsInFilesInvalid", ErrorCategory.InvalidResult, null)); } res = _pdhHelper.OpenQuery(); if (res != 0) { ReportPdhError(res, false); } if (_startTime != DateTime.MinValue || _endTime != DateTime.MaxValue) { res = _pdhHelper.SetQueryTimeRange(_startTime, _endTime); if (res != 0) { ReportPdhError(res, true); } } res = _pdhHelper.AddCounters(ref validPaths, true); if (res != 0) { ReportPdhError(res, true); } PerformanceCounterSampleSet nextSet; uint samplesRead = 0; while (!_stopping) { res = _pdhHelper.ReadNextSet(out nextSet, false); if (res == PdhResults.PDH_NO_MORE_DATA) { break; } if (res != 0 && res != PdhResults.PDH_INVALID_DATA) { ReportPdhError(res, false); continue; } // // Display data // WriteSampleSetObject(nextSet, (samplesRead == 0)); samplesRead++; if (_maxSamples != KEEP_ON_SAMPLING && samplesRead >= _maxSamples) { break; } } }