473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Perf counters, instances, and RemoveInstance

I'm instrumenting my app with a few performance counters
and I'd like to ask you all for some advice on how to handle
performance counter instances.

I have a class library that is a base library for most of our
..NET applications. It provides configuration, logging, exception
management/publishing, data access, etc.

I have my counters, but I'm curious how I should handle instances.
Right now, the instance name is exename-procid like test.exe-5129.

However, I need to remove the instance when the process exits,
so I hook the AppDomain.DomainUnload or whatever it is, but it
doesn't seem to fire reliably.

Does anyone have experience with this? What did you do about
counter instances and how did you solve the problem of when/how
to remove them effectively?

Thanks,
Chad
Nov 16 '05 #1
4 3668
Hi,

If you use one of the PerformanceCounter class constructor forms {see docs}
that explicitly takes an 'instance name' parameter then your counter will
exist only during the program run. You should not have to do anything
explicit at shutdown to remove an instance based counter - even if you crash
the counter will be removed when the process exits.

Also I don't think you need to explicitly invent a unique name {aka adding a
pid to the end of the instance name} for each counter instance - the OS will
not get confused if multiple instances with the same name are active...

--Richard

"Chad Myers" wrote:
I'm instrumenting my app with a few performance counters
and I'd like to ask you all for some advice on how to handle
performance counter instances.

I have a class library that is a base library for most of our
..NET applications. It provides configuration, logging, exception
management/publishing, data access, etc.

I have my counters, but I'm curious how I should handle instances.
Right now, the instance name is exename-procid like test.exe-5129.

However, I need to remove the instance when the process exits,
so I hook the AppDomain.DomainUnload or whatever it is, but it
doesn't seem to fire reliably.

Does anyone have experience with this? What did you do about
counter instances and how did you solve the problem of when/how
to remove them effectively?

Thanks,
Chad

Nov 16 '05 #2
Richard,

Thanks for the reply!

I believe you are incorrect about the instance disappearing. I know this
because if I don't
call RemoveInstance, the instance never gets removed. I can verify this
because the .NET
counters (like the ".NET CLR Data" category) shows a bunch of instances of
processes that
had died or been killed a long time ago (I don't reboot much).

Rebooting clears out all the instances and starts over from scratch.

I wish to keep the processes unique because we have an app which is a
service and you can
install this service multiple times to different directories to work on
different queues. The
process name is always the same. It would not be useful to customers it all
the processes of
that app shared the same instance in perfmon.

I came up with a solution in the meantime:

Create a singleton that holds a reference to the counters. In the finalize
of the singleton instance,
clear the counters.

This works for WinForms and Console apps, but not things like ASP.NET or
NUnit which create
app domains and close them.

I bind to the AppDomain.DomainUnload event and call GC.SupressFinalize(this)
so that the
finalizer doesn't get call (I don't want both to get called, only one or the
other).

This seems to work in every scenario I've tested so far (Winforms, console,
ASP.NET, NUnit,
ASP through COM interop, etc).

Thanks for the reply. Any comments/suggestions?

-c

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
Hi,

If you use one of the PerformanceCounter class constructor forms {see docs} that explicitly takes an 'instance name' parameter then your counter will
exist only during the program run. You should not have to do anything
explicit at shutdown to remove an instance based counter - even if you crash the counter will be removed when the process exits.

Also I don't think you need to explicitly invent a unique name {aka adding a pid to the end of the instance name} for each counter instance - the OS will not get confused if multiple instances with the same name are active...

--Richard

"Chad Myers" wrote:
I'm instrumenting my app with a few performance counters
and I'd like to ask you all for some advice on how to handle
performance counter instances.

I have a class library that is a base library for most of our
..NET applications. It provides configuration, logging, exception
management/publishing, data access, etc.

I have my counters, but I'm curious how I should handle instances.
Right now, the instance name is exename-procid like test.exe-5129.

However, I need to remove the instance when the process exits,
so I hook the AppDomain.DomainUnload or whatever it is, but it
doesn't seem to fire reliably.

Does anyone have experience with this? What did you do about
counter instances and how did you solve the problem of when/how
to remove them effectively?

