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 #include <sys/types.h>
26 #include <asm/types.h>
41 char *record_header = "CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle\n";
42 int record_header_len = 0;
44 static int past_eof(struct trace *trace, char *cur)
46 if (cur >= trace->mpstat_start + trace->mpstat_len)
51 int next_mpstat_line(struct trace *trace)
54 char *cur = trace->mpstat_cur;
56 next = strchr(cur, '\n');
60 if (past_eof(trace, next))
62 trace->mpstat_cur = next;
66 char *next_mpstat(struct trace *trace)
68 char *cur = trace->mpstat_cur;
70 cur = strstr(cur, record_header);
74 cur += record_header_len;
75 if (past_eof(trace, cur))
77 trace->mpstat_cur = cur;
81 char *first_mpstat(struct trace *trace)
83 char *cur = trace->mpstat_cur;
85 trace->mpstat_cur = trace->mpstat_start;
87 cur = next_mpstat(trace);
93 static void find_last_mpstat_time(struct trace *trace)
100 cur = first_mpstat(trace);
103 cur = next_mpstat(trace);
106 trace->mpstat_seconds = num_mpstats;
109 static int count_mpstat_cpus(struct trace *trace)
111 char *cur = trace->mpstat_start;
119 cpu = strstr(cur, " CPU)");
122 line = strndup(cur, cpu - cur);
124 record = strrchr(line, '(');
131 len = line + strlen(line) - record;
133 cur = strndup(record, len);
134 trace->mpstat_num_cpus = atoi(cur);
137 return trace->mpstat_num_cpus;
140 static char *guess_filename(char *trace_name)
147 snprintf(line, line_len, "%s.mpstat", trace_name);
148 ret = stat(line, &st);
152 cur = strrchr(trace_name, '.');
157 tmp = strndup(trace_name, cur - trace_name);
158 snprintf(line, line_len, "%s.mpstat", tmp);
159 ret = stat(line, &st);
167 int read_mpstat(struct trace *trace, char *trace_name)
174 if (record_header_len == 0) {
175 record_header_len = strlen(record_header);
178 snprintf(line, line_len, "%s.mpstat", guess_filename(trace_name));
180 fd = open(line, O_RDONLY);
184 ret = fstat(fd, &st);
186 fprintf(stderr, "stat failed on %s err %s\n", line, strerror(errno));
189 p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
190 if (p == MAP_FAILED) {
191 fprintf(stderr, "Unable to mmap trace file %s, err %s\n", line, strerror(errno));
195 trace->mpstat_start = p;
196 trace->mpstat_len = st.st_size;
197 trace->mpstat_cur = p;
198 trace->mpstat_fd = fd;
199 find_last_mpstat_time(trace);
200 count_mpstat_cpus(trace);
212 * 09:56:26 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
214 * this reads just one line in the mpstat
216 int read_mpstat_event(struct trace *trace, double *user,
217 double *sys, double *iowait, double *irq,
220 char *cur = trace->mpstat_cur;
224 /* jump past the date and CPU number */
226 if (past_eof(trace, cur))
230 val = strtod(cur, &nptr);
231 if (val == 0 && cur == nptr)
235 /* nice time, pitch this one */
237 val = strtod(cur, &nptr);
238 if (val == 0 && cur == nptr)
243 val = strtod(cur, &nptr);
244 if (val == 0 && cur == nptr)
249 val = strtod(cur, &nptr);
250 if (val == 0 && cur == nptr)
255 val = strtod(cur, &nptr);
256 if (val == 0 && cur == nptr)
261 val = strtod(cur, &nptr);
262 if (val == 0 && cur == nptr)
269 int add_mpstat_gld(int time, double sys, struct graph_line_data *gld)
271 gld->data[time].sum = sys;
272 gld->data[time].count = 1;