Thread - CPU usage

  • Thread starter Thread starter Michael Moreno
  • Start date Start date
M

Michael Moreno

Hello,

I have a C# server which has 4 worker threads running all the time.
When I let the server runs for several hours, for some reasons the CPU
usage of the application will shoot to 100% and remain there.
I would like to find out which thread(s) is/are the source of the
problem.

Would you have any idea on how to tackle the problem please?

Thanks,
Michael
 
Hello Michael,

Use Performance Counter, Thread Group. And u can track the threads behaviour

MM> Hello,
MM>
MM> I have a C# server which has 4 worker threads running all the time.
MM> When I let the server runs for several hours, for some reasons the
MM> CPU
MM> usage of the application will shoot to 100% and remain there.
MM> I would like to find out which thread(s) is/are the source of the
MM> problem.
MM> Would you have any idea on how to tackle the problem please?
MM>
MM> Thanks,
MM> Michael
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
I have a C# server which has 4 worker threads running all the time.
When I let the server runs for several hours, for some reasons the CPU
usage of the application will shoot to 100% and remain there.
I would like to find out which thread(s) is/are the source of the
problem.

Would you have any idea on how to tackle the problem please?

I would first log the thread ID of each thread to a debug log of some
sort, then use TaskInfo <http://www.iarsn.com/taskinfo.html> to look
at the individual CPU usage of each of my threads and identify the
culprit.
 
Hi thanks,

I have tried going this path using perfmon and "Process Explorer" but I
could not find a way to get the Windows ID of the thread.

The following value Thread.CurrentThread.GetHashCode() is, I believe,
an ID for the .Net runtime.

Would you know how to get the Windows thread ID without querying the
Windows API directly? I am trying to not use PInvoke as much as
possible.

Many thanks,
MM
 
| Hello,
|
| I have a C# server which has 4 worker threads running all the time.
| When I let the server runs for several hours, for some reasons the CPU
| usage of the application will shoot to 100% and remain there.
| I would like to find out which thread(s) is/are the source of the
| problem.
|
| Would you have any idea on how to tackle the problem please?
|
| Thanks,
| Michael
|
| --
| Michael
| ----
| http://michael.moreno.free.fr/
| http://port.cogolin.free.fr/
|
|

Your best bet is to use the High-Performance counters through
System.Management and WMI.
Note that the thread usage is per OS thread, somehow you will need to find
the corresponding CLR thread (logical thread).
Note that in a hosted environmant like SQL Server 2005, this is not possible
as logical threads are mapped to OS fibers.

Here is a complete sample that illustrates how you can monitor process
thread usage.

using System;
using System.Management;
using System.Globalization;
using System.Threading;

class Program
{
public static void Main() {
string procName = "SomeApplication"; // app. to monitor, used in LIKE
predicate in select clause (wildcard)
TimerCallback timerDelegate = new TimerCallback (ManagedProcess);
// Monitor process who's name starts with procName
SelectQuery selectQuery = new SelectQuery("select * from
Win32_PerfRawData_PerfProc_Thread where name like " + "\"" + procName +
"%\"");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(selectQuery))
{
using(Timer timer = new Timer (timerDelegate, searcher, 0, 2000))
{
Console.ReadLine(); // suspend main thread.
}
}
}
private static void ManagedProcess(object obj)
{
ManagementObjectSearcher searcher = obj as ManagementObjectSearcher;
ManagementObjectCollection threads = searcher.Get();
ConsoleColor fc = Console.ForegroundColor;
Console.ForegroundColor=ConsoleColor.Red;
Console.WriteLine("{0,-20}{1,-10}{2,-20}{3,-10}{4,-10}{5} ",
"Name", "IDThread", "Elapsed msec.", "Pct Priv.", "Pct Proc.",
"Pct User");
Console.ForegroundColor=fc;
foreach(ManagementObject thread in threads)
{
// CPU usage since thread start time
ulong interval = (ulong)thread["TimeStamp_Object"] -
(ulong)thread["ElapsedTime"];
float pctPrivileged =
Convert.ToSingle((ulong)thread["PercentPrivilegedTime"]) /
Convert.ToSingle(interval);
float pctProcessor =
Convert.ToSingle((ulong)thread["PercentProcessorTime"]) /
Convert.ToSingle(interval);
float pctUser = Convert.ToSingle((ulong)thread["PercentUserTime"]) /
Convert.ToSingle(interval);
Console.WriteLine("{0,-20}{1,-10}{2,-20}{3,-10}{4,-10}{5} ",
thread["Name"].ToString(),
thread["IDThread"].ToString(),
(interval/10000).ToString(),
pctPrivileged.ToString("P"),
pctProcessor.ToString("P"),
pctUser.ToString("P"));
}
}
}


