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 _ISOC99_SOURCE
25 #include <linux/if_ether.h>
27 #include <netinet/in.h>
31 #include "sync_chain.h"
32 #include "event_processing_lttv_common.h"
34 #include "event_processing_lttv_standard.h"
38 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
42 // Functions common to all processing modules
43 static void initProcessingLTTVStandard(SyncState
* const syncState
,
44 LttvTracesetContext
* const traceSetContext
);
45 static void destroyProcessingLTTVStandard(SyncState
* const syncState
);
47 static void finalizeProcessingLTTVStandard(SyncState
* const syncState
);
48 static void printProcessingStatsLTTVStandard(SyncState
* const syncState
);
49 static void writeProcessingGraphsPlotsLTTVStandard(FILE* stream
, SyncState
*
50 const syncState
, const unsigned int i
, const unsigned int j
);
51 static void writeProcessingGraphsOptionsLTTVStandard(FILE* stream
, SyncState
*
52 const syncState
, const unsigned int i
, const unsigned int j
);
54 // Functions specific to this module
55 static void registerProcessingLTTVStandard() __attribute__((constructor (102)));
56 static gboolean
processEventLTTVStandard(void* hookData
, void* callData
);
57 static void partialDestroyProcessingLTTVStandard(SyncState
* const syncState
);
60 static ProcessingModule processingModuleLTTVStandard
= {
61 .name
= "LTTV-standard",
62 .initProcessing
= &initProcessingLTTVStandard
,
63 .destroyProcessing
= &destroyProcessingLTTVStandard
,
64 .finalizeProcessing
= &finalizeProcessingLTTVStandard
,
65 .printProcessingStats
= &printProcessingStatsLTTVStandard
,
66 .writeProcessingGraphsPlots
= &writeProcessingGraphsPlotsLTTVStandard
,
67 .writeProcessingGraphsOptions
= &writeProcessingGraphsOptionsLTTVStandard
,
73 * Processing Module registering function
75 static void registerProcessingLTTVStandard()
77 g_queue_push_tail(&processingModules
, &processingModuleLTTVStandard
);
84 * Allocate and initialize data structures for synchronizing a traceset.
85 * Register event hooks.
88 * syncState: container for synchronization data.
89 * This function allocates these processingData members:
94 * traceSetContext: set of LTTV traces
96 static void initProcessingLTTVStandard(SyncState
* const syncState
, LttvTracesetContext
*
97 const traceSetContext
)
100 ProcessingDataLTTVStandard
* processingData
;
102 processingData
= malloc(sizeof(ProcessingDataLTTVStandard
));
103 syncState
->processingData
= processingData
;
104 processingData
->traceSetContext
= traceSetContext
;
106 if (syncState
->stats
)
108 processingData
->stats
= calloc(1, sizeof(ProcessingStatsLTTVStandard
));
112 processingData
->stats
= NULL
;
115 processingData
->traceNumTable
= g_hash_table_new(&g_direct_hash
, NULL
);
116 processingData
->hookListList
= g_array_sized_new(FALSE
, FALSE
,
117 sizeof(GArray
*), syncState
->traceNb
);
118 processingData
->pendingRecv
= malloc(sizeof(GHashTable
*) *
121 for(i
= 0; i
< syncState
->traceNb
; i
++)
123 g_hash_table_insert(processingData
->traceNumTable
,
124 processingData
->traceSetContext
->traces
[i
]->t
, (gpointer
) i
);
127 for(i
= 0; i
< syncState
->traceNb
; i
++)
129 processingData
->pendingRecv
[i
]= g_hash_table_new_full(&g_direct_hash
,
130 NULL
, NULL
, &gdnDestroyNetEvent
);
133 registerHooks(processingData
->hookListList
, traceSetContext
,
134 &processEventLTTVStandard
, syncState
);
139 * Call the partial processing destroyer, obtain and adjust the factors from
143 * syncState container for synchronization data.
145 static void finalizeProcessingLTTVStandard(SyncState
* const syncState
)
149 double minOffset
, minDrift
;
150 unsigned int refFreqTrace
;
151 ProcessingDataLTTVStandard
* processingData
;
153 processingData
= (ProcessingDataLTTVStandard
*) syncState
->processingData
;
155 partialDestroyProcessingLTTVStandard(syncState
);
157 factors
= syncState
->matchingModule
->finalizeMatching(syncState
);
159 /* The offsets are adjusted so the lowest one is 0. This is done because
160 * of a Lttv specific limitation: events cannot have negative times. By
161 * having non-negative offsets, events cannot be moved backwards to
165 for (i
= 0; i
< syncState
->traceNb
; i
++)
167 minOffset
= MIN(g_array_index(factors
, Factors
, i
).offset
, minOffset
);
170 for (i
= 0; i
< syncState
->traceNb
; i
++)
172 g_array_index(factors
, Factors
, i
).offset
-= minOffset
;
175 /* Because the timestamps are corrected at the TSC level (not at the
176 * LttTime level) all trace frequencies must be made equal. We choose to
177 * use the frequency of the system with the lowest drift
181 for (i
= 0; i
< syncState
->traceNb
; i
++)
183 if (g_array_index(factors
, Factors
, i
).drift
< minDrift
)
185 minDrift
= g_array_index(factors
, Factors
, i
).drift
;
189 g_assert(syncState
->traceNb
== 0 || minDrift
!= INFINITY
);
191 // Write the factors to the LttTrace structures
192 for (i
= 0; i
< syncState
->traceNb
; i
++)
195 Factors
* traceFactors
;
197 t
= processingData
->traceSetContext
->traces
[i
]->t
;
198 traceFactors
= &g_array_index(factors
, Factors
, i
);
200 t
->drift
= traceFactors
->drift
;
201 t
->offset
= traceFactors
->offset
;
203 processingData
->traceSetContext
->traces
[refFreqTrace
]->t
->start_freq
;
205 processingData
->traceSetContext
->traces
[refFreqTrace
]->t
->freq_scale
;
206 t
->start_time_from_tsc
=
207 ltt_time_from_uint64(tsc_to_uint64(t
->freq_scale
, t
->start_freq
,
208 t
->drift
* t
->start_tsc
+ t
->offset
));
211 g_array_free(factors
, TRUE
);
213 lttv_traceset_context_compute_time_span(processingData
->traceSetContext
,
214 &processingData
->traceSetContext
->time_span
);
216 g_debug("traceset start %ld.%09ld end %ld.%09ld\n",
217 processingData
->traceSetContext
->time_span
.start_time
.tv_sec
,
218 processingData
->traceSetContext
->time_span
.start_time
.tv_nsec
,
219 processingData
->traceSetContext
->time_span
.end_time
.tv_sec
,
220 processingData
->traceSetContext
->time_span
.end_time
.tv_nsec
);
227 * Print statistics related to processing and downstream modules. Must be
228 * called after finalizeProcessing.
231 * syncState container for synchronization data.
233 static void printProcessingStatsLTTVStandard(SyncState
* const syncState
)
236 ProcessingDataLTTVStandard
* processingData
;
238 if (!syncState
->stats
)
243 processingData
= (ProcessingDataLTTVStandard
*) syncState
->processingData
;
245 printf("LTTV processing stats:\n");
246 printf("\treceived frames: %d\n", processingData
->stats
->totRecv
);
247 printf("\treceived frames that are IP: %d\n",
248 processingData
->stats
->totRecvIp
);
249 printf("\treceived and processed packets that are TCP: %d\n",
250 processingData
->stats
->totInE
);
251 printf("\tsent packets that are TCP: %d\n",
252 processingData
->stats
->totOutE
);
254 if (syncState
->matchingModule
->printMatchingStats
!= NULL
)
256 syncState
->matchingModule
->printMatchingStats(syncState
);
259 printf("Resulting synchronization factors:\n");
260 for (i
= 0; i
< syncState
->traceNb
; i
++)
264 t
= processingData
->traceSetContext
->traces
[i
]->t
;
266 printf("\ttrace %u drift= %g offset= %g (%f) start time= %ld.%09ld\n",
267 i
, t
->drift
, t
->offset
, (double) tsc_to_uint64(t
->freq_scale
,
268 t
->start_freq
, t
->offset
) / NANOSECONDS_PER_SECOND
,
269 t
->start_time_from_tsc
.tv_sec
, t
->start_time_from_tsc
.tv_nsec
);
275 * Unregister event hooks. Deallocate processingData.
278 * syncState: container for synchronization data.
279 * This function deallocates these processingData members:
282 static void destroyProcessingLTTVStandard(SyncState
* const syncState
)
284 ProcessingDataLTTVStandard
* processingData
;
286 processingData
= (ProcessingDataLTTVStandard
*) syncState
->processingData
;
288 if (processingData
== NULL
)
293 partialDestroyProcessingLTTVStandard(syncState
);
295 if (syncState
->stats
)
297 free(processingData
->stats
);
300 free(syncState
->processingData
);
301 syncState
->processingData
= NULL
;
306 * Unregister event hooks. Deallocate some of processingData.
308 * This function can be called right after the events have been processed to
309 * free some data structures that are not needed for finalization.
312 * syncState: container for synchronization data.
313 * This function deallocates these members:
318 static void partialDestroyProcessingLTTVStandard(SyncState
* const syncState
)
321 ProcessingDataLTTVStandard
* processingData
;
323 processingData
= (ProcessingDataLTTVStandard
*) syncState
->processingData
;
325 if (processingData
== NULL
|| processingData
->traceNumTable
== NULL
)
330 g_hash_table_destroy(processingData
->traceNumTable
);
331 processingData
->traceNumTable
= NULL
;
333 for(i
= 0; i
< syncState
->traceNb
; i
++)
336 g_debug("Cleaning up pendingRecv list\n");
337 g_hash_table_destroy(processingData
->pendingRecv
[i
]);
339 free(processingData
->pendingRecv
);
341 unregisterHooks(processingData
->hookListList
,
342 processingData
->traceSetContext
);
347 * Lttv hook function that will be called for network events
350 * hookData: LttvTraceHook* for the type of event that generated the call
351 * callData: LttvTracefileContext* at the moment of the event
354 * FALSE Always returns FALSE, meaning to keep processing hooks for
357 static gboolean
processEventLTTVStandard(void* hookData
, void* callData
)
359 LttvTraceHook
* traceHook
;
360 LttvTracefileContext
* tfc
;
365 unsigned long traceNum
;
366 struct marker_info
* info
;
367 SyncState
* syncState
;
368 ProcessingDataLTTVStandard
* processingData
;
370 traceHook
= (LttvTraceHook
*) hookData
;
371 tfc
= (LttvTracefileContext
*) callData
;
372 syncState
= (SyncState
*) traceHook
->hook_data
;
373 processingData
= (ProcessingDataLTTVStandard
*) syncState
->processingData
;
374 event
= ltt_tracefile_get_event(tfc
->tf
);
375 time
= ltt_event_time(event
);
376 tsc
= ltt_event_cycle_count(event
);
377 trace
= tfc
->t_context
->t
;
378 info
= marker_get_info_from_id(tfc
->tf
->mdata
, event
->event_id
);
380 g_assert(g_hash_table_lookup_extended(processingData
->traceNumTable
,
381 trace
, NULL
, (gpointer
*) &traceNum
));
383 g_debug("XXXX process event: time: %ld.%09ld trace: %ld (%p) name: %s ",
384 (long) time
.tv_sec
, time
.tv_nsec
, traceNum
, trace
,
385 g_quark_to_string(info
->name
));
387 if (info
->name
== LTT_EVENT_DEV_XMIT
)
391 if (!ltt_event_get_unsigned(event
,
392 lttv_trace_get_hook_field(traceHook
, 1)) == ETH_P_IP
||
393 !ltt_event_get_unsigned(event
,
394 lttv_trace_get_hook_field(traceHook
, 2)) == IPPROTO_TCP
)
399 if (syncState
->stats
)
401 processingData
->stats
->totOutE
++;
404 outE
= malloc(sizeof(NetEvent
));
405 outE
->packetKey
= malloc(sizeof(PacketKey
));
407 outE
->traceNum
= traceNum
;
410 outE
->packetKey
->connectionKey
.saddr
= ltt_event_get_unsigned(event
,
411 lttv_trace_get_hook_field(traceHook
, 3));
412 outE
->packetKey
->connectionKey
.daddr
= ltt_event_get_unsigned(event
,
413 lttv_trace_get_hook_field(traceHook
, 4));
414 outE
->packetKey
->tot_len
= ltt_event_get_unsigned(event
,
415 lttv_trace_get_hook_field(traceHook
, 5));
416 outE
->packetKey
->ihl
= ltt_event_get_unsigned(event
,
417 lttv_trace_get_hook_field(traceHook
, 6));
418 outE
->packetKey
->connectionKey
.source
= ltt_event_get_unsigned(event
,
419 lttv_trace_get_hook_field(traceHook
, 7));
420 outE
->packetKey
->connectionKey
.dest
= ltt_event_get_unsigned(event
,
421 lttv_trace_get_hook_field(traceHook
, 8));
422 outE
->packetKey
->seq
= ltt_event_get_unsigned(event
,
423 lttv_trace_get_hook_field(traceHook
, 9));
424 outE
->packetKey
->ack_seq
= ltt_event_get_unsigned(event
,
425 lttv_trace_get_hook_field(traceHook
, 10));
426 outE
->packetKey
->doff
= ltt_event_get_unsigned(event
,
427 lttv_trace_get_hook_field(traceHook
, 11));
428 outE
->packetKey
->ack
= ltt_event_get_unsigned(event
,
429 lttv_trace_get_hook_field(traceHook
, 12));
430 outE
->packetKey
->rst
= ltt_event_get_unsigned(event
,
431 lttv_trace_get_hook_field(traceHook
, 13));
432 outE
->packetKey
->syn
= ltt_event_get_unsigned(event
,
433 lttv_trace_get_hook_field(traceHook
, 14));
434 outE
->packetKey
->fin
= ltt_event_get_unsigned(event
,
435 lttv_trace_get_hook_field(traceHook
, 15));
437 syncState
->matchingModule
->matchEvent(syncState
, outE
, OUT
);
439 g_debug("Output event done\n");
441 else if (info
->name
== LTT_EVENT_DEV_RECEIVE
)
445 if (syncState
->stats
)
447 processingData
->stats
->totRecv
++;
450 protocol
= ltt_event_get_unsigned(event
,
451 lttv_trace_get_hook_field(traceHook
, 1));
453 if (protocol
== ETH_P_IP
)
457 if (syncState
->stats
)
459 processingData
->stats
->totRecvIp
++;
462 inE
= malloc(sizeof(NetEvent
));
464 inE
->traceNum
= traceNum
;
466 inE
->skb
= (void*) (long) ltt_event_get_long_unsigned(event
,
467 lttv_trace_get_hook_field(traceHook
, 0));
468 inE
->packetKey
= NULL
;
470 g_hash_table_replace(processingData
->pendingRecv
[traceNum
],
473 g_debug("Adding inE %p for skb %p to pendingRecv\n", inE
, inE
->skb
);
480 else if (info
->name
== LTT_EVENT_TCPV4_RCV
)
485 // Search pendingRecv for an event with the same skb
486 skb
= (void*) (long) ltt_event_get_long_unsigned(event
,
487 lttv_trace_get_hook_field(traceHook
, 0));
490 g_hash_table_lookup(processingData
->pendingRecv
[traceNum
], skb
);
493 // This should only happen in case of lost events
494 g_debug("No matching pending receive event found\n");
498 if (syncState
->stats
)
500 processingData
->stats
->totInE
++;
503 // If it's there, remove it and proceed with a receive event
504 g_hash_table_steal(processingData
->pendingRecv
[traceNum
], skb
);
506 inE
->packetKey
= malloc(sizeof(PacketKey
));
508 inE
->packetKey
->connectionKey
.saddr
= ltt_event_get_unsigned(event
,
509 lttv_trace_get_hook_field(traceHook
, 1));
510 inE
->packetKey
->connectionKey
.daddr
= ltt_event_get_unsigned(event
,
511 lttv_trace_get_hook_field(traceHook
, 2));
512 inE
->packetKey
->tot_len
= ltt_event_get_unsigned(event
,
513 lttv_trace_get_hook_field(traceHook
, 3));
514 inE
->packetKey
->ihl
= ltt_event_get_unsigned(event
,
515 lttv_trace_get_hook_field(traceHook
, 4));
516 inE
->packetKey
->connectionKey
.source
= ltt_event_get_unsigned(event
,
517 lttv_trace_get_hook_field(traceHook
, 5));
518 inE
->packetKey
->connectionKey
.dest
= ltt_event_get_unsigned(event
,
519 lttv_trace_get_hook_field(traceHook
, 6));
520 inE
->packetKey
->seq
= ltt_event_get_unsigned(event
,
521 lttv_trace_get_hook_field(traceHook
, 7));
522 inE
->packetKey
->ack_seq
= ltt_event_get_unsigned(event
,
523 lttv_trace_get_hook_field(traceHook
, 8));
524 inE
->packetKey
->doff
= ltt_event_get_unsigned(event
,
525 lttv_trace_get_hook_field(traceHook
, 9));
526 inE
->packetKey
->ack
= ltt_event_get_unsigned(event
,
527 lttv_trace_get_hook_field(traceHook
, 10));
528 inE
->packetKey
->rst
= ltt_event_get_unsigned(event
,
529 lttv_trace_get_hook_field(traceHook
, 11));
530 inE
->packetKey
->syn
= ltt_event_get_unsigned(event
,
531 lttv_trace_get_hook_field(traceHook
, 12));
532 inE
->packetKey
->fin
= ltt_event_get_unsigned(event
,
533 lttv_trace_get_hook_field(traceHook
, 13));
535 syncState
->matchingModule
->matchEvent(syncState
, inE
, IN
);
537 g_debug("Input event %p for skb %p done\n", inE
, skb
);
540 else if (info
->name
== LTT_EVENT_NETWORK_IPV4_INTERFACE
)
545 char addressString
[17];
547 address
= ltt_event_get_long_unsigned(event
,
548 lttv_trace_get_hook_field(traceHook
, 1));
549 up
= ltt_event_get_long_int(event
, lttv_trace_get_hook_field(traceHook
,
551 /* name must be the last field to get or else copy the string, see the
552 * doc for ltt_event_get_string()
554 name
= ltt_event_get_string(event
, lttv_trace_get_hook_field(traceHook
,
557 convertIP(addressString
, address
);
559 g_debug("name \"%s\" address %s up %lld\n", name
, addressString
, up
);
563 g_assert_not_reached();
571 * Write the processing-specific graph lines in the gnuplot script (none at
572 * the moment). Call the downstream module's graph function.
575 * stream: stream where to write the data
576 * syncState: container for synchronization data
577 * i: first trace number
578 * j: second trace number, garanteed to be larger than i
580 static void writeProcessingGraphsPlotsLTTVStandard(FILE* stream
, SyncState
*
581 const syncState
, const unsigned int i
, const unsigned int j
)
583 if (syncState
->matchingModule
->writeMatchingGraphsPlots
!= NULL
)
585 syncState
->matchingModule
->writeMatchingGraphsPlots(stream
, syncState
,
592 * Write the processing-specific options in the gnuplot script. Call the
593 * downstream module's options function.
596 * stream: stream where to write the data
597 * syncState: container for synchronization data
598 * i: first trace number
599 * j: second trace number, garanteed to be larger than i
601 static void writeProcessingGraphsOptionsLTTVStandard(FILE* stream
, SyncState
*
602 const syncState
, const unsigned int i
, const unsigned int j
)
604 ProcessingDataLTTVStandard
* processingData
;
605 LttTrace
* traceI
, * traceJ
;
607 processingData
= (ProcessingDataLTTVStandard
*) syncState
->processingData
;
609 traceI
= processingData
->traceSetContext
->traces
[i
]->t
;
610 traceJ
= processingData
->traceSetContext
->traces
[j
]->t
;
613 "set x2label \"Clock %1$d (s)\"\n"
614 "set x2range [GPVAL_X_MIN / %2$.1f : GPVAL_X_MAX / %2$.1f]\n"
616 "set y2label \"Clock %3$d (s)\"\n"
617 "set y2range [GPVAL_Y_MIN / %4$.1f : GPVAL_Y_MAX / %4$.1f]\n"
618 "set y2tics\n", i
, (double) traceI
->start_freq
/ traceI
->freq_scale
,
619 j
, (double) traceJ
->start_freq
/ traceJ
->freq_scale
);
621 if (syncState
->matchingModule
->writeMatchingGraphsOptions
!= NULL
)
623 syncState
->matchingModule
->writeMatchingGraphsOptions(stream
,