Set thread in Real Time priority (SCHED_FIFO) or default (SCHED_OTHER)

Post Reply
Isard7777
Posts: 17
Joined: Fri Aug 10, 2018 5:25 pm
languages_spoken: english french
ODROIDs: C2
Has thanked: 0
Been thanked: 0
Contact:

Set thread in Real Time priority (SCHED_FIFO) or default (SCHED_OTHER)

Unread post by Isard7777 » Wed Jan 15, 2020 2:20 am

Hello,

I've tried to implement a multiple thread application where I need to set some thread in real time priority because they cannot be delayed in any case.

I've used the The Linux scheduling APIs (http://manpages.ubuntu.com/manpages/bio ... ler.2.html and http://manpages.ubuntu.com/manpages/bio ... ity.2.html) in the following code that I can call in any thread :

Code: Select all


#include <pthread.h>
#include <sched.h>
#include <sys/resource.h>
#include <cstdio>
#include <cstdint>

static bool THREADPRIO_SetThreadPolicy(int Policy_s16);

bool THREADPRIO_SetProcessPriority(const uint32_t NiceValue_u32)
{
	auto ReturnCode_b = false;
   if (setpriority(PRIO_PROCESS, 0, NiceValue_u32) != 0)
      printf("Error : Could not set priority in thread\n");
   else
      ReturnCode_b = true;

   return ReturnCode_b;
}

bool THREADPRIO_SetThreadRealTimePolicy()
{	
   const auto ReturnCode_b = THREADPRIO_SetThreadPolicy(SCHED_FIFO);

   if (ReturnCode_b)
      printf("Error : Could not set real time priority\n");   	
   	
   return ReturnCode_b;
}

bool THREADPRIO_SetThreadIdlePolicy()
{
   const auto ReturnCode_b = THREADPRIO_SetThreadPolicy(SCHED_IDLE);

   if (ReturnCode_b)
      printf("Error : Could not set idle priority\n");   	
   	
   return ReturnCode_b;
}

bool THREADPRIO_SetThreadDefaultPolicy()
{
   const auto ReturnCode_b = THREADPRIO_SetThreadPolicy(SCHED_OTHER);

   if (ReturnCode_b)
      printf("Error : Could not set default priority\n");   	
   	
   return ReturnCode_b;
}


bool THREADPRIO_SetThreadPolicy(int Policy_s16)
{
   auto ReturnCode_b = false;
   const auto ThisThread_X = pthread_self();
   struct sched_param SchedulerParams_X {};

   SchedulerParams_X.sched_priority = sched_get_priority_max(Policy_s16);
   ReturnCode_b = pthread_setschedparam(ThisThread_X, Policy_s16, &SchedulerParams_X) == 0 ? true : false;
   if (ReturnCode_b)
   {
      auto lPolicy_s16 = 0;
      ReturnCode_b = pthread_getschedparam(ThisThread_X, &lPolicy_s16, &SchedulerParams_X) == 0 ? true : false;

      if (Policy_s16 != lPolicy_s16)
         printf("Error : Priority is not set\n");
   }   
   	
   return ReturnCode_b;
}
The call of THREADPRIO_SetThreadPriority works well but are not enough and I want to set each thread's scheduler with the function THREADPRIO_SetThreadPolicy.

Unfortunately, this is not works, when I call for SCHED_IDLE and SCHED_OTHER, I have a return error (Not permitted operation) and when I call it the argument SCHED_FIFO, there is no error code but the command line

Code: Select all

chrt -p <pid>
for the thread pid says that a SCHED_OTHER scheduler.

Also, It is impossible to set manualy the scheduler through the command line

Code: Select all

chrt -o -f -p <priority> <pid>
and the following error is displayed :

Code: Select all

chrt: failed to set pid <pid>'s policy: Operation not permitted
Anybody has an idea how to change scheduler priority on Odroid C2?

Thanks in advance

User avatar
rooted
Posts: 7142
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Has thanked: 393 times
Been thanked: 109 times
Contact:

