473,569 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Application consumes close to 100% of CPU, how to profile and determine the cause

Hello,

I recently wrote an application that is used for testing units in a
burn-in chamber. It uses two external library that require the use of
P\Invoke in order to work with them. There is a primary thread and a
secondary polling thread. Every once in a while the process will
consume close to 100% of the CPU, but I am unclear why. I am wondering
how would I go about determine the cause of the problem? What sort of
tools or instrumentation should I use in order to determine what part
of the code is causing the problem. Any help is greatly appreciated.

I know in the hardware world I could implement a watchdog to restart
the sytem, would something like that be useful? I would have to keep
track of the system thus is would run slower and consume more
resources, but I might be able to recover when the process has
problems???

Mark

Nov 17 '05 #1
11 2118
Mark,

Are you finding that these spikes are impacting the performance of the
application, or, the performance of the machine overall?

It's very possible that the spikes could be the result of garbage
collection and memory management.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<Lo*****@hotmai l.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hello,

I recently wrote an application that is used for testing units in a
burn-in chamber. It uses two external library that require the use of
P\Invoke in order to work with them. There is a primary thread and a
secondary polling thread. Every once in a while the process will
consume close to 100% of the CPU, but I am unclear why. I am wondering
how would I go about determine the cause of the problem? What sort of
tools or instrumentation should I use in order to determine what part
of the code is causing the problem. Any help is greatly appreciated.

I know in the hardware world I could implement a watchdog to restart
the sytem, would something like that be useful? I would have to keep
track of the system thus is would run slower and consume more
resources, but I might be able to recover when the process has
problems???

Mark

Nov 17 '05 #2
Nicholas,

Sorry that I didn't explain myself well enough. Actually when the
process consumes close to 100% of the CPU it stays at that level until
I kill the task. I have let it run a few minutes to see if it would
recover, but it never did so I killed the task. I am unclear what is
triggering it.

For the P/Invoke functions they look like the following

[DllImport("SiF3 2xUSB.DLL", EntryPoint = "F32x_Write ", SetLastError
= true,
CharSet = CharSet.Unicode , ExactSpelling = true,
CallingConventi on = CallingConventi on.StdCall)]
private static extern
System.Int32 F32x_Write( System.UInt32 Handle,
System.IntPtr Buffer,
System.UInt32 NumBytesToRead,
ref System.UInt32
NumBytesWritten ToDevice
);

Then the correspond method that uses this function is as follows:

public bool Write( UInt32 handle, byte[] buffer )
{
UInt32 numBytesWritten = 0x00;

GCHandle gch = GCHandle.Alloc( buffer, GCHandleType.Pi nned );

m_LastErrorCode =
(SI_RETURN_CODE S)F32x_Write( handle,
gch.AddrOfPinne dObject(),
(UInt32)buffer. Length,
ref numBytesWritten );

gch.Free();

if ( m_LastErrorCode == SI_RETURN_CODES .SI_SUCCESS )
{
return true;
}
else
{
return false;
}
}

I don't know if either of these declarations are causing the problems
or not. I did notice I do have one P/Invoke function to where the
memory isn't pinned therefore perhaps the GC is executing at that
particular time and causing the problem. That method looks like

public MMCThermocouple Types TCType
{
get { return m_TCType; }
set
{
int apiResult;

try
{
m_TCType = value;
apiResult = cbSetConfig( (int)MCCCfgInfp Type.BOARDINFO,
(int)m_USBTCBoa rdNum,
(int)m_ChamberT CChannel,

(int)MMMBoardCf gInfoType.BITCC HANTYPE,
(int)m_TCType
);
}
catch ( Exception )
{
// TODO: Implement handler for this event.
}
}
}

The variables that start with "m_" are members to the class and are
private. Perhaps the GC is moving memory around at this particular
point in time causing problems? I really don't know.

Mark

Nov 17 '05 #3
From what I can see, you don't need to pin the byte array before passing
it to your unamanged code. The marshaler will pin the array in memory for
you, and release it when done.

As for the spike, there is nothing here that I would see that would
cause this, as you are releasing the pinned variable. Does your application
not respond during this time? You haven't given any indication that 100%
utilization is impacting your app, or the machine negatively.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<Lo*****@hotmai l.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Nicholas,

Sorry that I didn't explain myself well enough. Actually when the
process consumes close to 100% of the CPU it stays at that level until
I kill the task. I have let it run a few minutes to see if it would
recover, but it never did so I killed the task. I am unclear what is
triggering it.

