root / trunk / code / projects / libwireless / logger / analysis / analysis.c @ 179
History | View | Annotate | Download (5.76 KB)
1 |
#include <stdlib.h> |
---|---|
2 |
#include <stdio.h> |
3 |
|
4 |
#define MAX_ROBOTS 20 |
5 |
|
6 |
// all times in milliseconds
|
7 |
typedef struct RobotDef |
8 |
{ |
9 |
int alive;
|
10 |
|
11 |
//times when we last sent a packet
|
12 |
int join_time;
|
13 |
int bom_on_time;
|
14 |
// passed to us
|
15 |
int token_passed_time;
|
16 |
// passed from us
|
17 |
int token_pass_time;
|
18 |
|
19 |
//time we have spent in a given state
|
20 |
int total_join_time;
|
21 |
int total_token_held_time;
|
22 |
int total_token_waiting_time;
|
23 |
// delay between sent to us and our BOM on packet
|
24 |
int total_token_delay_time;
|
25 |
// times each state has occurred
|
26 |
int join_count;
|
27 |
int bom_on_count;
|
28 |
int token_pass_count;
|
29 |
int token_passed_count;
|
30 |
int death_count;
|
31 |
} Robot; |
32 |
|
33 |
Robot robots[MAX_ROBOTS]; |
34 |
|
35 |
int main(int argc, char** argv) |
36 |
{ |
37 |
if (argc < 2) |
38 |
{ |
39 |
printf("Must specify a log file to analyze.\n");
|
40 |
return 0; |
41 |
} |
42 |
FILE* f = fopen(argv[1], "r"); |
43 |
if (f == NULL) |
44 |
{ |
45 |
printf("Failed to open %s.\n", argv[1]); |
46 |
return 0; |
47 |
} |
48 |
|
49 |
int i;
|
50 |
for (i = 0; i < MAX_ROBOTS; i++) |
51 |
{ |
52 |
robots[i].alive = 0;
|
53 |
robots[i].join_time = -1;
|
54 |
robots[i].bom_on_time = -1;
|
55 |
robots[i].token_passed_time = -1;
|
56 |
robots[i].token_pass_time = -1;
|
57 |
robots[i].total_join_time = 0;
|
58 |
robots[i].total_token_delay_time = 0;
|
59 |
robots[i].total_token_held_time = 0;
|
60 |
robots[i].total_token_waiting_time = 0;
|
61 |
robots[i].join_count = 0;
|
62 |
robots[i].bom_on_count = 0;
|
63 |
robots[i].token_pass_count = 0;
|
64 |
robots[i].token_passed_count = 0;
|
65 |
robots[i].death_count = 0;
|
66 |
} |
67 |
|
68 |
int time;
|
69 |
|
70 |
while (!feof(f))
|
71 |
{ |
72 |
int minutes, seconds, millis, robot;
|
73 |
int ret = fscanf(f, "%d:%d.%d: Robot %d ", &minutes, &seconds, &millis, &robot); |
74 |
if (ret != 4) |
75 |
{ |
76 |
printf("Error reading times in file.\n");
|
77 |
exit(0);
|
78 |
} |
79 |
time = (60 * minutes + seconds) * 1000 + millis; |
80 |
|
81 |
char c = fgetc(f);
|
82 |
//token passed
|
83 |
if (c == 'p') |
84 |
{ |
85 |
int nextRobot;
|
86 |
ret = fscanf(f, "assed the token to robot %d. Sensor mat", &nextRobot);
|
87 |
if (ret != 1) |
88 |
{ |
89 |
printf("Error readings token passed.\n");
|
90 |
exit(0);
|
91 |
} |
92 |
|
93 |
int oldalive[MAX_ROBOTS];
|
94 |
for (i = 0; i < MAX_ROBOTS; i++) |
95 |
{ |
96 |
oldalive[i] = robots[i].alive; |
97 |
robots[i].alive = 0;
|
98 |
} |
99 |
|
100 |
c = fgetc(f); |
101 |
while (c != '\n') c = fgetc(f); |
102 |
|
103 |
do
|
104 |
{ |
105 |
int r, reading;
|
106 |
ret = fscanf(f, "(%d %d)", &r, &reading);
|
107 |
if (ret != 2) |
108 |
{ |
109 |
break;
|
110 |
} |
111 |
|
112 |
robots[r].alive = 1;
|
113 |
} while(1); |
114 |
robots[robot].alive = 1;
|
115 |
|
116 |
for (i = 0; i < MAX_ROBOTS; i++) |
117 |
if (!robots[i].alive && oldalive[i])
|
118 |
robots[i].death_count++; |
119 |
|
120 |
robots[robot].token_pass_time = time; |
121 |
if (robots[robot].bom_on_time != -1) |
122 |
{ |
123 |
robots[robot].token_pass_count++; |
124 |
robots[robot].total_token_held_time += time - robots[robot].bom_on_time; |
125 |
robots[robot].bom_on_time = -1;
|
126 |
} |
127 |
|
128 |
if (robots[robot].join_time != -1) |
129 |
{ |
130 |
robots[robot].total_join_time += time - robots[robot].join_time; |
131 |
robots[robot].join_time = -1;
|
132 |
} |
133 |
|
134 |
robots[nextRobot].token_passed_time = time; |
135 |
robots[nextRobot].token_passed_count++; |
136 |
if (robots[nextRobot].token_pass_time != -1) |
137 |
{ |
138 |
robots[nextRobot].total_token_waiting_time += time - robots[nextRobot].token_pass_time; |
139 |
robots[nextRobot].token_pass_time = -1;
|
140 |
} |
141 |
} |
142 |
else
|
143 |
{ |
144 |
ret = fscanf(f, "as ");
|
145 |
if (ret != 0) |
146 |
{ |
147 |
printf("Error reading line, unexpected value.\n");
|
148 |
exit(0);
|
149 |
} |
150 |
|
151 |
char c = fgetc(f);
|
152 |
// bom on
|
153 |
if (c == 't') |
154 |
{ |
155 |
ret = fscanf(f, "urned its BOM on.\n");
|
156 |
if (ret != 0) |
157 |
{ |
158 |
printf("Expected BOM on.\n");
|
159 |
exit(0);
|
160 |
} |
161 |
|
162 |
robots[robot].bom_on_time = time; |
163 |
if (robots[robot].token_passed_time != -1) |
164 |
{ |
165 |
robots[robot].bom_on_count++; |
166 |
robots[robot].total_token_delay_time += time - robots[robot].token_passed_time; |
167 |
} |
168 |
} |
169 |
// join request
|
170 |
else if (c == 'r') |
171 |
{ |
172 |
ret = fscanf(f, "equested to join the token ring.\n");
|
173 |
if (ret != 0) |
174 |
{ |
175 |
printf("Expected join request.\n");
|
176 |
exit(0);
|
177 |
} |
178 |
|
179 |
robots[robot].join_time = time; |
180 |
robots[robot].join_count++; |
181 |
} |
182 |
else
|
183 |
{ |
184 |
printf("Unexpected line.\n");
|
185 |
exit(0);
|
186 |
} |
187 |
|
188 |
} |
189 |
|
190 |
} |
191 |
|
192 |
int end_time = time;
|
193 |
|
194 |
// analyze information
|
195 |
int join_time = 0, token_held_time = 0, token_waiting_time = 0, token_delay_time = 0; |
196 |
int token_passed_count = 0, death_count = 0, join_count = 0; |
197 |
|
198 |
int num_robots = 0;; |
199 |
|
200 |
for (i = 0; i < MAX_ROBOTS; i++) |
201 |
{ |
202 |
if (robots[i].token_pass_count != 0) |
203 |
num_robots++; |
204 |
join_time += robots[i].total_join_time; |
205 |
token_held_time += robots[i].total_token_held_time; |
206 |
token_waiting_time += robots[i].total_token_waiting_time; |
207 |
token_delay_time += robots[i].total_token_delay_time; |
208 |
token_passed_count += robots[i].token_passed_count; |
209 |
death_count += robots[i].death_count; |
210 |
join_count += robots[i].join_count; |
211 |
} |
212 |
|
213 |
int join_average = join_time / join_count;
|
214 |
int token_held_average = token_held_time / token_passed_count;
|
215 |
int token_waiting_average = token_waiting_time / token_passed_count;
|
216 |
int token_delay_average = token_delay_time / token_passed_count;
|
217 |
|
218 |
// display information
|
219 |
printf("Robots:\t\t\t\t\t\t%d\n", num_robots);
|
220 |
printf("Time (seconds):\t\t\t\t\t%d\n", end_time / 1000 + 1); |
221 |
printf("Deaths:\t\t\t\t\t\t%d\n", death_count);
|
222 |
printf("\n");
|
223 |
printf("Deaths per Minute:\t\t\t\t%.2g\n", (double)death_count / end_time * 1000 * 60); |
224 |
printf("Rejoin Time (seconds):\t\t\t\t%.3g\n", (double)join_average / 1000); |
225 |
printf("Seconds Between BOM Flash and Token Sent:\t%.3g\n",(double)token_held_average / 1000); |
226 |
printf("Seconds Between Token Sent and Token Returned:\t%.3g\n", (double)token_waiting_average / 1000); |
227 |
printf("Seconds Between Token Sent and BOM Flash:\t%.3g\n", (double)token_delay_average / 1000); |
228 |
printf("\n");
|
229 |
printf("Token Passes per Second:\t\t\t%.3g\n", (double)token_passed_count / end_time * 1000); |
230 |
printf("\n");
|
231 |
printf("Deaths per Robot per Minute:\t\t\t%.2g\n", (double)death_count / end_time * 1000 * 60 / num_robots); |
232 |
printf("Token Passes per Robot per Second:\t\t%.2g\n", (double)token_passed_count / end_time * 1000 / num_robots); |
233 |
} |
234 |
|