Thanks,
Chad

Nov 16 '05 #3
Hmmm,

Are the processes still running in task manager? If they are then the
counters will continue to exist...

Also, if you're expecting the counter to disappear from perfmon it won't -
perfmon will display the last queried counter value until you exit and
restart perfmon... If you want behavior similiar to OS counters that display
dashed lines after process exit then you'll probably have to explicitly
delete the counter or set its raw value to some secret/special value; I never
tried to acheive that behavior so I'm unsure of how to do it exactly...

--Richard

"Chad Myers" wrote:
Richard,

Thanks for the reply!

I believe you are incorrect about the instance disappearing. I know this
because if I don't
call RemoveInstance, the instance never gets removed. I can verify this
because the .NET
counters (like the ".NET CLR Data" category) shows a bunch of instances of
processes that
had died or been killed a long time ago (I don't reboot much).

Rebooting clears out all the instances and starts over from scratch.

I wish to keep the processes unique because we have an app which is a
service and you can
install this service multiple times to different directories to work on
different queues. The
process name is always the same. It would not be useful to customers it all
the processes of
that app shared the same instance in perfmon.

I came up with a solution in the meantime:

Create a singleton that holds a reference to the counters. In the finalize
of the singleton instance,
clear the counters.

This works for WinForms and Console apps, but not things like ASP.NET or
NUnit which create
app domains and close them.

I bind to the AppDomain.DomainUnload event and call GC.SupressFinalize(this)
so that the
finalizer doesn't get call (I don't want both to get called, only one or the
other).

This seems to work in every scenario I've tested so far (Winforms, console,
ASP.NET, NUnit,
ASP through COM interop, etc).

Thanks for the reply. Any comments/suggestions?

-c

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
Hi,

If you use one of the PerformanceCounter class constructor forms {see

docs}
that explicitly takes an 'instance name' parameter then your counter will
exist only during the program run. You should not have to do anything
explicit at shutdown to remove an instance based counter - even if you

crash
the counter will be removed when the process exits.

Also I don't think you need to explicitly invent a unique name {aka adding

a
pid to the end of the instance name} for each counter instance - the OS

will
not get confused if multiple instances with the same name are active...

--Richard

"Chad Myers" wrote:
I'm instrumenting my app with a few performance counters
and I'd like to ask you all for some advice on how to handle
performance counter instances.

I have a class library that is a base library for most of our
..NET applications. It provides configuration, logging, exception
management/publishing, data access, etc.

I have my counters, but I'm curious how I should handle instances.
Right now, the instance name is exename-procid like test.exe-5129.

However, I need to remove the instance when the process exits,
so I hook the AppDomain.DomainUnload or whatever it is, but it
doesn't seem to fire reliably.

Does anyone have experience with this? What did you do about
counter instances and how did you solve the problem of when/how
to remove them effectively?

Thanks,
Chad


Nov 16 '05 #4
The processes are long gone. These are processes I killed a long time ago
because
they were misbehaving, yet they still show up as instances under ".NET CLR
Data".

Normal processes disappear from the list of instances for ".NET CLR Data",
however.

Likewise, when I set up my RemoveInstance() calls in the finalizer and
DomainUnload
handler, everything works great (unless the program is terminated
unexpectedly).

I have restarted perfmon, these derilict instances do not disappear until
reboot.
I suppose I could connect to them by creating a PerformanceCounter with
their
instance name and then call RemoveInstance to make it disappear.

What leads you to believe that program termination should remove the
instances?
There's nothing in the .NET or Win32 Performance stuff documentation that I
can
find that hints to this at all. Instances can be called anything, it's
merely a convention
that you create an instance-per-process, but this isn't the default or
anything.

Thanks again for keeping up on this. I hope that there's something obvious
I'm just missing.

The good news is, however, that it's working regardless. I'm just curious if
there's
another/better way.

Thanks again,
Chad
"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
Hmmm,

Are the processes still running in task manager? If they are then the
counters will continue to exist...