For the P/Invoke functions they look like the following

[DllImport("SiF3 2xUSB.DLL", EntryPoint = "F32x_Write ", SetLastError
= true,
CharSet = CharSet.Unicode , ExactSpelling = true,
CallingConventi on = CallingConventi on.StdCall)]
private static extern
System.Int32 F32x_Write( System.UInt32 Handle,
System.IntPtr Buffer,
System.UInt32 NumBytesToRead,
ref System.UInt32
NumBytesWritten ToDevice
);

Then the correspond method that uses this function is as follows:

public bool Write( UInt32 handle, byte[] buffer )
{
UInt32 numBytesWritten = 0x00;

GCHandle gch = GCHandle.Alloc( buffer, GCHandleType.Pi nned );

m_LastErrorCode =
(SI_RETURN_CODE S)F32x_Write( handle,
gch.AddrOfPinne dObject(),
(UInt32)buffer. Length,
ref numBytesWritten );

gch.Free();

if ( m_LastErrorCode == SI_RETURN_CODES .SI_SUCCESS )
{
return true;
}
else
{
return false;
}
}

I don't know if either of these declarations are causing the problems
or not. I did notice I do have one P/Invoke function to where the
memory isn't pinned therefore perhaps the GC is executing at that
particular time and causing the problem. That method looks like

public MMCThermocouple Types TCType
{
get { return m_TCType; }
set
{
int apiResult;

try
{
m_TCType = value;
apiResult = cbSetConfig( (int)MCCCfgInfp Type.BOARDINFO,
(int)m_USBTCBoa rdNum,
(int)m_ChamberT CChannel,

(int)MMMBoardCf gInfoType.BITCC HANTYPE,
(int)m_TCType
);
}
catch ( Exception )
{
// TODO: Implement handler for this event.
}
}
}

The variables that start with "m_" are members to the class and are
private. Perhaps the GC is moving memory around at this particular
point in time causing problems? I really don't know.

Mark

Nov 17 '05 #4
Nicholas,

The application under normal load appears to use anywhere from 25 to
50 megs of memory and about ~5% CPU utilization. When the application
peaks and holds near 100% CPU utilitization the GUI is unresponsive,
the cursor changes to an hour-glass indicating it is awaiting for
something to complete. If you try to move the GUI it will move, but
application window is never redrawn. So I would guess this is the
negative impact that you are looking for.

Thanks for the information regarding the marshaler will pin the array
down as I didn't know that. I assume that I needed to do that myself. I
did fail to state that I am using Visual C# 2005 Express Beta. I am
using this because I also need to use the new Serial Port class as v1.x
didn't include it.

Right now the application is running in the lab and has behaved good
for the past three hours. I can't seem to predict what causes the spike
as it might happen just after launching the application or five minutes
after running it.

Mark

Nov 17 '05 #5
Mark,

You can use performance monitor (perf mon) in 'Administrator Tools' to get a
view out what is going in your application. It has alot of .Net related
counters that include one for monitoring memory activates.

HTH

Ollie Riches

<Lo*****@hotmai l.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Nicholas,

The application under normal load appears to use anywhere from 25 to
50 megs of memory and about ~5% CPU utilization. When the application
peaks and holds near 100% CPU utilitization the GUI is unresponsive,
the cursor changes to an hour-glass indicating it is awaiting for
something to complete. If you try to move the GUI it will move, but
application window is never redrawn. So I would guess this is the
negative impact that you are looking for.

Thanks for the information regarding the marshaler will pin the array
down as I didn't know that. I assume that I needed to do that myself. I
did fail to state that I am using Visual C# 2005 Express Beta. I am
using this because I also need to use the new Serial Port class as v1.x
didn't include it.

Right now the application is running in the lab and has behaved good
for the past three hours. I can't seem to predict what causes the spike
as it might happen just after launching the application or five minutes
after running it.

Mark

Nov 17 '05 #6
Ollie,

I will check the perfmon msc tomorrow and hopefully it will lead to a
solution. I was checking the process using sysinternals process
explorer, but I wasn't seeing anything that would give me a clue or I
wasn't reading the data correctly.

Since there are a ton of items that I can monitor, do you (or anyone
else) have an idea of what items I should zero in on for the first go
around?

Mark

Nov 17 '05 #7
check out the '.Net memory' counters these will give you some info
about what is going on with memory allocation and GC

Ollie Riches

Nov 17 '05 #8
Ollie and Nicholas,

