473,287 Members | 2,263 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,287 software developers and data experts.

Thread - CPU usage

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
11 5962
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
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
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
Thanks I will have a deeper look at PerformanceCounter.

--

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

http://michael.moreno.free.fr/
Apr 16 '06 #5
You could probably use kernrate to identify which thread is causing the
bottleneck.

Apr 17 '06 #6

"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
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
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
> 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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark McKay | last post by:
I have a thread which is used for updating a display window. The normal paint message queue is being bypassed in favor of drawing on demand by this thread. This thread is passed a Graphics...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
8
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the...
3
by: Santosh | last post by:
Hi, I have a requirement while using ThreadPool class. I am creating n number of threads using ThreadPool class, using WaitCallback method. In this Method I want to stop all the threads started...
5
by: Kieran Benton | last post by:
Hello, I'm currently in the tail end process of developing a high scalability server for my employer. Essentially it receives short socket based connections with an ASCII message, parses that...
5
by: fniles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message...
4
by: Adam Benson | last post by:
Hi, We have an app which, every now and then, has very high CPU usage. Profiling under perfmon shows it to be thread #3 which is running at >=95%. I took a process dump and looked at thread #3...
2
by: jld | last post by:
Hi, I developed an asp.net based eCommerce Website for a client and it is hosted at discount asp. The site is quite interactive, queries a database a lot and uses ajax.asp.net to spice up...
0
by: ipramod | last post by:
Hi, I just wanted to calculate CPU Percentage Utilization in ASP.Net application using a thread. I have written following code: protected void Page_Load(object sender, EventArgs e) {...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.