Also, if you're expecting the counter to disappear from perfmon it won't -
perfmon will display the last queried counter value until you exit and
restart perfmon... If you want behavior similiar to OS counters that display dashed lines after process exit then you'll probably have to explicitly
delete the counter or set its raw value to some secret/special value; I never tried to acheive that behavior so I'm unsure of how to do it exactly...

--Richard

"Chad Myers" wrote:
Richard,

Thanks for the reply!

I believe you are incorrect about the instance disappearing. I know this
because if I don't
call RemoveInstance, the instance never gets removed. I can verify this
because the .NET
counters (like the ".NET CLR Data" category) shows a bunch of instances of processes that
had died or been killed a long time ago (I don't reboot much).

Rebooting clears out all the instances and starts over from scratch.

I wish to keep the processes unique because we have an app which is a
service and you can
install this service multiple times to different directories to work on
different queues. The
process name is always the same. It would not be useful to customers it all the processes of
that app shared the same instance in perfmon.

I came up with a solution in the meantime:

Create a singleton that holds a reference to the counters. In the finalize of the singleton instance,
clear the counters.

This works for WinForms and Console apps, but not things like ASP.NET or
NUnit which create
app domains and close them.

I bind to the AppDomain.DomainUnload event and call GC.SupressFinalize(this) so that the
finalizer doesn't get call (I don't want both to get called, only one or the other).

This seems to work in every scenario I've tested so far (Winforms, console, ASP.NET, NUnit,
ASP through COM interop, etc).

Thanks for the reply. Any comments/suggestions?

-c

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
Hi,

If you use one of the PerformanceCounter class constructor forms {see

docs}
that explicitly takes an 'instance name' parameter then your counter will exist only during the program run. You should not have to do anything
explicit at shutdown to remove an instance based counter - even if you
crash
the counter will be removed when the process exits.

Also I don't think you need to explicitly invent a unique name {aka
adding a
pid to the end of the instance name} for each counter instance - the
OS will
not get confused if multiple instances with the same name are

active...
--Richard

"Chad Myers" wrote:

> I'm instrumenting my app with a few performance counters
> and I'd like to ask you all for some advice on how to handle
> performance counter instances.
>
> I have a class library that is a base library for most of our
> ..NET applications. It provides configuration, logging, exception
> management/publishing, data access, etc.
>
> I have my counters, but I'm curious how I should handle instances.
> Right now, the instance name is exename-procid like test.exe-5129.
>
> However, I need to remove the instance when the process exits,
> so I hook the AppDomain.DomainUnload or whatever it is, but it
> doesn't seem to fire reliably.
>
> Does anyone have experience with this? What did you do about
> counter instances and how did you solve the problem of when/how
> to remove them effectively?
>
> Thanks,
> Chad
>
>
>


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Payne | last post by:
I am using the PerformanceCounter and PerformanceCounterCategory classes to build my own version of perfmon. The problem is that my computer has categories called "Indexing Service" and...
0
by: mark | last post by:
I'm using the ACT object model/wscript to automate the running of ACT tests (application center test). For some reason when I run the test from the ACT UI, the performance counters get collected,...
0
by: Christopher Attard | last post by:
Hi, I need to create a dialog like the 'Add Counters' dialog box in perfmon. I'm using the System.Diagnostics namespace class in .NET and I've managed to do it. The problem arises when I'm...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
0
by: Erik Cruz | last post by:
Hi. I have one asp.net application running on my server. I am playing with the ..NET Clr DATA performance counters in order to control the connections created by the application. When I select...
4
by: Z D | last post by:
Hello, I have a Windows Server 2003 machine running IIS6.0. I recently xcopied over an ASP.NET application that was originally running on a Windows 2000 Server machine. The application works...
7
by: James | last post by:
Hi Has anybody had any experience of ASP.Net performance counters not updating. In the performance monitor application when I try to add the groups ASP.NET and ASP.NET Applications the...
2
by: Stefan Kuhr | last post by:
Hello everyone, I hope this is not an FAQ and that somebody can answer this: As part of our webservice installation we run aspnet_regiis.exe -ir -enable on computers where the web...
1
by: Ben | last post by:
Hi, I registered some custom perf counters that i want to use in my app (all of type NumberOfItems32). I added them under the same category name but different counter names... Same code work...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.