Recent

Author Topic: Cpu Usage  (Read 14453 times)

loaded

  • Hero Member
  • *****
  • Posts: 878
Cpu Usage
« on: October 07, 2016, 09:36:38 am »
Hi all
When the sample code is running;
Code: Pascal  [Select][+][-]
  1. var
  2.   test:integer=0;
  3. begin
  4.  repeat
  5.  inc(test,1);
  6.  until test>High(integer);
  7.  

In the Process Explorer program when examining processor, So why not reach a hundred ?


The more memory computers have, the less memory people seem to use. 😅

Thaddy

  • Hero Member
  • *****
  • Posts: 18764
  • To Europe: simply sell USA bonds: dollar collapses
Re: Cpu Usage
« Reply #1 on: October 07, 2016, 09:40:55 am »
You have a quad core, so it's just 25% ;) One core fully occupied at 100%.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: Cpu Usage
« Reply #2 on: October 07, 2016, 09:47:05 am »
Thanks Thaddy
So, how do we get it to 100%
or
What should we do in order to use 4 cores simultaneously.
« Last Edit: October 07, 2016, 09:54:28 am by loaded »
The more memory computers have, the less memory people seem to use. 😅

mig-31

  • Sr. Member
  • ****
  • Posts: 308
Re: Cpu Usage
« Reply #3 on: October 07, 2016, 10:26:27 am »
TThread
Lazarus 4.0 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: Cpu Usage
« Reply #4 on: October 07, 2016, 10:43:42 am »
Thanks mig-31
See coincidence: Well you respond, I was looking into https://tr.wikipedia.org/wiki/mig-29 page  :)

TThread, Can you give an example regarding the issue?

The more memory computers have, the less memory people seem to use. 😅

Thaddy

  • Hero Member
  • *****
  • Posts: 18764
  • To Europe: simply sell USA bonds: dollar collapses
Re: Cpu Usage
« Reply #5 on: October 07, 2016, 10:59:21 am »
Very quick, very dirty but it will eat 100% cpu time for a little
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$ifdef fpc}{$mode delphi}{$endif}
  3. uses
  4.   {$ifdef unix}cthreads,{$endif}classes;
  5. type  
  6.   TMyThread= class(TThread)
  7.   procedure Execute;override;
  8.   end;
  9.   procedure TMyThread.Execute;
  10.   var
  11.     test:integer=0;
  12.     begin
  13.       repeat
  14.       inc(test);
  15.     until test>=High(integer);
  16.   end;
  17. var
  18.  T:Array[0..3] of TMyThread;
  19.  s:string;
  20. begin
  21.   S:='';
  22.  var
  23.  T:Array[0..3] of TMyThread;
  24.  s:string;
  25. begin
  26.   S:='';
  27.   T[0] := TMyThread.Create(false);T[0].FreeOnTerminate := true;
  28.   T[1] := TMyThread.Create(false);T[1].FreeOnTerminate := true;
  29.   T[2] := TMyThread.Create(false);T[2].FreeOnTerminate := true;
  30.   T[3] := TMyThread.Create(false);T[3].FreeOnTerminate := true;
  31.  
  32.   repeat
  33.     readln(s);
  34.   until length(s) > 0;
  35.  
  36.  end.
