Setting thread affinity for threads in a native library

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting thread affinity for threads in a native library

John Dallman
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

One of my shared libraries can split its long tasks into several smaller packets, and create additional threads to run them in parallel for better performance. This is used on Windows, Mac and Linux. I have not enabled this functionality on Android yet, because customers aren't asking for it, and it would require a lot more routine testing. 

I am designing interfaces to allow the applications that use this library to control the use of additional threads. As far as I've been able to discover, Android does not seem to have functions to control thread affinity, as of NDK21. One can set affinity for the whole process, with sched_setaffinity(), but not for individual threads. 

Have I missed something? Has this been deliberately omitted from Android? 

Thanks,

John Dallman

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgkaSdz8YXnMWt%3DjPDCd13st5ONCaRTd%2Bxn9z-%2BuHUs2kA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Setting thread affinity for threads in a native library

Elliott Hughes
you just need to pass the thread id pid_t to sched_setaffinity(). see https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html for more.

(you may have noticed that we link to man7.org in many of our man pages, but <sched.h> isn't one of them. as penance i'll get that done this afternoon...)

Android *doesn't* have pthread_setaffinity_np(), in part because sched_setaffinity() has been available "forever" and if we added pthread_setaffinity_np() today you wouldn't be able to use it for years anyway and it's just a one-line wrapper around sched_setaffinity().

On Friday, February 19, 2021 at 4:34:56 AM UTC-8 [hidden email] wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

One of my shared libraries can split its long tasks into several smaller packets, and create additional threads to run them in parallel for better performance. This is used on Windows, Mac and Linux. I have not enabled this functionality on Android yet, because customers aren't asking for it, and it would require a lot more routine testing. 

I am designing interfaces to allow the applications that use this library to control the use of additional threads. As far as I've been able to discover, Android does not seem to have functions to control thread affinity, as of NDK21. One can set affinity for the whole process, with sched_setaffinity(), but not for individual threads. 

Have I missed something? Has this been deliberately omitted from Android? 

Thanks,

John Dallman

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/00e89413-9a50-449f-be04-e57eb54a66c2n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Setting thread affinity for threads in a native library

Yury Habets
We're using

int result = syscall(__NR_sched_setaffinity, tid, sizeof(affinity), &affinity);

and it works well. Make sure to pass -1 as the affinity by default and not 0.
On Friday, February 19, 2021 at 10:09:51 PM UTC+1 [hidden email] wrote:
you just need to pass the thread id pid_t to sched_setaffinity(). see https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html for more.

(you may have noticed that we link to man7.org in many of our man pages, but <sched.h> isn't one of them. as penance i'll get that done this afternoon...)

Android *doesn't* have pthread_setaffinity_np(), in part because sched_setaffinity() has been available "forever" and if we added pthread_setaffinity_np() today you wouldn't be able to use it for years anyway and it's just a one-line wrapper around sched_setaffinity().

On Friday, February 19, 2021 at 4:34:56 AM UTC-8 [hidden email] wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

One of my shared libraries can split its long tasks into several smaller packets, and create additional threads to run them in parallel for better performance. This is used on Windows, Mac and Linux. I have not enabled this functionality on Android yet, because customers aren't asking for it, and it would require a lot more routine testing. 

I am designing interfaces to allow the applications that use this library to control the use of additional threads. As far as I've been able to discover, Android does not seem to have functions to control thread affinity, as of NDK21. One can set affinity for the whole process, with sched_setaffinity(), but not for individual threads. 

Have I missed something? Has this been deliberately omitted from Android? 

Thanks,

John Dallman

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/fd845c27-cad8-4bbe-af04-4ae8fad01accn%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Setting thread affinity for threads in a native library

John Dallman
In reply to this post by Elliott Hughes
OK. How do I get the pid_t of a thread other than the current thread? I have their pthread_t identifiers, but don't know how to go from one to the other. 

Thanks,

John

On Fri, Feb 19, 2021 at 9:09 PM 'Elliott Hughes' via android-ndk <[hidden email]> wrote:
you just need to pass the thread id pid_t to sched_setaffinity(). see https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html for more.

