cpu_usage_profiler示例
源码路径:example/system/cpu_usage_profiler
支持的平台
sf32lb52-lcd_n16r8
概述
例程演示cpu_usage_profiler的配置使用,功能包括:
CPU使用统计:空闲时间(idle_run_time)、非空闲时间(other_run_time)、CPU使用率(默认2s统计一次)。
线程切换历史记录。记录每次线程切换的线程名、切换发生的时间。
中断历史记录。记录中断的中断号、中断触发时间。
CPU Usage Metrics。记录CPU空闲时间(idle time)、非空闲时间(other run time)、线程运行时间(thread run time)。支持:
周期打印。
通过
Metrics Collector
功能收集存储。
小技巧
线程切换、中断记录的时间是根据GTIMER计算的开机时间(秒/微秒)。
CPU Usage Metrics
的统计信息在每次收集或者打印后清除重启开始统计。
例程的使用
硬件需求
运行该例程前,需要准备一块本例程支持的开发板。
编译和烧录
切换到例程project目录,运行scons命令执行编译:
scons --board=sf32lb52-lcd_n16r8 -j32
运行build_sf32lb52-lcd_n16r8_hcpu\uart_download.bat
,按提示选择端口即可进行下载:
$ ./uart_download.bat
Uart Download
please input the serial port num:5
关于编译、下载的详细步骤,请参考上手指南的相关介绍。
例程的预期结果
线程切换历史保存在
thread_switch_hist
,中断历史记录保存在isr_hist
,可以通过ozone\T32
等在线或者离线手段查看变量。比如,发生异常时,可以通过T32方便地看到线程切换记录:配置CPU Usage Metrics为周期打印时,会周期打印
CPU Usage
信息(这里配置的打印周期为10s):07-03 17:59:52:616 [32m[328360] I/NO_TAG: ============================ 07-03 17:59:52:625 [0m[32m[328382] I/NO_TAG: CPU Usage: 9.93/ 9.94 ( 0.15%) 07-03 17:59:52:632 [0m[32m[328411] I/NO_TAG: ======================================================== 07-03 17:59:52:641 [0m[32m[328442] I/NO_TAG: thread run_time(s) load(%) 07-03 17:59:52:648 [0m[32m[328465] I/NO_TAG: -------------------------------------------------------- 07-03 17:59:52:662 [0m[32m[328496] I/NO_TAG: [tshell ]: 0.01 0.15% 07-03 17:59:52:672 [0m[32m[328518] I/NO_TAG: [tidle ]: 9.93 99.85% 07-03 17:59:52:680 [0m[32m[328540] I/NO_TAG: [timer ]: 0.00 0.00% 07-03 17:59:52:692 [0m[32m[328562] I/NO_TAG: [main ]: 0.00 0.00% 07-03 17:59:52:813 [0m 07-03 18:00:02:607 [32m[656066] I/NO_TAG: ============================ 07-03 18:00:02:619 [0m[32m[656087] I/NO_TAG: CPU Usage: 9.99/ 10.00 ( 0.07%) 07-03 18:00:02:627 [0m[32m[656112] I/NO_TAG: ======================================================== 07-03 18:00:02:637 [0m[32m[656143] I/NO_TAG: thread run_time(s) load(%) 07-03 18:00:02:644 [0m[32m[656166] I/NO_TAG: -------------------------------------------------------- 07-03 18:00:02:653 [0m[32m[656197] I/NO_TAG: [tshell ]: 0.00 0.00% 07-03 18:00:02:660 [0m[32m[656218] I/NO_TAG: [tidle ]: 9.99 99.93% 07-03 18:00:02:670 [0m[32m[656240] I/NO_TAG: [timer ]: 0.01 0.07% 07-03 18:00:02:678 [0m[32m[656262] I/NO_TAG: [main ]: 0.00 0.00% 07-03 18:00:02:801 [0m 07-03 18:00:12:602 [32m[983795] I/NO_TAG: ============================ 07-03 18:00:12:607 [0m[32m[983816] I/NO_TAG: CPU Usage: 9.99/ 10.00 ( 0.07%) 07-03 18:00:12:809 [0m[32m[983841] I/NO_TAG: ======================================================== 07-03 18:00:12:830 [0m[32m[983872] I/NO_TAG: thread run_time(s) load(%) 07-03 18:00:12:853 [0m[32m[983895] I/NO_TAG: -------------------------------------------------------- 07-03 18:00:12:865 [0m[32m[983926] I/NO_TAG: [tshell ]: 0.00 0.00% 07-03 18:00:12:874 [0m[32m[983948] I/NO_TAG: [tidle ]: 9.99 99.93% 07-03 18:00:12:883 [0m[32m[983970] I/NO_TAG: [timer ]: 0.01 0.07% 07-03 18:00:12:897 [0m[32m[983991] I/NO_TAG: [main ]: 0.00 0.00%
支持的FINSH命令:
命令
说明
cpu_prof_init
初始化profiler功能
cpu_prof_deinit
取消profiler功能
cpu_prof_reset
重置CPU统计数据
cpu
打印CPU统计信息(如果使能了
CPU Usage Metrics
,也会打印各线程负载)如下为
cpu
命令打印(这里使能了CPU Usage Metrics
):07-03 23:35:53:806 TX:cpu 07-03 23:35:53:811 [32m[1049619] I/NO_TAG: CPU usage: 0.33 07-03 23:35:53:814 07-03 23:35:53:819 [0m[32m[1049637] I/NO_TAG: ============================ 07-03 23:35:53:822 [0m[32m[1049659] I/NO_TAG: CPU Usage: 1.94/ 1.95 ( 0.34%) 07-03 23:35:53:828 [0m[32m[1049684] I/NO_TAG: ======================================================== 07-03 23:35:53:832 [0m[32m[1049715] I/NO_TAG: thread run_time(s) load(%) 07-03 23:35:53:837 [0m[32m[1049739] I/NO_TAG: -------------------------------------------------------- 07-03 23:35:53:839 [0m[32m[1049770] I/NO_TAG: [tshell ]: 0.00 0.00% 07-03 23:35:53:844 [0m[32m[1049792] I/NO_TAG: [tidle ]: 1.94 99.66% 07-03 23:35:53:848 [0m[32m[1049814] I/NO_TAG: [timer ]: 0.01 0.34% 07-03 23:35:53:852 [0m[32m[1049836] I/NO_TAG: [main ]: 0.00 0.00%
异常诊断
参考文档
更新记录
版本 |
日期 |
发布说明 |
---|---|---|
0.0.1 |
07/2025 |
初始版本 |