1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2009 Benjamin Poirier <benjamin.poirier@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 #define NANOSECONDS_PER_SECOND 1000000000
32 #include "sync_chain.h"
34 #include "event_processing_text.h"
37 // Functions common to all processing modules
38 static void initProcessingText(SyncState
* const syncState
, ...);
39 static void destroyProcessingText(SyncState
* const syncState
);
40 static void 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
);
49 // Functions specific to this module
50 static void registerProcessingText() __attribute__((constructor (102)));
52 static unsigned int readTraceNb(FILE* testCase
);
53 static void skipCommentLines(FILE* testCase
);
56 static ProcessingModule processingModuleText
= {
58 .initProcessing
= &initProcessingText
,
59 .destroyProcessing
= &destroyProcessingText
,
60 .finalizeProcessing
= &finalizeProcessingText
,
61 .printProcessingStats
= &printProcessingStatsText
,
63 .writeVariables
= &writeProcessingGraphVariablesText
,
64 .writeTraceTraceOptions
= &writeProcessingTraceTraceOptionsText
,
65 .writeTraceTimeOptions
= &writeProcessingTraceTimeOptionsText
,
71 * Processing Module registering function
73 static void registerProcessingText()
75 g_queue_push_tail(&processingModules
, &processingModuleText
);
80 * Allocate and initialize data structures for synchronizing a traceset.
81 * Open test case file.
84 * syncState: container for synchronization data.
85 * testCaseName: const char*, test case file name
87 static void initProcessingText(SyncState
* const syncState
, ...)
89 ProcessingDataText
* processingData
;
90 const char* testCaseName
;
93 processingData
= malloc(sizeof(ProcessingDataText
));
94 syncState
->processingData
= processingData
;
95 va_start(ap
, syncState
);
96 testCaseName
= va_arg(ap
, const char*);
99 processingData
->testCase
= fopen(testCaseName
, "r");
100 if (processingData
->testCase
== NULL
)
102 g_error(strerror(errno
));
104 syncState
->traceNb
= readTraceNb(processingData
->testCase
);
106 if (syncState
->stats
)
108 processingData
->factors
= NULL
;
113 static void destroyProcessingText(SyncState
* const syncState
)
115 ProcessingDataText
* processingData
= (ProcessingDataText
*)
116 syncState
->processingData
;
118 if (processingData
== NULL
)
123 if (syncState
->stats
&& processingData
->factors
)
125 g_array_free(processingData
->factors
, TRUE
);
128 free(syncState
->processingData
);
129 syncState
->processingData
= NULL
;
134 * Read the test case file and make up events. Dispatch those events to the
138 * syncState: container for synchronization data.
140 static void finalizeProcessingText(SyncState
* const syncState
)
146 ProcessingDataText
* processingData
= (ProcessingDataText
*)
147 syncState
->processingData
;
148 FILE* testCase
= processingData
->testCase
;
151 seq
= calloc(syncState
->traceNb
, sizeof(unsigned int));
153 skipCommentLines(testCase
);
154 retval
= getline(&line
, &len
, testCase
);
155 while(!feof(testCase
))
157 unsigned int sender
, receiver
;
158 double sendTime
, recvTime
;
162 if (retval
== -1 && !feof(testCase
))
164 g_error(strerror(errno
));
167 if (line
[len
- 1] == '\n')
172 retval
= sscanf(line
, " %u %u %lf %lf %c", &sender
, &receiver
,
173 &sendTime
, &recvTime
, &tmp
);
176 g_error(strerror(errno
));
178 else if (retval
!= 4)
180 g_error("Error parsing test file while looking for data point, line was '%s'", line
);
183 if (sender
+ 1 > syncState
->traceNb
)
185 g_error("Error parsing test file, sender is out of range, line was '%s'", line
);
188 if (receiver
+ 1 > syncState
->traceNb
)
190 g_error("Error parsing test file, receiver is out of range, line was '%s'", line
);
195 g_error("Error parsing test file, send time is negative, line was '%s'", line
);
200 g_error("Error parsing test file, receive time is negative, line was '%s'", line
);
203 // Generate ouput and input events
205 unsigned int addressOffset
;
207 unsigned int traceNum
;
209 enum Direction direction
;
211 {sender
, sendTime
, OUT
},
212 {receiver
, recvTime
, IN
},
215 /* addressOffset is added to a traceNum to convert it to an address so
216 * that the address is not plainly the same as the traceNb. */
217 if (syncState
->traceNb
> 1)
219 addressOffset
= pow(10, floor(log(syncState
->traceNb
- 1) /
227 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
231 event
= malloc(sizeof(Event
));
232 event
->traceNum
= loopValues
[i
].traceNum
;
233 event
->wallTime
.seconds
= floor(loopValues
[i
].time
);
234 event
->wallTime
.nanosec
= floor((loopValues
[i
].time
-
235 floor(loopValues
[i
].time
)) * NANOSECONDS_PER_SECOND
);
236 event
->cpuTime
= round(loopValues
[i
].time
* CPU_FREQ
);
238 event
->destroy
= &destroyTCPEvent
;
239 event
->event
.tcpEvent
= malloc(sizeof(TCPEvent
));
240 event
->event
.tcpEvent
->direction
= loopValues
[i
].direction
;
241 event
->event
.tcpEvent
->segmentKey
= malloc(sizeof(SegmentKey
));
242 event
->event
.tcpEvent
->segmentKey
->ihl
= 5;
243 event
->event
.tcpEvent
->segmentKey
->tot_len
= 40;
244 event
->event
.tcpEvent
->segmentKey
->connectionKey
.saddr
= sender
+
246 event
->event
.tcpEvent
->segmentKey
->connectionKey
.daddr
= receiver
+
248 event
->event
.tcpEvent
->segmentKey
->connectionKey
.source
= 57645;
249 event
->event
.tcpEvent
->segmentKey
->connectionKey
.dest
= 80;
250 event
->event
.tcpEvent
->segmentKey
->seq
= seq
[sender
];
251 event
->event
.tcpEvent
->segmentKey
->ack_seq
= 0;
252 event
->event
.tcpEvent
->segmentKey
->doff
= 5;
253 event
->event
.tcpEvent
->segmentKey
->ack
= 0;
254 event
->event
.tcpEvent
->segmentKey
->rst
= 0;
255 event
->event
.tcpEvent
->segmentKey
->syn
= 1;
256 event
->event
.tcpEvent
->segmentKey
->fin
= 0;
258 syncState
->matchingModule
->matchEvent(syncState
, event
);
264 skipCommentLines(testCase
);
265 retval
= getline(&line
, &len
, testCase
);
275 factors
= syncState
->matchingModule
->finalizeMatching(syncState
);
276 if (syncState
->stats
)
278 processingData
->factors
= factors
;
282 g_array_free(factors
, TRUE
);
288 * Print statistics related to processing. Must be called after
289 * finalizeProcessing.
292 * syncState container for synchronization data.
294 static void printProcessingStatsText(SyncState
* const syncState
)
298 printf("Resulting synchronization factors:\n");
299 for (i
= 0; i
< syncState
->traceNb
; i
++)
301 Factors
* factors
= &g_array_index(((ProcessingDataText
*)
302 syncState
->processingData
)->factors
, Factors
, i
);
304 printf("\ttrace %u drift= %g offset= %g (%f)\n", i
, factors
->drift
,
305 factors
->offset
, factors
->offset
/ CPU_FREQ
);
311 * Read trace number from the test case stream. The trace number should be the
312 * first non-comment line and should be an unsigned int by itself on a line.
315 * testCase: test case stream
320 static unsigned int readTraceNb(FILE* testCase
)
328 skipCommentLines(testCase
);
329 retval
= getline(&line
, &len
, testCase
);
334 g_error("Unexpected end of file while looking for number of traces");
338 g_error(strerror(errno
));
341 if (line
[retval
- 1] == '\n')
343 line
[retval
- 1]= '\0';
346 retval
= sscanf(line
, " %u %c", &result
, &tmp
);
347 if (retval
== EOF
|| retval
!= 1)
349 g_error("Error parsing test file while looking for number of traces, line was '%s'", line
);
351 // Not really needed but avoids warning from gcc
360 * Advance testCase stream over empty space, empty lines and lines that begin
364 * testCase: test case stream
366 static void skipCommentLines(FILE* testCase
)
375 firstChar
= fgetc(testCase
);
376 if (firstChar
== (int) '#')
378 retval
= getline(&line
, &len
, testCase
);
387 g_error(strerror(errno
));
391 else if (firstChar
== (int) '\n' || firstChar
== (int) ' ')
393 else if (firstChar
== EOF
)
402 retval
= ungetc(firstChar
, testCase
);
405 g_error("Error: ungetc()");
417 * Write the processing-specific variables in the gnuplot script.
420 * syncState: container for synchronization data
423 static void writeProcessingGraphVariablesText(SyncState
* const syncState
,
424 const unsigned int i
)
426 fprintf(syncState
->graphsStream
, "clock_freq_%u= %.3f\n", i
, CPU_FREQ
);
431 * Write the processing-specific options in the gnuplot script.
434 * syncState: container for synchronization data
435 * i: first trace number
436 * j: second trace number, garanteed to be larger than i
438 static void writeProcessingTraceTraceOptionsText(SyncState
* const syncState
,
439 const unsigned int i
, const unsigned int j
)
441 fprintf(syncState
->graphsStream
,
442 "set key inside right bottom\n"
443 "set xlabel \"Clock %1$u\"\n"
444 "set xtics nomirror\n"
445 "set ylabel \"Clock %2$u\"\n"
446 "set ytics nomirror\n"
447 "set x2label \"Clock %1$d (s)\"\n"
448 "set x2range [GPVAL_X_MIN / clock_freq_%1$u : GPVAL_X_MAX / clock_freq_%1$u]\n"
450 "set y2label \"Clock %2$d (s)\"\n"
451 "set y2range [GPVAL_Y_MIN / clock_freq_%2$u : GPVAL_Y_MAX / clock_freq_%2$u]\n"
452 "set y2tics\n", i
, j
);
457 * Write the processing-specific options in the gnuplot script.
460 * syncState: container for synchronization data
461 * i: first trace number
462 * j: second trace number, garanteed to be larger than i
464 static void writeProcessingTraceTimeOptionsText(SyncState
* const syncState
,
465 const unsigned int i
, const unsigned int j
)
467 fprintf(syncState
->graphsStream
,
468 "set key inside right bottom\n"
469 "set xlabel \"Clock %1$u\"\n"
470 "set xtics nomirror\n"
471 "set ylabel \"time (s)\"\n"
472 "set ytics nomirror\n"
473 "set x2label \"Clock %1$d (s)\"\n"
474 "set x2range [GPVAL_X_MIN / clock_freq_%1$u : GPVAL_X_MAX / clock_freq_%1$u]\n"