1# 插件 2 3## 概述 4### 基本概念 5 - begetctl介绍 6 7 具体参考[begetctl命令](#table14737791480)。 8 - bootchart 插件 9 10 bootchart是一个用于linux启动过程性能分析的开源工具软件,在系统中自动收集CPU占用率、磁盘吞吐率、进程等信息,并以图形方式显示分析结果,可用作指导优化系统启动过程。begetctl命令参考:[begetctl命令](#table14737791480)。 11 - bootevent 插件 12 13 bootevent是一个记录init进程及各个服务的启动关键事件的插件,记录内容包括事件名称,关联事件的服务的启动时间,事件发生时间。导出文件支持tracing解析,可用作指导优化系统启动过程。 14 15### 约束与限制 16 17bootchart和bootevent只支持标准系统, begetctl 支持小型系统和标准系统。 18 19## 开发指导 20### 参数说明 21 **表1** begetctl 命令说明<a name="table14737791480"></a> 22| 命令 | 说明 | 格式和示例| 23| :---------- | :---------- |:-------------- | 24| init group test [stage] | init moduletest 测试用 | stage参见[ServiceStatus](#section001) | 25| param ls [-r] [name] | 显示系统参数dac和selinux信息<br> -r: 展示所有参数信息 <br> name:展示指定参数信息 | begetctl param ls xxx.xxx 或 <br>param ls xxx.xxx<br>例如:<br>begetctl param ls persist.sys.usb | 26| param get [name] | 获取系参数统值 | begetctl param get xxxx.xxx 或 <br>param get xxxx.xxx<br>例如:<br>param get const.product.name| 27| param set name value| 设置系统参数值 |begetctl param set xxxx.xxx value 或<br> param set xxxx.xxx value <br>例如:<br>begetctl param set ohos.servicectrl.display 1 | 28| param wait name [value] [timeout] | 等待参数被设置成某值<br> timeout默认值:30秒| begetctl param wait xxx.xxx value 或<br> wait xxx.xxx value <br>例如:<br>param wait xx.sss 1 | 29| param dump [verbose] | dump 系统参数信息|begetctl param dump <br>或 param dump | 30| param shell [name] | 进入Parameter shell|begetctl param shell <br>或 param shell | 31| param save | 持久化workspace中的所有带持久化标记的参数|begetctl param save <br>或 param save | 32| dump api | dump 显示init 系统参数接口信息|begetctl dump api | 33| getloglevel | 获取当前init的log等级<br>读取persist.init.debug.loglevel<br>0:debug<br> 1:info<br> 2:warning<br> 3:err<br> 4:fatal|begetctl getloglevel | 34| setloglevel level | 设置log等级为info <br>log等级设置范围0~4<br> 0:debug<br>1:info<br> 2:warning<br> 3:err<br>4:fatal|begetctl setloglevel 1 | 35| timer_stop servicename | 停止服务计时器<br>servicename长度不超过96字符|begetctl timer_stop servicename | begetctl timer_stop appspawn | 36| timer_start servicename timeout | 启动服务计时器<br>servicename长度不超过96<br>timeout默认值:10毫秒 |begetctl timer_start servicename | begetctl timer_start appspawn| 37| start_service servicename | 启动服务| begetctl start_service servicename 或<br>start_service servicename|begetctl start_service appspawn | 38| stop_service servicename | 停止服务|begetctl stop_service servicename 或<br> stop_service servicename|begetctl stop_service appspawn | 39| service_control start servicename | 启动服务|begetctl service_control start servicename 或<br> service_control start servicename|begetctl service_control start appspawn | 40| service_control stop servicename | 停止服务|begetctl service_control stop servicename 或<br> service_control stop servicename|begetctl service_control stop appspawn | 41| sandbox -b pid | 进入native应用沙盒 | begetctl sandbox -b pid| 42| sandbox -p process_name | 进程进沙盒| begetctl sandbox -p /bin/sh | 43| sandbox -n sandbox_name | 进入配置的system或者chipset沙盒| begetctl sandbox -n system| 44| sandbox -s service_name | 服务进沙盒| begetctl sandbox -s service_name | 45| sandbox -h | 展示sandbox 命令帮助信息| begetctl sandbox -h | 46| modulectl uninstall moduleName | 卸载动态插件| begetctl modulectl uninstall moduleName <br>例如:<br>卸载bootchart:<br> begetctl modulectl uninstall bootchart | 47| modulectl install moduleName | 安装动态插件|begetctl modulectl install moduleName <br>例如:<br>安装bootchart:<br>begetctl modulectl install bootchart | 48| modulectl list | 查询动态插件列表|begetctl modulectl list | 49| misc_daemon --write_logo xxx.rgb | 写入开机logo<br>rgb文件最大不超过1024*2038,仅支持hi3516dv300|begetctl misc_daemon --write_logo logo.rgb 或<br> misc_daemon --write_logo logo.rgb| 50| reboot | 重启系统|begetctl reboot 或<br> reboot| 51| reboot shutdown[:options] | 关闭系统|begetctl reboot shutdown 或<br> reboot shutdown | 52| reboot suspend | 暂停系统|begetctl reboot suspend 或<br> reboot suspend | 53| reboot updater | 重新启动并进入updater|begetctl reboot updater 或<br> reboot updater | 54| reboot updater[:options] | 重新启动并进入updater|begetctl reboot updater 或<br> reboot updater | 55| reboot flashd | 重新启动并进入flashd|begetctl reboot flashd 或<br> reboot flashd | 56| reboot flashd[:options] | 重新启动并进入flashd|begetctl reboot flashd 或<br> reboot flashd | 57| reboot charge | 重新启动并进入charge|begetctl reboot charge 或<br> reboot charge | 58| reboot loader | 重新启动并进入烧写模式|begetctl reboot loader 或<br> reboot loader | 59| bootevent disable | 关闭bootevent插件功能|begetctl bootevent disable | 60| bootevent enable | 开启bootevent插件功能|begetctl bootevent enable | 61| dump_service parameter_service trigger | 命令行展示所有trigger信息|begetctl dump_service parameter_service trigger | 62| dump_service all | 命令行展示所有服务的信息|begetctl dump_service all | 63| dump_service serviceName | 命令行展示单个服务信息|begetctl dump_service param_watcher | 64| dump_service loop | dump loop 中fd信息 |begetctl dump_service loop| 65| bootchart stop | 停止图形分析,仅支持rk3568|begetctl bootchart stop | 66| bootchart start | 开始图形分析|begetctl bootchart start | 67| bootchart disable | 图形分析不使能|begetctl bootchart disable | 68| bootchart enable | 图形分析使能|begetctl bootchart enable | 69| dac uid username | 查询username 对应的uid|begetctl dac uid username <br>例如:<br>begetctl dac uid root| 70| dac gid groupname | groupname 对应的gid以及组成员|begetctl dac gid groupname <br>例如:<br> begetctl dac gid shell | 71| get log level | 获取当前init的log等级<br>读取persist.init.debug.loglevel<br>0:debug<br> 1:info<br> 2:warning<br> 3:err<br> 4:fatal|begetctl get log level | 72| set log level | 设置log等级为info <br>log等级设置范围0~4<br> 0:debug<br>1:info<br> 2:warning<br> 3:err<br>4:fatal|begetctl set log level 1 | 73| appspawn_time | 查询应用孵化的最大最小时间 | begetctl appspawn_time <br>例如:<br>begetctl appspawn_time| 74| dump_nwebspawn | dump nwebspawn 应用信息|begetctl dump_nwebspawn <br>例如:<br>打开备忘录<br>begetctl dump_nwebspawn | 75| dump_appspawn | dump appspawn 应用信息|begetctl dump_appspawn<br>例如:<br>begetctl dump_appspawn| 76 77- <a name = "section001"> **ServiceStatus** 78``` 79typedef enum { 80 SERVICE_IDLE = 0, // service add 81 SERVICE_STARTING, // service start 82 SERVICE_STARTED, // 2 service ok running 83 SERVICE_READY, // 3 service ok running 84 SERVICE_STOPPING, 85 SERVICE_STOPPED, // 5 86 SERVICE_ERROR, 87 SERVICE_SUSPENDED, 88 SERVICE_FREEZED, 89 SERVICE_DISABLED, 90 SERVICE_CRITICAL 91} ServiceStatus; 92``` 93### 开发步骤 94 新增一个插件, 以bootchart为例: 951. 安装so文件, 定义单独文件,实现下面函数。 96 ```c 97 static int bootchartEarlyHook(int stage, int prio, void *cookie) 98 { 99 char enable[4] = {}; // 4 enable size 100 uint32_t size = sizeof(enable); 101 SystemReadParam("persist.init.bootchart.enabled", enable, &size); 102 if (strcmp(enable, "1") != 0) { 103 PLUGIN_LOGI("bootchart disabled."); 104 return 0; 105 } 106 InitModuleMgrInstall("libbootchart"); 107 PLUGIN_LOGI("bootchart enabled."); 108 return 0; 109 } 110 111 MODULE_CONSTRUCTOR(void) 112 { 113 // Depends on parameter service 114 InitAddPostPersistParamLoadHook(0, bootchartEarlyHook); 115 } 116 ``` 1172. 编译成静态库libbootchart_static,并加入static_modules组。 118 ``` 119 group("static_modules") { 120 if (!defined(ohos_lite)) { 121 deps = [ ":libbootchart_static" ] 122 } 123 } 124 ``` 125 1263. 初始化bootchart插件, 可以安装插件命令。 127 ```c 128 static int BootchartInit(void) 129 { 130 if (g_executorId == -1) { 131 g_executorId = AddCmdExecutor("bootchart", DoBootchartCmd); 132 PLUGIN_LOGI("BootchartInit executorId %d", g_executorId); 133 } 134 return 0; 135 } 136 137 MODULE_CONSTRUCTOR(void) 138 { 139 PLUGIN_LOGI("DoBootchartStart now ..."); 140 BootchartInit(); 141 } 142 ``` 1434. 退出bootchart插件。 144 ```c 145 MODULE_DESTRUCTOR(void) 146 { 147 PLUGIN_LOGI("DoBootchartStop now ..."); 148 DoBootchartStop(); 149 BootchartExit(); 150 } 151 ``` 1525. 执行bootchart命令。 153 ```c 154 static int DoBootchartCmd(int id, const char *name, int argc, const char **argv) 155 { 156 PLUGIN_LOGI("DoBootchartCmd argc %d %s", argc, name); 157 ... 158 return 0; 159 } 160 ``` 161 162### 开发实例 163#### bootchart 使用示例 164 预制条件: 165 1. 准备bootchart测试环境:linux操作系统下安装python及pycairo pip install pycairo 166 2. 在linux解压bootchart-master.tar 167 168 tar -zxvf bootchart-master.tar 169 170 执行步骤: 171 1. 启动系统。 172 2. 执行命令行:begetctl bootchart enable 173 3. 重启系统。 174 4. 执行命令行:begetctl bootchart stop 175 5. 执行命令行:begetctl bootchart disable 176 6. 在/data/service/el0/startup/init/目录下导出如下文件并存放在bootchart文件夹:<br> 177 header<br> 178 proc_diskstats.log<br> 179 proc_ps.log<br> 180 proc_stat.log<br> 181 7. 使用tar -zcvf bootchart.tgz * 命令进行打包(只支持linux版本)并将该打包文件拷贝到linux:bootchart-master目录下。 182 8. 在bootchart-master目录下运行命令: 183 ``` 184 python3 pybootchartgui.py -f pdf bootchart.tgz 185 ``` 186 187 预期结果: 188 <br> 在bootchart-master目录下生成bootchart.pdf。 189#### bootevent 使用示例 1901. 在服务的cfg文件中配置bootevent事件,支持配置一个或多个bootevent事件。 191 192 配置单个bootevent事件: 193 ```json 194 bootevents : "bootevent.xxxbootevent", 195 ``` 196 配置多个bootevent事件: 197 ```json 198 bootevents : ["bootevent.xxxbootevent1", "bootevent.xxxbootevent2.xxx"], 199 ``` 200> **注意:** 配置的bootevent事件必须以“bootevent.”开始。 2012. 服务代码中发送bootevent事件。 202 203 服务自身代码中调用init提供的SetParameter接口发送bootevent事件,例如发送上一步骤中设置的XXXbootevent1事件: 204 ```c 205 SetParameter("bootevent.XXXbootevent1", "true"); 206 ``` 2073. 命令行启用bootevent功能。 208 209 - 执行begetctl bootevent enable命令后再次启动系统,bootevent功能开启。 210 - 执行begetctl bootevent disable命令后再次启动系统,bootevent功能关闭。 2114. 导出文件支持trace分析。 212 213 - 导出的bootevent文件目录:/data/service/el0/startup/init/。 214 - 导出文件命名规则:“时间戳.bootevent”。 215 - 导出的bootevent信息文件可以通过trace分析工具进行可视化展示。