change native thread priority on Android in c/c++

13,436

Solution 1

Here's simple answer: use int nice(int increment). It's declared in unistd.h

here's the code of the function from Android:

int nice(int increment)
{
    int  priority = getpriority(PRIO_PROCESS, 0);
    return setpriority( PRIO_PROCESS, 0, priority+increment);
}

so, setpriority can be used to set thread priority.

I actually got difference as expected in my code by using nice(-10) or nice(10)

Solution 2

Android threads are Linux threads. Nothing special about them.

setpriority() alters the thread's "nice" value, which affects the priority at which the thread is scheduled.

pthread_setschedparam() can be used to change the policy as well.

Generally speaking, you can't raise the priority or change its policy without elevated privileges (CAP_SYS_NICE). You can lower it, or raise it back to "normal" priority.

If you use adb shell ps -t -p you can see the priorities assigned to every thread. This information is also included in Dalvik stack dumps.

Dalvik uses setpriority() to temporarily raise the GC thread's priority if necessary (to avoid priority inversion). If you look at this bit of code around line 625 you can see it. You can also see the call to set_sched_policy(), an Android library function that changes which cgroup the thread is in.

Solution 3

There is no way to set the priority of a POSIX thread in Android. The nice() and setpriority() functions configure the priority of a process, not of a single thread.

Share:
13,436
Pavel P
Author by

Pavel P

Updated on June 05, 2022

Comments

  • Pavel P
    Pavel P almost 2 years

    Insanely obscure pthread api for thread priority not only outlandishly incomprehensible, but also it just doesn't work on android.

    So, is there a way for me to reduce or increase a thread's priority?

    int currentPolicy;
    struct sched_param sched;
    status = pthread_getschedparam(pthread_self(), &currentPolicy, &sched);
    printf("sched.sched_priority:%d currentPolicy:%d", sched.sched_priority, currentPolicy);
    printf("priority min/max:%d/%d", sched_get_priority_min(currentPolicy), sched_get_priority_max(currentPolicy));
    

    outputs:

    sched.sched_priority:0 currentPolicy:0

    priority min/max:0/0

    Also, no matter what I do, pthread_setschedparam always return an error for me (Invalid argument). In my code, I need to reduce priority of my handler thread to make sure that the other thread uses more cpu time. Or, alternatively, I could boost thread priority of my other thread that I want to use as more cpu compared to other threads in my process.

    Is there any way for me to do that? I use native c++ code only.

    Edit: From this post looks like android's threads are some kind of light weight processes and to modify priorities for them setpriority should be used. It looks that the function works (getpriority before and after indicates that priority was modified). However, I don't see expected effect in my app.

  • Pavel P
    Pavel P almost 11 years
    That's close. nice(int) should be used on android which uses setpriority. Everything else just doesn't work. I also tried to search entire android source code and other functions aren't used.
  • voidStar
    voidStar over 10 years
    that incorrect, i have verified it myself. nice & set-priority(default) apply to current process which in android native world a linux LWP. your confusion may be stemming from fact that the priorities are inhereted by all launched threads. so if you set prio before launching child/worker threads the'll all have that prio.
  • Remy Lebeau
    Remy Lebeau almost 10 years
    Buy that does not mean you can change the policy/priority of a given thread after it is created/running.
  • RonTLV
    RonTLV over 6 years
    where should I place this code ? is it for threads or the process ? also, what is the return value ? thanks
  • nmr
    nmr over 6 years
    FWIW looks like this is declared in <sys/resource.h>
  • Pavel P
    Pavel P over 6 years
    @nmr 5 years ago it might not have been the case
  • Jenix
    Jenix over 4 years
    So, you mean, either nice() or setpriority() can be used? Also, is there any way for a user-level application to alter sched_priority other than SCHED_OTHER on a non-rooted Android device? I don't think so but just asking.
  • Jenix
    Jenix over 4 years
    In your original question, about setpriority(), you said "However, I don't see expected effect in my app", and then you wrote your own answer like this.. With setpriority(), did you actually see any difference or not?
  • Pavel P
    Pavel P over 4 years
    @Jenix I don't know what I meant 6 years ago :) Perhaps I didn't use setpriority correctly. My own answer shows what works on android.
  • Constantin Geier
    Constantin Geier about 4 years
    I agree. There is no way to set the priority of ndk thread(s). Calling setpriority() with PRIO_PROCESS changes some priority (yet unknown to me which exactly) but In a simple test I created 2 std::thread and was calling setpriority() with different priorities in them - they alternating set the priority of themself and its coaleque.
  • Constantin Geier
    Constantin Geier about 4 years
    This won't set the priority of the current thread, but the current process.