Okay, I ran the perfmon against the assembly, but I really don't see
anything that my eyes can detect what might be the problem. I am not
sure how to interpret the log effectively. There were the items that I
monitored

"\.NET CLR Interop(SDU ATP-BurnIn)\# of CCWs",
"\.NET CLR Interop(SDU ATP-BurnIn)\# of marshalling",
"\.NET CLR Interop(SDU ATP-BurnIn)\# of Stubs",
"\.NET CLR Interop(SDU ATP-BurnIn)\# of TLB exports / sec",
"\.NET CLR Interop(SDU ATP-BurnIn)\# of TLB imports / sec",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\# of current logical
Threads",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\# of current physical
Threads",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\# of current recognized
threads",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\# of total recognized
threads",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Content ion Rate / sec",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Current Queue Length",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Queue Length / sec",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Queue Length Peak",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\rate of recognized threads /
sec",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Total # of Contentions",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Bytes in all Heaps",
"\.NET CLR Memory(SDU ATP-BurnIn)\# GC Handles",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Gen 0 Collections",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Gen 1 Collections",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Gen 2 Collections",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Induced GC",
"\.NET CLR Memory(SDU ATP-BurnIn)\# of Pinned Objects",
"\.NET CLR Memory(SDU ATP-BurnIn)\# of Sink Blocks in use",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Total committed Bytes",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Total reserved Bytes",
"\.NET CLR Memory(SDU ATP-BurnIn)\% Time in GC",
"\.NET CLR Memory(SDU ATP-BurnIn)\Allocat ed Bytes/sec",
"\.NET CLR Memory(SDU ATP-BurnIn)\Finaliz ation Survivors",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 0 heap size",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 0 Promoted Bytes/Sec",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 1 heap size",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 1 Promoted Bytes/Sec",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 2 heap size",
"\.NET CLR Memory(SDU ATP-BurnIn)\Large Object Heap size",
"\.NET CLR Memory(SDU ATP-BurnIn)\Promote d Finalization-Memory from Gen
0",
"\.NET CLR Memory(SDU ATP-BurnIn)\Promote d Finalization-Memory from Gen
1",
"\.NET CLR Memory(SDU ATP-BurnIn)\Promote d Memory from Gen 0",
"\.NET CLR Memory(SDU ATP-BurnIn)\Promote d Memory from Gen 1",
"\Process(S DU ATP-BurnIn)\% Privileged Time",
"\Process(S DU ATP-BurnIn)\% Processor Time",
"\Processor(_To tal)\% Processor Time"

I did see pattern develop, but I didn't see a pattern when the
application consumed all the processor time. If any one cares to look
at the log that was captured you can find it here...

http://www.liquid-minds.com/SDU_BurnIn_000002.csv

It is case sensitive. I am not sure what else I should capture. I am
not 100% sure, but I think the problem is in the area with the interop
service. If I disconnect the two external USB devices I don't see the
problem or at least been able to induce it. In order to induce the
problem there is no cookbook method, I just click between windows,
minimize, maximize etc and wait for the application to consume all the
CPU time.

Mark

Nov 17 '05 #9
Mark,
FYI - the microsoft.publi c.dotnet.framew ork.performance newsgroup is the
best place for performance related questions.

Ollie

<Lo*****@hotmai l.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Ollie and Nicholas,

Okay, I ran the perfmon against the assembly, but I really don't see
anything that my eyes can detect what might be the problem. I am not
sure how to interpret the log effectively. There were the items that I
monitored

