Reputation: 3119
I'm Completely Baffled(TM) by this one: On a Win7SP1, 64-bit machine, PerfMon
appears to be completely disavowing the knowledge of installed, custom performance counters. I'm working with an existing code base that is installing counters perfectly fine on production machines, but when I run it on my machine, when I run it with the counters I'd added, or if I run a completely contrived assembly (the meat of which is pasted below), I get seriously weird behavior.
It's probably easiest to describe using the following code snippet:
var category = "SuperTest";
var counterName = "Test Counter 1";
var shouldInstall = true;
if (PerformanceCounterCategory.Exists(category))
{
shouldInstall = false;
Console.WriteLine("{0} Category Exists. Overwrite? [n]", category);
var input = Console.ReadLine();
if (bool.TryParse(input, out shouldInstall))
{
PerformanceCounterCategory.Delete(category);
}
}
if (shouldInstall)
{
var col = new CounterCreationDataCollection();
col.Add(new CounterCreationData()
{
CounterName = counterName,
CounterType = PerformanceCounterType.NumberOfItems64
});
PerformanceCounterCategory.Create(category, "Test category.", PerformanceCounterCategoryType.SingleInstance, col);
// Magical voodoo line that may indicate my inexperience, but whose inclusion or
// exclusion does not affect discernibly affect behavior.
PerformanceCounter.CloseSharedResources();
}
// Multithreading setup, each thread repeats block below infinitely:
{
System.Threading.Thread.Sleep((new Random()).Next(100));
try
{
var counter = new PerformanceCounter(category, counterName, false));
c.Increment();
}
catch (Exception ex) { /* ... */ }
}
The first time it's run, the category doesn't exist, and it goes on creating the category and counter. I kill the process, then open PerfMon
. At this point, I can Add Counter
, see the category and counter, add it perfectly fine, and watch it sit at 0.000
. Perfect. At this point, if I close PerfMon
and reopen it? I can see all of the system performance counters just fine, but all of my custom ones--as aforementioned, the ones that work in productions, the ones I created based on those, and the contrived ones--are just gone.
The interesting thing is that if I run the code above, it will consistently tell me the group exists. Diving deeper, the counter even exists. This seems weird to me. Leaving it still in the disappeared state, and taking a cue from here, I can run: lodctr /R
, and they do come back.
So it looks like somehow I'm corrupting my own performance counter store. My question has two parts:
In my mind, this is somewhat disparate from other "performance counters gone" question, because they do exist, and I'm watching them disappear.
Upvotes: 2
Views: 4628
Reputation: 11
Make sure that you're not mixing x86 and x64.
I.e. if your perf counters were created using a x64 process, then make sure you're also reading them using a x64 process.
Additionally, try running as Admin.
Upvotes: 1
Reputation: 127
It is likely that the problem lies in the computer, and/or in its configuration, rather than in the code you've posted. I've encountered the same situation, and have failed to encounter why the counters disappear when opening performance monitor. However, I can help you with this:
Perfmon will sometimes disable performance counters by flagging it as disabled in the registry. The link might help you find why the counters are being disabled.
Monitoring counters can also be done with the tool typeperf. In my experience, typeperf has not disabled the same counters perfmon will, providing you with an alternative in monitoring your counters.
Upvotes: 2