Recent

Author Topic: Cpu Usage  (Read 14462 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: Cpu Usage
« Reply #15 on: October 07, 2016, 05:09:38 pm »
Ok then I hereby remove the strike-through.
It is in normal use nonsense to adjust the thread priority for  threads that already occupy full cycles.
Knowingly loading all cpu's....
It is even stupid, because you have no way of aborting.
And under full load even the OS can not schedule tasks, which it needs to do.
That's a denial of service.

Very grumpy mode ON but just towards one person. >:D

What is allowed is -give four cores - to have two or even three cores dedicated to specific tasks, not all of them.
The mis-use of thread priority is largely due to a lack of education.
« Last Edit: October 07, 2016, 05:16:45 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: Cpu Usage
« Reply #16 on: October 07, 2016, 05:17:42 pm »
It was a theoretical question... why was the cpu not at 100%.
We showed why and also how you can get it to that 100% (although the priority is indeed not needed for the closed loop).
Of course it absolutely nonsense to stress out the cpu (or any core) to the full 100%.
« Last Edit: October 07, 2016, 05:26:47 pm by rvk »

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: Cpu Usage
« Reply #17 on: October 07, 2016, 05:28:15 pm »
Yes, but I am still grumpy ;)
But OP got his answer. Which makes me somewhat less grumpy. O:-)
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: Cpu Usage
« Reply #18 on: October 07, 2016, 07:21:31 pm »
If a process spawns 4 threads this will give a total of 5 threads including the process' primary thread. On a CPU with 4 cores this must (depending on thread affinity) cause 2 threads to occupy the same core. In order to make sure that the threads performing the primary task is not slowed down by any less important thread you can/should/must elevate the thread priority. I would consider it very sad if this fact is considered "nonsense" or "stupid" around here.

Buckling the system to the point of "denial of service" should be a non-issue in modern OS's. Even running i CPU-torture test with something like Prime95 or whatever will not render the system unresponsive, even though this will stress the cores much more than any of the code provided in this thread.

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: Cpu Usage
« Reply #19 on: October 07, 2016, 07:35:42 pm »
Buckling the system to the point of "denial of service" should be a non-issue in modern OS's. Even running i CPU-torture test with something like Prime95 or whatever will not render the system unresponsive, even though this will stress the cores much more than any of the code provided in this thread.
I tried the code with 6 threads (so 7 total) and it was at 86%. Next I tried 8 threads and I can tell you... My system became completely unresponsive. I have a i7 4 cores Windows 10. Had to do a hard test because I didn't build in the timeout yet. So yeah, it's really not a non-issue.

Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: Cpu Usage
« Reply #20 on: October 07, 2016, 07:40:07 pm »
I tried the code with 6 threads (so 7 total) and it was at 86%. Next I tried 8 threads and I can tell you... My system became completely unresponsive. I have a i7 4 cores Windows 10. Had to do a hard test because I didn't build in the timeout yet. So yeah, it's really not a non-issue.

Could this be because the code is run in a debugger?

P.S: I'll agree to the fact that loading the CPU with tpTimeCritical threads is bad practise, elevating priority should be done with care.
« Last Edit: October 07, 2016, 08:05:18 pm by Fungus »

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: Cpu Usage
« Reply #21 on: October 07, 2016, 08:03:45 pm »
I tried the code with 6 threads (so 7 total) and it was at 86%. Next I tried 8 threads and I can tell you... My system became completely unresponsive. I have a i7 4 cores Windows 10. Had to do a hard test because I didn't build in the timeout yet. So yeah, it's really not a non-issue.

Could this be because the code is run in a debugger?
Nope, even outside the debugger/IDE.

I've got the following code. I noticed that I have a 4 core 4 thread at home (while a 4 core 8 thread at work) :)

Don't worry... I build in a 20 second time-out so the machine doesn't hang too long :)

Without the tpTimeCritical the machine doesn't hang while it does go to "100%". But with the tpTimeCritical it becomes completely unresponsive for 20 seconds. Even on a modern OS and outside the debugger/IDE :)

Code: Pascal  [Select][+][-]
  1. program cpu100;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses Classes, Windows;
  6.  
  7. type
  8.   TMyThread = class(TThread)
  9.   protected
  10.     ThreadNumber: Integer;
  11.     procedure Execute; override;
  12.   public
  13.     constructor Create(CreateSuspended: Boolean; Number: Integer);
  14.   end;
  15.  
  16.   constructor TMyThread.Create(CreateSuspended: Boolean; Number: Integer);
  17.   begin
  18.     inherited Create(CreateSuspended);
  19.     ThreadNumber := Number;
  20.   end;
  21.  
  22.   procedure TMyThread.Execute;
  23.   var
  24.     Tick: int64;
  25.   begin
  26.     writeln('thread started ', ThreadNumber);
  27.     Tick := GetTickCount64;
  28.     repeat
  29.       // do nothing
  30.     until GetTickCount64 - Tick >= 20000; // 20 seconds
  31.     beep(300, 300);
  32.     writeln('thread done ', ThreadNumber);
  33.   end;
  34.  
  35. var
  36.   I: integer;
  37.   T: array[1..8] of TMyThread;
  38. begin
  39.   for I := 1 to 8 do
  40.   begin
  41.     T[I] := TMyThread.Create(true, I);
  42.     T[I].FreeOnTerminate := true;
  43.     T[I].Priority := tpTimeCritical;
  44.     T[I].Resume;
  45.   end;
  46.   writeln('wait until all threads are done and press enter');
  47.   readln;
  48. end.

Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: Cpu Usage
« Reply #22 on: October 07, 2016, 08:07:26 pm »
Without the tpTimeCritical the machine doesn't hang while it does go to "100%". But with the tpTimeCritical it becomes completely unresponsive for 20 seconds. Even on a modern OS and outside the debugger/IDE :)

