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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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[] = {
58 char *mpstat_args[] = {
64 #define DEVICE_INDEX 2
65 #define DEST_DIR_INDEX 8
66 #define TRACE_NAME_INDEX 6
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 *device, char *trace_name, char *dest)
110 char **arg = blktrace_args;
111 blktrace_args[DEVICE_INDEX] = device;
113 fprintf(stderr, "running blktrace");
115 blktrace_args[DEST_DIR_INDEX] = dest;
117 blktrace_args[TRACE_NAME_INDEX] = trace_name;
120 fprintf(stderr, " %s", *arg);
123 fprintf(stderr, "\n");
128 ret = execvp("blktrace", blktrace_args);
130 fprintf(stderr, "failed to exec blktrace error %s\n", strerror(errno));
136 signal(SIGTERM, sig_handler_for_quit);
137 signal(SIGINT, sig_handler_for_quit);
142 int run_program(char *str)
146 fprintf(stderr, "running program %s\n", str);
149 fprintf(stderr, "failed to run program %s error %s\n", str, strerror(errno));
157 int wait_for_tracers(void)
160 if (blktrace_pid != 0) {
161 waitpid(blktrace_pid, &status, WUNTRACED);
164 if (mpstat_pid != 0) {
165 waitpid(mpstat_pid, &status, WUNTRACED);
171 int blktrace_to_dump(char *trace_name)
173 snprintf(line, line_len, "blkparse -O -i %s -d '%s.%s'",
174 trace_name, trace_name, "dump");
180 int start_mpstat(char *trace_name)
185 snprintf(line, line_len, "%s.mpstat", trace_name);
187 fd = open(line, O_WRONLY | O_CREAT | O_TRUNC);
189 fprintf(stderr, "unable to open %s for writing err %s\n",
190 line, strerror(errno));
200 fprintf(stderr, "failed to setup output file for mpstat\n");
203 ret = execvp("mpstat", mpstat_args);
205 fprintf(stderr, "failed to exec mpstat err %s\n",