性能计数器创建,但无法正常工作,直到Windows重新启动重新启动、无法正常、计数器、性能

2023-09-04 02:10:31 作者:南笙

我有一个奇怪的问题产生在现有组新的计数器。我有一个窗口服务,其中做了一些工作,让我通过监控性能计数器的状态。我有性能计数器组和一些性能计数器。集团的服务过程中创建的安装阶段(具有管理员权限),并且计数器初始化时,服务启动(如本地系统用户)。一切工作正常,组存在,计数器也存在,我可以监视他们,并写入性能计数器日志。服务不断运行。

然后我想添加一些更多的性能计数器。我加入他们的服务code,重建并通过卸载previous服务实例部署到服务器(性能计数器组被卸载code删除),并重新安装服务(性能计数器组安装阶段期间创建与所有新的计数器)。然后,我开始服务。

这是故事的共同,无趣部分的末端。奇怪的事情从这里开始。

我去PerMon,添加所有计数器系统监控视图。我可以看到我的性能计数器组,我看到所有性能计数器包括我刚刚添加了新的性能计数器。我可以将它们添加到系统监控视图。而我所看到的,旧的计数器工作。但是,新的计数器不工作,他们不收集任何数据。好吧,好吧,也许是我的错,我切换到登录的视图,并尝试登录性能计数器数据。当他们登录之前旧的计数器记录。但是,当我尝试添加新的计数器,我发现在事件查看器以下警告:

  

该服务无法添加计数器\ AGENT \ MyCountersGroupName \ MyNewCounter'到NewCountersLog日志或警报。此日志或警报将继续,但该计数器的数据将不会被收集。返回的错误是:指定的柜台找不到

我试图重新安装服务,删除旧的计数器,再添加它们,并没有什么改变。旧的计数器工作,和新的计数器不工作。然后我重新启动Windows和新的计数器开始工作!没有服务改变了,我刚刚重新启动服务器。我遇到过的2个服务器这个问题,无论是运行在Windows Server 2003 SP1上运行。在code所有性能计数器都是相同的,因为我用code泛型创建。

您可以说:嘿,不要怕麻烦,当你需要添加新的性能计数器,重新启动您的Windows每一次,但我不能。我的服务在服务器上运行以及其他服务,我们需要这些服务的工作不断,我们无法重新启动服务器每次我改变一个服务。

任何人都可以帮助解决这个问题?

更新: 看起来有类似的问题:http://stackoverflow.com/questions/2180005/adding-performance-counters-to-existing-category

更新 我不认为这个问题是在我的code,但如果它帮助我在这里张贴。

