blkparse: Fix a potential coredump issue
[blktrace.git] / iowatcher / tracers.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2012 Fusion-io
3 *
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.
7 *
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.
12 *
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
16 *
17 * Parts of this file were imported from Jens Axboe's blktrace sources (also GPL)
18 */
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <math.h>
26#include <inttypes.h>
27#include <string.h>
28#include <asm/types.h>
29#include <errno.h>
30#include <sys/mman.h>
31#include <time.h>
32#include <signal.h>
33#include <sys/wait.h>
34#include <spawn.h>
35
36#include "plot.h"
37#include "blkparse.h"
38#include "list.h"
39#include "tracers.h"
40
41extern char **environ;
42
43static pid_t blktrace_pid = 0;
44static pid_t mpstat_pid = 0;
45
46static void sig_handler_for_quit(int val)
47{
48 fprintf(stderr, "Received signal %d. Terminating tracers.\n", val);
49 wait_for_tracers(SIGTERM);
50}
51
52int start_blktrace(char **devices, int num_devices, char *trace_name, char *dest)
53{
54 int ret;
55 int i;
56 char *argv[15 + MAX_DEVICES_PER_TRACE * 2];
57 int argc = 0;
58
59 if (!trace_name)
60 trace_name = "trace";
61 if (!dest)
62 dest = ".";
63
64 argv[argc++] = "blktrace";
65 argv[argc++] = "-b";
66 argv[argc++] = "8192";
67 argv[argc++] = "-a";
68 argv[argc++] = "queue";
69 argv[argc++] = "-a";
70 argv[argc++] = "complete";
71 argv[argc++] = "-a";
72 argv[argc++] = "issue";
73 argv[argc++] = "-a";
74 argv[argc++] = "notify";
75
76 if (num_devices == 1) {
77 argv[argc++] = "-o";
78 argv[argc++] = trace_name;
79 } else {
80 /* Multiple devices output to a directory named trace_name */
81 dest = trace_name;
82 }
83 argv[argc++] = "-D";
84 argv[argc++] = dest;
85
86 for (i = 0; i < num_devices; i++) {
87 argv[argc++] = "-d";
88 argv[argc++] = devices[i];
89 }
90 argv[argc] = NULL;
91 signal(SIGTERM, sig_handler_for_quit);
92 signal(SIGINT, sig_handler_for_quit);
93 ret = run_program(argc, argv, 0, &blktrace_pid, NULL);
94 return ret;
95}
96
97int wait_program(pid_t pid, const char *pname, int sig)
98{
99 int status;
100 int ret = 0;
101
102 if (sig) {
103 ret = kill(pid, sig);
104 if (ret) {
105 fprintf(stderr, "Failed to send signal %d to %s (%lu): %s\n",
106 sig, pname, (long)pid, strerror(errno));
107 return ret;
108 }
109 fprintf(stderr, "Kill (%d): %s (%ld)\n", sig, pname, (long)pid);
110 }
111
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);
117 else
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));
121 ret = -1;
122 }
123 return ret;
124}
125
126int run_program(int argc, char **argv, int wait, pid_t *pid, char *outpath)
127{
128 int i;
129 int err;
130 pid_t _pid;
131 posix_spawn_file_actions_t facts;
132 posix_spawn_file_actions_t *factp = NULL;
133
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);
137 factp = &facts;
138 }
139
140 fprintf(stderr, "Start");
141 for (i = 0; i < argc; i++)
142 fprintf(stderr, " %s", argv[i]);
143 fprintf(stderr, "\n");
144
145 err = posix_spawnp(&_pid, argv[0], factp, NULL, argv, environ);
146 if (err != 0) {
147 fprintf(stderr, "Could not run '%s': %s\n", argv[0], strerror(err));
148 } else if (wait) {
149 err = wait_program(_pid, argv[0], 0);
150 } else if (!pid) {
151 fprintf(stderr, "Warning: %s (%ld): Not saving pid and not waiting for it.\n",
152 argv[0], (long)_pid);
153 } else {
154 *pid = _pid;
155 }
156 return err;
157}
158
159int wait_for_tracers(int sig)
160{
161 int err;
162 if (blktrace_pid != 0) {
163 err = wait_program(blktrace_pid, "blktrace", sig);
164 if (err)
165 exit(1);
166 blktrace_pid = 0;
167 }
168 if (mpstat_pid != 0) {
169 err = wait_program(mpstat_pid, "mpstat", sig);
170 if (err)
171 exit(1);
172 mpstat_pid = 0;
173 }
174 return 0;
175}
176
177int start_mpstat(char *path)
178{
179 int ret;
180 int argc = 4;
181 char *argv[] = {
182 "mpstat",
183 "-P", "ALL", "1",
184 NULL,
185 };
186
187 ret = run_program(argc, argv, 0, &mpstat_pid, path);
188 return ret;
189}