« Last Edit: October 07, 2016, 11:05:59 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: Cpu Usage
« Reply #6 on: October 07, 2016, 01:27:10 pm »
Very thanks Thaddy
Codes worked. CPU usage reached 100.
But the problem has occurred.
Threads are running slow:
The main thread running codes in 3.83 ms, 5:21 ms took the thread particles.
Is there any way we can speed up the thread?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10.  
  11. type
  12.   TMyThread= class(TThread)
  13.   procedure Execute;override;
  14.   public
  15.     timed:cardinal;
  16.     starts:integer;
  17.     stopts:integer;
  18.  
  19.   end;
  20.  
  21. type
  22.  
  23.   { TForm1 }
  24.  
  25.   TForm1 = class(TForm)
  26.     Button1: TButton;
  27.     Button2: TButton;
  28.     Button3: TButton;
  29.     procedure Button1Click(Sender: TObject);
  30.     procedure Button2Click(Sender: TObject);
  31.     procedure Button3Click(Sender: TObject);
  32.   private
  33.     { private declarations }
  34.   public
  35.     { public declarations }
  36.   end;
  37.  
  38. var
  39.   Form1: TForm1;
  40. var
  41.  T:Array[0..3] of TMyThread;
  42. implementation
  43.  
  44. {$R *.lfm}
  45.  
  46. { TForm1 }
  47.  
  48.  
  49. procedure TMyThread.Execute;
  50. var
  51.   test:integer=0;
  52.   crono:cardinal;
  53.   begin
  54.     crono:=GetTickCount;
  55.     for test:=starts to stopts do ;
  56.     timed:=GetTickCount-crono;
  57. end;
  58.  
  59. procedure TForm1.Button1Click(Sender: TObject);
  60. begin
  61.   T[0] := TMyThread.Create(false);
  62.   T[0].starts:=0;
  63.   T[0].stopts:=536870911;
  64.   T[0].FreeOnTerminate := true;
  65.  
  66.  
  67.   T[1] := TMyThread.Create(false);
  68.   T[1].starts:=536870912;
  69.   T[1].stopts:=1073741822;
  70.   T[1].FreeOnTerminate := true;
  71.  
  72.  
  73.   T[2] := TMyThread.Create(false);
  74.   T[2].starts:=1073741823;
  75.   T[2].stopts:=1610612733;
  76.   T[2].FreeOnTerminate := true;
  77.  
  78.  
  79.   T[3] := TMyThread.Create(false);
  80.   T[3].starts:=1610612734;
  81.   T[3].stopts:=2147483647;
  82.   T[3].FreeOnTerminate := true;
  83.  
  84. end;
  85.  
  86. procedure TForm1.Button2Click(Sender: TObject);
  87. var
  88.   test:integer=0;
  89.   crono:cardinal;
  90.   begin
  91.     crono:=GetTickCount;
  92.     repeat
  93.     inc(test);
  94.     until test>=High(integer);
  95.     showmessage('Total run time of Main thread : ' + floattostr((GetTickCount-crono)/1000));
  96. end;
  97.  
  98. procedure TForm1.Button3Click(Sender: TObject);
  99. begin
  100.   showmessage('Total run time of all threads : ' + floattostr(( T[0].timed+T[1].timed+T[2].timed+T[3].timed)/1000));
  101.  
  102. end;
  103.  
  104. end.
  105.  
« Last Edit: October 07, 2016, 01:29:17 pm by loaded »
The more memory computers have, the less memory people seem to use. 😅

Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: Cpu Usage
« Reply #7 on: October 07, 2016, 01:42:17 pm »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12706
  • FPC developer.
Re: Cpu Usage
« Reply #8 on: October 07, 2016, 01:46:02 pm »
Note that depending on the CPU it can be that this defeats turboboost.

IOW if only one core is occupied, the CPU increases its frequency, but if all four are active, it won't. Specially on quadcore laptop this can be significant frequency difference (e.g. mine has a 2.2GHz base frequency and a 3.2GHz turboboost)

Thaddy

  • Hero Member
  • *****
  • Posts: 18764
  • To Europe: simply sell USA bonds: dollar collapses
Re: Cpu Usage
« Reply #9 on: October 07, 2016, 02:55:25 pm »
Also code alignment is sometimes an issue on intels.
Try {$codealign=16}

Also note that threading has a slight speed disadvantage per thread, but still should run close to 4 times faster in total.
This is due to the caches and maybe a shared cache depending on processor model.
« Last Edit: October 07, 2016, 02:57:42 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Thaddy

  • Hero Member
  • *****
  • Posts: 18764
  • To Europe: simply sell USA bonds: dollar collapses
