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>
40 extern char **environ;
42 static int line_len = 1024;
43 static char line[1024];
45 static pid_t blktrace_pid = 0;
46 static pid_t mpstat_pid = 0;
48 char *blktrace_args[17 + MAX_DEVICES_PER_TRACE * 2] = {
59 char *mpstat_args[] = {
65 #define DEST_DIR_INDEX 12
68 int stop_tracer(pid_t *tracer_pid)
71 pid_t pid = *tracer_pid;
79 ret = kill(pid, SIGTERM);
81 fprintf(stderr, "failed to stop tracer pid %lu error %s\n",
82 (unsigned long)pid, strerror(errno));
85 pid_ret = waitpid(pid, &status, WUNTRACED);
86 if (pid_ret == pid && WIFEXITED(status) == 0) {
87 fprintf(stderr, "blktrace returns error %d\n", WEXITSTATUS(status));
93 void stop_all_tracers(void)
95 stop_tracer(&blktrace_pid);
96 stop_tracer(&mpstat_pid);
99 void sig_handler_for_quit(int val)
101 fprintf(stderr, "iowatcher exiting with %d, stopping tracers\n", val);
106 int start_blktrace(char **devices, int num_devices, char *trace_name, char *dest)
110 char **arg = blktrace_args;
114 fprintf(stderr, "running blktrace");
116 trace_name = "trace";
118 arg_index = LAST_ARG;
119 for (i = 0; i < num_devices; i++) {
120 blktrace_args[arg_index++] = "-d";
121 blktrace_args[arg_index++] = devices[i];
125 * single device traces use -o and are put into
126 * the dest dir if provided
128 if (num_devices == 1) {
129 blktrace_args[arg_index++] = "-o";
130 blktrace_args[arg_index++] = trace_name;
132 blktrace_args[DEST_DIR_INDEX] = dest;
135 * multi device traces are put into a dest
136 * dir based on the trace name
138 blktrace_args[DEST_DIR_INDEX] = trace_name;
141 blktrace_args[arg_index] = NULL;
144 fprintf(stderr, " %s", *arg);
147 fprintf(stderr, "\n");
152 ret = execvp("blktrace", blktrace_args);
154 fprintf(stderr, "failed to exec blktrace error %s\n", strerror(errno));
160 signal(SIGTERM, sig_handler_for_quit);
161 signal(SIGINT, sig_handler_for_quit);
166 int run_program(char *str)
170 fprintf(stderr, "running program %s\n", str);
173 fprintf(stderr, "failed to run program %s error %s\n", str, strerror(errno));
181 int run_program2(int argc, char **argv)
187 fprintf(stderr, "running");
188 for (i = 0; i < argc; i++)
189 fprintf(stderr, " '%s'", argv[i]);
190 fprintf(stderr, "\n");
192 err = posix_spawnp(&pid, argv[0], NULL, NULL, argv, environ);
194 fprintf(stderr, "%s failed with exit code %d\n", argv[0], err);
197 waitpid(pid, NULL, 0);
201 int wait_for_tracers(void)
204 if (blktrace_pid != 0) {
205 waitpid(blktrace_pid, &status, WUNTRACED);
208 if (mpstat_pid != 0) {
209 waitpid(mpstat_pid, &status, WUNTRACED);
215 int blktrace_to_dump(char *trace_name)
224 argv[3] = trace_name;
225 snprintf(line, line_len, "%s.dump", trace_name);
228 return run_program2(6, argv);
231 int start_mpstat(char *trace_name)
236 snprintf(line, line_len, "%s.mpstat", trace_name);
238 fd = open(line, O_WRONLY | O_CREAT | O_TRUNC, 0600);
240 fprintf(stderr, "unable to open %s for writing err %s\n",
241 line, strerror(errno));
251 fprintf(stderr, "failed to setup output file for mpstat\n");
254 ret = execvp("mpstat", mpstat_args);
256 fprintf(stderr, "failed to exec mpstat err %s\n",