2 * Copyright (C) 2012 Fusion-io
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 * Parts of this file were imported from Jens Axboe's blktrace sources (also GPL)
19 #include <sys/types.h>
28 #include <asm/types.h>
41 extern char **environ;
43 static pid_t blktrace_pid = 0;
44 static pid_t mpstat_pid = 0;
46 static void sig_handler_for_quit(int val)
48 fprintf(stderr, "Received signal %d. Terminating tracers.\n", val);
49 wait_for_tracers(SIGTERM);
52 int start_blktrace(char **devices, int num_devices, char *trace_name, char *dest)
56 char *argv[15 + MAX_DEVICES_PER_TRACE * 2];
64 argv[argc++] = "blktrace";
66 argv[argc++] = "8192";
68 argv[argc++] = "queue";
70 argv[argc++] = "complete";
72 argv[argc++] = "issue";
74 argv[argc++] = "notify";
76 if (num_devices == 1) {
78 argv[argc++] = trace_name;
80 /* Multiple devices output to a directory named trace_name */
86 for (i = 0; i < num_devices; i++) {
88 argv[argc++] = devices[i];
91 signal(SIGTERM, sig_handler_for_quit);
92 signal(SIGINT, sig_handler_for_quit);
93 ret = run_program(argc, argv, 0, &blktrace_pid, NULL);
97 int wait_program(pid_t pid, const char *pname, int sig)
103 ret = kill(pid, sig);
105 fprintf(stderr, "Failed to send signal %d to %s (%lu): %s\n",
106 sig, pname, (long)pid, strerror(errno));
109 fprintf(stderr, "Kill (%d): %s (%ld)\n", sig, pname, (long)pid);
112 waitpid(pid, &status, 0);
113 if (WIFEXITED(status)) {
114 ret = WEXITSTATUS(status);
115 if (ret == 127) /* spawnp failed after forking */
116 fprintf(stderr, "Failed to run '%s'\n", pname);
118 fprintf(stderr, "Exit (%d): %s\n", ret, pname);
119 } else if (WIFSIGNALED(status) && sig && WTERMSIG(status) != sig) {
120 fprintf(stderr, "'%s' killed by signal %d\n", pname, WTERMSIG(status));
126 int run_program(int argc, char **argv, int wait, pid_t *pid, char *outpath)
131 posix_spawn_file_actions_t facts;
132 posix_spawn_file_actions_t *factp = NULL;
134 if (outpath != NULL) {
135 posix_spawn_file_actions_init(&facts);
136 posix_spawn_file_actions_addopen(&facts, 1, outpath, O_WRONLY|O_CREAT|O_TRUNC, 0600);
140 fprintf(stderr, "Start");
141 for (i = 0; i < argc; i++)
142 fprintf(stderr, " %s", argv[i]);
143 fprintf(stderr, "\n");
145 err = posix_spawnp(&_pid, argv[0], factp, NULL, argv, environ);
147 fprintf(stderr, "Could not run '%s': %s\n", argv[0], strerror(err));
149 err = wait_program(_pid, argv[0], 0);
151 fprintf(stderr, "Warning: %s (%ld): Not saving pid and not waiting for it.\n",
152 argv[0], (long)_pid);
159 int wait_for_tracers(int sig)
162 if (blktrace_pid != 0) {
163 err = wait_program(blktrace_pid, "blktrace", sig);
168 if (mpstat_pid != 0) {
169 err = wait_program(mpstat_pid, "mpstat", sig);
177 int start_mpstat(char *path)
187 ret = run_program(argc, argv, 0, &mpstat_pid, path);