【入门到实战】OpenHarmony南向子系统开发:插件

往期鸿蒙5.0全套实战文章必看:(文中附带全栈鸿蒙5.0学习资料)


插件

概述

基本概念

  • begetctl介绍

  • bootchart 插件

    bootchart是一个用于linux启动过程性能分析的开源工具软件,在系统中自动收集CPU占用率、磁盘吞吐率、进程等信息,并以图形方式显示分析结果,可用作指导优化系统启动过程。

  • bootevent 插件

    bootevent是一个记录init进程及各个服务的启动关键事件的插件,记录内容包括事件名称,关联事件的服务的启动时间,事件发生时间。导出文件支持tracing解析,可用作指导优化系统启动过程。

约束与限制

bootchart和bootevent只支持标准系统, begetctl 支持小型系统和标准系统。

开发指导

参数说明

表1 begetctl 命令说明

命令说明格式和示例
init group test [stage]init moduletest 测试用stage参见ServiceStatus
param ls [-r] [name]显示系统参数dac和selinux信息
-r: 展示所有参数信息
name:展示指定参数信息
begetctl param ls xxx.xxx 或
param ls xxx.xxx
例如:
begetctl param ls persist.sys.usb
param get [name]获取系参数统值begetctl param get xxxx.xxx 或
param get xxxx.xxx
例如:
param get const.product.name
param set name value设置系统参数值begetctl param set xxxx.xxx value 或
param set xxxx.xxx value
例如:
begetctl param set ohos.servicectrl.display 1
param wait name [value] [timeout]等待参数被设置成某值
timeout默认值:30秒
begetctl param wait xxx.xxx value 或
wait xxx.xxx value
例如:
param wait xx.sss 1
param dump [verbose]dump 系统参数信息begetctl param dump
或 param dump
param shell [name]进入Parameter shellbegetctl param shell
或 param shell
param save持久化workspace中的所有带持久化标记的参数begetctl param save
或 param save
dump apidump 显示init 系统参数接口信息begetctl dump api
getloglevel获取当前init的log等级
读取persist.init.debug.loglevel
0:debug
1:info
2:warning
3:err
4:fatal
begetctl getloglevel
setloglevel level设置log等级为info
log等级设置范围0~4
0:debug
1:info
2:warning
3:err
4:fatal
begetctl setloglevel 1
timer_stop servicename停止服务计时器
servicename长度不超过96字符
begetctl timer_stop servicename
timer_start servicename timeout启动服务计时器
servicename长度不超过96
timeout默认值:10毫秒
begetctl timer_start servicename
start_service servicename启动服务begetctl start_service servicename 或
start_service servicename
stop_service servicename停止服务begetctl stop_service servicename 或
stop_service servicename
service_control start servicename启动服务begetctl service_control start servicename 或
service_control start servicename
service_control stop servicename停止服务begetctl service_control stop servicename 或
service_control stop servicename
sandbox -b pid进入native应用沙盒begetctl sandbox -b pid
sandbox -p process_name进程进沙盒begetctl sandbox -p /bin/sh
sandbox -n sandbox_name进入配置的system或者chipset沙盒begetctl sandbox -n system
sandbox -s service_name服务进沙盒begetctl sandbox -s service_name
sandbox -h展示sandbox 命令帮助信息begetctl sandbox -h
modulectl uninstall moduleName卸载动态插件begetctl modulectl uninstall moduleName
例如:
卸载bootchart:
begetctl modulectl uninstall bootchart
modulectl install moduleName安装动态插件begetctl modulectl install moduleName
例如:
安装bootchart:
begetctl modulectl install bootchart
modulectl list查询动态插件列表begetctl modulectl list
misc_daemon --write_logo xxx.rgb写入开机logo
rgb文件最大不超过1024*2038,仅支持hi3516dv300
begetctl misc_daemon --write_logo logo.rgb 或
misc_daemon --write_logo logo.rgb
reboot重启系统begetctl reboot 或
reboot
reboot shutdown[:options]关闭系统begetctl reboot shutdown 或
reboot shutdown
reboot suspend暂停系统begetctl reboot suspend 或
reboot suspend
reboot updater重新启动并进入updaterbegetctl reboot updater 或
reboot updater
reboot updater[:options]重新启动并进入updaterbegetctl reboot updater 或
reboot updater
reboot flashd重新启动并进入flashdbegetctl reboot flashd 或
reboot flashd
reboot flashd[:options]重新启动并进入flashdbegetctl reboot flashd 或
reboot flashd
reboot charge重新启动并进入chargebegetctl reboot charge 或
reboot charge
reboot loader重新启动并进入烧写模式begetctl reboot loader 或
reboot loader
bootevent disable关闭bootevent插件功能begetctl bootevent disable
bootevent enable开启bootevent插件功能begetctl bootevent enable
dump_service parameter_service trigger命令行展示所有trigger信息begetctl dump_service parameter_service trigger
dump_service all命令行展示所有服务的信息begetctl dump_service all
dump_service serviceName命令行展示单个服务信息begetctl dump_service param_watcher
dump_service loopdump loop 中fd信息begetctl dump_service loop
bootchart stop停止图形分析,仅支持rk3568begetctl bootchart stop
bootchart start开始图形分析begetctl bootchart start
bootchart disable图形分析不使能begetctl bootchart disable
bootchart enable图形分析使能begetctl bootchart enable
dac uid username查询username 对应的uidbegetctl dac uid username
例如:
begetctl dac uid root
dac gid groupnamegroupname 对应的gid以及组成员begetctl dac gid groupname
例如:
begetctl dac gid shell
get log level获取当前init的log等级
读取persist.init.debug.loglevel
0:debug
1:info
2:warning
3:err
4:fatal
begetctl get log level
set log level设置log等级为info
log等级设置范围0~4
0:debug
1:info
2:warning
3:err
4:fatal
begetctl set log level 1
appspawn_time查询应用孵化的最大最小时间begetctl appspawn_time
例如:
begetctl appspawn_time
dump_nwebspawndump nwebspawn 应用信息begetctl dump_nwebspawn
例如:
打开备忘录
begetctl dump_nwebspawn
dump_appspawndump appspawn 应用信息begetctl dump_appspawn
例如:
begetctl dump_appspawn

