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,
20 #define NANOSECONDS_PER_SECOND 1000000000
33 #include "sync_chain.h"
35 #include "event_processing_text.h"
38 // Functions common to all processing modules
39 static void initProcessingText(SyncState
* const syncState
, ...);
40 static void destroyProcessingText(SyncState
* const syncState
);
41 static void finalizeProcessingText(SyncState
* const syncState
);
42 static void printProcessingStatsText(SyncState
* const syncState
);
43 static void writeProcessingTraceTimeOptionsText(SyncState
* const syncState
,
44 const unsigned int i
, const unsigned int j
);
45 static void writeProcessingTraceTraceOptionsText(SyncState
* const syncState
,
46 const unsigned int i
, const unsigned int j
);
47 static void writeProcessingGraphVariablesText(SyncState
* const syncState
,
48 const unsigned int i
);
50 // Functions specific to this module
51 static unsigned int readTraceNb(FILE* testCase
);
52 static void skipCommentLines(FILE* testCase
);
55 static ProcessingModule processingModuleText
= {
57 .initProcessing
= &initProcessingText
,
58 .destroyProcessing
= &destroyProcessingText
,
59 .finalizeProcessing
= &finalizeProcessingText
,
60 .printProcessingStats
= &printProcessingStatsText
,
62 .writeVariables
= &writeProcessingGraphVariablesText
,
63 .writeTraceTraceOptions
= &writeProcessingTraceTraceOptionsText
,
64 .writeTraceTimeOptions
= &writeProcessingTraceTimeOptionsText
,
70 * Processing Module registering function
72 void registerProcessingText()
74 g_queue_push_tail(&processingModules
, &processingModuleText
);
79 * Allocate and initialize data structures for synchronizing a traceset.
80 * Open test case file.
83 * syncState: container for synchronization data.
84 * testCaseName: const char*, test case file name
86 static void initProcessingText(SyncState
* const syncState
, ...)
88 ProcessingDataText
* processingData
;
89 const char* testCaseName
;
92 processingData
= malloc(sizeof(ProcessingDataText
));
93 syncState
->processingData
= processingData
;
94 va_start(ap
, syncState
);
95 testCaseName
= va_arg(ap
, const char*);
98 processingData
->testCase
= fopen(testCaseName
, "r");
99 if (processingData
->testCase
== NULL
)
101 g_error(strerror(errno
));
103 syncState
->traceNb
= readTraceNb(processingData
->testCase
);
105 if (syncState
->stats
)
107 processingData
->factors
= NULL
;
112 static void destroyProcessingText(SyncState
* const syncState
)
114 ProcessingDataText
* processingData
= (ProcessingDataText
*)
115 syncState
->processingData
;
117 if (processingData
== NULL
)
122 fclose(processingData
->testCase
);
124 if (syncState
->stats
&& processingData
->factors
)
126 g_array_free(processingData
->factors
, TRUE
);
129 free(syncState
->processingData
);
130 syncState
->processingData
= NULL
;
135 * Read the test case file and make up events. Dispatch those events to the
139 * syncState: container for synchronization data.
141 static void finalizeProcessingText(SyncState
* const syncState
)
146 ProcessingDataText
* processingData
= (ProcessingDataText
*)
147 syncState
->processingData
;
148 FILE* testCase
= processingData
->testCase
;
152 seq
= calloc(syncState
->traceNb
, sizeof(unsigned int));
154 skipCommentLines(testCase
);
155 retval
= getline(&line
, &bufLen
, testCase
);
156 while(!feof(testCase
))
158 unsigned int sender
, receiver
;
159 double sendTime
, recvTime
;
163 if (retval
== -1 && !feof(testCase
))
165 g_error(strerror(errno
));
168 if (line
[retval
- 1] == '\n')
170 line
[retval
- 1]= '\0';
173 retval
= sscanf(line
, " %u %u %lf %lf %c", &sender
, &receiver
,
174 &sendTime
, &recvTime
, &tmp
);
177 g_error(strerror(errno
));
179 else if (retval
!= 4)
181 g_error("Error parsing test file while looking for data point, line was '%s'", line
);
184 if (sender
+ 1 > syncState
->traceNb
)
186 g_error("Error parsing test file, sender is out of range, line was '%s'", line
);
189 if (receiver
+ 1 > syncState
->traceNb
)
191 g_error("Error parsing test file, receiver is out of range, line was '%s'", line
);
196 g_error("Error parsing test file, send time is negative, line was '%s'", line
);
201 g_error("Error parsing test file, receive time is negative, line was '%s'", line
);
204 // Generate ouput and input events
206 unsigned int addressOffset
;
208 unsigned int traceNum
;
210 enum Direction direction
;
212 {sender
, sendTime
, OUT
},
213 {receiver
, recvTime
, IN
},
216 /* addressOffset is added to a traceNum to convert it to an address so
217 * that the address is not plainly the same as the traceNb. */
218 if (syncState
->traceNb
> 1)
220 addressOffset
= pow(10, floor(log(syncState
->traceNb
- 1) /
228 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
232 event
= malloc(sizeof(Event
));
233 event
->traceNum
= loopValues
[i
].traceNum
;
234 event
->wallTime
.seconds
= floor(loopValues
[i
].time
);
235 event
->wallTime
.nanosec
= floor((loopValues
[i
].time
-
236 floor(loopValues
[i
].time
)) * NANOSECONDS_PER_SECOND
);
237 event
->cpuTime
= round(loopValues
[i
].time
* CPU_FREQ
);
239 event
->destroy
= &destroyTCPEvent
;
240 event
->event
.tcpEvent
= malloc(sizeof(TCPEvent
));
241 event
->event
.tcpEvent
->direction
= loopValues
[i
].direction
;
242 event
->event
.tcpEvent
->segmentKey
= malloc(sizeof(SegmentKey
));
243 event
->event
.tcpEvent
->segmentKey
->ihl
= 5;
244 event
->event
.tcpEvent
->segmentKey
->tot_len
= 40;
245 event
->event
.tcpEvent
->segmentKey
->connectionKey
.saddr
= sender
+
247 event
->event
.tcpEvent
->segmentKey
->connectionKey
.daddr
= receiver
+
249 event
->event
.tcpEvent
->segmentKey
->connectionKey
.source
= 57645;
250 event
->event
.tcpEvent
->segmentKey
->connectionKey
.dest
= 80;
251 event
->event
.tcpEvent
->segmentKey
->seq
= seq
[sender
];
252 event
->event
.tcpEvent
->segmentKey
->ack_seq
= 0;
253 event
->event
.tcpEvent
->segmentKey
->doff
= 5;
254 event
->event
.tcpEvent
->segmentKey
->ack
= 0;
255 event
->event
.tcpEvent
->segmentKey
->rst
= 0;
256 event
->event
.tcpEvent
->segmentKey
->syn
= 1;
257 event
->event
.tcpEvent
->segmentKey
->fin
= 0;
259 syncState
->matchingModule
->matchEvent(syncState
, event
);
265 skipCommentLines(testCase
);
266 retval
= getline(&line
, &bufLen
, testCase
);
276 factors
= syncState
->matchingModule
->finalizeMatching(syncState
);
277 if (syncState
->stats
)
279 processingData
->factors
= factors
;
283 g_array_free(factors
, TRUE
);
289 * Print statistics related to processing. Must be called after
290 * finalizeProcessing.
293 * syncState container for synchronization data.
295 static void printProcessingStatsText(SyncState
* const syncState
)
299 printf("Resulting synchronization factors:\n");
300 for (i
= 0; i
< syncState
->traceNb
; i
++)
302 Factors
* factors
= &g_array_index(((ProcessingDataText
*)
303 syncState
->processingData
)->factors
, Factors
, i
);
305 printf("\ttrace %u drift= %g offset= %g (%f)\n", i
, factors
->drift
,
306 factors
->offset
, factors
->offset
/ CPU_FREQ
);
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.
316 * testCase: test case stream
321 static unsigned int readTraceNb(FILE* testCase
)
329 skipCommentLines(testCase
);
330 retval
= getline(&line
, &len
, testCase
);
335 g_error("Unexpected end of file while looking for number of traces");
339 g_error(strerror(errno
));
342 if (line
[retval
- 1] == '\n')
344 line
[retval
- 1]= '\0';
347 retval
= sscanf(line
, " %u %c", &result
, &tmp
);
348 if (retval
== EOF
|| retval
!= 1)
350 g_error("Error parsing test file while looking for number of traces, line was '%s'", line
);
352 // Not really needed but avoids warning from gcc
366 * Advance testCase stream over empty space, empty lines and lines that begin
370 * testCase: test case stream
372 static void skipCommentLines(FILE* testCase
)
381 firstChar
= fgetc(testCase
);
382 if (firstChar
== (int) '#')
384 retval
= getline(&line
, &len
, testCase
);
393 g_error(strerror(errno
));
397 else if (firstChar
== (int) '\n' || firstChar
== (int) ' ')
399 else if (firstChar
== EOF
)
408 retval
= ungetc(firstChar
, testCase
);
411 g_error("Error: ungetc()");
423 * Write the processing-specific variables in the gnuplot script.
426 * syncState: container for synchronization data
429 static void writeProcessingGraphVariablesText(SyncState
* const syncState
,
430 const unsigned int i
)
432 fprintf(syncState
->graphsStream
, "clock_freq_%u= %.3f\n", i
, CPU_FREQ
);
437 * Write the processing-specific options in the gnuplot script.
440 * syncState: container for synchronization data
441 * i: first trace number
442 * j: second trace number, garanteed to be larger than i
444 static void writeProcessingTraceTraceOptionsText(SyncState
* const syncState
,
445 const unsigned int i
, const unsigned int j
)
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"
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
);
463 * Write the processing-specific options in the gnuplot script.
466 * syncState: container for synchronization data
467 * i: first trace number
468 * j: second trace number, garanteed to be larger than i
470 static void writeProcessingTraceTimeOptionsText(SyncState
* const syncState
,
471 const unsigned int i
, const unsigned int j
)
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"