Setting thread's scheduling/priority at native (C/C++) side within .apk

173 views
Skip to first unread message

DAMIR LJUBIC

unread,
Dec 27, 2022, 3:36:49 PM12/27/22
to android-ndk

Problem: I have native (C++) code within the apk.

Explicit setting scheduling/priority doesn’t have any effect.

Description: There is 3rd party code  (objective C), where all threads

will be created with POSIX APIs, specifying for the scheduling SCHED_RR and the corresponding priority.

This seems to have no effect, since my apk has no root privileges.

It seems that in implementation of the related POSIX APIs (like pthread_setschedparam), kernel checks for the CAP_SYS_NICE capability for any unprivileged process.

capabilities(7) - Linux manual page (man7.org)

Facts:

  • SELinux currently running in permission mode
  • The .apk is not root process, there is no init.rc, where this capability can be set – applied

Is there any way to overcome this issue?

@note Using JNI to attach the native thread - to create the Java thread, from which the 

priority (CFS: niceness) will be set (Process.setThreadPriority) is working, but in Java world there is no way to set real-time scheduling

https://godbolt.org/z/x5W4WjdY9

Thanks in advance,

Damir

yu...@unity3d.com

unread,
Jan 3, 2023, 2:23:20 AM1/3/23
to android-ndk
You can't set real-time scheduling in user space on non-rooted devices on Android.
setThreadPriority() is setting only niceness, that should be mentioned in the documentation.
that's all "by design". will let google folks speak but guess - security and safety being the reason, and so that the apps won't overuse this superpower.
Why would you need real-time scheduling for your thread?

Yury

enh

unread,
Jan 6, 2023, 8:03:53 PM1/6/23
to andro...@googlegroups.com
On Mon, Jan 2, 2023 at 11:23 PM 'yu...@unity3d.com' via android-ndk <andro...@googlegroups.com> wrote:
You can't set real-time scheduling in user space on non-rooted devices on Android.
setThreadPriority() is setting only niceness, that should be mentioned in the documentation.
that's all "by design". will let google folks speak but guess - security and safety being the reason, and so that the apps won't overuse this superpower.

yeah, "exactly what he said" :-)

funnily enough, when i inherited bionic it didn't report failures to set scheduler parameters from a pthread_create() call. i fixed that, and we found that basically everything failed because everyone was asking for real-time priority --- which they'd never actually been granted, but now those of them that were checking for errors were failing. i had to put in a hack that meant we'd continue to not report errors to any apk whose targetSdkVersion was lower than the new version.
 
Why would you need real-time scheduling for your thread?

Yury

On Tuesday, December 27, 2022 at 9:36:49 PM UTC+1 damir.m...@gmail.com wrote:

Problem: I have native (C++) code within the apk.

Explicit setting scheduling/priority doesn’t have any effect.

Description: There is 3rd party code  (objective C), where all threads

will be created with POSIX APIs, specifying for the scheduling SCHED_RR and the corresponding priority.

This seems to have no effect, since my apk has no root privileges.

It seems that in implementation of the related POSIX APIs (like pthread_setschedparam), kernel checks for the CAP_SYS_NICE capability for any unprivileged process.

capabilities(7) - Linux manual page (man7.org)

Facts:

  • SELinux currently running in permission mode
  • The .apk is not root process, there is no init.rc, where this capability can be set – applied

Is there any way to overcome this issue?

@note Using JNI to attach the native thread - to create the Java thread, from which the 

priority (CFS: niceness) will be set (Process.setThreadPriority) is working, but in Java world there is no way to set real-time scheduling

https://godbolt.org/z/x5W4WjdY9

Thanks in advance,

Damir

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/16bd9c83-56eb-4df7-998b-8a108b828f07n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages