Introduction
I want to introduce you to a package in nuget DevUtils ETW IMBA, which will help you simply and quickly create performance counters that support the new architecture (2.0) introduced in Windows Vista..
Background
In .NET Framework 4.5, classes in the space System.Diagnostics.PerformanceData
have been added, which allow you to create counters on this new architecture. But to use them, you must do the following:
- To describe counters in the XML manifest manually, these counters are logically grouped into sets of counters. The counter in the set defined by the numerical identifier, unique within the set of counters. The provider may define one or more sets of counters. Counter set is identified by a GUID unique to the provider.
- After recording a manifest, you need to compile it with the means CTRPP, which creates a .rc file. Next, create a compiled resource file (.res), which is added to the project.
- Sign-up counters in the computer by using the LodCtr.
This package does all of these steps for you.
Using
You need to simply describe their 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 data in these counters:
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 build a project.
After build, you will have two files in "bin\Debug\":
- <project name>.IM.xml: This is the manifest itself, automatically generated.
- <project name>.IM.dll: This is the file that contains only resources. These are the names of counters, set names, line description, etc.
Also, if you have a VS with administrator rights, your counter will be registered in the system and you can immediately see their results.
Note: You can disable automatic registration by inserting section into the project file.
<PropertyGroup>
<IMBASkipInstallManifest>true</IMBASkipInstallManifest>
</PropertyGroup>
History