static float[] ExtractSamples(int categoryIndex, string counter, InstanceDataCollectionCollection[][] samples, string instance) { float[] result = new float[samples.Length - 1]; CounterSample oldSample = CounterSample.Empty; for (int i = 0; i < samples.Length; i++) { InstanceDataCollectionCollection idcc = samples[i][categoryIndex]; if (idcc == null) { return(null); } InstanceDataCollection idc = idcc[counter]; if (idc == null) { return(null); } InstanceData id = idc[instance]; if (id == null) { id = idc[TruncatedAppName(instance)]; if (id == null) { return(null); } } CounterSample sample = id.Sample; if (i > 0) { result[i - 1] = Calculate(oldSample, sample); } oldSample = sample; } return(result); }
//<snippet3> //<snippet2> // Display the contents of an InstanceDataCollection. public static void ProcessInstanceDataCollection(InstanceDataCollection idCol) { //</snippet2> ICollection idColKeys = idCol.Keys; string[] idColKeysArray = new string[idColKeys.Count]; idColKeys.CopyTo(idColKeysArray, 0); ICollection idColValues = idCol.Values; InstanceData[] idColValuesArray = new InstanceData[idColValues.Count]; idColValues.CopyTo(idColValuesArray, 0); //<snippet6> Console.WriteLine(" InstanceDataCollection for \"{0}\" " + "has {1} elements.", idCol.CounterName, idCol.Count); //</snippet6> // Display the InstanceDataCollection Keys and Values. // The Keys and Values collections have the same number of elements. int index; for (index = 0; index < idColKeysArray.Length; index++) { Console.WriteLine(" Next InstanceDataCollection " + "Key is \"{0}\"", idColKeysArray[index]); ProcessInstanceDataObject(idColValuesArray[index]); } //<snippet7> }
public static void InstanceDataCollection_NullTest() { InstanceDataCollection idc = GetInstanceDataCollection(); Assert.Throws <ArgumentNullException>(() => idc[null]); Assert.Throws <ArgumentNullException>(() => idc.Contains(null)); }
public Dictionary <string, float> Refresh(float minValue) { InstanceDataCollectionCollection allData = TryGetPerformanceData(() => this.category.ReadCategory(), out Exception ex); Dictionary <string, float> result = new Dictionary <string, float>(StringComparer.CurrentCultureIgnoreCase); if (allData != null) { InstanceDataCollection processData = allData["% Processor Time"]; if (processData != null) { Dictionary <string, CounterSample> currentSamples = new Dictionary <string, CounterSample>(); foreach (InstanceData data in processData.Values) { string instanceName = data.InstanceName; CounterSample currentSample = data.Sample; currentSamples[instanceName] = currentSample; if (this.previousSamples != null && this.previousSamples.TryGetValue(instanceName, out CounterSample previousSample)) { // We have to average the usage across all the logical processors. // On an 8 processor machine, the counter can return up to 800! float value = CounterSampleCalculator.ComputeCounterValue(previousSample, currentSample) / ProcessorCount; result[data.InstanceName] = value >= minValue ? value : float.NaN; } } this.previousSamples = currentSamples; } } return(result); }
/// <summary> /// <para> /// Initialize a new instance of the <see cref="ConnectionStringCollectionNode"/> class with a <see cref="InstanceDataCollection"/> object. /// </para> /// </summary> /// <param name="instanceDataCollection"> /// <para>A reference to the <see cref="InstanceDataCollection"/> runtime configuration.</para> /// </param> public InstanceCollectionNode(InstanceDataCollection instanceDataCollection) : base() { if (instanceDataCollection == null) { throw new ArgumentNullException("instanceDataCollection"); } this.instanceDataCollection = instanceDataCollection; }
private static string FindProcessInstance(int pid, IEnumerable <string> instances, string categoryName, string counterName) { Tuple <DateTime, PerformanceCounterCategory, InstanceDataCollectionCollection> cached; DateTime utcNow = DateTime.UtcNow; InstanceDataCollectionCollection result = null; PerformanceCounterCategory category = null; if (cache.TryGetValue(categoryName, out cached)) { category = cached.Item2; if (cached.Item1 < utcNow) { result = cached.Item3; } } if (result == null) { if (category == null) { category = new PerformanceCounterCategory(categoryName); } result = category.ReadCategory(); cache.TryAdd(categoryName, new Tuple <DateTime, PerformanceCounterCategory, InstanceDataCollectionCollection>(utcNow.AddMinutes(1), category, result)); } InstanceDataCollection counters = result[counterName]; if (counters != null) { foreach (string i in instances) { InstanceData instance = counters[i]; if ((instance != null) && (pid == instance.RawValue)) { return(i); } } } return(null); }
//<snippet4> // Display the contents of an InstanceDataCollection. public static void ProcessInstanceDataCollection(InstanceDataCollection idCol) { InstanceData[] instDataArray = new InstanceData[idCol.Count]; Console.WriteLine(" InstanceDataCollection for \"{0}\" " + "has {1} elements.", idCol.CounterName, idCol.Count); // Copy and process the InstanceData array. idCol.CopyTo(instDataArray, 0); int idX; for (idX = 0; idX < instDataArray.Length; idX++) { ProcessInstanceDataObject(instDataArray[idX].InstanceName, instDataArray[idX].Sample); } }
private void GetInstanceCollectionData() { InstanceCollectionNode node = Hierarchy.FindNodeByType(typeof(InstanceCollectionNode)) as InstanceCollectionNode; if (node == null) { return; } InstanceDataCollection data = node.InstanceDataCollection; if (Object.ReferenceEquals(dynamicQuerySettings.Instances, data)) { return; } dynamicQuerySettings.Instances.Clear(); foreach (InstanceData instanceData in data) { dynamicQuerySettings.Instances[instanceData.Name] = instanceData; } }
public void Service(Logging.IBasicLogger logger, InstanceDataCollectionCollection categorySample, bool rethrow = false) { if (!isUsable) { return; } try { InstanceDataCollection instanceDataCollection = categorySample[pcs.CounterName]; InstanceData instanceData = instanceDataCollection[pcs.InstanceName.MapNullToEmpty()]; // use empty string for counters that only have one value CounterSample counterSample = instanceData.Sample; if (!useRawPerfCtrValue) { gpInfo.VC = new ValueContainer(CounterSample.Calculate(lastCounterSample, counterSample)); } else { gpInfo.VC = new ValueContainer(counterSample.RawValue); } lastCounterSample = counterSample; logExceptionElevationHoldoffTimer.StopIfNeeded(); } catch (System.Exception ex) { bool useHighLevelMesg = (logExceptionElevationHoldoffTimer.IsTriggered || !logExceptionElevationHoldoffTimer.Started); logExceptionElevationHoldoffTimer.StartIfNeeded(); Logging.IMesgEmitter emitter = (logger == null ? Logging.NullEmitter : (useHighLevelMesg ? logger.Info : logger.Trace)); emitter.Emit("{0} for '{1}' generated exception: {2}", CurrentMethodName, gpInfo.Name, ex.ToString(ExceptionFormat.TypeAndMessage)); } }
//</snippet7> public static void Main() { string catNumStr; int categoryNum; PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories(); // Create and sort an array of category names. string[] categoryNames = new string[categories.Length]; int catX; for (catX = 0; catX < categories.Length; catX++) { categoryNames[catX] = categories[catX].CategoryName; } Array.Sort(categoryNames); Console.WriteLine("These categories are registered on this computer:"); for (catX = 0; catX < categories.Length; catX++) { Console.WriteLine("{0,4} - {1}", catX + 1, categoryNames[catX]); } // Ask the user to choose a category. Console.Write("Enter the category number from the above list: "); catNumStr = Console.ReadLine(); // Validate the entered category number. try { categoryNum = int.Parse(catNumStr); if (categoryNum < 1 || categoryNum > categories.Length) { throw new Exception(String.Format("The category number must be in the " + "range 1..{0}.", categories.Length)); } categoryName = categoryNames[(categoryNum - 1)]; } catch (Exception ex) { Console.WriteLine("\"{0}\" is not a valid category number." + "\r\n{1}", catNumStr, ex.Message); return; } //<snippet5> //<snippet6> // Process the InstanceDataCollectionCollection for this category. PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName); InstanceDataCollectionCollection idColCol = pcc.ReadCategory(); InstanceDataCollection[] idColArray = new InstanceDataCollection[idColCol.Count]; Console.WriteLine("InstanceDataCollectionCollection for \"{0}\" " + "has {1} elements.", categoryName, idColCol.Count); //</snippet6> // Copy and process the InstanceDataCollection array. idColCol.CopyTo(idColArray, 0); foreach (InstanceDataCollection idCol in idColArray) { ProcessInstanceDataCollection(idCol); } //</snippet5> }
public static void Main() { string catNumStr; int categoryNum; PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories(); Console.WriteLine("These categories are registered on this computer:"); int catX; for (catX = 0; catX < categories.Length; catX++) { Console.WriteLine("{0,4} - {1}", catX + 1, categories[catX].CategoryName); } // Ask the user to choose a category. Console.Write("Enter the category number from the above list: "); catNumStr = Console.ReadLine(); // Validate the entered category number. try { categoryNum = int.Parse(catNumStr); if (categoryNum < 1 || categoryNum > categories.Length) { throw new Exception(String.Format("The category number must be in the " + "range 1..{0}.", categories.Length)); } categoryName = categories[(categoryNum - 1)].CategoryName; } catch (Exception ex) { Console.WriteLine("\"{0}\" is not a valid category number." + "\r\n{1}", catNumStr, ex.Message); return; } // Process the InstanceDataCollectionCollection for this category. PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName); InstanceDataCollectionCollection idColCol = pcc.ReadCategory(); ICollection idColColKeys = idColCol.Keys; string[] idCCKeysArray = new string[idColColKeys.Count]; idColColKeys.CopyTo(idCCKeysArray, 0); ICollection idColColValues = idColCol.Values; InstanceDataCollection[] idCCValuesArray = new InstanceDataCollection[idColColValues.Count]; idColColValues.CopyTo(idCCValuesArray, 0); Console.WriteLine("InstanceDataCollectionCollection for \"{0}\" " + "has {1} elements.", categoryName, idColCol.Count); // Display the InstanceDataCollectionCollection Keys and Values. // The Keys and Values collections have the same number of elements. int index; for (index = 0; index < idCCKeysArray.Length; index++) { Console.WriteLine(" Next InstanceDataCollectionCollection " + "Key is \"{0}\"", idCCKeysArray[index]); ProcessInstanceDataCollection(idCCValuesArray[index]); } }
public void SetUp() { instances = new InstanceDataCollection(); }
public void CopyTo(InstanceDataCollection[] counters, int index) {}
//<snippet2> public static void Main(string[] args) { // The following values can be used as arguments. string categoryName = "Process"; string counterName = "Private Bytes"; InstanceDataCollectionCollection idColCol; // Copy the supplied arguments into the local variables. try { categoryName = args[0]; counterName = args[1]; } catch { // Ignore the exception from non-supplied arguments. } try { // Get the InstanceDataCollectionCollection for this category. PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName); idColCol = pcc.ReadCategory(); } catch (Exception ex) { Console.WriteLine("An error occurred getting the InstanceDataCollection for " + "category \"{0}\"." + "\n" + ex.Message, categoryName); return; } //<snippet3> // Check if this counter name exists using the Contains // method of the InstanceDataCollectionCollection. if (!idColCol.Contains(counterName)) //</snippet3> { Console.WriteLine("Counter \"{0}\" does not exist in category \"{1}\".", counterName, categoryName); return; } else { //<snippet4> // Now get the counter's InstanceDataCollection object using the // indexer (Item property) for the InstanceDataCollectionCollection. InstanceDataCollection countData = idColCol[counterName]; //</snippet4> ICollection idColKeys = countData.Keys; string[] idColKeysArray = new string[idColKeys.Count]; idColKeys.CopyTo(idColKeysArray, 0); Console.WriteLine("Counter \"{0}\" of category \"{1}\" " + "has {2} instances.", counterName, categoryName, idColKeys.Count); // Display the instance names for this counter. int index; for (index = 0; index < idColKeysArray.Length; index++) { Console.WriteLine("{0,4} -- {1}", index + 1, idColKeysArray[index]); } } }