Mercurial > hg > toybox
annotate toys/pending/bootchartd.c @ 1349:bf2e5591bdf0 draft
Cleanup strings.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 11 Jun 2014 08:02:55 -0500 |
parents | 85f297591693 |
children | c0c91437138b |
rev | line source |
---|---|
1287 | 1 /* bootchartd.c - bootchartd is commonly used to profile the boot process. |
2 * | |
3 * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com> | |
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com> | |
5 * | |
6 * No Standard | |
7 | |
1292 | 8 USE_BOOTCHARTD(NEWTOY(bootchartd, 0, TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN)) |
1287 | 9 |
10 config BOOTCHARTD | |
11 bool "bootchartd" | |
12 default n | |
13 help | |
1292 | 14 usage: bootchartd {start [PROG ARGS]}|stop|init |
1287 | 15 |
16 Create /var/log/bootlog.tgz with boot chart data | |
17 | |
1292 | 18 start: start background logging; with PROG, run PROG, |
19 then kill logging with USR1 | |
1287 | 20 stop: send USR1 to all bootchartd processes |
1292 | 21 init: start background logging; stop when getty/xdm is seen |
22 (for init scripts) | |
23 | |
1287 | 24 Under PID 1: as init, then exec $bootchart_init, /init, /sbin/init |
25 */ | |
1292 | 26 |
1287 | 27 #define FOR_bootchartd |
28 #include "toys.h" | |
29 | |
30 GLOBALS( | |
31 char buf[32]; | |
32 long smpl_period_usec; | |
33 int proc_accounting; | |
34 int is_login; | |
1292 | 35 |
36 void *head; | |
1287 | 37 ) |
38 | |
39 struct pid_list { | |
1292 | 40 struct pid_list *next, *prev; |
1287 | 41 int pid; |
42 }; | |
43 | |
44 static int push_pids_in_list(pid_t pid, char *name) | |
45 { | |
1292 | 46 struct pid_list *new = xzalloc(sizeof(struct pid_list)); |
47 | |
48 new->pid = pid; | |
49 dlist_add_nomalloc((void *)&TT.head, (void *)new); | |
50 | |
1287 | 51 return 0; |
52 } | |
53 | |
54 static void dump_data_in_file(char *fname, int wfd) | |
55 { | |
56 int rfd = open(fname, O_RDONLY); | |
57 | |
58 if (rfd != -1) { | |
59 xwrite(wfd, TT.buf, strlen(TT.buf)); | |
60 xsendfile(rfd, wfd); | |
61 close(rfd); | |
62 xwrite(wfd, "\n", 1); | |
63 } | |
64 } | |
65 | |
66 static int dump_proc_data(FILE *fp) | |
67 { | |
68 struct dirent *pid_dir; | |
69 int login_flag = 0; | |
70 pid_t pid; | |
71 DIR *proc_dir = opendir("/proc"); | |
72 | |
73 fputs(TT.buf, fp); | |
74 while ((pid_dir = readdir(proc_dir))) { | |
75 char filename[64]; | |
76 int fd; | |
77 | |
78 if (!isdigit(pid_dir->d_name[0])) continue; | |
79 sscanf(pid_dir->d_name, "%d", &pid); | |
80 sprintf(filename, "/proc/%d/stat", pid); | |
81 if ((fd = open(filename, O_RDONLY)) != -1 ) { | |
82 char *ptr; | |
83 ssize_t len; | |
84 | |
85 if ((len = readall(fd, toybuf, sizeof(toybuf)-1)) < 0) continue; | |
86 toybuf[len] = '\0'; | |
87 close(fd); | |
88 fputs(toybuf, fp); | |
89 if (!TT.is_login) continue; | |
90 if ((ptr = strchr(toybuf, '('))) { | |
91 char *tmp = strchr(++ptr, ')'); | |
92 | |
93 if (tmp) *tmp = '\0'; | |
94 } | |
95 // Checks for gdm, kdm or getty | |
96 if (((ptr[0] == 'g' || ptr[0] == 'k' || ptr[0] == 'x') && ptr[1] == 'd' | |
97 && ptr[2] == 'm') || strstr(ptr, "getty")) login_flag = 1; | |
98 } | |
99 } | |
100 closedir(proc_dir); | |
101 fputc('\n', fp); | |
102 return login_flag; | |
103 } | |
104 | |
105 static int parse_config_file(char *fname) | |
106 { | |
107 size_t len = 0; | |
108 char *line = NULL; | |
109 FILE *fp = fopen(fname, "r"); | |
110 | |
111 if (!fp) return 0; | |
112 for (;getline(&line, &len, fp) != -1; line = NULL) { | |
113 char *ptr = line; | |
114 | |
115 while (*ptr == ' ' || *ptr == '\t') ptr++; | |
116 if (!*ptr || *ptr == '#' || *ptr == '\n') continue; | |
117 if (!strncmp(ptr, "SAMPLE_PERIOD", strlen("SAMPLE_PERIOD"))) { | |
118 double smpl_val; | |
119 | |
120 if ((ptr = strchr(ptr, '='))) ptr += 1; | |
121 else continue; | |
122 sscanf(ptr, "%lf", &smpl_val); | |
123 TT.smpl_period_usec = smpl_val * 1000000; | |
124 if (TT.smpl_period_usec <= 0) TT.smpl_period_usec = 1; | |
125 } | |
126 if (!strncmp(ptr, "PROCESS_ACCOUNTING", strlen("PROCESS_ACCOUNTING"))) { | |
127 if ((ptr = strchr(ptr, '='))) ptr += 1; | |
128 else continue; | |
129 sscanf(ptr, "%s", toybuf); // string will come with double quotes. | |
130 if (!(strncmp(toybuf+1, "on", strlen("on"))) || | |
131 !(strncmp(toybuf+1, "yes", strlen("yes")))) TT.proc_accounting = 1; | |
132 } | |
133 free(line); | |
134 } | |
135 fclose(fp); | |
136 return 1; | |
137 } | |
138 | |
139 static char *create_tmp_dir() | |
140 { | |
141 char *dir_list[] = {"/tmp", "/mnt", "/boot", "/proc"}, **target = dir_list; | |
142 char *dir, dir_path[] = "/tmp/bootchart.XXXXXX"; | |
143 | |
144 if ((dir = mkdtemp(dir_path))) { | |
145 xchdir((dir = xstrdup(dir))); | |
146 return dir; | |
147 } | |
148 while (mount("none", *target, "tmpfs", (1<<15), "size=16m")) //MS_SILENT | |
149 if (!++target) perror_exit("can't mount tmpfs"); | |
150 xchdir(*target); | |
151 if (umount2(*target, MNT_DETACH)) perror_exit("Can't unmount tmpfs"); | |
152 return *target; | |
153 } | |
154 | |
155 static void start_logging() | |
156 { | |
157 int proc_stat_fd = xcreate("proc_stat.log", | |
158 O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
159 int proc_diskstats_fd = xcreate("proc_diskstats.log", | |
160 O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
161 FILE *proc_ps_fp = xfopen("proc_ps.log", "w"); | |
162 long tcnt = 60 * 1000 * 1000 / TT.smpl_period_usec; | |
163 | |
164 if (tcnt <= 0) tcnt = 1; | |
165 if (TT.proc_accounting) { | |
166 int kp_fd = xcreate("kernel_procs_acct", O_WRONLY | O_CREAT | O_TRUNC,0666); | |
167 | |
168 xclose(kp_fd); | |
169 acct("kernel_procs_acct"); | |
170 } | |
171 memset(TT.buf, 0, sizeof(TT.buf)); | |
1302
1253f0c3dd53
Convert bootchartd to generic_signal().
Rob Landley <rob@landley.net>
parents:
1292
diff
changeset
|
172 while (--tcnt && !toys.signal) { |
1287 | 173 int i = 0, j = 0, fd = open("/proc/uptime", O_RDONLY); |
174 if (fd < 0) goto wait_usec; | |
175 char *line = get_line(fd); | |
176 | |
177 if (!line) goto wait_usec; | |
178 while (line[i] != ' ') { | |
179 if (line[i] == '.') { | |
180 i++; | |
181 continue; | |
182 } | |
183 TT.buf[j++] = line[i++]; | |
184 } | |
185 TT.buf[j++] = '\n'; | |
186 TT.buf[j] = '\0'; | |
187 free(line); | |
188 close(fd); | |
189 dump_data_in_file("/proc/stat", proc_stat_fd); | |
190 dump_data_in_file("/proc/diskstats", proc_diskstats_fd); | |
191 // stop proc dumping in 2 secs if getty or gdm, kdm, xdm found | |
192 if (dump_proc_data(proc_ps_fp)) | |
193 if (tcnt > 2 * 1000 * 1000 / TT.smpl_period_usec) | |
194 tcnt = 2 * 1000 * 1000 / TT.smpl_period_usec; | |
195 fflush(NULL); | |
196 wait_usec: | |
197 usleep(TT.smpl_period_usec); | |
198 } | |
199 xclose(proc_stat_fd); | |
200 xclose(proc_diskstats_fd); | |
201 fclose(proc_ps_fp); | |
202 } | |
203 | |
204 static void stop_logging(char *tmp_dir, char *prog) | |
205 { | |
206 char host_name[32]; | |
207 int kcmd_line_fd; | |
208 time_t t; | |
209 struct tm st; | |
210 struct utsname uts; | |
211 FILE *hdr_fp = xfopen("header", "w"); | |
212 | |
213 if (TT.proc_accounting) acct(NULL); | |
214 if (prog) fprintf(hdr_fp, "profile.process = %s\n", prog); | |
215 gethostname(host_name, sizeof(host_name)); | |
216 time(&t); | |
217 localtime_r(&t, &st); | |
218 memset(toybuf, 0, sizeof(toybuf)); | |
219 strftime(toybuf, sizeof(toybuf), "%a %b %e %H:%M:%S %Z %Y", &st); | |
220 fprintf(hdr_fp, "version = TBX_BCHARTD_VER 1.0.0\n"); | |
221 fprintf(hdr_fp, "title = Boot chart for %s (%s)\n", host_name, toybuf); | |
222 if (uname(&uts) < 0) perror_exit("uname"); | |
223 fprintf(hdr_fp, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, | |
224 uts.version, uts.machine); | |
225 memset(toybuf, 0, sizeof(toybuf)); | |
226 if ((kcmd_line_fd = open("/proc/cmdline", O_RDONLY)) != -1) { | |
227 ssize_t len; | |
1292 | 228 |
1287 | 229 len = readall(kcmd_line_fd, toybuf, sizeof(toybuf)-1); |
1292 | 230 toybuf[len] = 0; |
231 while (--len >= 0 && !toybuf[len]) continue; | |
232 for (; len > 0; len--) if (toybuf[len] < ' ') toybuf[len] = ' '; | |
1287 | 233 } |
234 fprintf(hdr_fp, "system.kernel.options = %s", toybuf); | |
235 close(kcmd_line_fd); | |
236 fclose(hdr_fp); | |
237 memset(toybuf, 0, sizeof(toybuf)); | |
238 snprintf(toybuf, sizeof(toybuf), "tar -zcf /var/log/bootlog.tgz header %s *.log", | |
239 TT.proc_accounting ? "kernel_procs_acct" : ""); | |
240 system(toybuf); | |
241 if (tmp_dir) { | |
242 unlink("header"); | |
243 unlink("proc_stat.log"); | |
244 unlink("proc_diskstats.log"); | |
245 unlink("proc_ps.log"); | |
246 if (TT.proc_accounting) unlink("kernel_procs_acct"); | |
247 rmdir(tmp_dir); | |
248 } | |
249 } | |
250 | |
251 void bootchartd_main() | |
252 { | |
253 pid_t lgr_pid, self_pid = getpid(); | |
1292 | 254 int bchartd_opt = 0; // 0=PID1, 1=start, 2=stop, 3=init |
1287 | 255 TT.smpl_period_usec = 200 * 1000; |
256 | |
1292 | 257 TT.is_login = (self_pid == 1); |
258 if (*toys.optargs) { | |
259 if (!strcmp("start", *toys.optargs)) bchartd_opt = 1; | |
260 else if (!strcmp("stop", *toys.optargs)) bchartd_opt = 2; | |
261 else if (!strcmp("init", *toys.optargs)) bchartd_opt = 3; | |
262 else error_exit("Unknown option '%s'", *toys.optargs); | |
263 | |
1287 | 264 if (bchartd_opt == 2) { |
265 struct pid_list *temp; | |
266 char *process_name[] = {"bootchartd", NULL}; | |
267 | |
268 names_to_pid(process_name, push_pids_in_list); | |
1292 | 269 temp = TT.head; |
270 if (temp) temp->prev->next = 0; | |
271 for (; temp; temp = temp->next) | |
1287 | 272 if (temp->pid != self_pid) kill(temp->pid, SIGUSR1); |
1292 | 273 llist_traverse(TT.head, free); |
274 | |
1287 | 275 return; |
276 } | |
1292 | 277 } else if (!TT.is_login) error_exit("not PID 1"); |
278 | |
1287 | 279 // Execute the code below for start or init or PID1 |
280 if (!parse_config_file("bootchartd.conf")) | |
281 parse_config_file("/etc/bootchartd.conf"); | |
282 | |
283 memset(toybuf, 0, sizeof(toybuf)); | |
1327
85f297591693
Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents:
1302
diff
changeset
|
284 if (!(lgr_pid = xfork())) { |
1287 | 285 char *tmp_dir = create_tmp_dir(); |
286 | |
1302
1253f0c3dd53
Convert bootchartd to generic_signal().
Rob Landley <rob@landley.net>
parents:
1292
diff
changeset
|
287 sigatexit(generic_signal); |
1287 | 288 raise(SIGSTOP); |
1292 | 289 if (!bchartd_opt && !getenv("PATH")) |
290 putenv("PATH=/sbin:/usr/sbin:/bin:/usr/bin"); | |
1287 | 291 start_logging(); |
292 stop_logging(tmp_dir, bchartd_opt == 1 ? toys.optargs[1] : NULL); | |
293 free(tmp_dir); | |
294 return; | |
295 } | |
296 waitpid(lgr_pid, NULL, WUNTRACED); | |
297 kill(lgr_pid, SIGCONT); | |
298 | |
1292 | 299 if (!bchartd_opt) { |
1287 | 300 char *pbchart_init = getenv("bootchart_init"); |
301 | |
302 if (pbchart_init) execl(pbchart_init, pbchart_init, NULL); | |
1292 | 303 execl("/init", "init", (void *)0); |
304 execl("/sbin/init", "init", (void *)0); | |
1287 | 305 } |
306 if (bchartd_opt == 1 && toys.optargs[1]) { | |
307 pid_t prog_pid; | |
308 | |
1327
85f297591693
Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents:
1302
diff
changeset
|
309 if (!(prog_pid = xfork())) xexec_optargs(1); |
1287 | 310 waitpid(prog_pid, NULL, 0); |
311 kill(lgr_pid, SIGUSR1); | |
312 } | |
313 } |