windows 为性能计数器安装 Total 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1051890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing a Total instance for Performance Counters
提问by Alan McBee - MSFT
VS 2005, C# 2.0, .NET 2.0/3.0, Win2003
VS 2005、C# 2.0、.NET 2.0/3.0、Win2003
I'm trying to install a set of performance counters for a MultiInstance. I noticed that some system performance counter categories manage to keep a "total" alive even when there are no other instances. ASP.NET Apps 2.0.50727 is an example.
我正在尝试为 MultiInstance 安装一组性能计数器。我注意到一些系统性能计数器类别即使在没有其他实例的情况下也设法保持“总”存活。ASP.NET Apps 2.0.50727 就是一个例子。
So I've been trying to duplicate this. I created the following routine in an Installer class which I then add to a Custom Action in a setup project.
所以我一直试图复制这个。我在安装程序类中创建了以下例程,然后将其添加到安装项目中的自定义操作中。
public override void Install(System.Collections.IDictionary stateSaver)
{
//Debugger.Break();
CounterCreationData data = new CounterCreationData("ZCounter", "ZCtrHelp", PerformanceCounterType.NumberOfItems32);
PerformanceCounterCategory.Create("ZCategory", "ZCatHelp", PerformanceCounterCategoryType.MultiInstance, new CounterCreationDataCollection(new CounterCreationData[] { data }));
PerformanceCounter counter = new PerformanceCounter();
counter.CategoryName = "ZCategory";
counter.CounterName = "ZCounter";
counter.InstanceName = "ZTotal";
counter.InstanceLifetime = PerformanceCounterInstanceLifetime.Global;
counter.ReadOnly = false;
counter.RawValue = 0;
base.Install(stateSaver);
}
If I uncomment the Debugger.Break()
line, and step through, I can see the counter is actually created with the right instance name, and Visual Studio Server Explorer shows the instance along with the InstanceLifetime set to Global. I do not call the RemoveInstance() method in the setup program.
如果我取消注释该Debugger.Break()
行并逐步执行,我可以看到计数器实际上是使用正确的实例名称创建的,并且 Visual Studio Server Explorer 显示实例以及 InstanceLifetime 设置为 Global。我没有在安装程序中调用 RemoveInstance() 方法。
Nevertheless, a few seconds after the setup program completes, that instance disappears from the Performance Monitor and from the VS Server Explorer. How do I make it stick? Or can I?
然而,在安装程序完成几秒钟后,该实例就会从性能监视器和 VS 服务器资源管理器中消失。我怎么让它粘住?或者我可以吗?
回答by EWizard
Some code has to be actively maintaining the counter. In all the instances you can think of, such as ASP.Net, there is a service keeping the counter up.
某些代码必须主动维护计数器。在您能想到的所有实例中,例如 ASP.Net,都有一项服务可以保持计数器的上升。
As you aren't happy with having a _Total instance only active while some instance of your app is running, you have to write some code that will maintain the performance counteroutside of your application. There's no magic.
由于您对 _Total 实例仅在应用程序的某些实例运行时处于活动状态感到不满意,因此您必须编写一些代码来维护应用程序外部的性能计数器。没有魔法。
You can write a small service that does your monitoring needs. This will maintain the _Total counter. You need to decide on an update regimen. The easiest is to have every instance of your app update both instances (their own and _Total).
您可以编写一个满足您的监控需求的小型服务。这将维护 _Total 计数器。您需要决定更新方案。最简单的方法是让您的应用程序的每个实例更新这两个实例(它们自己的和 _Total)。
A bit of background on performance counters - the main thing to understand is that there is typically a shared memory region shared between processes that is updated via interlocked operations. A tool like PerfMon (or any other app) ends up connecting to the shared memory region to get the current values. So some process has to have that shared memory region opened and owned. Thus why you need code running. PerfMon is not creating the _Total instance for you (it does have some pretty convoluted combinations of counters allowing for averages and rates, but not a sum to create a summary instance).
关于性能计数器的一些背景知识 - 要了解的主要内容是通常有一个共享内存区域在进程之间共享,该区域通过互锁操作更新。像 PerfMon(或任何其他应用程序)这样的工具最终会连接到共享内存区域以获取当前值。因此,某些进程必须打开并拥有该共享内存区域。这就是为什么您需要运行代码的原因。PerfMon 不会为您创建 _Total 实例(它确实有一些非常复杂的计数器组合,允许平均值和速率,但不是创建汇总实例的总和)。
回答by CriGoT
AFAIK a Global Performance Counter category will remain alive as long as any process has an active handle referencing it. System counters achieve this by keeping a handle to the total counter instance in some service.
AFAIK 全局性能计数器类别将保持活动状态,只要任何进程具有引用它的活动句柄。系统计数器通过在某些服务中保留总计数器实例的句柄来实现这一点。
回答by Christian.K
Try to create the category with "PerformanceCounterCategoryType.SingleInstance".
尝试使用“PerformanceCounterCategoryType.SingleInstance”创建类别。
回答by Mike Fielden
Just a suggestion but try making
只是一个建议,但尝试制作
Counter.ReadOnly = true;
the first pass through and only setting it to False when you need to increment the counter.
第一次通过,只有在需要增加计数器时才将其设置为 False。
And also if you make the Counter Readonly you clearly need to comment out the line:
而且,如果您将计数器设为只读,您显然需要注释掉该行:
Counter.RawData = 0;
I believe its set to 0 by default the first pass anyhow.
我相信它在默认情况下设置为 0 无论如何第一次通过。
Hope this helps...
希望这可以帮助...