Willy.
 
Thanks,

When I run the code I get an Exception on the line:

ManagementObjectCollection threads = searcher.Get();

Exception Message:

An unhandled exception of type 'System.UnauthorizedAccessException'
occurred in mscorlib.dll

Additional information: Access is denied.



Would you have by chance any idea of the problem please (I am running
XP SP2 with Admin privilegies)?


Many thanks,
MM
 
Weird, admins should have no issues. What happens if you run wbemtest from
the command line?
Try to connect to the root\cimv2 namespace and issue the following query:
Select * from Win32_PerfRawData_PerfProc_Thread where name like 'progname'

Willy.

| Thanks,
|
| When I run the code I get an Exception on the line:
|
| ManagementObjectCollection threads = searcher.Get();
|
| Exception Message:
|
| An unhandled exception of type 'System.UnauthorizedAccessException'
| occurred in mscorlib.dll
|
| Additional information: Access is denied.
|
|
|
| Would you have by chance any idea of the problem please (I am running
| XP SP2 with Admin privilegies)?
|
|
| Many thanks,
| MM
|
| --
| Michael
| ----
| http://michael.moreno.free.fr/
| http://port.cogolin.free.fr/
|
|
 
Weird, admins should have no issues. What happens if you run wbemtest from
the command line?

I get the same problem.
Try to connect to the root\cimv2 namespace and issue the following query:
Select * from Win32_PerfRawData_PerfProc_Thread where name like 'progname'

Thank you. Since I had never heard about it I googled "root\cimv2" and
the answers were all very complex to my eyes. Despite my 10 years
professional development on Windows, I fear it would take me weeks
before I get a crasp of WMI.

Maybe the problem is that I use VS 2003 which may have different
settings than VS 2005?

regards,
MM
 
|> Weird, admins should have no issues. What happens if you run wbemtest
from
| > the command line?
|
| I get the same problem.
|

When you do what? start wbemtest, when you connect to root\cimv2 or when you
run the query.
If you can't start wbemtest you have a broken WMI set-up, if you can't
connect make sure you are an administrator.


| > Try to connect to the root\cimv2 namespace and issue the following
query:
| > Select * from Win32_PerfRawData_PerfProc_Thread where name like
'progname'
|
| Thank you. Since I had never heard about it I googled "root\cimv2" and
| the answers were all very complex to my eyes. Despite my 10 years
| professional development on Windows, I fear it would take me weeks
| before I get a crasp of WMI.
|
Not necessarily but that's you to decide.

| Maybe the problem is that I use VS 2003 which may have different
| settings than VS 2005?
|

No, VS has nothing to with wbemtest, if it doesn't work with wbemtest it
makes no sense to try from VS.

Willy.
 
I have tried going this path using perfmon and "Process Explorer" but I
could not find a way to get the Windows ID of the thread.

The following value Thread.CurrentThread.GetHashCode() is, I believe,
an ID for the .Net runtime.

Correct, though I don't think that it's a _thread_ ID, but rather an
object ID.
Would you know how to get the Windows thread ID without querying the
Windows API directly? I am trying to not use PInvoke as much as
possible.

I googled it a bit but didn't find anything other than
AppDomain.GetCurrentThreadId(), which wraps a call to Win32's
GetCurrentThreadId(). I understand why you want to avoid it, but in
this case I wouldn't worry about it since we're discussing a debugging
mechanism that wouldn't, presumably, be in your release code.
 

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

Similar Threads


Back
Top