-
Notifications
You must be signed in to change notification settings - Fork 0
/
blkdata.c
144 lines (123 loc) · 2.86 KB
/
blkdata.c
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
/*
* blkdata.c
*
* Line format:
* 8,0 0 3 0.000393417 6781 D W 0 + 4 [test_seq]
* 8,0 0 4 0.000759113 2 C W 0 + 4 [0]
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#define MAXLINE 128
#define PAGESIZE 2048
#define SECTORSIZE 512
static char linebuf[MAXLINE];
static unsigned long i;
static void sig_SIGUSR1(int arg)
{
i = 0;
}
static char *strtrim(char *str)
{
char *tmp = str;
while (isspace(*tmp))
tmp++;
return tmp;
}
int main(int argc, char **argv)
{
unsigned long linenum = 0;
int issued_flag = 0;
FILE *fp;
unsigned long count = 0;
float max_time = 0.00F;
if (argc < 2)
fp = stdin;
else
if ((fp = fopen(argv[1], "r")) == NULL) {
perror("fopen() failed");
return -1;
}
if (signal(SIGUSR1, sig_SIGUSR1) == SIG_ERR) {
perror("signal");
fclose(fp);
return -1;
}
printf("# SEQ\tOP\tCHUNK\tTIME\n");
while (fgets(linebuf, MAXLINE-1, fp) != NULL) {
char *line = strtrim(linebuf);
char action, command;
int major, minor, cpuid, seq, pid;
int cpu_issue, cpu_complete;
unsigned long start_offset, length;
unsigned long issue[2], complete[2];
double timestamp;
double time_issue, time_complete;
++linenum;
if (line[0] == '#')
continue; /* Ignore the line. */
/* We reached the end of trace data. */
if (!strncmp("CPU", line, strlen("CPU"))) {
break;
}
sscanf(line, "%d,%d %d %d %lf %d %c %c %lu + %lu",
&major, &minor, &cpuid, &seq, ×tamp, &pid, &action,
&command, &start_offset, &length);
if (command == 'N')
continue;
if (action == 'D') {
issued_flag = 1;
time_issue = timestamp;
cpu_issue = cpuid;
issue[0] = start_offset;
issue[1] = length;
}
else if (action == 'C') {
double current;
/*
unsigned long pageno = start_offset * SECTORSIZE / PAGESIZE;
*/
unsigned long chunkno = start_offset / length;
time_complete = timestamp;
current = (time_complete - time_issue) * 1000000;
cpu_complete = cpuid;
complete[0] = start_offset;
complete[1] = length;
/*
if (length * SECTORSIZE != PAGESIZE) {
fprintf(stderr, "length(%lu) is not page size(%d).\n",
length * SECTORSIZE, PAGESIZE);
goto err;
}
*/
if (!issued_flag)
goto err;
/*
printf("%lu\t[%c]\t%lu\t%10.3lf\n", i++, command, pageno, current);
*/
printf("%lu\t[%c]\t%lu\t%10.3lf\n", i++, command, chunkno, current);
fflush(stdout); /* Is this line really needed? */
issued_flag = 0;
if (max_time < current)
max_time = current;
count++;
}
else {
err:
fprintf(stderr, "file format wrong: %s(linenum = %lu)\n", line, linenum);
fclose(fp);
return -1;
}
}
if (ferror(fp)) {
perror("fgets() failed");
fclose(fp);
return -1;
}
fprintf(stderr, "xrange [0:%lu]\n", count);
fprintf(stderr, "yrange [0:%.3f]\n", max_time);
fclose(fp);
return 0;
}