• [Android][DevTips]chrt命令修改线程优先级与调度策略


    前言

    DevTips是一个旨在记录一些开发过程中的小技巧,没有太大的技术含量的系列,更多的作用是备忘;

    此次介绍的是Android系统中内置的chrt命令的使用和一些注意事项;

    测试平台

    • Android P
    • Pixel 3

    Tip

    用法

    首先列一下帮助页面

    blueline:/ $ chrt --help                                                                                                                                                                                            
    usage: chrt [-Rmofrbi] {-p PID [PRIORITY] | [PRIORITY COMMAND...]}
    
    Get/set a process' real-time scheduling policy and priority.
    
    -p	Set/query given pid (instead of running COMMAND)
    -R	Set SCHED_RESET_ON_FORK
    -m	Show min/max priorities available
    
    Set policy (default -r):
    
      -o  SCHED_OTHER    -f  SCHED_FIFO    -r  SCHED_RR
      -b  SCHED_BATCH    -i  SCHED_IDLE
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    注意一下,这里的-p参数与PC端不太一样,以Ubuntu为例:

    $ chrt --help
    Show or change the real-time scheduling attributes of a process.
    
    Set policy:
     chrt [options] <priority> <command> [<arg>...]
     chrt [options] -p <priority> <pid>
    
    ...
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在Ubuntu上,-p参数传递的是 ,而手机上为 ,这两个参数顺序相反,需要注意;

    查看

    如果要查看某个进程的调度策略与优先级,可以使用如下命令:

    chrt -p `pidof com.android.systemui`
    
    • 1

    需要注意的是,这里的参数并非单指进程ID,线程ID也是可以的:

    # 查看1445进程(com.android.systemui)下的线程
    blueline:/ # ls /proc/1445/task/                                                                     
    1445 1453 1463 1468 1618 1625 1636 1646 1650 1659 1703 1709 1716 1976 3457 7716 
    1451 1454 1464 1480 1620 1626 1640 1648 1651 1667 1706 1711 1717 2770 7040 9574 
    1452 1456 1466 1558 1621 1628 1642 1649 1656 1676 1707 1713 1921 3212 7357 
    
    # 选择其中一个查看
    blueline:/ # chrt -p 1453                                                                                
    pid 1453's current scheduling policy: SCHED_OTHER
    pid 1453's current scheduling priority: 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    设置

    如果需要修改一个线程的调度策略为FIFO
    以上面的1445进程(com.android.systemui)的UI线程为例:

    blueline:/ # chrt -f -p 1453 50                                                                        
    blueline:/ # chrt -p 1453                                                                                
    pid 1453's current scheduling policy: SCHED_FIFO
    pid 1453's current scheduling priority: 50
    
    • 1
    • 2
    • 3
    • 4

    注意,如果优先级设置不合理,会失败,并输出如下提示:

    #修改前查看
    blueline:/ # chrt -p 1453                                                                                
    pid 1453's current scheduling policy: SCHED_OTHER
    pid 1453's current scheduling priority: 0
    #尝试修改
    blueline:/ # chrt -f -p 1453 0                                                                           
    chrt: 0 < 1
    #再次查看,未能生效
    blueline:/ # chrt -p 1453                                                                                
    pid 1453's current scheduling policy: SCHED_OTHER
    pid 1453's current scheduling priority: 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    这里输出的意思是,要求修改为0的优先级小于了FIFO调度策略允许的最小优先级1,因此设置失败;

    由于这里是DevTips系列,仅记录用法,就不展开各种调度策略的原理介绍了;

  • 相关阅读:
    Python中列表生成器的使用
    Python武器库开发-flask篇之模板渲染(二十四)
    LeetCode-617. Merge Two Binary Trees [C++][Java]
    webpack学习记录
    日常开发小汇总(5)元素跟随鼠标移动(在视口下移动)
    Mybatis
    leetcode 61. 旋转链表
    MATLAB非均匀网格梯度计算
    Redis—听说你速度跟甲斗一样快?——cluster
    Vue 面试题:了解 Vue 中的 Mixin 吗?
  • 原文地址:https://blog.csdn.net/u014175785/article/details/126400467