By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,114 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,114 IT Pros & Developers. It's quick & easy.

Thread - CPU usage

P: n/a
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/
Apr 15 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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 Nietzsche
Apr 15 '06 #2

P: n/a
On Sat, 15 Apr 2006 11:17:32 +0100, Michael Moreno
<mi*********************@free.fr> wrote in
<mn***********************@free.fr>:
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.
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Apr 16 '06 #3

P: n/a
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

--

----------------------------------------------

http://michael.moreno.free.fr/
Apr 16 '06 #4

P: n/a
Thanks I will have a deeper look at PerformanceCounter.

--

----------------------------------------------

http://michael.moreno.free.fr/
Apr 16 '06 #5

P: n/a
You could probably use kernrate to identify which thread is causing the
bottleneck.

Apr 17 '06 #6

P: n/a

"Michael Moreno" <mi*********************@free.fr> wrote in message
news:mn***********************@free.fr...
| 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.
Apr 17 '06 #7

P: n/a
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/
Apr 19 '06 #8

P: n/a
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.

"Michael Moreno" <mi*********************@free.fr> wrote in message
news:mn***********************@free.fr...
| 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/
|
|
Apr 19 '06 #9

P: n/a
> 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

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/
Apr 19 '06 #10

P: n/a

"Michael Moreno" <mi*********************@free.fr> wrote in message
news:mn***********************@free.fr...
|> 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.
Apr 19 '06 #11

P: n/a
On Sun, 16 Apr 2006 18:14:57 +0100, Michael Moreno <ab*@abc.abc> wrote
in <mn***********************@abc.abc>:
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.
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Apr 20 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.