Strange Performance Counter performance problem in Windows Service application

T

Tomasz Jastrzebski

Hello All,

I experience a strange problem I do not how to approach.
My windows service application uses several performance counter.
Before when staring the service deletes category and attempts to recreate
it.

*If* the category previously existed and has been deleted prior to
recreation an attempt to create a counter causes Service Manager timeout
after 30 sec. So, category gets deleted, but it can not be recreated.

I modified my service so it deletes category upon termination, but then it
fails during the restart while checking if the category exists.
It is completely weird.

Now, the really strange part: the same code executed as a console
application with *the same security credentials* executes momentarily.

Sample code attached below. The code uses .Net 2.0 methods but the same
issue can be seen in .Net 1.1.

Did anyone experience a similar issue? Is there any known solution?

Thank you,

Tomasz


Const ThisServiceName As String = "My Service"

Trace.WriteLine("Checking performance counters category.")
If PerformanceCounterCategory.Exists(ThisServiceName) Then
Trace.WriteLine("Deleting performance counters.")
PerformanceCounterCategory.Delete(ThisServiceName)
Trace.WriteLine("Performance counters deleted.")
End If

WriteToLog("Creating performance counters.")
PerformanceCounterCategory.Create(ThisServiceName, "",
PerformanceCounterCategoryType.MultiInstance, "test counter", "")
WriteToLog("Performance counters created.")
 
K

Kevin Yu [MSFT]

Hi Tomasz,

I tried the same code on my machine in a windows service, the service works
fine. Could you let me know under which account as the service running?
Currently, I'm testing with Local System account. Also If you try to create
a new windows service and put these code in OnStart method. Does the new
service start successfully? (Please remove the log part to mitigate the
affect of logging.)

Const ThisServiceName As String = "My Service"

Trace.WriteLine("Checking performance counters category.")
If PerformanceCounterCategory.Exists(ThisServiceName) Then
Trace.WriteLine("Deleting performance counters.")
PerformanceCounterCategory.Delete(ThisServiceName)
Trace.WriteLine("Performance counters deleted.")
End If

PerformanceCounterCategory.Create(ThisServiceName, "",
PerformanceCounterCategoryType.MultiInstance, "test counter", "")

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
D

David Levine

I had trouble with similar code on some server OS's . It was not consistent
in that different installations on different computers with the same OS
would behave differently. I don't think I was able to delete the counters,
let alone add new ones, but it's been years since it occurred and my memory
is hazy on some details.

The problem was that on some OS's the security settings on some registry
keys prevented any app from modifying the registry key. I had to modify the
ACL attached to the registry key before I could add performance counters.

The registry keys were at HKLM\Software\Microsoft\Windows
NT\CurrentVersion\Perflib\009. The values Counter and Help get modified
whenever a performance counter is added/removed from the system, and the
application must have Write access to these keys.You should also check the
values in the key immediately above this one, at
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Perflib.

I never did find a workaround, I had to modify the ACL attached to the
registry key before I could write to it. I believe these settings were part
of some security lockdown.
 
T

Tomasz Jastrzebski

Hi David,

I do not think this is the problem.
I have forgotten to say that on every other attempt the service starts and
counters get created.
Under certain conditions it just takes forever to crate them.
There are *no errors* being thrown and it is quite inconsistent.
Also, I am part of local "Performance Log Users" and "Performance Monitor
Users" groups.

Thank you,

Tomasz
 
T

Tomasz Jastrzebski

Hi Kevin,

You helped me to find a solution!
I have been trying to create counters within the Main() method.
The code, when executed in the OnStart() works just fine!
The remaining question is WHY???

Thank you,

Tomasz
 
K

Kevin Yu [MSFT]

Hi Tomasz,

I tried the same code in the Main() method, and it's working fine. Did you
put the code after System.ServiceProcess.ServiceBase.Run(ServicesToRun)? If
true, it might be the case that your code hangs. We should put it before
the service itself starts. My code that is working fine looks like the
follwing.

' The main entry point for the process
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New
Service1, New MySecondUserService}
'
Const ThisServiceName As String = "My Service"

Trace.WriteLine("Checking performance counters category.")
If PerformanceCounterCategory.Exists(ThisServiceName) Then
Trace.WriteLine("Deleting performance counters.")
PerformanceCounterCategory.Delete(ThisServiceName)
Trace.WriteLine("Performance counters deleted.")
End If

PerformanceCounterCategory.Create(ThisServiceName, "",
PerformanceCounterCategoryType.MultiInstance, "test counter", "")

ServicesToRun = New System.ServiceProcess.ServiceBase() {New
Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

Furthermore, it is not suggested to put your code in the main method. You
can put it in the OnStart method. And start your service in another thread
that does the main jobs.

If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
T

Tomasz Jastrzebski

Hi Kevin,

No, I create counters *before*
"System.ServiceProcess.ServiceBase.Run(ServicesToRun)".
Moving call to a method creating counters to the OnStart() was the *only
change* I made. The behavior is/was very consistent.
I do not understand why, but it helped.

Tomasz
 
K

Kevin Yu [MSFT]

Hi Tomasz,

It seems to be a little complicated, since the same code works fine on my
machine both in OnStart and Main. There might be some other issues that
prevent Main from working properly. You can try to create a new windows
service and test the code again. Anyway, it was nice to know that the
solution works for you.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
K

Kevin Yu [MSFT]

Hi Tomasz,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top