100% CPU usage is not always a problem… but unfortunatly it often is. In seldom cases, processes that need to do a lot of calculations use the CPU (or one of it’s cores) completely. This is good: what must be calculated must be calculated, and then it is better to do it as fast as possible.
However, sometimes, it is a problem, or a result of a none optimal implementation and then it leads to a report for “1OO% CPU usage by process XYZ”; a description that does not really tell us much. Also, this often happens on production environments with lesser or no logging enabled, making it hard to find out what is causing the CPU usage. Attaching windows debugging tools is also not an alternative: it must be installed, and as soon as it is attached, it blocks your process and the CPU usage will of course be gone.
CPU Usage is visible in the task manager, and thus easy to spot. More advanced users will check this with the performance monitor and check the process’ CPU usage there. However, all of Voxtron services are multithreaded services, so “the process” cannot cause this CPU usage; it is always caused by one ore more threads. For these issues it is thus interesting for a developer to know which thread is causing this CPU usage. Little or no people know that you can also use the standard performance monitor to see which thread in the process uses that much CPU, or, whether the total 100% CPU usage is maybe caused by several threads. Big advantage of this method: you do not need any additional tools, nor will you stop the process.
To get information on a process’ threads, open up the windows performance monitors and click the + sign to add counters.
Search for the “Thread” counters in the top left window; open the list of counters and select the “% Processor time” and the “ID Thread” counter (use shift and/or control to select multiple).
In the “Instances of selected object” search for the process that is using the CPU. The process name will now be trailed with a /<number>, which are all the threads of that process. Select all the threads of that process and then click the “Add>>” button to add the selected counters.
The number of threads can be “significant”, sometimes several hundreds of threads may exist. Even then you should add them all and then later on remove the ones that are not using any CPU, so that you only have the CPU consuming threads in the performance monitor.
The number that is behind the / after the process name is NOT the thread ID. Therefore we added the “ID Thread” counter, which is a static number representing the actual thread ID that you can also find in our log files.
Now that you have all the counters, it will be easy to select the thread(s) that consume(s) all the CPU. You can click on the line in the perfomance counter to select the correct item in the list of counters, or you can work with the colors of the line. However, with a lot of threads, it is hard to see the correct color, and clicking on the lines works better then.
Write down this threads instance number (right column) and scroll down in the list until you find the counter “ID thread” with the same instance number. The value of this counter is a static integer number that you can see on top of the list.
This number is the thread ID, however, formatted as an integer. Convert this number to a hexadecimal number and format this as (for example) x0CF4. This thread ID can then be used to futher investigate the problem as we can now identify the threads in our log file that causes the CPU usage. If you also write multithreaded software and have this problem in your software, make sure you have the thread ID logged at all times.