Re: Set thread in Real Time priority (SCHED_FIFO) or default (SCHED_OTHER)

Unread post by rooted » Wed Jan 15, 2020 6:17 am

Have you tried using sudo?

Isard7777
Posts: 17
Joined: Fri Aug 10, 2018 5:25 pm
languages_spoken: english french
ODROIDs: C2
Has thanked: 0
Been thanked: 0
Contact:

Re: Set thread in Real Time priority (SCHED_FIFO) or default (SCHED_OTHER)

Unread post by Isard7777 » Wed Jan 15, 2020 5:35 pm

My software is executed with the root user and the command as well. "sudo" do not change the behaviour.

User avatar
rooted
Posts: 7142
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Has thanked: 393 times
Been thanked: 109 times
Contact:

Re: Set thread in Real Time priority (SCHED_FIFO) or default (SCHED_OTHER)

Unread post by rooted » Wed Jan 15, 2020 7:51 pm

Isard7777 wrote:My software is executed with the root user and the command as well. "sudo" do not change the behaviour.
You didn't state you were running as root.

Isard7777
Posts: 17
Joined: Fri Aug 10, 2018 5:25 pm
languages_spoken: english french
ODROIDs: C2
Has thanked: 0
Been thanked: 0
Contact:

Re: Set thread in Real Time priority (SCHED_FIFO) or default (SCHED_OTHER)

Unread post by Isard7777 » Thu Jan 16, 2020 12:13 am

Yes, I've forgotten this detail. Sorry.

brad
Posts: 1029
Joined: Tue Mar 29, 2016 1:22 pm
languages_spoken: english
ODROIDs: C2 N1 N2 H2 (64 bit ftw)
Location: Australia
Has thanked: 23 times
Been thanked: 51 times
Contact:

Re: Set thread in Real Time priority (SCHED_FIFO) or default (SCHED_OTHER)

Unread post by brad » Wed Jan 22, 2020 6:31 pm

Isard7777 wrote:
Wed Jan 15, 2020 2:20 am

Anybody has an idea how to change scheduler priority on Odroid C2?

Thanks in advance
It would appear that the kernel your are using does not support fifo? Which one is it, (uname -a) I thought that 3.x.y one did?

Isard7777
Posts: 17
Joined: Fri Aug 10, 2018 5:25 pm
languages_spoken: english french
ODROIDs: C2
Has thanked: 0
Been thanked: 0
Contact:

Re: Set thread in Real Time priority (SCHED_FIFO) or default (SCHED_OTHER)

Unread post by Isard7777 » Thu Jan 23, 2020 9:10 pm

This is the 3.16.60+ (the configuration has been cutomized for the purpose of the application but the scheduling policies are normaly supported).

I have found that I was unable to set manually these policies or by running code by command line but, if I configure the software as a linux service launch at start, scheduling policies are working.

brad
Posts: 1029
Joined: Tue Mar 29, 2016 1:22 pm
languages_spoken: english
ODROIDs: C2 N1 N2 H2 (64 bit ftw)
Location: Australia
Has thanked: 23 times
Been thanked: 51 times
Contact:

Re: Set thread in Real Time priority (SCHED_FIFO) or default (SCHED_OTHER)

Unread post by brad » Fri Jan 24, 2020 4:05 pm

Isard7777 wrote:
Thu Jan 23, 2020 9:10 pm
This is the 3.16.60+ (the configuration has been cutomized for the purpose of the application but the scheduling policies are normaly supported).

I have found that I was unable to set manually these policies or by running code by command line but, if I configure the software as a linux service launch at start, scheduling policies are working.
I think it will come down to limits.conf or .d as it works with your systemd environment, what is the output of "ulimit -a" in your session when it fails?

Also can try to add the following to /etc/security/limits.conf to see if it helps (if odroid is your user). make sure to restart your session / c2 to pick up the change.

Code: Select all

odroid   -  rtprio     99
odroid   -  memlock    unlimited

Post Reply

Return to “Ubuntu”

Who is online

Users browsing this forum: No registered users and 0 guests