blob: a514261f3870013fa5cfff11b93a41931d11873d [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* this code is used to generate a boot sequence profile that can be used
18 * with the 'bootchart' graphics generation tool. see www.bootchart.org
19 * note that unlike the original bootchartd, this is not a Bash script but
20 * some C code that is run right from the init script.
21 */
22
23#include <stdio.h>
24#include <time.h>
25#include <dirent.h>
26#include <unistd.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <errno.h>
33#include <stdlib.h>
34#include <sys/stat.h>
35#include "bootchart.h"
36
37#define VERSION "0.8"
38#define SAMPLE_PERIOD 0.2
39#define LOG_ROOT "/data/bootchart"
40#define LOG_STAT LOG_ROOT"/proc_stat.log"
41#define LOG_PROCS LOG_ROOT"/proc_ps.log"
42#define LOG_DISK LOG_ROOT"/proc_diskstats.log"
43#define LOG_HEADER LOG_ROOT"/header"
44#define LOG_ACCT LOG_ROOT"/kernel_pacct"
45
46#define LOG_STARTFILE "/data/bootchart-start"
47#define LOG_STOPFILE "/data/bootchart-stop"
48
49static int
50unix_read(int fd, void* buff, int len)
51{
52 int ret;
53 do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR);
54 return ret;
55}
56
57static int
58unix_write(int fd, const void* buff, int len)
59{
60 int ret;
61 do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR);
62 return ret;
63}
64
65static int
66proc_read(const char* filename, char* buff, size_t buffsize)
67{
68 int len = 0;
69 int fd = open(filename, O_RDONLY);
70 if (fd >= 0) {
71 len = unix_read(fd, buff, buffsize-1);
72 close(fd);
73 }
74 buff[len > 0 ? len : 0] = 0;
75 return len;
76}
77
78#define FILE_BUFF_SIZE 65536
79
80typedef struct {
81 int count;
82 int fd;
83 char data[FILE_BUFF_SIZE];
84} FileBuffRec, *FileBuff;
85
86static void
87file_buff_open( FileBuff buff, const char* path )
88{
89 buff->count = 0;
90 buff->fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0755);
91}
92
93static void
94file_buff_write( FileBuff buff, const void* src, int len )
95{
96 while (len > 0) {
97 int avail = sizeof(buff->data) - buff->count;
98 if (avail > len)
99 avail = len;
100
101 memcpy( buff->data + buff->count, src, avail );
102 len -= avail;
103 src = (char*)src + avail;
104
105 buff->count += avail;
106 if (buff->count == FILE_BUFF_SIZE) {
107 unix_write( buff->fd, buff->data, buff->count );
108 buff->count = 0;
109 }
110 }
111}
112
113static void
114file_buff_done( FileBuff buff )
115{
116 if (buff->count > 0) {
117 unix_write( buff->fd, buff->data, buff->count );
118 buff->count = 0;
119 }
120}
121
Bo (Andover) Zhang37003732014-07-24 13:11:35 -0400122static long long
123get_uptime_jiffies()
124{
125 char buff[64];
126 long long jiffies = 0;
127
128 if (proc_read("/proc/uptime", buff, sizeof(buff)) > 0)
129 jiffies = 100LL*strtod(buff,NULL);
130
131 return jiffies;
132}
133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134static void
135log_header(void)
136{
137 FILE* out;
138 char cmdline[1024];
139 char uname[128];
140 char cpuinfo[128];
141 char* cpu;
142 char date[32];
143 time_t now_t = time(NULL);
144 struct tm now = *localtime(&now_t);
145 strftime(date, sizeof(date), "%x %X", &now);
146
147 out = fopen( LOG_HEADER, "w" );
148 if (out == NULL)
149 return;
150
151 proc_read("/proc/cmdline", cmdline, sizeof(cmdline));
152 proc_read("/proc/version", uname, sizeof(uname));
153 proc_read("/proc/cpuinfo", cpuinfo, sizeof(cpuinfo));
154
155 cpu = strchr( cpuinfo, ':' );
156 if (cpu) {
157 char* p = strchr(cpu, '\n');
158 cpu += 2;
159 if (p)
160 *p = 0;
161 }
162
163 fprintf(out, "version = %s\n", VERSION);
164 fprintf(out, "title = Boot chart for Android ( %s )\n", date);
165 fprintf(out, "system.uname = %s\n", uname);
166 fprintf(out, "system.release = 0.0\n");
167 fprintf(out, "system.cpu = %s\n", cpu);
168 fprintf(out, "system.kernel.options = %s\n", cmdline);
169 fclose(out);
170}
171
172static void
173close_on_exec(int fd)
174{
175 fcntl(fd, F_SETFD, FD_CLOEXEC);
176}
177
178static void
179open_log_file(int* plogfd, const char* logfile)
180{
181 int logfd = *plogfd;
182
183 /* create log file if needed */
184 if (logfd < 0)
185 {
186 logfd = open(logfile,O_WRONLY|O_CREAT|O_TRUNC,0755);
187 if (logfd < 0) {
188 *plogfd = -2;
189 return;
190 }
191 close_on_exec(logfd);
192 *plogfd = logfd;
193 }
194}
195
196static void
197do_log_uptime(FileBuff log)
198{
199 char buff[65];
Bo (Andover) Zhang37003732014-07-24 13:11:35 -0400200 int len;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Bo (Andover) Zhang37003732014-07-24 13:11:35 -0400202 snprintf(buff,sizeof(buff),"%lld\n",get_uptime_jiffies());
203 len = strlen(buff);
204 file_buff_write(log, buff, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205}
206
207static void
208do_log_ln(FileBuff log)
209{
210 file_buff_write(log, "\n", 1);
211}
212
213
214static void
215do_log_file(FileBuff log, const char* procfile)
216{
217 char buff[1024];
218 int fd;
219
220 do_log_uptime(log);
221
222 /* append file content */
223 fd = open(procfile,O_RDONLY);
224 if (fd >= 0) {
225 close_on_exec(fd);
226 for (;;) {
227 int ret;
228 ret = unix_read(fd, buff, sizeof(buff));
229 if (ret <= 0)
230 break;
231
232 file_buff_write(log, buff, ret);
233 if (ret < (int)sizeof(buff))
234 break;
235 }
236 close(fd);
237 }
238
239 do_log_ln(log);
240}
241
242static void
243do_log_procs(FileBuff log)
244{
245 DIR* dir = opendir("/proc");
246 struct dirent* entry;
247
248 do_log_uptime(log);
249
250 while ((entry = readdir(dir)) != NULL) {
251 /* only match numeric values */
252 char* end;
253 int pid = strtol( entry->d_name, &end, 10);
254 if (end != NULL && end > entry->d_name && *end == 0) {
255 char filename[32];
256 char buff[1024];
257 char cmdline[1024];
258 int len;
259 int fd;
260
261 /* read command line and extract program name */
262 snprintf(filename,sizeof(filename),"/proc/%d/cmdline",pid);
263 proc_read(filename, cmdline, sizeof(cmdline));
264
265 /* read process stat line */
266 snprintf(filename,sizeof(filename),"/proc/%d/stat",pid);
267 fd = open(filename,O_RDONLY);
268 if (fd >= 0) {
269 len = unix_read(fd, buff, sizeof(buff)-1);
270 close(fd);
271 if (len > 0) {
272 int len2 = strlen(cmdline);
273 if (len2 > 0) {
274 /* we want to substitute the process name with its real name */
275 const char* p1;
276 const char* p2;
277 buff[len] = 0;
278 p1 = strchr(buff, '(');
279 p2 = strchr(p1, ')');
280 file_buff_write(log, buff, p1+1-buff);
281 file_buff_write(log, cmdline, strlen(cmdline));
282 file_buff_write(log, p2, strlen(p2));
283 } else {
284 /* no substitution */
285 file_buff_write(log,buff,len);
286 }
287 }
288 }
289 }
290 }
291 closedir(dir);
292 do_log_ln(log);
293}
294
295static FileBuffRec log_stat[1];
296static FileBuffRec log_procs[1];
297static FileBuffRec log_disks[1];
298
299/* called to setup bootcharting */
300int bootchart_init( void )
301{
302 int ret;
303 char buff[4];
304 int timeout = 0, count = 0;
305
306 buff[0] = 0;
307 proc_read( LOG_STARTFILE, buff, sizeof(buff) );
308 if (buff[0] != 0) {
309 timeout = atoi(buff);
310 }
311 else {
312 /* when running with emulator, androidboot.bootchart=<timeout>
313 * might be passed by as kernel parameters to specify the bootchart
314 * timeout. this is useful when using -wipe-data since the /data
315 * partition is fresh
316 */
317 char cmdline[1024];
318 char* s;
319#define KERNEL_OPTION "androidboot.bootchart="
320 proc_read( "/proc/cmdline", cmdline, sizeof(cmdline) );
321 s = strstr(cmdline, KERNEL_OPTION);
322 if (s) {
323 s += sizeof(KERNEL_OPTION)-1;
324 timeout = atoi(s);
325 }
326 }
327 if (timeout == 0)
328 return 0;
329
330 if (timeout > BOOTCHART_MAX_TIME_SEC)
331 timeout = BOOTCHART_MAX_TIME_SEC;
332
333 count = (timeout*1000 + BOOTCHART_POLLING_MS-1)/BOOTCHART_POLLING_MS;
334
335 do {ret=mkdir(LOG_ROOT,0755);}while (ret < 0 && errno == EINTR);
336
337 file_buff_open(log_stat, LOG_STAT);
338 file_buff_open(log_procs, LOG_PROCS);
339 file_buff_open(log_disks, LOG_DISK);
340
341 /* create kernel process accounting file */
342 {
343 int fd = open( LOG_ACCT, O_WRONLY|O_CREAT|O_TRUNC,0644);
344 if (fd >= 0) {
345 close(fd);
346 acct( LOG_ACCT );
347 }
348 }
349
350 log_header();
351 return count;
352}
353
354/* called each time you want to perform a bootchart sampling op */
355int bootchart_step( void )
356{
357 do_log_file(log_stat, "/proc/stat");
358 do_log_file(log_disks, "/proc/diskstats");
359 do_log_procs(log_procs);
360
361 /* we stop when /data/bootchart-stop contains 1 */
362 {
363 char buff[2];
364 if (proc_read(LOG_STOPFILE,buff,sizeof(buff)) > 0 && buff[0] == '1') {
365 return -1;
366 }
367 }
368
369 return 0;
370}
371
372void bootchart_finish( void )
373{
374 unlink( LOG_STOPFILE );
375 file_buff_done(log_stat);
376 file_buff_done(log_disks);
377 file_buff_done(log_procs);
378 acct(NULL);
379}
Bo (Andover) Zhang37003732014-07-24 13:11:35 -0400380
381/* called to get time (in ms) used by bootchart */
382long long bootchart_gettime( void )
383{
384 return 10LL*get_uptime_jiffies();
385}