bootchart实现分析

本文详细阐述了系统初始化过程中的Bootchart监控机制,包括如何记录CPU、闪存IO等关键信息,以及如何通过定时调用bootchart_step函数来收集系统状态数据。此外,文章还介绍了Bootchart的启动条件、监视周期和停止条件,以及如何处理收集到的数据以生成详细的系统运行报告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考 :  system/core/init/bootchart.c
             system/core/init/init.c

// 记录当前的状况
int  bootchart_step( void )
{
    // CPU 等信息
    do_log_file(log_stat,   "/proc/stat");
    // flash io 等信息
    do_log_file(log_disks,  "/proc/diskstats");
    //循环检测 /proc 下每个进程的信息
    do_log_procs(log_procs);

    /* we stop when /data/bootchart-stop contains 1 */
    {
        char  buff[2];
        if (proc_read(LOG_STOPFILE,buff,sizeof(buff)) > 0 && buff[0] == '1') {
            return -1;
        }
    }
    return 0;
}


 

init.c
系统默认是监视 200ms统计一次
# define BOOTCHART_POLLING_MS   200   
默认监视前 120 秒
# define BOOTCHART_DEFAULT_TIME_SEC    (2*60) 
for(;;) 
{
        if (bootchart_count > 0) {
            if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
                timeout = BOOTCHART_POLLING_MS;
            // 不再监视的条件:时间到或者写入信息出现错误
            if (bootchart_step() < 0 || --bootchart_count == 0) {
                bootchart_finish();
                bootchart_count = 0;
            }
        }
}


 

// 保存当前记录的 jiffies 值
static void
do_log_uptime(FileBuff  log)
{
    char  buff[65];
    int   fd, ret, len;

    fd = open("/proc/uptime",O_RDONLY);
    if (fd >= 0) {
        int  ret;
        ret = unix_read(fd, buff, 64);
        close(fd);
        buff[64] = 0;
        if (ret >= 0) {
            long long  jiffies = 100LL*strtod(buff,NULL);
            int        len;
            snprintf(buff,sizeof(buff),"%lld\n",jiffies);
            len = strlen(buff);
            file_buff_write(log, buff, len);
        }
    }
}


下面看看真实获取的数据
// proc_stat.log
997
cpu  28 0 496 823 492 0 1 0 0 0
cpu0 21 0 250 277 375 0 1 0 0 0
cpu1 7 0 246 546 117 0 0 0 0 0
intr 15283 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2693 0 0 0 0 0 0 0 0 0 0 0 0 0 0 989 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2900 0 0 160 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 1643 0 561 573 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 15556
btime 1356970674
processes 190
procs_running 3
procs_blocked 14
softirq 2018 0 938 0 0 0 0 4 273 2 801
......
// proc_ps.log
997
1 (/init) R 0 0 0 0 -1 4194560 539 6435 0 12 2 330 14 47 20 0 1 0 7 954368 151 4294967295 32768 229048 3203583712 3203581192 67320 0 0 0 65536 0 0 0 17 1 0 0 0 0 0 234784 239648 466944
2 (kthreadd) S 0 0 0 0 -1 2129984 0 0 0 0 1 0 0 0 20 0 1 0 7 0 0 4294967295 0 0 0 0 0 0 0 2147483647 0 3221701260 0 0 0 1 0 0 0 0 0 0 0 0
3 (ksoftirqd/0) S 2 0 0 0 -1 69238848 0 0 0 0 0 0 0 0 20 0 1 0 13 0 0 4294967295 0 0 0 0 0 0 0 2147483647 0 3221594880 0 0 17 0 0 0 0 0 0 0 0 0
4 (kworker/0:0) S 2 0 0 0 -1 69238880 0 0 0 0 0 0 0 0 20 0 1 0 15 0 0 4294967295 0 0 0 0 0 0 0 2147483647 0 3221681228 0 0 17 0 0 0 0 0 0 0 0 0
5 (kworker/u:0) S 2 0 0 0 -1 69238880 0 0 0 0 0 27 0 0 20 0 1 0 17 0 0 4294967295 0 0 0 0 0 0 0 2147483647 0 3221681228 0 0 17 0 0 0 0 0 0 0 0 0
......
解析程序应该根据997 (同一个jiffies)解析上述文件,
统计出这个时间段那些程序在运行及IO等的使用情况,最终生成bootchart

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值