📜 ⬆️ ⬇️

To help create a supplier of performance counters v 2.0 in NET. Framework

Hello, friends!
I want to introduce you to the package in nuget DevUtils ETW IMBA , which will help you to quickly and easily create performance counters that support the new architecture (version 2.0) introduced in Windows Vista.

Classes in the System.Diagnostics.PerformanceData space have been added to the .NET Framework 4.5, which allow you to create counters on this new architecture, but to use them you must:
  1. To describe the counters in the XML manifest manually, these counters are logically grouped into sets of counters. A counter in a set is defined by a numeric identifier unique within a given set of counters. For a supplier, one or more sets of counters can be defined. A set of counters is identified by a GUID unique to this provider.
  2. After the manifest is written, it is necessary to compile it using the CTRPP tool , which creates a .rc file, then creates a compiled resource file (.res), which is added to the project.
  3. Register counters on your computer using the LodCtr tool


This tool does all these steps for you. You just need to describe your counters in the code:

[CounterSource(Guid = "{ab8e1320-965a-4cf9-9c07-fe25378c2a23}")] sealed class MyCounterSource { #region MyLogicalDiskSet [CounterSet(CounterSetInstances.Multiple, Name = "My LogicalDisk", Description = "This is a sample counter set with multiple instances.")] public enum MyLogicalDiskSet { [Counter(CounterType.PerfCounterRawcount, DefaultScale = 1, Name = "My Free Megabytes", Description = "First sample counter.", DetailLevel = CounterDetailLevel.Standard)] MyFreeMegabytes = 1, [CounterAttributeReference] [CounterAttributeDisplayAsReal] [Counter(CounterType.PerfAverageTimer, DefaultScale = 1, BaseId = (int)MyAvgDiskTransfer, Name = "My Avg. Disk sec/Transfer", Description = "Second sample counter.", DetailLevel = CounterDetailLevel.Advanced)] MyAvgDiskSec, [CounterAttributeNoDisplay] [Counter(CounterType.PerfAverageBase, DetailLevel = CounterDetailLevel.Advanced)] MyAvgDiskTransfer, } #endregion #region MySystemObjectsSet [CounterSet(CounterSetInstances.Single, Name = "My System Objects", Description = "My System Objects Help.")] public enum MySystemObjectsSet { [CounterAttributeDisplayAsHex] [CounterAttributeNoDigitGrouping] [Counter(CounterType.PerfCounterRawcount, DefaultScale = 1, Name = "Process Count", Description = "Process Count Help.")] ProcessCount = 1, [Counter(CounterType.PerfCounterRawcount, Name = "Thread Count", Description = "Thread Count Help.")] ThreadCount, [Counter(CounterType.PerfElapsedTime, DefaultScale = 1, PerfTimeId = (int)SystemTime, PerfFreqId = (int)SystemFreq, Name = "System Elapsed Time", Description = "System Elapsed Time Help.", DetailLevel = CounterDetailLevel.Advanced)] SystemElapsedTime, [CounterAttributeNoDisplay] [Counter(CounterType.PerfCounterLargeRawcount)] SystemTime, [CounterAttributeNoDisplay] [Counter(CounterType.PerfCounterLargeRawcount)] SystemFreq } #endregion } 

')
And provide these counters with data:

 public static void Test() { using (var diskSet = new CounterSet<MyLogicalDiskSet>()) using (var objectsSet = new CounterSet<MySystemObjectsSet>()) using (var diskSetInst = diskSet.CreateInstance("Default")) using (var objectsSetInst = objectsSet.CreateInstance("Default")) { var processCount = objectsSetInst[MySystemObjectsSet.ProcessCount]; var myAvgDiskSec = diskSetInst[MyLogicalDiskSet.MyAvgDiskSec]; var myAvgDiskTransfer = diskSetInst[MyLogicalDiskSet.MyAvgDiskTransfer]; processCount.Value = 2; for (var i = 0; i < 10; ++i) { var beginTicks = Stopwatch.GetTimestamp(); //  -  Thread.Sleep(1000); var endTicks = Stopwatch.GetTimestamp(); myAvgDiskSec.IncrementBy(endTicks - beginTicks); myAvgDiskTransfer.IncrementBy(1); } } } 


And run the program.

After compilation two files will be created in bin \ Debug \:

  1. <project name> .IM.xml This is the manifest itself, generated automatically.
  2. <project name> .IM.dll This is a file that contains only resources. These are counter names, set names, description lines, etc.


Also, if your studio has administrator rights, your counters will be registered in the system and you can immediately see their results.

The auto-registration feature can be disabled by inserting into the project file.

 <PropertyGroup> <IMBASkipInstallManifest>true</IMBASkipInstallManifest> </PropertyGroup> 


Here are a few words that I wanted to write for a quick start using this package. I will be glad to answer all your questions.

Source: https://habr.com/ru/post/232673/


All Articles