I edited my post above while you were posting your example. I added "I'll agree to the fact that loading the CPU with tpTimeCritical threads is bad practise, elevating priority should be done with care."

fjabouley

  • Full Member
  • ***
  • Posts: 129
Re: Cpu Usage
« Reply #23 on: October 08, 2016, 08:20:11 am »
Hello all,
I got a stupid question (Thaddy, please don't kill me)


Is there a way to find the number of cores available in the CPU, in order to generate the number of threads required ?
« Last Edit: October 08, 2016, 08:32:02 am by jabounet »

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1869
Re: Cpu Usage
« Reply #24 on: October 08, 2016, 08:25:27 am »
With all credits from BigChimp (RIP):

Code: Pascal  [Select][+][-]
  1. function GetLogicalCpuCount: integer;
  2. // returns a good default for the number of threads on this system
  3. {$IF defined(windows)}
  4. // returns total number of processors available to system including logical hyperthreaded processors
  5. var
  6.   i: Integer;
  7.   ProcessAffinityMask, SystemAffinityMask: DWORD_PTR;
  8.   Mask: DWORD;
  9.   SystemInfo: SYSTEM_INFO;
  10. begin
  11.   if GetProcessAffinityMask(GetCurrentProcess, ProcessAffinityMask, SystemAffinityMask)
  12.   then begin
  13.     Result := 0;
  14.     for i := 0 to 31 do begin
  15.       Mask := DWord(1) shl i;
  16.       if (ProcessAffinityMask and Mask)<>0 then
  17.         inc(Result);
  18.     end;
  19.   end else begin
  20.     //can't get the affinity mask so we just report the total number of processors
  21.     GetSystemInfo(SystemInfo);
  22.     Result := SystemInfo.dwNumberOfProcessors;
  23.   end;
  24. end;
  25. {$ELSEIF defined(solaris)}
  26. //untested
  27.   begin
  28.     t = sysconf(_SC_NPROC_ONLN);
  29.   end;
  30. {$ELSEIF defined(freebsd) or defined(darwin)}
  31. var
  32.   mib: array[0..1] of cint;
  33.   len: cint;
  34.   t: cint;
  35. begin
  36.   mib[0] := CTL_HW;
  37.   mib[1] := HW_NCPU;
  38.   len := sizeof(t);
  39.   fpsysctl(pchar(@mib), 2, @t, @len, Nil, 0);
  40.   Result:=t;
  41. end;
  42. {$ELSEIF defined(linux)}
  43.   begin
  44.     Result:=sysconf(_SC_NPROCESSORS_ONLN);
  45.   end;
  46. {$ELSE}
  47.   // Fallback for other platforms
  48.   begin
  49.     Result:=1;
  50.   end;
  51. {$ENDIF}
  52. end.

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: Cpu Usage
« Reply #25 on: October 08, 2016, 08:27:02 am »
Is there a way to find the number of cores available in the CPU, in order to generate then number of threads required ?
Also note that you would need to know the number of threads a core can handle simultaneously. My work computer had 4 cores but can handle 2 threads per core simultaneously. (I also only had 60% with 4 threads) My home computer can only handle one thread per core.

Quote
A thread is a single line of commands that are getting processed, each application has at least one thread, most have multiples. A core is the physical hardware that works on the thread. In general a processor can only work on one thread per core, CPUs with hyper threading can work on up to two threads per core.

fjabouley

  • Full Member
  • ***
  • Posts: 129
Re: Cpu Usage
« Reply #26 on: October 08, 2016, 08:27:29 am »
Thanks !!


loaded

  • Hero Member
  • *****
  • Posts: 878
Re: Cpu Usage
« Reply #28 on: October 08, 2016, 09:52:09 am »
For you to answer, Very thanks , DonAlfredo,jabounet,rvk,Fungus,Thaddy
I understood:
I need a lot of work on threads.
The more memory computers have, the less memory people seem to use. 😅

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: Cpu Usage
« Reply #29 on: October 08, 2016, 11:40:13 am »
You may want to start here:
http://www.nickhodges.com/MultiThreadingInDelphi/ToC.html

Although from 2001 it is still the definitive guide to threading in Object Pascal.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018