Threads and Processors

General discussion about anything related to Ultibo.
pik33
Posts: 852
Joined: Fri Sep 30, 2016 6:30 pm
Location: Poland
Contact:

Re: Threads and Processors

Postby pik33 » Wed Feb 06, 2019 8:16 am

Is it possible to configure dedicated cpu(s) at the very start of the system: to tell the scheduler to not allocate any threads to selected cores so you don't need to move them later?
User avatar
Ultibo
Site Admin
Posts: 2183
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Threads and Processors

Postby Ultibo » Wed Feb 06, 2019 11:22 pm

pik33 wrote:Is it possible to configure dedicated cpu(s) at the very start of the system: to tell the scheduler to not allocate any threads to selected cores so you don't need to move them later?

There is an option we added recently that allows setting the number of CPUs that Ultibo should use and leaving the rest untouched.

For example if you add CPU_COUNT=3 to the cmdline.txt file (there can be other options as well as long as they are all on one line) then Ultibo will only start 3 of the CPUs and leave the fourth one (CPU3) parked in a loop waiting to be started. You can set any number for CPU_COUNT between 1 and 3 (of course 4 is the default value).

It's possible to then include the necessary code in an application and send the idle CPU the address to start executing, the code has to perform the complete setup of the CPU including interrupts, stacks, exceptions, MMU, FPU etc and we don't have any example yet that shows how to do it but it is possible.

The dedicated CPU example is much easier to setup but the new CPU_COUNT option gives 100% control for those that might need it.
Ultibo.org | Make something amazing
https://ultibo.org
Brutus
Posts: 22
Joined: Sun Jan 20, 2019 1:24 pm

Re: Threads and Processors

Postby Brutus » Thu Feb 07, 2019 3:01 am

The code I posted earlier for migration implementing Garry's advices worked perfectly for me without modification.
I will just try to reduce the sleep time in the loops to speed things up a bit and use the interval parameter of the WorkerScheduleEx and TimerCreateEx which I set to 0, which is probably not the way to go.


Code: Select all

      Debug('Migration',9, 'Migrating thread "' + ThreadGetName(ThreadCurrent^.Handle) + '" to ' + CPUIDToString(TARGETCPU) + ' ');
      CountLimit := 0;
      Sleep(100);
      while (ThreadGetCPU(ThreadCurrent^.Handle) <> CPU_ID_0) and (CountLimit < 5) do
       begin
        Debug ('Migration',9, '#');
        Inc(CountLimit);
        if pos('PRIORITY_WORKER', ThreadGetName(ThreadCurrent^.Handle)) = 0 then
          WorkerScheduleEx(100,WORKER_FLAG_PRIORITY,TWorkerTask(ThreadCurrent^.Handle),nil,nil);
        if pos('PRIORITY_TIMER', ThreadGetName(ThreadCurrent^.Handle)) = 0 then
          TimerCreateEx(100,TIMER_STATE_DISABLED,TIMER_FLAG_PRIORITY, nil, nil);
        Sleep(100);
       end;
      DebugNL ('Migration',9, ' ');
      if CountLimit = 5 then
      begin
        DebugNl('Migration',9, 'Failed to migrate thread "' + ThreadGetName(ThreadCurrent^.Handle) + '"');
      end;


Another quick question: Since the threads 'priority worker' and 'priority timer' are suffixed with the CPU number, I would expect they are there to use that CPU ressources and it doesn't really make sense to migrate them somewhere else?

Since the CPU will stay dedicated and they will not be needed anymore, it might be more efficient to kill them instead of migrating them? (Sorry if I sound a bit like Hitler on this one :mrgreen: ).
User avatar
Ultibo
Site Admin
Posts: 2183
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Threads and Processors

Postby Ultibo » Thu Feb 07, 2019 9:39 am

Brutus wrote:The code I posted earlier for migration implementing Garry's advices worked perfectly for me without modification.

I would make some adjustments to that just to be sure you cover all your bases.

Add an empty procedure in your code like this:

Code: Select all

procedure EmptyWorkerTimer(Data:Pointer);
begin
  //Nothing, just an empty procedure
end;


Now modify your worker and timer calls like this:

Code: Select all

        if pos('PRIORITY_WORKER', ThreadGetName(ThreadCurrent^.Handle)) = 0 then
          WorkerScheduleEx(0,WORKER_FLAG_PRIORITY,TWorkerTask(@EmptyWorkerTimer),nil,nil);
        if pos('PRIORITY_TIMER', ThreadGetName(ThreadCurrent^.Handle)) = 0 then
          TimerCreateEx(1,TIMER_STATE_ENABLED ,TIMER_FLAG_PRIORITY, TTimerEvent(@EmptyWorkerTimer), nil);

That takes advantage of the fact that workers can be scheduled immediately (interval = 0) and timers can be any value from 1 millisecond or above (interval = 1). The EmptyWorkerTimer procedure just gives the request something to execute and avoids passing an invalid pointer.

Brutus wrote:Another quick question: Since the threads 'priority worker' and 'priority timer' are suffixed with the CPU number, I would expect they are there to use that CPU ressources and it doesn't really make sense to migrate them somewhere else?

The suffix isn't the CPU number, it is just a sequential number to make the threads easier to identify. All worker and timer threads wait on a message queue for work to do and if they are next in line they get the job regardless of which CPU they are running on. If you miss migrating any of them the system will eventually run into a problem with that.

Brutus wrote:Since the CPU will stay dedicated and they will not be needed anymore, it might be more efficient to kill them instead of migrating them?

Just a quick heads up that it is not (never ever) safe to kill a thread in Ultibo, same applies to Windows, Linux, BSD, Darwin etc, because there is no way to safely determine that the thread is not holding any critical resources (locks, semaphores etc).

You can ask for one or more worker threads to terminate by calling WorkerDecrease() or WorkerDecreaseEx() but the system will decide which threads are terminated so that won't help in this case.
Ultibo.org | Make something amazing
https://ultibo.org
Brutus
Posts: 22
Joined: Sun Jan 20, 2019 1:24 pm

Re: Threads and Processors

Postby Brutus » Fri Feb 08, 2019 2:06 am

Everything is clarified in my mind now!

Thank you Garry!

Return to “Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest