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,
27 #include <sys/resource.h>
29 #include <sys/types.h>
33 #include <lttv/module.h>
34 #include <lttv/option.h>
36 #include "sync_chain.h"
40 static void destroy();
42 static void gfAppendAnalysisName(gpointer data
, gpointer user_data
);
43 static void gfAddModuleOption(gpointer data
, gpointer user_data
);
44 static void gfRemoveModuleOption(gpointer data
, gpointer user_data
);
46 static char* argHelpNone
= "none";
47 static ModuleOption optionSync
= {
51 .optionHelp
= "synchronize the time between the traces",
53 static char graphsDir
[20];
54 static ModuleOption optionSyncStats
= {
55 .longName
= "sync-stats",
58 .optionHelp
= "print statistics about the time synchronization",
60 static ModuleOption optionSyncNull
= {
61 .longName
= "sync-null",
64 .optionHelp
= "read the events but do not perform any processing",
66 static GString
* analysisModulesNames
;
67 static ModuleOption optionSyncAnalysis
= {
68 .longName
= "sync-analysis",
69 .hasArg
= REQUIRED_ARG
,
70 .optionHelp
= "specify the algorithm to use for event analysis",
72 static ModuleOption optionSyncGraphs
= {
73 .longName
= "sync-graphs",
76 .optionHelp
= "output gnuplot graph showing synchronization points",
78 static ModuleOption optionSyncGraphsDir
= {
79 .longName
= "sync-graphs-dir",
80 .hasArg
= REQUIRED_ARG
,
81 .optionHelp
= "specify the directory where to store the graphs",
85 * Module init function
87 * This function is declared to be the module initialization function. Event
88 * modules are registered with a "constructor (102)" attribute except one in
89 * each class (processing, matching, analysis) which is chosen to be the
90 * default and which is registered with a "constructor (101)" attribute.
91 * Constructors with no priority are called after constructors with
92 * priorities. The result is that the list of event modules is known when this
93 * function is executed.
101 g_assert(g_queue_get_length(&analysisModules
) > 0);
102 optionSyncAnalysis
.arg
= ((AnalysisModule
*)
103 g_queue_peek_head(&analysisModules
))->name
;
104 analysisModulesNames
= g_string_new("");
105 g_queue_foreach(&analysisModules
, &gfAppendAnalysisName
,
106 analysisModulesNames
);
107 // remove the last ", "
108 g_string_truncate(analysisModulesNames
, analysisModulesNames
->len
- 2);
109 optionSyncAnalysis
.argHelp
= analysisModulesNames
->str
;
111 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "graphs-%d", getpid());
112 if (retval
> sizeof(graphsDir
) - 1)
114 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
116 optionSyncGraphsDir
.arg
= graphsDir
;
117 optionSyncGraphsDir
.argHelp
= graphsDir
;
119 g_queue_push_head(&moduleOptions
, &optionSyncGraphsDir
);
120 g_queue_push_head(&moduleOptions
, &optionSyncGraphs
);
121 g_queue_push_head(&moduleOptions
, &optionSyncAnalysis
);
122 g_queue_push_head(&moduleOptions
, &optionSyncNull
);
123 g_queue_push_head(&moduleOptions
, &optionSyncStats
);
124 g_queue_push_head(&moduleOptions
, &optionSync
);
126 g_queue_foreach(&moduleOptions
, &gfAddModuleOption
, NULL
);
132 * Module unload function
134 static void destroy()
136 g_debug("Sync destroy");
138 g_queue_foreach(&moduleOptions
, &gfRemoveModuleOption
, NULL
);
139 g_string_free(analysisModulesNames
, TRUE
);
141 g_queue_clear(&processingModules
);
142 g_queue_clear(&matchingModules
);
143 g_queue_clear(&analysisModules
);
144 g_queue_clear(&moduleOptions
);
149 * Calculate a traceset's drift and offset values based on network events
151 * The individual correction factors are written out to each trace.
154 * traceSetContext: traceset
156 void syncTraceset(LttvTracesetContext
* const traceSetContext
)
158 SyncState
* syncState
;
159 struct timeval startTime
, endTime
;
160 struct rusage startUsage
, endUsage
;
165 if (!optionSync
.present
)
167 g_debug("Not synchronizing traceset because option is disabled");
171 if (optionSyncStats
.present
)
173 gettimeofday(&startTime
, 0);
174 getrusage(RUSAGE_SELF
, &startUsage
);
177 // Initialize data structures
178 syncState
= malloc(sizeof(SyncState
));
179 syncState
->traceNb
= lttv_traceset_number(traceSetContext
->ts
);
181 if (optionSyncStats
.present
)
183 syncState
->stats
= true;
187 syncState
->stats
= false;
190 if (optionSyncGraphs
.present
)
192 // Create the graph directory right away in case the module initialization
193 // functions have something to write in it.
194 syncState
->graphsDir
= optionSyncGraphsDir
.arg
;
195 syncState
->graphsStream
= createGraphsDir(syncState
->graphsDir
);
199 syncState
->graphsStream
= NULL
;
200 syncState
->graphsDir
= NULL
;
203 // Identify and initialize modules
204 syncState
->processingData
= NULL
;
205 if (optionSyncNull
.present
)
207 result
= g_queue_find_custom(&processingModules
, "LTTV-null",
208 &gcfCompareProcessing
);
212 result
= g_queue_find_custom(&processingModules
, "LTTV-standard",
213 &gcfCompareProcessing
);
215 g_assert(result
!= NULL
);
216 syncState
->processingModule
= (ProcessingModule
*) result
->data
;
218 syncState
->matchingData
= NULL
;
219 result
= g_queue_find_custom(&matchingModules
, "TCP", &gcfCompareMatching
);
220 g_assert(result
!= NULL
);
221 syncState
->matchingModule
= (MatchingModule
*) result
->data
;
223 syncState
->analysisData
= NULL
;
224 result
= g_queue_find_custom(&analysisModules
, optionSyncAnalysis
.arg
,
225 &gcfCompareAnalysis
);
228 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
232 g_error("Analysis module '%s' not found", optionSyncAnalysis
.arg
);
235 if (!optionSyncNull
.present
)
237 syncState
->analysisModule
->initAnalysis(syncState
);
238 syncState
->matchingModule
->initMatching(syncState
);
240 syncState
->processingModule
->initProcessing(syncState
, traceSetContext
);
243 lttv_process_traceset_seek_time(traceSetContext
, ltt_time_zero
);
244 lttv_process_traceset_middle(traceSetContext
, ltt_time_infinite
,
246 lttv_process_traceset_seek_time(traceSetContext
, ltt_time_zero
);
248 syncState
->processingModule
->finalizeProcessing(syncState
);
251 if (optionSyncGraphs
.present
)
253 writeGraphsScript(syncState
);
255 if (fclose(syncState
->graphsStream
) != 0)
257 g_error(strerror(errno
));
261 if (syncState
->processingModule
->printProcessingStats
!= NULL
)
263 syncState
->processingModule
->printProcessingStats(syncState
);
265 if (syncState
->matchingModule
->printMatchingStats
!= NULL
)
267 syncState
->matchingModule
->printMatchingStats(syncState
);
269 if (syncState
->analysisModule
->printAnalysisStats
!= NULL
)
271 syncState
->analysisModule
->printAnalysisStats(syncState
);
274 if (optionSyncStats
.present
)
276 printf("Resulting synchronization factors:\n");
277 for (i
= 0; i
< syncState
->traceNb
; i
++)
281 t
= traceSetContext
->traces
[i
]->t
;
283 printf("\ttrace %u drift= %g offset= %g (%f) start time= %ld.%09ld\n",
284 i
, t
->drift
, t
->offset
, (double) tsc_to_uint64(t
->freq_scale
,
285 t
->start_freq
, t
->offset
) / NANOSECONDS_PER_SECOND
,
286 t
->start_time_from_tsc
.tv_sec
,
287 t
->start_time_from_tsc
.tv_nsec
);
291 syncState
->processingModule
->destroyProcessing(syncState
);
292 if (syncState
->matchingModule
!= NULL
)
294 syncState
->matchingModule
->destroyMatching(syncState
);
296 if (syncState
->analysisModule
!= NULL
)
298 syncState
->analysisModule
->destroyAnalysis(syncState
);
303 if (optionSyncStats
.present
)
305 gettimeofday(&endTime
, 0);
306 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
308 timeDiff(&endTime
, &startTime
);
309 timeDiff(&endUsage
.ru_utime
, &startUsage
.ru_utime
);
310 timeDiff(&endUsage
.ru_stime
, &startUsage
.ru_stime
);
312 printf("Synchronization time:\n");
313 printf("\treal time: %ld.%06ld\n", endTime
.tv_sec
, endTime
.tv_usec
);
314 printf("\tuser time: %ld.%06ld\n", endUsage
.ru_utime
.tv_sec
,
315 endUsage
.ru_utime
.tv_usec
);
316 printf("\tsystem time: %ld.%06ld\n", endUsage
.ru_stime
.tv_sec
,
317 endUsage
.ru_stime
.tv_usec
);
323 * A GFunc for g_queue_foreach()
325 * Concatenate analysis module names.
328 * data: AnalysisModule*
329 * user_data: GString*, concatenated names
331 static void gfAppendAnalysisName(gpointer data
, gpointer user_data
)
333 g_string_append((GString
*) user_data
, ((AnalysisModule
*) data
)->name
);
334 g_string_append((GString
*) user_data
, ", ");
339 * A GFunc for g_queue_foreach()
342 * data: ModuleOption*
345 static void gfAddModuleOption(gpointer data
, gpointer user_data
)
347 ModuleOption
* option
;
348 LttvOptionType conversion
[]= {
349 [NO_ARG
]= LTTV_OPT_NONE
,
350 [REQUIRED_ARG
]= LTTV_OPT_STRING
,
353 g_assert_cmpuint(sizeof(conversion
) / sizeof(*conversion
), ==,
355 option
= (ModuleOption
*) data
;
356 lttv_option_add(option
->longName
, '\0', option
->optionHelp
,
357 option
->argHelp
? option
->argHelp
: argHelpNone
,
358 conversion
[option
->hasArg
], &option
->arg
, NULL
, NULL
);
363 * A GFunc for g_queue_foreach()
366 * data: ModuleOption*
369 static void gfRemoveModuleOption(gpointer data
, gpointer user_data
)
371 lttv_option_remove(((ModuleOption
*) data
)->longName
);
375 LTTV_MODULE("sync", "Synchronize traces", \
376 "Synchronizes a traceset based on the correspondance of network events", \
377 init
, destroy
, "option")