code安装和卸载(期间调用安装阶段): PerformanceCountersManagerBase.GetCreationData()是一个通用的包装获得收集数据创建的性能计数器。的

 公共静态无效的安装()
{
    CounterCreationDataCollection专柜=新CounterCreationDataCollection(
    PerformanceCountersManagerBase.GetCreationData&其中; TManager>()的ToArray())。

    如果(PerformanceCounterCategory.Exists(PerformanceCountersManagerBase.GroupName))
        PerformanceCounterCategory.Delete(PerformanceCountersManagerBase。

    PerformanceCounterCategory.Create(PerformanceCountersManagerBase.GroupName,
        集团介绍,
        PerformanceCounterCategoryType.SingleInstance,柜);
}

公共静态无效的卸载()
{
    如果(!PerformanceCounterCategory.Exists(PerformanceCountersManagerBase.GroupName))
        返回;

    PerformanceCounterCategory.Delete(PerformanceCountersManagerBase.GroupName);
}
 

code初始化服务和更新计数器:

 内部空隙初始化(字符串名称,字符串组名)
{
    _counter =新的PerformanceCounter(组名,姓名,假);
    _counter.RawValue = 0;
}

内部空隙增量()
{
    如果((_counter == NULL)|| _counter.ReadOnly)
        返回;

    锁(_counter)
    {
        _counter.Increment();
    }
}
 
电脑开机时显示 插入windows安装光盘并重新启动计算机 怎么解决 急救

更新3 我改变code通过.NET组件PerformanceCounterInstaller安装计数器,并没有什么改变。因为他们的工作老柜台工作,新创建的不工作,尝试登录他们的结果事件日志中确切的错误(警告)消息中。安装程序创建code是如下:

 公共静态PerformanceCounterInstaller GetInstaller()
{
    PerformanceCounterGroupAttribute组
        = PerformanceCountersManagerBase.ExtractGroupSettings(typeof运算(TManager));

    PerformanceCounterInstaller安装=新PerformanceCounterInstaller();
    installer.CategoryName = group.Name;
    installer.CategoryHelp = group.Description;
    installer.CategoryType = PerformanceCounterCategoryType.SingleInstance;
    installer.UninstallAction = UninstallAction.Remove;
    installer.Counters.AddRange(PerformanceCountersManagerBase.GetCreationData<TManager>().ToArray());

    返回安装程序;
}
 

解决方案

PERF的柜台都是基于共享内存段。我已经看到了类似的问题时,有人仍然有一个处理共享内存段。这可以包括性能监视器工具本身。您可以使用手柄工具,看看是谁在持有到共享内存。 你不应该需要重新启动,以便尝试,看看这个问题消失了,你先关闭性能监视器,然后重新创建的计数器。另外,还要确保你的服务没有运行。

I have a strange problem creating new counters in existing group. I have a windows service, which does some work and allow me to monitor its state via performance counter. I have performance counters group and some performance counters. Group is created during service install phase (with administrator privileges), and counters are initialized when service starts (as LocalSystem user). Everything works fine, group exists, counter exist too, I can monitor them and write to Performance counters log. Service is constantly running.

Then I want to add some more performance counters. I add them in service code, rebuild and deploy to server by uninstalling previous service instance (performance counters group is deleted by uninstall code), and installing service again (performance counters group is created during install phase with all new counters). Then I start service.

That's the end of common, uninteresting part of the story. The strange things start here.

I go to PerMon, add all counters to System monitor view. I can see my performance counters group, I see all performance counters including NEW performance counters that I've just added. I can add them to System monitor view. And I can see, that old counters are working. But new counters doesn't work, they don't collect any data. Well, ok, perhaps there was my mistake, I switch to log view and try to log performance counters data. Old counters are logged as they logged before. But when I try to add new counter, I find in Event Viewer following warning:

The service was unable to add the counter '\AGENT\MyCountersGroupName\MyNewCounter' to the NewCountersLog log or alert. This log or alert will continue, but data for that counter will not be collected. The error returned is: The specified counter could not be found.

I tried to reinstall service, remove old counters, add them again, and nothing changed. Old counters are working, and new counters don't work. Then I restart Windows and new counters started working! Nothing changed in service, I've just restarted the server. I encountered this problem on 2 servers, the both are running under Windows Server 2003 SP1. The code for all performance counters is identical, 'cause I create them using code with generics.

You can say "hey, don't bother, restart your Windows every time when you need to add new performance counters", but I can't. My service is running on server along with other services, and we need these services work constantly, we can't restart server every time I change one service.

Can anybody help with this issue?

Update: Looks like there's the similar problem: http://stackoverflow.com/questions/2180005/adding-performance-counters-to-existing-category

Update I don't think that the problem is in my code, but if it helps I post it here.

Code to install and uninstall (called during install phase): PerformanceCountersManagerBase.GetCreationData() is a generic wrapper for obtaining collection of data to create performance counters.

public static void Install()
{
    CounterCreationDataCollection counters = new CounterCreationDataCollection(
    PerformanceCountersManagerBase.GetCreationData<TManager>().ToArray());

    if (PerformanceCounterCategory.Exists(PerformanceCountersManagerBase.GroupName))
        PerformanceCounterCategory.Delete(PerformanceCountersManagerBase.

    PerformanceCounterCategory.Create(PerformanceCountersManagerBase.GroupName, 
        "Group description",
        PerformanceCounterCategoryType.SingleInstance, counters);
}

public static void Uninstall()
{
    if (!PerformanceCounterCategory.Exists(PerformanceCountersManagerBase.GroupName))
        return;

    PerformanceCounterCategory.Delete(PerformanceCountersManagerBase.GroupName);
}

Code to initialize counters in service and update:

internal void Initialize(string name, string groupName)
{
    _counter = new PerformanceCounter(groupName, name, false);
    _counter.RawValue = 0;
}

internal void Increment()
{
    if ((_counter == null) || _counter.ReadOnly)
        return;

    lock (_counter)
    {
        _counter.Increment();
    }
}

Update 3 I changed code to install counters via .NET component PerformanceCounterInstaller, and nothing changed. Old counters work as they worked, and newly created don't work and attempt to log them results in the exact error (warning) message in event log. The installer creation code is as follows:

public static PerformanceCounterInstaller GetInstaller()
{
    PerformanceCounterGroupAttribute group
        = PerformanceCountersManagerBase.ExtractGroupSettings(typeof(TManager));

    PerformanceCounterInstaller installer = new PerformanceCounterInstaller();
    installer.CategoryName = group.Name;
    installer.CategoryHelp = group.Description;
    installer.CategoryType = PerformanceCounterCategoryType.SingleInstance;
    installer.UninstallAction = UninstallAction.Remove;
    installer.Counters.AddRange(PerformanceCountersManagerBase.GetCreationData<TManager>().ToArray());

    return installer;
}

解决方案

Perf counters are based on shared memory segments. I have seen similar problems when someone still has a handle to the shared memory segment. This can include the perfmon tool itself. You may be able to use the handle utility to see who is holding onto the shared memory. You shouldn't need to reboot so try to see if this problem goes away of you shut down perfmon first and then re-create the counters. Also make sure that your service is not running.