Re: Cpu Usage
« Reply #10 on: October 07, 2016, 03:00:07 pm »
Increase thread priority: http://www.freepascal.org/docs-html/rtl/classes/tthread.priority.html
That is nonsense really not necessary. The 4 threads will eat all cores. They are closed loops.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Thaddy

  • Hero Member
  • *****
  • Posts: 18764
  • To Europe: simply sell USA bonds: dollar collapses
Re: Cpu Usage
« Reply #11 on: October 07, 2016, 03:02:20 pm »
5:21 ms took the thread particles.
I put in a delay. The delay is the cause, not the threads.
Also note the example was really quick and really dirty.
don't use it in the real world, it was just to demonstrate how to get the cpu's fully loaded.
There is a lot to optimize besides caching, pipelining and code aligning.
« Last Edit: October 07, 2016, 03:05:20 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: Cpu Usage
« Reply #12 on: October 07, 2016, 03:55:01 pm »
Very Thanks Fungus,marcov,Thaddy

Hey Fungus,
Increase thread priority: http://www.freepascal.org/docs-html/rtl/classes/tthread.priority.html
The suggestion, I added the following code.
Code: Pascal  [Select][+][-]
  1. ...
  2. T[0].Priority:=tpTimeCritical;
  3. ...
  4.  
threads accelerated, but was undecided,
sometimes 0.9 ms
sometimes 1.82 ms
sometimes 4.9 ms
This can cause ?

Thaddy , Can you give a simple example,could you please ?
The more memory computers have, the less memory people seem to use. 😅

rvk

  • Hero Member
  • *****
  • Posts: 6948
Re: Cpu Usage
« Reply #13 on: October 07, 2016, 04:03:47 pm »
You start your thread directly when create it: TMyThread.Create(false);
(the false is for the CreateSuspended parameter)

You need to set the priority before executing the thread. Also... the assignment of starts and stops could be too late as the thread might have already started with the execution. So you could need to override Create() with your parameters and set them in create() of create the thread with CreateSuspended = true. Like so:

Code: Pascal  [Select][+][-]
  1.   T[0] := TMyThread.Create(true);
  2.   T[0].starts := 0;
  3.   T[0].stopts := 536870911;
  4.   T[0].FreeOnTerminate := true;
  5.   T[0].Priority := tpTimeCritical;
  6.   T[0].Resume;

O, and B.T.W. your program only runs for about 2 or 3 seconds for me. The loops are too short to really see the CPU go to 100%.
If you change it to "repeat until false;" then it will run forever (or until you kill the program).

4 thread got me to 60%
6 thread got me to 88%
On a machine with 4 cores and 8 threads according to CPU-Z.

Edit: Woops... well. if you really want the 100% you need to create 8 threads (as stated by cpu-z). It locked my system up completely (needed to do a hard reboot). Don't forget to make the loop longer but not indefinite because then you'll have to restart you machine too :)

What does having 4 cores and 8 threads?
« Last Edit: October 07, 2016, 04:23:40 pm by rvk »

Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: Cpu Usage
« Reply #14 on: October 07, 2016, 04:14:15 pm »
Very Thanks Fungus,marcov,Thaddy

Code: Pascal  [Select][+][-]
  1. ...
  2. T[0].Priority:=tpTimeCritical;
  3. ...
  4.  
threads accelerated, but was undecided,
sometimes 0.9 ms
sometimes 1.82 ms
sometimes 4.9 ms
This can cause ?

Nice to see a speed gain although someone considered the suggestion nonsense. You will never get "clean" CPU when your application is run in an operating system. Other (background) processes will require CPU once in a while and the system will grant this even if your process runs at highest priority. To get less variance you must increase the loop length - you could put in a time check that allows the loops to run for 10 (or more) seconds to decrease variance.

Code: Pascal  [Select][+][-]
  1. Var Tick: Int64;
  2. Begin
  3.   Tick:= GetTickCount64;
  4.   Repeat
  5.     //Loop code here
  6.   Until GetTickCount64 - Tick >= 10000; //10 seconds (10 times 1000 milliseconds)
  7. End;
  8.  

 

TinyPortal © 2005-2018