前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android thread Scheduling

Android thread Scheduling

作者头像
用户9732312
发布2022-05-13 18:48:44
4620
发布2022-05-13 18:48:44
举报
文章被收录于专栏:ADAS性能优化

Normal scheduling

Android is based on Linux and uses the Linux kernel’s scheduling mechanisms for determining scheduling policies. This is also true for Java code and threads. The Linux’s time sliced scheduling policy combines static and dynamic priorities. Processes can be given an initial priority from 19 to -20 (very low to very high priority). This priority will assure that higher priority processes will get more CPU time when when needed. These level are however dynamic, low level priority tasks that do not consume their CPU time will fine their dynamic priority increased. This dynamic behaviour results is an overall better responsiveness.In terms of dynamic priorities it is ensured that lower priority processes will always have a lower dynamic priority than processes with real-time priorities.Android uses two different mechanisms when scheduling the Linux kernel to perform process level scheduling

Real-time scheduling

The standard Linux kernel provides two real-time scheduling policies, SCHED_FIFO and SCHED_RR. The main real-time policy is SCHED_FIFO. It implements a first-in, first-out scheduling algorithm. When a SCHED_FIFO task starts running, it continues to run until it voluntarily yields the processor, blocks or is preempted by a higher-priority real-time task. It has no timeslices. All other tasks of lower priority will not be scheduled until it relinquishes the CPU. Two equal-priority SCHED_FIFO tasks do not preempt each other. SCHED_RR is similar to SCHED_FIFO, except that such tasks are allotted timeslices based on their priority and run until they exhaust their timeslice. Non-real-time tasks use the SCHED_NORMAL scheduling policy (older kernels had a policy named SCHED_OTHER).

see Documentation/cgroups/cgroups.txt

http://www.kernel.org/doc/Documentation/scheduler/sched-rt-group.txt

The default Android kernel is configured to allow group scheduling of real time processes and the file system to control this is mounted under /dev/cpuctl.

Android uses two different scheduling classes (using linux cgroups) bg_non_interactive and default (foreground). The configuration is that bg_non_interactive is low priority and can maximum utilize ~5% of the cpu (including all background tasks) and foreground ~95%. Forground means either an Activity or a service that is started foregound. At startup Services are running in bg_non_interactive unless they have been elevated to foreground scheduling group using startForeground (HMI applications are always set to foreground).

Binder and priorities

The binder mechanism also propagates priorities. That is the binder process called will run with the same priority as the caller.

JVM thread and process scheduling

An Android system will have a set of unix processes running . Some are native processes but many will be processes that run a Java virtual machine. These processes usually will be multi threaded, All android threads are native pthreads (no green threads). There are two ways to change the priority handling one by Calling Thread.setPriority that is part of the standard Java API and contains a value from MIN_PRIORITY(1) to MAX_PRIORITY(10).As all threads are pthreads these priorities will be mapped to unix process priorities (MIN_PRIORITY being 19 and MAX_PRIORITY -8).

代码语言:javascript
复制
-Combined dalvik/vm/Thread.c and  
-frameworks/base/include/utils/threads.h
Thread.priority , Java name    Android property name, Unix priority
1              MIN_PRIORITY,   ANDROID_PRIORITY_LOWEST, 19
2                              ANDROID_PRIORITY_BACKGROUND + 6,16
3                              ANDROID_PRIORITY_BACKGROUND + 3,13
4                              ANDROID_PRIORITY_BACKGROUND, 10
5              NORM_PRIORITY,  ANDROID_PRIORITY_NORMAL,0
6                              ANDROID_PRIORITY_NORMAL - 2 , -2
7                              ANDROID_PRIORITY_NORMAL - 4  , -4
8                             ANDROID_PRIORITY_URGENT_DISPLAY +3,-5
9                     ANDROID_PRIORITY_URGENT_DISPLAY + 2 ,   -6
10         MAX_PRIORITY   ANDROID_PRIORITY_URGENT_DISPLAY ,    -8

The second way to set priorities is to call android.os.Process.setThreadPriority(). This allows to set the pririty to higer priorities for that Declare: in your AndroidManifest and call Process.setThreadPriority(Process.myTid(), Process.THREAD_PRIORITY_URGENT_DISPLAY )

代码语言:javascript
复制
frameworks/base/include/utils/threads.h
    ANDROID_PRIORITY_LOWEST         =  19,

    /* use for background tasks */
    ANDROID_PRIORITY_BACKGROUND     =  10,

    /* most threads run at normal priority */
    ANDROID_PRIORITY_NORMAL         =   0,

    /* threads currently running a UI that the user is interacting with */
    ANDROID_PRIORITY_FOREGROUND     =  -2,

    /* the main UI thread has a slightly more favorable priority */
    ANDROID_PRIORITY_DISPLAY        =  -4,
    /* ui service treads might want to run at a urgent display (uncommon) */
    ANDROID_PRIORITY_URGENT_DISPLAY =  -8,

    /* all normal audio threads */
    ANDROID_PRIORITY_AUDIO          = -16,

    /* service audio threads (uncommon) */
    ANDROID_PRIORITY_URGENT_AUDIO   = -19,

    /* should never be used in practice. regular process might not
     * be allowed to use this level */
    ANDROID_PRIORITY_HIGHEST        = -20,

    ANDROID_PRIORITY_DEFAULT        = ANDROID_PRIORITY_NORMAL,
    ANDROID_PRIORITY_MORE_FAVORABLE = -1,
    ANDROID_PRIORITY_LESS_FAVORABLE = +1,
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2016-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Android性能优化 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Normal scheduling
  • Real-time scheduling
  • Binder and priorities
  • The binder mechanism also propagates priorities. That is the binder process called will run with the same priority as the caller.
  • JVM thread and process scheduling
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档