Android nice 进程优先级设置

1.简介

在Android中,可以使用nice值来设定一个进程的优先级,系统的调度器可以根据nice值来合理的调度进程,主要特点如下:

在Android中,nice的取值范围为**-20~19**
在Android中,nice值的默认大小为0
在Android中,nice值越大,进程的优先级越低,获得的CPU调用机会就越少;nice值越低,进程的优先级越高,获得的CPU调用机会越多
在Android中,父进程fork出来的子进程nice值与父进程相同,你进程renice后,子进程的nice值不会改变.

2.查看 nice 值

命令查看:

adb shell top

在这里插入图片描述

3.修改 nice 的值

以 systemui 为例:

1) adb 命令修改
adb shell renice -n  -10 7386(pid 号)

在这里插入图片描述

我们可以看到修改之后 systemui nice 的变小了,那么在cpu 调度中它会优先使用.

2) 在源码中修改

源码中进行进程优先级设置的接口是 setThreadPriority(); 具体用法可以去查看这个函数

/**
731       * Set the priority of a thread, based on Linux priorities.
732       *
733       * @param tid The identifier of the thread/process to change.
734       * @param priority A Linux priority level, from -20 for highest scheduling
735       * priority to 19 for lowest scheduling priority.
736       *
737       * @throws IllegalArgumentException Throws IllegalArgumentException if
738       * <var>tid</var> does not exist.
739       * @throws SecurityException Throws SecurityException if your process does
740       * not have permission to modify the given thread, or to use the given
741       * priority.
742       */
743      public static final native void setThreadPriority(int tid, int priority)
744              throws IllegalArgumentException, SecurityException;
745  

知道这个方法,那么我们就可以在 app 打开时对其进行 进程优先级的设定,仿照源码中
OomAdjuster.java 文件.
在 OomAdjuster.java 文件 的 applyOomAdjLocked 函数中 我们对 systemui 进行 nice 优先级 修改:

只需要在此函数 优先级设置的后面添加


省略````
                } catch (Exception e) {
                 if (DEBUG_ALL) {
                        Slog.w(TAG, "Failed setting thread priority of " + app.pid, e);
                    }
                 }
             }
         }

  +      if (app.processName == "com.android.systemui") {
  +             setThreadPriority(app.pid, -20);
  +       }

 if (app.repForegroundActivities != app.hasForegroundActivities()) {
省略```

然后重新烧写编译出来的镜像就可以.

源码出自 源码在线 AOSPXRef

文件参考:
在Android中修改进程nice值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值