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>
39 static int line_len = 1024;
40 static char line[1024];
42 static pid_t blktrace_pid = 0;
43 static pid_t mpstat_pid = 0;
45 char *blktrace_args[17 + MAX_DEVICES_PER_TRACE * 2] = {
56 char *mpstat_args[] = {
62 #define DEST_DIR_INDEX 12
65 int stop_tracer(pid_t *tracer_pid)
68 pid_t pid = *tracer_pid;
76 ret = kill(pid, SIGTERM);
78 fprintf(stderr, "failed to stop tracer pid %lu error %s\n",
79 (unsigned long)pid, strerror(errno));
82 pid_ret = waitpid(pid, &status, WUNTRACED);
83 if (pid_ret == pid && WIFEXITED(status) == 0) {
84 fprintf(stderr, "blktrace returns error %d\n", WEXITSTATUS(status));
90 void stop_all_tracers(void)
92 stop_tracer(&blktrace_pid);
93 stop_tracer(&mpstat_pid);
96 void sig_handler_for_quit(int val)
98 fprintf(stderr, "iowatcher exiting with %d, stopping tracers\n", val);
103 int start_blktrace(char **devices, int num_devices, char *trace_name, char *dest)
107 char **arg = blktrace_args;
111 fprintf(stderr, "running blktrace");
113 trace_name = "trace";
115 arg_index = LAST_ARG;
116 for (i = 0; i < num_devices; i++) {
117 blktrace_args[arg_index++] = "-d";
118 blktrace_args[arg_index++] = devices[i];
122 * single device traces use -o and are put into
123 * the dest dir if provided
125 if (num_devices == 1) {
126 blktrace_args[arg_index++] = "-o";
127 blktrace_args[arg_index++] = trace_name;
129 blktrace_args[DEST_DIR_INDEX] = dest;
132 * multi device traces are put into a dest
133 * dir based on the trace name
135 blktrace_args[DEST_DIR_INDEX] = trace_name;
138 blktrace_args[arg_index] = NULL;
141 fprintf(stderr, " %s", *arg);
144 fprintf(stderr, "\n");
149 ret = execvp("blktrace", blktrace_args);
151 fprintf(stderr, "failed to exec blktrace error %s\n", strerror(errno));
157 signal(SIGTERM, sig_handler_for_quit);
158 signal(SIGINT, sig_handler_for_quit);
163 int run_program(char *str)
167 fprintf(stderr, "running program %s\n", str);
170 fprintf(stderr, "failed to run program %s error %s\n", str, strerror(errno));
178 int wait_for_tracers(void)
181 if (blktrace_pid != 0) {
182 waitpid(blktrace_pid, &status, WUNTRACED);
185 if (mpstat_pid != 0) {
186 waitpid(mpstat_pid, &status, WUNTRACED);
192 int blktrace_to_dump(char *trace_name)
194 snprintf(line, line_len, "blkparse -O -i %s -d '%s.%s'",
195 trace_name, trace_name, "dump");
201 int start_mpstat(char *trace_name)
206 snprintf(line, line_len, "%s.mpstat", trace_name);
208 fd = open(line, O_WRONLY | O_CREAT | O_TRUNC, 0600);
210 fprintf(stderr, "unable to open %s for writing err %s\n",
211 line, strerror(errno));
221 fprintf(stderr, "failed to setup output file for mpstat\n");
224 ret = execvp("mpstat", mpstat_args);
226 fprintf(stderr, "failed to exec mpstat err %s\n",