(you may have noticed that we link to man7.org in many of our man pages, but <sched.h> isn't one of them. as penance i'll get that done this afternoon...)

Android *doesn't* have pthread_setaffinity_np(), in part because sched_setaffinity() has been available "forever" and if we added pthread_setaffinity_np() today you wouldn't be able to use it for years anyway and it's just a one-line wrapper around sched_setaffinity().

On Friday, February 19, 2021 at 4:34:56 AM UTC-8 [hidden email] wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

One of my shared libraries can split its long tasks into several smaller packets, and create additional threads to run them in parallel for better performance. This is used on Windows, Mac and Linux. I have not enabled this functionality on Android yet, because customers aren't asking for it, and it would require a lot more routine testing. 

I am designing interfaces to allow the applications that use this library to control the use of additional threads. As far as I've been able to discover, Android does not seem to have functions to control thread affinity, as of NDK21. One can set affinity for the whole process, with sched_setaffinity(), but not for individual threads. 

Have I missed something? Has this been deliberately omitted from Android? 

Thanks,

John Dallman

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/00e89413-9a50-449f-be04-e57eb54a66c2n%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgn9%2BgoqXkUQQchV5JdEW_3e2okKGDuFgakN_hng75_9pA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Setting thread affinity for threads in a native library

Elliott Hughes
if you're targeting API level >= 21, you can use pthread_gettid_np() to convert a pthread_t to a pid_t (for any thread, not just the caller). if you need to support older API levels, the easiest way is to have each pthread call gettid().

On Mon, Feb 22, 2021 at 3:59 AM John Dallman <[hidden email]> wrote:
OK. How do I get the pid_t of a thread other than the current thread? I have their pthread_t identifiers, but don't know how to go from one to the other. 

Thanks,

John

On Fri, Feb 19, 2021 at 9:09 PM 'Elliott Hughes' via android-ndk <[hidden email]> wrote:
you just need to pass the thread id pid_t to sched_setaffinity(). see https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html for more.

(you may have noticed that we link to man7.org in many of our man pages, but <sched.h> isn't one of them. as penance i'll get that done this afternoon...)

Android *doesn't* have pthread_setaffinity_np(), in part because sched_setaffinity() has been available "forever" and if we added pthread_setaffinity_np() today you wouldn't be able to use it for years anyway and it's just a one-line wrapper around sched_setaffinity().

On Friday, February 19, 2021 at 4:34:56 AM UTC-8 [hidden email] wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

One of my shared libraries can split its long tasks into several smaller packets, and create additional threads to run them in parallel for better performance. This is used on Windows, Mac and Linux. I have not enabled this functionality on Android yet, because customers aren't asking for it, and it would require a lot more routine testing. 

I am designing interfaces to allow the applications that use this library to control the use of additional threads. As far as I've been able to discover, Android does not seem to have functions to control thread affinity, as of NDK21. One can set affinity for the whole process, with sched_setaffinity(), but not for individual threads. 

Have I missed something? Has this been deliberately omitted from Android? 

Thanks,

John Dallman

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/00e89413-9a50-449f-be04-e57eb54a66c2n%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgn9%2BgoqXkUQQchV5JdEW_3e2okKGDuFgakN_hng75_9pA%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAJgzZoq5Ub-N4ta9YOrPEd_1GkZHoAEeo%3DfXBfAtr2%3D0d9JRig%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Setting thread affinity for threads in a native library

Elliott Hughes
In reply to this post by Yury Habets
new code should just use sched_setaffinity() in libc now. it's been there since API level 14 in 2011 (https://android.googlesource.com/platform/bionic/+/72e6fd42421dca80fb2776a9185c186d4a04e5f7), and the NDK doesn't support targeting anything older than jellybean at this point. but you guys may well have been doing this from before API 14 :-)

On Mon, Feb 22, 2021 at 3:05 PM [hidden email] <[hidden email]> wrote:
We're using

