1

Say I have the following code:

PerformanceCounter c = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);

and then I keep a reference to c, does c consume CPU time? Or does it only consume CPU when calling:

c.NextValue();

?

I.E. Does the PerformanceCounter do work all the time, "counting" resource use, or is it just a tool with which we can access that information, which is being recorded by the computer independently of the PerformanceCounter?

My question is not only about this specific counter. So if the answer depends on the type of counter, so be it. Though some explanation as to how I would go about telling the CPU-users from the non CPU-users would be welcome.

EDIT

Since it was suggested that this is a duplicate of another question, please read a) The other question. b) The answers. Since the question is different and the answers only tangentially touch upon the subject (and don't even agree with one another) I don't think it's a duplicate.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • 1
    Does this answer your question? [What is the performance hit of Performance Counters](https://stackoverflow.com/questions/290634/what-is-the-performance-hit-of-performance-counters) – Charlieface Jul 24 '22 at 21:03
  • `PerformanceCounter` is just a managed wrapper around the unmanaged functionality. It does very little on its own, save for some calculations to normalize the retrieved value based on the counter type. The linked question is therefore quite relevant. All client access to performance counters is simply reading values -- any overhead from maintaining them is on the code updating the counter (which, in the case of processor usage, is the kernel). Indeed, it couldn't be otherwise -- the "users" of the counter wouldn't know how to count, because if they did they wouldn't need it in the first place. – Jeroen Mostert Jul 25 '22 at 12:29
  • @JeroenMostert But we don't know if the counter is updated anyway or just when we ask for it by creating the PerformanceCounter. _That_ is my question, and is not (sufficiently) addressed there. I made that clear in my question. – ispiro Jul 25 '22 at 12:32
  • There is perhaps only some question because it appears that a counter like "% Processor Time" *could* be maintained from client code, but it's like every other counter. It should be obvious that in the general case it's either undesirable to "only" update a counter if it's in use (because it becomes impossible to ask for an accurate value without history), or else this would add overhead that defeats the entire purpose of checking ("if nobody is looking at `WidgetsProduced`, don't increase it, otherwise do" as opposed to "atomically increase `WidgetsProduced`"). – Jeroen Mostert Jul 25 '22 at 12:36
  • 1
    @JeroenMostert The cpu might have some counter but only by instantiating the PerformanceCounter will the OS start loading it somewhere. – ispiro Jul 25 '22 at 13:26
  • @JeroenMostert For some counters ("number of times X occurred") it's simple to just increment it unconditionally. But other counters may be more expensive ("number of times X occurred on behalf of process P" may require a lot of work to decide which process P to charge each occurrence to. Some counters may require installing a context switch hook to update their values, and you don't want to add overhead to context switches if you can at all avoid it.) – Raymond Chen Aug 02 '22 at 22:01

0 Answers0