前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CPU Cgroup in android N and Huawei Mate 9 Implement

CPU Cgroup in android N and Huawei Mate 9 Implement

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

The Android framework knows a lot about tasks that are running and can help inform scheduler decisions. AOSP uses CPU Cgroup to implement tasks scheduler in framework in android N.

App schedule policy

There are 7 app task schedule policies defined in android N.

typedef enum { SP_DEFAULT = -1, SP_BACKGROUND= 0, SP_FOREGROUND= 1, //Not used in the framework SP_SYSTEM = 2, // can't be used with set_sched_policy() SP_AUDIO_APP = 3, SP_AUDIO_SYS = 4, SP_TOP_APP = 5, SP_CNT, SP_MAX = SP_CNT - 1, SP_SYSTEM_DEFAULT= SP_FOREGROUND, }SchedPolicy;

Thread SP

There are two classification threads in Andorid OS that are Java thread and native thread. Both threads are created by pthread_create in the end.It will set the JAVA thread SP when create .While it does not set the SP for native thread when create it. Framework provides 3 methods to set the thread SP.

  • Process.setThreadPriority() for java thread
  • Threads.androidSetThreadPriority() for native thread
  • set_cpuset_policy() for native thread Note: Framework only sets the threads SP to SP_BACKGROUND or SP_FOREGROUND by calling Process.setThreadPriority() or Threads.androidSetThreadPriority().

Thread::SetNativePriority(int newPriority) { ... if (newNice >= ANDROID_PRIORITY_BACKGROUND){ set_sched_policy(tid, SP_BACKGROUND); }else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND){ set_sched_policy(tid,SP_FOREGROUND); } ... }

int androidSetThreadPriority(pid_t tid, int pri){

... if (pri >= ANDROID_PRIORITY_BACKGROUND) { rc = set_sched_policy(tid, SP_BACKGROUND); }else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND){ rc = set_sched_policy(tid, SP_FOREGROUND); }

....

}

CPU Cgroup

There are 4 cgroups defined cpu, cpuset, cpuboots and cpuschedtune so far in android N.

CPU_CTL

This cgroup uses two different scheduling classes 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 foreground. At startup Services are running in bg_non_interactive unless they have been elevated to foreground scheduling group using startForeground. All the app tasks which belong to THREAD_GROUP_BG_NONINTERACTIVE BP are put into the bg_non_interactive tasks and other tasks to foreground.

int set_sched_policy(int tid, SchedPolicy policy)

{

if (__sys_supports_schedgroups) {

int fd;

switch (policy) {

case SP_BACKGROUND:

fd = bg_cgroup_fd;

break;

case SP_FOREGROUND:

case SP_AUDIO_APP:

case SP_AUDIO_SYS:

fd = fg_cgroup_fd;

break;

default:

fd = -1;

break;

}

if (add_tid_to_cgroup(tid, fd) != 0) {

if (errno != ESRCH && errno != ENOENT)

return -errno;

}

}

}

  • Foreground

/dev/cpuctl/cpu.shares 1024 /dev/cpuctl/cpu.rt_runtime_us 800000 /dev/cpuctl/cpu.rt_period_us 1000000

  • bg_non_interactive

/dev/cpuctl/bg_non_interactive/cpu.shares 52 /dev/cpuctl/bg_non_interactive/cpu.rt_runtime_us 700000 /dev/cpuctl/bg_non_interactive/cpu.rt_period_us 1000000

CPUSET

Framework use CPUSET implement app tasks run different CPUS according to tasks SP and runtime state in order to get the better UX and power save. There are two scenarios in framework to use CPUSET. One is start app and the other is app runtime.

App start

Framework will migrate all the tasks from foreground group to foreground_boost cgroup when the app start.After 3000ms ,it will migrate back the tasks from foreground_boost cgroup to foreground cgroup.

Note,this feature shall enable the CPUSET and USE_SCHED_BOOST.

App runtime

Framework classifies all the tasks into 4 schedule group according to thread SP. Correspondingly,there are 4 cgroups in CPUSET,the default design as below,

  • Foreground Tasks run all cores.
  • Background Tasks run little cores.
  • System-background Tasks run all little cores for system processes that shouldn’t run on big cores.
  • TOP-APP Tasks run all cores big cores

int set_cpuset_policy(int tid, SchedPolicy policy)

{

// in the absence of cpusets, use the old sched policy

#ifndef USE_CPUSETS

return set_sched_policy(tid, policy);

#else

if (tid == 0) {

tid = gettid();

}

policy = _policy(policy);

pthread_once(&the_once, __initialize);

int fd;

switch (policy) {

case SP_BACKGROUND:

fd = bg_cpuset_fd;

break;

case SP_FOREGROUND:

case SP_AUDIO_APP:

case SP_AUDIO_SYS:

fd = fg_cpuset_fd;

break;

default:

fd = -1;

break;

}

if(add_tid_to_cgroup(tid, fd) != 0) {

if (errno != ESRCH && errno != ENOENT)

return -errno;

}

return 0;

#endif

}

  • Foreground

//CPUS use to boost /dev/cpuset/foreground/cpus /dev/cpuset/foreground/mems //tasksput into this cgroup /dev/cpuset/foreground/tasks

  • Top-app

/dev/cpuset/top-app/cpus /dev/cpuset/top-app/mems //tasks put into this cgroup /dev/cpuset/top-app/tasks

  • Background

/dev/cpuset/background/cpus /dev/cpuset/background/mems //tasks put into this cgroup /dev/cpuset/background/tasks

  • System-background

/dev/cpuset/system-background/cpus /dev/cpuset/system-background/mems //tasksput into this cgroup /dev/cpuset/system-background/tasks

Cpuschedtune

It is for task "performance boosting". While seems that AOSP has not complete implement it.

/dev/stune/foreground /dev/stune/tasks /dev/stune/foreground/tasks /dev/stune/background /dev/stune/background/tasks /dev/stune/top-app /dev/stune/top-app/tasks /dev/stune/system-background /dev/stune/system-background/tasks

Huawei Mate9 Implement

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档