typedef enum {
    SERVICE_IDLE = 0, // service add
    SERVICE_STARTING, // service start
    SERVICE_STARTED, // 2 service ok running
    SERVICE_READY, // 3 service ok running
    SERVICE_STOPPING,
    SERVICE_STOPPED, // 5
    SERVICE_ERROR,
    SERVICE_SUSPENDED,
    SERVICE_FREEZED,
    SERVICE_DISABLED,
    SERVICE_CRITICAL
} ServiceStatus;

开发步骤

新增一个插件, 以bootchart为例:

  1. 安装so文件, 定义单独文件,实现下面函数。

    static int bootchartEarlyHook(int stage, int prio, void *cookie)
    {
        char enable[4] = {}; // 4 enable size
        uint32_t size = sizeof(enable);
        SystemReadParam("persist.init.bootchart.enabled", enable, &size);
        if (strcmp(enable, "1") != 0) {
            PLUGIN_LOGI("bootchart disabled.");
            return 0;
        }
        InitModuleMgrInstall("libbootchart");
        PLUGIN_LOGI("bootchart enabled.");
        return 0;
    }
    
    MODULE_CONSTRUCTOR(void)
    {
        // Depends on parameter service
        InitAddPostPersistParamLoadHook(0, bootchartEarlyHook);
    }
    
  2. 编译成静态库libbootchart_static,并加入static_modules组。

    group("static_modules") {
      if (!defined(ohos_lite)) {
        deps = [ ":libbootchart_static" ]
      }
    }
    
  3. 初始化bootchart插件, 可以安装插件命令。

    static int BootchartInit(void)
    {
        if (g_executorId == -1) {
            g_executorId = AddCmdExecutor("bootchart", DoBootchartCmd);
            PLUGIN_LOGI("BootchartInit executorId %d", g_executorId);
        }
        return 0;
    }
    
    MODULE_CONSTRUCTOR(void)
    {
        PLUGIN_LOGI("DoBootchartStart now ...");
        BootchartInit();
    }
    
  4. 退出bootchart插件。

    MODULE_DESTRUCTOR(void)
    {
        PLUGIN_LOGI("DoBootchartStop now ...");
        DoBootchartStop();
        BootchartExit();
    }
    
  5. 执行bootchart命令。

    static int DoBootchartCmd(int id, const char *name, int argc, const char **argv)
    {
        PLUGIN_LOGI("DoBootchartCmd argc %d %s", argc, name);
        ...
        return 0;
    }
    

开发实例

bootchart 使用示例

预制条件:

  1. 准备bootchart测试环境:linux操作系统下安装python及pycairo pip install pycairo

  2. 在linux解压bootchart-master.tar

    tar -zxvf bootchart-master.tar

执行步骤:

  1. 启动系统。
  2. 执行命令行:begetctl bootchart enable
  3. 重启系统。
  4. 执行命令行:begetctl bootchart stop
  5. 执行命令行:begetctl bootchart disable
  6. 在/data/service/el0/startup/init/目录下导出如下文件并存放在bootchart文件夹:
    header
    proc_diskstats.log
    proc_ps.log
    proc_stat.log
  7. 使用tar -zcvf bootchart.tgz * 命令进行打包(只支持linux版本)并将该打包文件拷贝到linux:bootchart-master目录下。
  8. 在bootchart-master目录下运行命令:
    python3 pybootchartgui.py -f pdf bootchart.tgz
    

预期结果:
        在bootchart-master目录下生成bootchart.pdf。

bootevent 使用示例
  1. 在服务的cfg文件中配置bootevent事件,支持配置一个或多个bootevent事件。

    配置单个bootevent事件:

    bootevents : "bootevent.xxxbootevent",
    

    配置多个bootevent事件:

    bootevents : ["bootevent.xxxbootevent1", "bootevent.xxxbootevent2.xxx"],
    

注意: 配置的bootevent事件必须以“bootevent.”开始。

  1. 服务代码中发送bootevent事件。

    服务自身代码中调用init提供的SetParameter接口发送bootevent事件,例如发送上一步骤中设置的XXXbootevent1事件:

    SetParameter("bootevent.XXXbootevent1", "true");
    
  2. 命令行启用bootevent功能。

    • 执行begetctl bootevent enable命令后再次启动系统,bootevent功能开启。
    • 执行begetctl bootevent disable命令后再次启动系统,bootevent功能关闭。
  3. 导出文件支持trace分析。

    • 导出的bootevent文件目录:/data/service/el0/startup/init/。
    • 导出文件命名规则:“时间戳.bootevent”。
    • 导出的bootevent信息文件可以通过trace分析工具进行可视化展示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值