参考 : 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