37f4194e8f02987cba2d35ae8be6a79cfb82b3d5
[lttv.git] / lttv / lttv / sync / event_processing_text.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2009, 2010 Benjamin Poirier <benjamin.poirier@polymtl.ca>
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 2.1 of the License, or (at
7 * your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 * License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #define _GNU_SOURCE
19 #define NANOSECONDS_PER_SECOND 1000000000
20 #define CPU_FREQ 1e9
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <math.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "sync_chain.h"
33
34 #include "event_processing_text.h"
35
36
37 // Functions common to all processing modules
38 static void initProcessingText(SyncState* const syncState, ...);
39 static void destroyProcessingText(SyncState* const syncState);
40 static GArray* finalizeProcessingText(SyncState* const syncState);
41 static void printProcessingStatsText(SyncState* const syncState);
42 static void writeProcessingTraceTimeOptionsText(SyncState* const syncState,
43 const unsigned int i, const unsigned int j);
44 static void writeProcessingTraceTraceOptionsText(SyncState* const syncState,
45 const unsigned int i, const unsigned int j);
46 static void writeProcessingGraphVariablesText(SyncState* const syncState,
47 const unsigned int i);
48
49 // Functions specific to this module
50 static unsigned int readTraceNb(FILE* testCase);
51 static void skipCommentLines(FILE* testCase);
52
53
54 static ProcessingModule processingModuleText = {
55 .name= "text",
56 .initProcessing= &initProcessingText,
57 .destroyProcessing= &destroyProcessingText,
58 .finalizeProcessing= &finalizeProcessingText,
59 .printProcessingStats= &printProcessingStatsText,
60 .graphFunctions= {
61 .writeVariables= &writeProcessingGraphVariablesText,
62 .writeTraceTraceOptions= &writeProcessingTraceTraceOptionsText,
63 .writeTraceTimeOptions= &writeProcessingTraceTimeOptionsText,
64 },
65 };
66
67
68 /*
69 * Processing Module registering function
70 */
71 void registerProcessingText()
72 {
73 g_queue_push_tail(&processingModules, &processingModuleText);
74 }
75
76
77 /*
78 * Allocate and initialize data structures for synchronizing a traceset.
79 * Open test case file.
80 *
81 * Args:
82 * syncState: container for synchronization data.
83 * testCaseName: const char*, test case file name
84 */
85 static void initProcessingText(SyncState* const syncState, ...)
86 {
87 ProcessingDataText* processingData;
88 const char* testCaseName;
89 va_list ap;
90
91 processingData= malloc(sizeof(ProcessingDataText));
92 syncState->processingData= processingData;
93 va_start(ap, syncState);
94 testCaseName= va_arg(ap, const char*);
95 va_end(ap);
96
97 processingData->testCase= fopen(testCaseName, "r");
98 if (processingData->testCase == NULL)
99 {
100 g_error(strerror(errno));
101 }
102 syncState->traceNb= readTraceNb(processingData->testCase);
103
104 if (syncState->stats)
105 {
106 processingData->factors= NULL;
107 }
108 }
109
110
111 static void destroyProcessingText(SyncState* const syncState)
112 {
113 ProcessingDataText* processingData= (ProcessingDataText*)
114 syncState->processingData;
115
116 if (processingData == NULL)
117 {
118 return;
119 }
120
121 fclose(processingData->testCase);
122
123 if (syncState->stats && processingData->factors)
124 {
125 g_array_free(processingData->factors, TRUE);
126 }
127
128 free(syncState->processingData);
129 syncState->processingData= NULL;
130 }
131
132
133 /*
134 * Read the test case file and make up events. Dispatch those events to the
135 * matching module.
136 *
137 * Args:
138 * syncState: container for synchronization data.
139 *
140 * Returns:
141 * Factors[traceNb] synchronization factors for each trace
142 */
143 static GArray* finalizeProcessingText(SyncState* const syncState)
144 {
145 int retval;
146 unsigned int* seq;
147 GArray* factors;
148 ProcessingDataText* processingData= (ProcessingDataText*)
149 syncState->processingData;
150 FILE* testCase= processingData->testCase;
151 char* line= NULL;
152 size_t bufLen;
153
154 seq= calloc(syncState->traceNb, sizeof(unsigned int));
155
156 skipCommentLines(testCase);
157 retval= getline(&line, &bufLen, testCase);
158 while(!feof(testCase))
159 {
160 unsigned int sender, receiver;
161 double sendTime, recvTime;
162 char tmp;
163 unsigned int i;
164
165 if (retval == -1 && !feof(testCase))
166 {
167 g_error(strerror(errno));
168 }
169
170 if (line[retval - 1] == '\n')
171 {
172 line[retval - 1]= '\0';
173 }
174
175 retval= sscanf(line, " %u %u %lf %lf %c", &sender, &receiver,
176 &sendTime, &recvTime, &tmp);
177 if (retval == EOF)
178 {
179 g_error(strerror(errno));
180 }
181 else if (retval != 4)
182 {
183 g_error("Error parsing test file while looking for data point, line was '%s'", line);
184 }
185
186 if (sender + 1 > syncState->traceNb)
187 {
188 g_error("Error parsing test file, sender is out of range, line was '%s'", line);
189 }
190
191 if (receiver + 1 > syncState->traceNb)
192 {
193 g_error("Error parsing test file, receiver is out of range, line was '%s'", line);
194 }
195
196 if (sendTime < 0)
197 {
198 g_error("Error parsing test file, send time is negative, line was '%s'", line);
199 }
200
201 if (recvTime < 0)
202 {
203 g_error("Error parsing test file, receive time is negative, line was '%s'", line);
204 }
205
206 // Generate ouput and input events
207 {
208 unsigned int addressOffset;
209 struct {
210 unsigned int traceNum;
211 double time;
212 enum Direction direction;
213 } loopValues[]= {
214 {sender, sendTime, OUT},
215 {receiver, recvTime, IN},
216 };
217
218 /* addressOffset is added to a traceNum to convert it to an address so
219 * that the address is not plainly the same as the traceNb. */
220 if (syncState->traceNb > 1)
221 {
222 addressOffset= pow(10, floor(log(syncState->traceNb - 1) /
223 log(10)) + 1);
224 }
225 else
226 {
227 addressOffset= 0;
228 }
229
230 for (i= 0; i < sizeof(loopValues) / sizeof(*loopValues); i++)
231 {
232 Event* event;
233
234 event= malloc(sizeof(Event));
235 event->traceNum= loopValues[i].traceNum;
236 event->wallTime.seconds= floor(loopValues[i].time);
237 event->wallTime.nanosec= floor((loopValues[i].time -
238 floor(loopValues[i].time)) * NANOSECONDS_PER_SECOND);
239 event->cpuTime= round(loopValues[i].time * CPU_FREQ);
240 event->type= TCP;
241 event->destroy= &destroyTCPEvent;
242 event->event.tcpEvent= malloc(sizeof(TCPEvent));
243 event->event.tcpEvent->direction= loopValues[i].direction;
244 event->event.tcpEvent->segmentKey= malloc(sizeof(SegmentKey));
245 event->event.tcpEvent->segmentKey->ihl= 5;
246 event->event.tcpEvent->segmentKey->tot_len= 40;
247 event->event.tcpEvent->segmentKey->connectionKey.saddr= sender +
248 addressOffset;
249 event->event.tcpEvent->segmentKey->connectionKey.daddr= receiver +
250 addressOffset;
251 event->event.tcpEvent->segmentKey->connectionKey.source= 57645;
252 event->event.tcpEvent->segmentKey->connectionKey.dest= 80;
253 event->event.tcpEvent->segmentKey->seq= seq[sender];
254 event->event.tcpEvent->segmentKey->ack_seq= 0;
255 event->event.tcpEvent->segmentKey->doff= 5;
256 event->event.tcpEvent->segmentKey->ack= 0;
257 event->event.tcpEvent->segmentKey->rst= 0;
258 event->event.tcpEvent->segmentKey->syn= 1;
259 event->event.tcpEvent->segmentKey->fin= 0;
260
261 syncState->matchingModule->matchEvent(syncState, event);
262 }
263 }
264
265 seq[sender]++;
266
267 skipCommentLines(testCase);
268 retval= getline(&line, &bufLen, testCase);
269 }
270
271 free(seq);
272
273 if (line)
274 {
275 free(line);
276 }
277
278 factors= syncState->matchingModule->finalizeMatching(syncState);
279 if (syncState->stats)
280 {
281 processingData->factors= factors;
282 }
283
284 return factors;
285 }
286
287
288 /*
289 * Print statistics related to processing. Must be called after
290 * finalizeProcessing.
291 *
292 * Args:
293 * syncState container for synchronization data.
294 */
295 static void printProcessingStatsText(SyncState* const syncState)
296 {
297 unsigned int i;
298
299 printf("Resulting synchronization factors:\n");
300 for (i= 0; i < syncState->traceNb; i++)
301 {
302 Factors* factors= &g_array_index(((ProcessingDataText*)
303 syncState->processingData)->factors, Factors, i);
304
305 printf("\ttrace %u drift= %g offset= %g (%f)\n", i, factors->drift,
306 factors->offset, factors->offset / CPU_FREQ);
307 }
308 }
309
310
311 /*
312 * Read trace number from the test case stream. The trace number should be the
313 * first non-comment line and should be an unsigned int by itself on a line.
314 *
315 * Args:
316 * testCase: test case stream
317 *
318 * Returns:
319 * The trace number
320 */
321 static unsigned int readTraceNb(FILE* testCase)
322 {
323 unsigned int result;
324 int retval;
325 char* line= NULL;
326 size_t len;
327 char tmp;
328
329 skipCommentLines(testCase);
330 retval= getline(&line, &len, testCase);
331 if (retval == -1)
332 {
333 if (feof(testCase))
334 {
335 g_error("Unexpected end of file while looking for number of traces");
336 }
337 else
338 {
339 g_error(strerror(errno));
340 }
341 }
342 if (line[retval - 1] == '\n')
343 {
344 line[retval - 1]= '\0';
345 }
346
347 retval= sscanf(line, " %u %c", &result, &tmp);
348 if (retval == EOF || retval != 1)
349 {
350 g_error("Error parsing test file while looking for number of traces, line was '%s'", line);
351
352 // Not really needed but avoids warning from gcc
353 abort();
354 }
355
356 if (line)
357 {
358 free(line);
359 }
360
361 return result;
362 }
363
364
365 /*
366 * Advance testCase stream over empty space, empty lines and lines that begin
367 * with '#'
368 *
369 * Args:
370 * testCase: test case stream
371 */
372 static void skipCommentLines(FILE* testCase)
373 {
374 int firstChar;
375 ssize_t retval;
376 char* line= NULL;
377 size_t len;
378
379 do
380 {
381 firstChar= fgetc(testCase);
382 if (firstChar == (int) '#')
383 {
384 retval= getline(&line, &len, testCase);
385 if (retval == -1)
386 {
387 if (feof(testCase))
388 {
389 goto outEof;
390 }
391 else
392 {
393 g_error(strerror(errno));
394 }
395 }
396 }
397 else if (firstChar == (int) '\n' || firstChar == (int) ' ')
398 {}
399 else if (firstChar == EOF)
400 {
401 goto outEof;
402 }
403 else
404 {
405 break;
406 }
407 } while (true);
408 retval= ungetc(firstChar, testCase);
409 if (retval == EOF)
410 {
411 g_error("Error: ungetc()");
412 }
413
414 outEof:
415 if (line)
416 {
417 free(line);
418 }
419 }
420
421
422 /*
423 * Write the processing-specific variables in the gnuplot script.
424 *
425 * Args:
426 * syncState: container for synchronization data
427 * i: trace number
428 */
429 static void writeProcessingGraphVariablesText(SyncState* const syncState,
430 const unsigned int i)
431 {
432 fprintf(syncState->graphsStream, "clock_freq_%u= %.3f\n", i, CPU_FREQ);
433 }
434
435
436 /*
437 * Write the processing-specific options in the gnuplot script.
438 *
439 * Args:
440 * syncState: container for synchronization data
441 * i: first trace number
442 * j: second trace number, garanteed to be larger than i
443 */
444 static void writeProcessingTraceTraceOptionsText(SyncState* const syncState,
445 const unsigned int i, const unsigned int j)
446 {
447 fprintf(syncState->graphsStream,
448 "set key inside right bottom\n"
449 "set xlabel \"Clock %1$u\"\n"
450 "set xtics nomirror\n"
451 "set ylabel \"Clock %2$u\"\n"
452 "set ytics nomirror\n"
453 "set x2label \"Clock %1$d (s)\"\n"
454 "set x2range [GPVAL_X_MIN / clock_freq_%1$u : GPVAL_X_MAX / clock_freq_%1$u]\n"
455 "set x2tics\n"
456 "set y2label \"Clock %2$d (s)\"\n"
457 "set y2range [GPVAL_Y_MIN / clock_freq_%2$u : GPVAL_Y_MAX / clock_freq_%2$u]\n"
458 "set y2tics\n", i, j);
459 }
460
461
462 /*
463 * Write the processing-specific options in the gnuplot script.
464 *
465 * Args:
466 * syncState: container for synchronization data
467 * i: first trace number
468 * j: second trace number, garanteed to be larger than i
469 */
470 static void writeProcessingTraceTimeOptionsText(SyncState* const syncState,
471 const unsigned int i, const unsigned int j)
472 {
473 fprintf(syncState->graphsStream,
474 "set key inside right bottom\n"
475 "set xlabel \"Clock %1$u\"\n"
476 "set xtics nomirror\n"
477 "set ylabel \"time (s)\"\n"
478 "set ytics nomirror\n"
479 "set x2label \"Clock %1$d (s)\"\n"
480 "set x2range [GPVAL_X_MIN / clock_freq_%1$u : GPVAL_X_MAX / clock_freq_%1$u]\n"
481 "set x2tics\n", i);
482 }
This page took 0.039682 seconds and 3 git commands to generate.