using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace HeuristicLab.Services.Optimization.Scaler { public static class PerformanceCounterHelper { public const string HiveCounterCategory = "Hive"; public const string HiveCounterGlobalUsageName = "% Global Usage"; public const string HiveCounterGlobalUsage = @"\" + HiveCounterCategory + @"\" + HiveCounterGlobalUsageName; public class PerformanceCounterEntry { public string Name { get; set; } public string HelpText { get; set; } public PerformanceCounterType Type { get; set; } } private static void InitializePerformanceCounters(string category, string categoryHelpText, params PerformanceCounterEntry[] entries) { if (PerformanceCounterCategory.Exists(category)) PerformanceCounterCategory.Delete(category); var counterCollection = new CounterCreationDataCollection(); foreach (var entry in entries) { var creationData = new CounterCreationData() { CounterName = entry.Name, CounterHelp = entry.HelpText, CounterType = entry.Type }; counterCollection.Add(creationData); } PerformanceCounterCategory.Create( category, categoryHelpText, PerformanceCounterCategoryType.SingleInstance, counterCollection); Trace.WriteLine(string.Format("Category {0} created.", category)); } public static void InitializePerformanceCounters() { InitializePerformanceCounters(HiveCounterCategory, "Hive performance counters", new PerformanceCounterEntry() { Name = HiveCounterGlobalUsageName, Type = PerformanceCounterType.NumberOfItems32, HelpText = "The current usage in % over all hive slaves in the system" } ); } private static PerformanceCounter hiveGlovalUsage; public static PerformanceCounter HiveGlobalUsage { get { if (hiveGlovalUsage == null) { hiveGlovalUsage = new PerformanceCounter( PerformanceCounterHelper.HiveCounterCategory, PerformanceCounterHelper.HiveCounterGlobalUsageName, string.Empty, false); } return hiveGlovalUsage; } } } }