int result = syscall(__NR_sched_setaffinity, tid, sizeof(affinity), &affinity);

and it works well. Make sure to pass -1 as the affinity by default and not 0.
On Friday, February 19, 2021 at 10:09:51 PM UTC+1 [hidden email] wrote:
you just need to pass the thread id pid_t to sched_setaffinity(). see https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html for more.

(you may have noticed that we link to man7.org in many of our man pages, but <sched.h> isn't one of them. as penance i'll get that done this afternoon...)

Android *doesn't* have pthread_setaffinity_np(), in part because sched_setaffinity() has been available "forever" and if we added pthread_setaffinity_np() today you wouldn't be able to use it for years anyway and it's just a one-line wrapper around sched_setaffinity().

On Friday, February 19, 2021 at 4:34:56 AM UTC-8 [hidden email] wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

One of my shared libraries can split its long tasks into several smaller packets, and create additional threads to run them in parallel for better performance. This is used on Windows, Mac and Linux. I have not enabled this functionality on Android yet, because customers aren't asking for it, and it would require a lot more routine testing. 

I am designing interfaces to allow the applications that use this library to control the use of additional threads. As far as I've been able to discover, Android does not seem to have functions to control thread affinity, as of NDK21. One can set affinity for the whole process, with sched_setaffinity(), but not for individual threads. 

Have I missed something? Has this been deliberately omitted from Android? 

Thanks,

John Dallman

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/fd845c27-cad8-4bbe-af04-4ae8fad01accn%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAJgzZorgZcttwsFiowifATjw6bLady6q_V4SGRZM0hzX%3DeVJKA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Setting thread affinity for threads in a native library

John Dallman
In reply to this post by Elliott Hughes
Thanks, everyone. I'm good now.

John

On Mon, Feb 22, 2021 at 5:01 PM 'enh' via android-ndk <[hidden email]> wrote:
if you're targeting API level >= 21, you can use pthread_gettid_np() to convert a pthread_t to a pid_t (for any thread, not just the caller). if you need to support older API levels, the easiest way is to have each pthread call gettid().

On Mon, Feb 22, 2021 at 3:59 AM John Dallman <[hidden email]> wrote:
OK. How do I get the pid_t of a thread other than the current thread? I have their pthread_t identifiers, but don't know how to go from one to the other. 

Thanks,

John

On Fri, Feb 19, 2021 at 9:09 PM 'Elliott Hughes' via android-ndk <[hidden email]> wrote:
you just need to pass the thread id pid_t to sched_setaffinity(). see https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html for more.

(you may have noticed that we link to man7.org in many of our man pages, but <sched.h> isn't one of them. as penance i'll get that done this afternoon...)

Android *doesn't* have pthread_setaffinity_np(), in part because sched_setaffinity() has been available "forever" and if we added pthread_setaffinity_np() today you wouldn't be able to use it for years anyway and it's just a one-line wrapper around sched_setaffinity().

On Friday, February 19, 2021 at 4:34:56 AM UTC-8 [hidden email] wrote:
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

One of my shared libraries can split its long tasks into several smaller packets, and create additional threads to run them in parallel for better performance. This is used on Windows, Mac and Linux. I have not enabled this functionality on Android yet, because customers aren't asking for it, and it would require a lot more routine testing. 

I am designing interfaces to allow the applications that use this library to control the use of additional threads. As far as I've been able to discover, Android does not seem to have functions to control thread affinity, as of NDK21. One can set affinity for the whole process, with sched_setaffinity(), but not for individual threads. 

Have I missed something? Has this been deliberately omitted from Android? 

Thanks,

John Dallman

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/00e89413-9a50-449f-be04-e57eb54a66c2n%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgn9%2BgoqXkUQQchV5JdEW_3e2okKGDuFgakN_hng75_9pA%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAJgzZoq5Ub-N4ta9YOrPEd_1GkZHoAEeo%3DfXBfAtr2%3D0d9JRig%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgkPx6NFOGoOYWJa%3DryECXFL7eCnjVWwLw63dpAA3Py38Q%40mail.gmail.com.