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 #include <sys/types.h>
26 #include <asm/types.h>
41 static char record_header[] = "CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle\n";
42 static char record_header_v2[] = "CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle\n";
44 int record_header_len = sizeof(record_header);
45 int record_header_v2_len = sizeof(record_header_v2);
47 static int past_eof(struct trace *trace, char *cur)
49 if (cur >= trace->mpstat_start + trace->mpstat_len)
54 int next_mpstat_line(struct trace *trace)
57 char *cur = trace->mpstat_cur;
59 next = strchr(cur, '\n');
63 if (past_eof(trace, next))
65 trace->mpstat_cur = next;
69 char *next_mpstat(struct trace *trace)
73 cur = strstr(trace->mpstat_cur, record_header);
75 cur += record_header_len;
77 cur = strstr(trace->mpstat_cur, record_header_v2);
79 cur += record_header_v2_len;
84 if (past_eof(trace, cur))
86 trace->mpstat_cur = cur;
90 char *first_mpstat(struct trace *trace)
92 char *cur = trace->mpstat_cur;
94 trace->mpstat_cur = trace->mpstat_start;
96 cur = next_mpstat(trace);
102 static void find_last_mpstat_time(struct trace *trace)
109 cur = first_mpstat(trace);
112 cur = next_mpstat(trace);
115 trace->mpstat_seconds = num_mpstats;
118 static int guess_mpstat_cpus(struct trace *trace)
124 cur = first_mpstat(trace);
129 ret = next_mpstat_line(trace);
133 cur = trace->mpstat_cur;
142 trace->mpstat_num_cpus = count - 1;
146 static int count_mpstat_cpus(struct trace *trace)
148 char *cur = trace->mpstat_start;
154 cpu = strstr(cur, " CPU)");
156 return guess_mpstat_cpus(trace);
158 line = strndup(cur, cpu - cur);
160 record = strrchr(line, '(');
167 len = line + strlen(line) - record;
169 cur = strndup(record, len);
170 trace->mpstat_num_cpus = atoi(cur);
174 return trace->mpstat_num_cpus;
177 static char *guess_filename(char *trace_name)
184 snprintf(line, line_len, "%s.mpstat", trace_name);
185 ret = stat(line, &st);
189 cur = strrchr(trace_name, '.');
194 tmp = strndup(trace_name, cur - trace_name);
195 snprintf(line, line_len, "%s.mpstat", tmp);
196 ret = stat(line, &st);
204 int read_mpstat(struct trace *trace, char *trace_name)
211 if (record_header_len == 0) {
212 record_header_len = strlen(record_header);
215 snprintf(line, line_len, "%s.mpstat", guess_filename(trace_name));
216 fd = open(line, O_RDONLY);
220 ret = fstat(fd, &st);
222 fprintf(stderr, "stat failed on %s err %s\n", line, strerror(errno));
225 p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
226 if (p == MAP_FAILED) {
227 fprintf(stderr, "Unable to mmap trace file %s, err %s\n", line, strerror(errno));
230 trace->mpstat_start = p;
231 trace->mpstat_len = st.st_size;
232 trace->mpstat_cur = p;
233 trace->mpstat_fd = fd;
234 find_last_mpstat_time(trace);
235 count_mpstat_cpus(trace);
247 * 09:56:26 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
251 * 10:18:51 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
254 * this reads just one line in the mpstat
256 int read_mpstat_event(struct trace *trace, double *user,
257 double *sys, double *iowait, double *irq,
260 char *cur = trace->mpstat_cur;
264 /* jump past the date and CPU number */
266 if (past_eof(trace, cur))
270 val = strtod(cur, &nptr);
271 if (val == 0 && cur == nptr)
275 /* nice time, pitch this one */
277 val = strtod(cur, &nptr);
278 if (val == 0 && cur == nptr)
283 val = strtod(cur, &nptr);
284 if (val == 0 && cur == nptr)
289 val = strtod(cur, &nptr);
290 if (val == 0 && cur == nptr)
295 val = strtod(cur, &nptr);
296 if (val == 0 && cur == nptr)
301 val = strtod(cur, &nptr);
302 if (val == 0 && cur == nptr)
309 int add_mpstat_gld(int time, double sys, struct graph_line_data *gld)
311 gld->data[time].sum = sys;
312 gld->data[time].count = 1;