root / trunk / code / projects / libwireless / logger / analysis / analysis.c @ 190
History | View | Annotate | Download (5.94 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 |
if (feof(f))
|
77 |
break;
|
78 |
printf("Error reading times in file.\n");
|
79 |
exit(0);
|
80 |
} |
81 |
time = (60 * minutes + seconds) * 1000 + millis; |
82 |
|
83 |
char c = fgetc(f);
|
84 |
//token passed
|
85 |
if (c == 'p') |
86 |
{ |
87 |
int nextRobot;
|
88 |
ret = fscanf(f, "assed the token to robot %d. Sensor mat", &nextRobot);
|
89 |
if (ret != 1) |
90 |
{ |
91 |
printf("Error readings token passed.\n");
|
92 |
exit(0);
|
93 |
} |
94 |
|
95 |
int oldalive[MAX_ROBOTS];
|
96 |
for (i = 0; i < MAX_ROBOTS; i++) |
97 |
{ |
98 |
oldalive[i] = robots[i].alive; |
99 |
robots[i].alive = 0;
|
100 |
} |
101 |
|
102 |
c = fgetc(f); |
103 |
while (c != '\n') c = fgetc(f); |
104 |
|
105 |
do
|
106 |
{ |
107 |
int r, reading;
|
108 |
ret = fscanf(f, "(%d %d)", &r, &reading);
|
109 |
if (ret != 2) |
110 |
{ |
111 |
break;
|
112 |
} |
113 |
|
114 |
robots[r].alive = 1;
|
115 |
} while(1); |
116 |
robots[robot].alive = 1;
|
117 |
|
118 |
for (i = 0; i < MAX_ROBOTS; i++) |
119 |
if (!robots[i].alive && oldalive[i])
|
120 |
robots[i].death_count++; |
121 |
|
122 |
robots[robot].token_pass_time = time; |
123 |
if (robots[robot].bom_on_time != -1) |
124 |
{ |
125 |
robots[robot].token_pass_count++; |
126 |
robots[robot].total_token_held_time += time - robots[robot].bom_on_time; |
127 |
robots[robot].bom_on_time = -1;
|
128 |
} |
129 |
|
130 |
if (robots[robot].join_time != -1) |
131 |
{ |
132 |
robots[robot].total_join_time += time - robots[robot].join_time; |
133 |
robots[robot].join_time = -1;
|
134 |
} |
135 |
|
136 |
robots[nextRobot].token_passed_time = time; |
137 |
robots[nextRobot].token_passed_count++; |
138 |
if (robots[nextRobot].token_pass_time != -1) |
139 |
{ |
140 |
robots[nextRobot].total_token_waiting_time += time - robots[nextRobot].token_pass_time; |
141 |
robots[nextRobot].token_pass_time = -1;
|
142 |
} |
143 |
} |
144 |
else
|
145 |
{ |
146 |
ret = fscanf(f, "as ");
|
147 |
if (ret != 0) |
148 |
{ |
149 |
printf("Error reading line, unexpected value.\n");
|
150 |
exit(0);
|
151 |
} |
152 |
|
153 |
char c = fgetc(f);
|
154 |
// bom on
|
155 |
if (c == 't') |
156 |
{ |
157 |
ret = fscanf(f, "urned its BOM on.\n");
|
158 |
if (ret != 0) |
159 |
{ |
160 |
printf("Expected BOM on.\n");
|
161 |
exit(0);
|
162 |
} |
163 |
|
164 |
robots[robot].bom_on_time = time; |
165 |
if (robots[robot].token_passed_time != -1) |
166 |
{ |
167 |
robots[robot].bom_on_count++; |
168 |
robots[robot].total_token_delay_time += time - robots[robot].token_passed_time; |
169 |
} |
170 |
} |
171 |
// join request
|
172 |
else if (c == 'r') |
173 |
{ |
174 |
ret = fscanf(f, "equested to join the token ring.\n");
|
175 |
if (ret != 0) |
176 |
{ |
177 |
printf("Expected join request.\n");
|
178 |
exit(0);
|
179 |
} |
180 |
|
181 |
robots[robot].join_time = time; |
182 |
robots[robot].join_count++; |
183 |
} |
184 |
else
|
185 |
{ |
186 |
printf("Unexpected line.\n");
|
187 |
exit(0);
|
188 |
} |
189 |
|
190 |
} |
191 |
|
192 |
} |
193 |
|
194 |
int end_time = time;
|
195 |
|
196 |
// analyze information
|
197 |
int join_time = 0, token_held_time = 0, token_waiting_time = 0, token_delay_time = 0; |
198 |
int token_passed_count = 0, death_count = 0, join_count = 0; |
199 |
|
200 |
int num_robots = 0;; |
201 |
|
202 |
for (i = 0; i < MAX_ROBOTS; i++) |
203 |
{ |
204 |
if (robots[i].token_pass_count != 0) |
205 |
num_robots++; |
206 |
join_time += robots[i].total_join_time; |
207 |
token_held_time += robots[i].total_token_held_time; |
208 |
token_waiting_time += robots[i].total_token_waiting_time; |
209 |
token_delay_time += robots[i].total_token_delay_time; |
210 |
token_passed_count += robots[i].token_passed_count; |
211 |
death_count += robots[i].death_count; |
212 |
join_count += robots[i].join_count; |
213 |
} |
214 |
|
215 |
int join_average = 0; |
216 |
if (join_count != 0) |
217 |
join_average = join_time / join_count; |
218 |
int token_held_average = 0, token_waiting_average = 0, token_delay_average = 0; |
219 |
if (token_passed_count != 0) |
220 |
{ |
221 |
token_held_average = token_held_time / token_passed_count; |
222 |
token_waiting_average = token_waiting_time / token_passed_count; |
223 |
token_delay_average = token_delay_time / token_passed_count; |
224 |
} |
225 |
|
226 |
// display information
|
227 |
printf("Robots:\t\t\t\t\t\t%d\n", num_robots);
|
228 |
printf("Time (seconds):\t\t\t\t\t%d\n", end_time / 1000 + 1); |
229 |
printf("Deaths:\t\t\t\t\t\t%d\n", death_count);
|
230 |
printf("\n");
|
231 |
printf("Deaths per Minute:\t\t\t\t%.2g\n", (double)death_count / end_time * 1000 * 60); |
232 |
printf("Rejoin Time (seconds):\t\t\t\t%.3g\n", (double)join_average / 1000); |
233 |
printf("Seconds Between BOM Flash and Token Sent:\t%.3g\n",(double)token_held_average / 1000); |
234 |
printf("Seconds Between Token Sent and Token Returned:\t%.3g\n", (double)token_waiting_average / 1000); |
235 |
printf("Seconds Between Token Sent and BOM Flash:\t%.3g\n", (double)token_delay_average / 1000); |
236 |
printf("\n");
|
237 |
printf("Token Passes per Second:\t\t\t%.3g\n", (double)token_passed_count / end_time * 1000); |
238 |
printf("\n");
|
239 |
printf("Deaths per Robot per Minute:\t\t\t%.2g\n", (double)death_count / end_time * 1000 * 60 / num_robots); |
240 |
printf("Token Passes per Robot per Second:\t\t%.2g\n", (double)token_passed_count / end_time * 1000 / num_robots); |
241 |
} |
242 |
|