1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*
* Copyright (C) 2012 Fusion-io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <inttypes.h>
#include <string.h>
#include <asm/types.h>
#include <errno.h>
#include <sys/mman.h>
#include <time.h>
#include <math.h>
#include "plot.h"
#include "blkparse.h"
#include "list.h"
#include "tracers.h"
#include "mpstat.h"
char line[1024];
int line_len = 1024;
static char record_header[] = "CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle\n";
static char record_header_v2[] = "CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle\n";
int record_header_len = sizeof(record_header);
int record_header_v2_len = sizeof(record_header_v2);
static int past_eof(struct trace *trace, char *cur)
{
if (cur >= trace->mpstat_start + trace->mpstat_len)
return 1;
return 0;
}
int next_mpstat_line(struct trace *trace)
{
char *next;
char *cur = trace->mpstat_cur;
next = strchr(cur, '\n');
if (!next)
return 1;
next++;
if (past_eof(trace, next))
return 1;
trace->mpstat_cur = next;
return 0;
}
char *next_mpstat(struct trace *trace)
{
char *cur;
cur = strstr(trace->mpstat_cur, record_header);
if (cur) {
cur += record_header_len;
} else {
cur = strstr(trace->mpstat_cur, record_header_v2);
if (cur)
cur += record_header_v2_len;
}
if (!cur)
return NULL;
if (past_eof(trace, cur))
return NULL;
trace->mpstat_cur = cur;
return cur;
}
char *first_mpstat(struct trace *trace)
{
char *cur = trace->mpstat_cur;
trace->mpstat_cur = trace->mpstat_start;
cur = next_mpstat(trace);
if (!cur)
return NULL;
return cur;
}
static void find_last_mpstat_time(struct trace *trace)
{
int num_mpstats = 0;
char *cur;
first_mpstat(trace);
cur = first_mpstat(trace);
while (cur) {
num_mpstats++;
cur = next_mpstat(trace);
}
first_mpstat(trace);
trace->mpstat_seconds = num_mpstats;
}
static int guess_mpstat_cpus(struct trace *trace)
{
char *cur;
int ret;
int count = 0;
cur = first_mpstat(trace);
if (!cur)
return 0;
while (1) {
ret = next_mpstat_line(trace);
if (ret)
break;
cur = trace->mpstat_cur;
count++;
if (!cur)
break;
if (cur[0] == '\n')
break;
}
trace->mpstat_num_cpus = count - 1;
return 0;
}
static int count_mpstat_cpus(struct trace *trace)
{
char *cur = trace->mpstat_start;
char *cpu;
char *record;
int len; char *line;
first_mpstat(trace);
cpu = strstr(cur, " CPU)");
if (!cpu)
return guess_mpstat_cpus(trace);
line = strndup(cur, cpu - cur);
record = strrchr(line, '(');
if (!record) {
free(line);
return 0;
}
record++;
len = line + strlen(line) - record;
cur = strndup(record, len);
trace->mpstat_num_cpus = atoi(cur);
first_mpstat(trace);
free(line);
return trace->mpstat_num_cpus;
}
static char *guess_filename(char *trace_name)
{
struct stat st;
int ret;
char *cur;
char *tmp;
snprintf(line, line_len, "%s.mpstat", trace_name);
ret = stat(line, &st);
if (ret == 0)
return trace_name;
cur = strrchr(trace_name, '.');
if (!cur) {
return trace_name;
}
tmp = strndup(trace_name, cur - trace_name);
snprintf(line, line_len, "%s.mpstat", tmp);
ret = stat(line, &st);
if (ret == 0)
return tmp;
free(tmp);
return trace_name;
}
int read_mpstat(struct trace *trace, char *trace_name)
{
int fd;
struct stat st;
int ret;
char *p;
if (record_header_len == 0) {
record_header_len = strlen(record_header);
}
snprintf(line, line_len, "%s.mpstat", guess_filename(trace_name));
fd = open(line, O_RDONLY);
if (fd < 0)
return 0;
ret = fstat(fd, &st);
if (ret < 0) {
fprintf(stderr, "stat failed on %s err %s\n", line, strerror(errno));
goto fail_fd;
}
p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (p == MAP_FAILED) {
fprintf(stderr, "Unable to mmap trace file %s, err %s\n", line, strerror(errno));
goto fail_fd;
}
trace->mpstat_start = p;
trace->mpstat_len = st.st_size;
trace->mpstat_cur = p;
trace->mpstat_fd = fd;
find_last_mpstat_time(trace);
count_mpstat_cpus(trace);
first_mpstat(trace);
return 0;
fail_fd:
close(fd);
return 0;
}
/*
* 09:56:26 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
*
* or
*
* 10:18:51 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
*
*
* this reads just one line in the mpstat
*/
int read_mpstat_event(struct trace *trace, double *user,
double *sys, double *iowait, double *irq,
double *soft)
{
char *cur = trace->mpstat_cur;
char *nptr;
double val;
/* jump past the date and CPU number */
cur += 16;
if (past_eof(trace, cur))
return 1;
/* usr time */
val = strtod(cur, &nptr);
if (val == 0 && cur == nptr)
return 1;
*user = val;
/* nice time, pitch this one */
cur = nptr;
val = strtod(cur, &nptr);
if (val == 0 && cur == nptr)
return 1;
/* system time */
cur = nptr;
val = strtod(cur, &nptr);
if (val == 0 && cur == nptr)
return 1;
*sys = val;
cur = nptr;
val = strtod(cur, &nptr);
if (val == 0 && cur == nptr)
return 1;
*iowait = val;
cur = nptr;
val = strtod(cur, &nptr);
if (val == 0 && cur == nptr)
return 1;
*irq = val;
cur = nptr;
val = strtod(cur, &nptr);
if (val == 0 && cur == nptr)
return 1;
*soft = val;
return 0;
}
int add_mpstat_gld(int time, double sys, struct graph_line_data *gld)
{
gld->data[time].sum = sys;
gld->data[time].count = 1;
return 0;
}
|