1

I have a program running several threads, but some threads sometimes overload the CPU. so I need to limit these threads CPU usage to %50 something, is it possible in Delphi?

edit: sorry guys my question was not clear.

I actually want to know how could I track threads ( at least make a thread list with their thread IDs) and see how much CPU uses each thread. But I want to do this so I could see which thread is responsible for CPU overload.

sorry for the inconvenience again.

waffles
  • 33
  • 1
  • 4
  • Run the threads at a lower priority than the threads which you want to run with higher priority. – David Heffernan Jan 14 '11 at 16:19
  • all the threads are doing the same thing. But sometimes some threads start overloading the CPU.. – waffles Jan 14 '11 at 16:23
  • @waffles I mean lower priority than the other threads that aren't owned by your app. I mean what himself has said. But I don't think you should do Sleep(0) like he said, that makes no sense to me. – David Heffernan Jan 14 '11 at 16:36
  • Do you want to track the cpu usage inside your program or from external tool for debugging purposes? – Harriv Jan 14 '11 at 16:50
  • 1
    @waffles I also wonder what "overload the CPU" means. Could you elaborate on the symptoms. – David Heffernan Jan 14 '11 at 16:52
  • @David: No, not like @waffles said. But you can use a temporary sleep to confirm you have an inner loop going round in circles, hoarding the cpu. Eg two nested loops, of which the outer one has a nice "WaitFor" but the inner one doesn't and under certain conditions is going around and around in circles, not accomplishing anything. This can happen with a missing condition to break out of the inner loop and allowing the outer one to go into a wait state. Putting in a temporary sleep(bit more than zero), confirms this when it results in the cpu usage no longer going up to 100%. – Marjan Venema Jan 14 '11 at 16:53
  • @Marjan Maybe. But I get the impression that @waffles is talking about a program that he wants to be running hot, but he has a problem that the machine becomes unresponsive and other programs don't get a chance. That is usually caused because the hardware (e.g. disk, network) is being hammered rather than the processor being used. – David Heffernan Jan 14 '11 at 16:58
  • @waffles if you want to see which thread is maxing out then I suggest you use the wonder Process Explorer from SysInternals. – David Heffernan Jan 14 '11 at 16:59
  • @David Heffernan,, Yes I know that program and I'm already using it. When I list the threads with Process exp , I can see the thread which consistently uses high CPU comparing to other threads, so when I manually kill the thread, this resolves unresponsiveness of PC. – waffles Jan 14 '11 at 17:08
  • @David Heffernan, so If I could list my threads in my application I thougth I could make an auto-kill process so I wouldn't have to do it manually. – waffles Jan 14 '11 at 17:09
  • 1
    @waffles You need to ask your self why that thread is behaving the way it is. If you are happy to kill it when it misbehaves then why do you need it at all. Why not sidestep the problem by never starting the problematic thread? You are shooting the messenger. – David Heffernan Jan 14 '11 at 17:11
  • 1
    possible duplicate of [How to get the cpu usage per thread on windows (win32)](http://stackoverflow.com/questions/1393006/how-to-get-the-cpu-usage-per-thread-on-windows-win32) – David Heffernan Jan 14 '11 at 17:22
  • For info on how to enumerate threads in a process, see: "Enumerating threads in a process": http://blogs.msdn.com/b/oldnewthing/archive/2006/02/23/537856.aspx – Marjan Venema Jan 14 '11 at 19:39

3 Answers3

3

I think the answer to your question can be found in the following Stack Overflow question: How to get the cpu usage per thread on windows (win32).

However, I would advise you to endeavour to understand why your program is behaving as it does and attack the root of the problem rather than killing any threads that you take a dislike to. Of course, if the program in question is purely for your own private use then your approach may be perfectly expedient and pragmatic. But if you are writing professional software then I can't see a situation where killing busy threads sounds like a reasonable approach.

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

You cannot "limit CPU usage", not in Delphi nor in Windows itself, as far as I know.

You likely want something else: not to interfere with user actions or with other threads. But if there's nothing going on and user aren't doing anything, why run slower than you could? Just use the 100% of the CPU, nobody needs it!

So, if you need those threads not to interfere with user actions, just set them to lower priority with Windows function SetThreadPriority. They'll only run when user doesn't need processor power.

Another trick to give more chance for other threads to run, call Sleep(0) from time to time in your thread body. Every time you call Sleep(), you ask OS to switch to another thread, simply speaking.

himself
  • 4,806
  • 2
  • 27
  • 43
  • the QUESTION IS: "How can I TRACK a thread's Cpu usage in Delphi" –  Jan 14 '11 at 16:47
  • @Dorin Read the question again. That's the title, but the question specifically asks how to limit it. Perhaps the title should be modified to reflect the question, but it doesn't justify a down vote. – David Heffernan Jan 14 '11 at 16:51
0

I track a rolling CPU usage per thread for every thread in all my applications using some code in my framework (http://www.csinnovations.com/framework/framework.htm). A log output looks like:

15/01/2011 11:17:59.631,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,Memory Check,Verbose,Globals,"System allocated memory = 8282615808 bytes (change since last check = 4872478720 bytes)"

15/01/2011 11:17:59.632,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,Memory Check,Verbose,Globals,"Process allocated memory = 152580096 bytes (change since last check = -4579328 bytes)"

15/01/2011 11:17:59.633,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"System CPU usage = 15.6 % (average over lifetime = 3.0 %)"

15/01/2011 11:17:59.634,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Process CPU usage = 0.5 % (average over lifetime = 0.7 %)"

15/01/2011 11:17:59.634,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.0 % (average over lifetime = 0.0 %)"

15/01/2011 11:17:59.634,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.0 % (average over lifetime = 0.0 %)"

15/01/2011 11:17:59.634,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.0 % (average over lifetime = 0.0 %)"

15/01/2011 11:17:59.635,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.1 % (average over lifetime = 0.1 %)"

15/01/2011 11:17:59.635,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.0 % (average over lifetime = 0.0 %)"

15/01/2011 11:17:59.635,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.3 % (average over lifetime = 0.5 %)"

15/01/2011 11:17:59.635,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.0 % (average over lifetime = 0.0 %)"

15/01/2011 11:17:59.635,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.0 % (average over lifetime = 0.0 %)"

15/01/2011 11:17:59.636,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.0 % (average over lifetime = 0.0 %)"

15/01/2011 11:17:59.636,Misha,MISHA-DCDEL,Scores Client,V0.2.0.1,Main Thread,CPU Check,Verbose,Globals,"Thread CPU usage = 0.1 % (average over lifetime = 0.1 %)"

The time period is configurable, and I tend to use either 10 seconds, a minute, or 10 minutes. Have a look in the CsiSystemUnt.pas and AppGlobalsUnt.pas files to see how it is done.

Cheers, Misha

PS I also check memory usage as well.

Misha
  • 1,816
  • 1
  • 13
  • 16