Windows性能计数器从性能监视器消失性能、监视器、计数器、Windows

2023-09-03 06:19:31 作者:痴心错付

我完全莫名其妙(TM)本之一:在Win7SP1,64位机,性能监视器似乎完全disavowing安装,自定义性能知识计数器。我的工作与正在安装计数器完全正常生产的机器现有的code基,但是当我在我的机器上运行它,当我用计数器运行它,我会补充说,如果我运行一个完全人为的组件(肉将在下面粘贴),我得到认真怪异的行为。

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.

这可能是最简单的描述使用以下code片断:

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) { /* ... */ }
}

第一次它的运行,该类别不存在,并且它继续创建类别和计数器。我杀死进程,然后打开性能监视器。在这一点上,我可以添加计数器,看到的类别和计数器,加上它完美的罚款,并看着它坐在 0.000 。完善。在这一点上,如果我关闭性能监视器并重新打开它?我可以看到所有的系统性能计数器就好了,但我所有的自定义的 - 如前面提到的,那些工作在生产,那些我创建基于这些,和的做作全1 -are只是走了。

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.

有趣的是,如果我运行上面的code,它会一直告诉我的组存在。潜水更深,计数器的存在。这似乎怪我。离开它仍处于消失状态,并采取了线索,从here,我可以运行: LODCTR / R ,和他们的执行的回来

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:

这是我在做什么(破坏性能计数器店)? 因为它是可重复的,有什么突出的,在code或我的过程中,我做创造这种行为?

在我看来,这是问题的其他走了性能计数器有所不同,因为它们确实存在,而我看着他们消失了。

In my mind, this is somewhat disparate from other "performance counters gone" question, because they do exist, and I'm watching them disappear.

推荐答案

这可能是问题出在计算机,和/或它的配置,而不​​是在code您发布。我遇到了同样的情况,并没有遇到为什么计数器打开性能监视器时消失。不过,我可以帮你这一点:

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.

监控计数器,也可以使用该工具的 typeperf 。根据我的经验,typeperf没有禁用同一柜台PERFMON会,为您提供监测你的柜台替代。

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.