"\.NET CLR Interop(SDU ATP-BurnIn)\# of CCWs",
"\.NET CLR Interop(SDU ATP-BurnIn)\# of marshalling",
"\.NET CLR Interop(SDU ATP-BurnIn)\# of Stubs",
"\.NET CLR Interop(SDU ATP-BurnIn)\# of TLB exports / sec",
"\.NET CLR Interop(SDU ATP-BurnIn)\# of TLB imports / sec",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\# of current logical
Threads",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\# of current physical
Threads",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\# of current recognized
threads",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\# of total recognized
threads",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Content ion Rate / sec",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Current Queue Length",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Queue Length / sec",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Queue Length Peak",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\rate of recognized threads /
sec",
"\.NET CLR LocksAndThreads (SDU ATP-BurnIn)\Total # of Contentions",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Bytes in all Heaps",
"\.NET CLR Memory(SDU ATP-BurnIn)\# GC Handles",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Gen 0 Collections",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Gen 1 Collections",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Gen 2 Collections",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Induced GC",
"\.NET CLR Memory(SDU ATP-BurnIn)\# of Pinned Objects",
"\.NET CLR Memory(SDU ATP-BurnIn)\# of Sink Blocks in use",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Total committed Bytes",
"\.NET CLR Memory(SDU ATP-BurnIn)\# Total reserved Bytes",
"\.NET CLR Memory(SDU ATP-BurnIn)\% Time in GC",
"\.NET CLR Memory(SDU ATP-BurnIn)\Allocat ed Bytes/sec",
"\.NET CLR Memory(SDU ATP-BurnIn)\Finaliz ation Survivors",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 0 heap size",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 0 Promoted Bytes/Sec",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 1 heap size",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 1 Promoted Bytes/Sec",
"\.NET CLR Memory(SDU ATP-BurnIn)\Gen 2 heap size",
"\.NET CLR Memory(SDU ATP-BurnIn)\Large Object Heap size",
"\.NET CLR Memory(SDU ATP-BurnIn)\Promote d Finalization-Memory from Gen
0",
"\.NET CLR Memory(SDU ATP-BurnIn)\Promote d Finalization-Memory from Gen
1",
"\.NET CLR Memory(SDU ATP-BurnIn)\Promote d Memory from Gen 0",
"\.NET CLR Memory(SDU ATP-BurnIn)\Promote d Memory from Gen 1",
"\Process(S DU ATP-BurnIn)\% Privileged Time",
"\Process(S DU ATP-BurnIn)\% Processor Time",
"\Processor(_To tal)\% Processor Time"

I did see pattern develop, but I didn't see a pattern when the
application consumed all the processor time. If any one cares to look
at the log that was captured you can find it here...

http://www.liquid-minds.com/SDU_BurnIn_000002.csv

It is case sensitive. I am not sure what else I should capture. I am
not 100% sure, but I think the problem is in the area with the interop
service. If I disconnect the two external USB devices I don't see the
problem or at least been able to induce it. In order to induce the
problem there is no cookbook method, I just click between windows,
minimize, maximize etc and wait for the application to consume all the
CPU time.

Mark

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1463
by: Tom L | last post by:
Like the subject says, how do I figure out how much memory an object takes.. If I create an employees collection, which is a collection of employee objects, and I load up a handful of records, I want to know if it consumes, say, 20k, or 100k or whatever of memory/resources. Thanks..
4
18097
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call...
0
1850
by: Tomas | last post by:
I have two questions: (1) How (if possible) can you, with ASP.NET (and with the IIS 5 included with win2000) specify an maximum limit of the memory that a web application may consume, as an absolute number of megabytes ??? ( I am aware of the "memoryLimit" attribute in the "processModel" element in the Web.config but that only specifies...
1
1168
by: daniel | last post by:
I'm devloping a Web Application in VB.NET. In my web.config file I have specified that untrapped errors are to be sent to the page "errorpage.aspx". This is working fine - if an untrapped error occurs the application is indeed routed to this page. On this page I would like to determine the cause of the error and either log it in a file or...
1
1238
by: YC | last post by:
Hi. I have a question regarding the profile and sessiom performance when using SQL server a a provider. The mecahnizm in general uses blob as the datatype for the state persistente. In high volumes, will it cause a peformence problem?
1
1772
by: John Dalberg | last post by:
I converted a working web site project to a web application project and I am getting a bunch of warnings and compile errors. All code using .NET's profile class is giving an error. example line: txtName.Text = Profile.FirstName gives an error: ... FirstName' is not a member of 'Profile'.
1
3057
by: enginhorzum | last post by:
Hi all i wrote a windows app with c# which consumes a web-service also written in c#. i wrote a method in web-service like this. public function Test() { System.Threading.Thread.Sleep(10*60*100); return true; } When i call this method from windows application, it time-outs after appx. 2 minutes, win. app. doesn't wait for the response and...
2
4306
by: aagarwal8 | last post by:
Hi, I have a chat application which i have created using WinForms 2.0 (C# lang). The situation i am faced with is that i need to close all the opened (chat, public chat, chat invite, chat history, user profile) windows at the time of logout, except for the main window. In my application, all the chat windows are opened on a separate
3
1397
by: sewid | last post by:
Hi! I have a very simple windows form (Visual Basic, .NET-Framework 2.0). This is my whole code: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myThread As New System.Threading.Thread(AddressOf testThread)
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.