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"
37 #include "sync_chain_lttv.h"
41 static void destroy();
43 static void gfAddModuleOption(gpointer data
, gpointer user_data
);
44 static void gfRemoveModuleOption(gpointer data
, gpointer user_data
);
46 static ModuleOption optionSync
= {
49 .optionHelp
= "synchronize the time between the traces",
51 static ModuleOption optionSyncStats
= {
52 .longName
= "sync-stats",
54 .optionHelp
= "print statistics about the time synchronization",
56 static ModuleOption optionSyncNull
= {
57 .longName
= "sync-null",
59 .optionHelp
= "read the events but do not perform any processing",
61 static GString
* analysisModulesNames
;
62 static ModuleOption optionSyncAnalysis
= {
63 .longName
= "sync-analysis",
64 .hasArg
= REQUIRED_ARG
,
65 .optionHelp
= "specify the algorithm to use for event analysis",
67 static ModuleOption optionSyncGraphs
= {
68 .longName
= "sync-graphs",
70 .optionHelp
= "output gnuplot graph showing synchronization points",
72 static char graphsDir
[20];
73 static ModuleOption optionSyncGraphsDir
= {
74 .longName
= "sync-graphs-dir",
75 .hasArg
= REQUIRED_ARG
,
76 .optionHelp
= "specify the directory where to store the graphs",
81 * Module init function
83 * This function is declared to be the module initialization function. Event
84 * modules are registered with a "constructor (102)" attribute except one in
85 * each class (processing, matching, analysis) which is chosen to be the
86 * default and which is registered with a "constructor (101)" attribute.
87 * Constructors with no priority are called after constructors with
88 * priorities. The result is that the list of event modules is known when this
89 * function is executed.
97 g_assert(g_queue_get_length(&analysisModules
) > 0);
98 optionSyncAnalysis
.arg
= ((AnalysisModule
*)
99 g_queue_peek_head(&analysisModules
))->name
;
100 analysisModulesNames
= g_string_new("");
101 g_queue_foreach(&analysisModules
, &gfAppendAnalysisName
,
102 analysisModulesNames
);
103 // remove the last ", "
104 g_string_truncate(analysisModulesNames
, analysisModulesNames
->len
- 2);
105 optionSyncAnalysis
.argHelp
= analysisModulesNames
->str
;
107 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "graphs-%d", getpid());
108 if (retval
> sizeof(graphsDir
) - 1)
110 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
112 optionSyncGraphsDir
.arg
= graphsDir
;
113 optionSyncGraphsDir
.argHelp
= graphsDir
;
115 g_queue_push_head(&moduleOptions
, &optionSyncGraphsDir
);
116 g_queue_push_head(&moduleOptions
, &optionSyncGraphs
);
117 g_queue_push_head(&moduleOptions
, &optionSyncAnalysis
);
118 g_queue_push_head(&moduleOptions
, &optionSyncNull
);
119 g_queue_push_head(&moduleOptions
, &optionSyncStats
);
120 g_queue_push_head(&moduleOptions
, &optionSync
);
122 g_queue_foreach(&moduleOptions
, &gfAddModuleOption
, NULL
);
127 * Module unload function
129 static void destroy()
131 g_debug("Sync destroy");
133 g_queue_foreach(&moduleOptions
, &gfRemoveModuleOption
, NULL
);
134 g_string_free(analysisModulesNames
, TRUE
);
136 g_queue_clear(&processingModules
);
137 g_queue_clear(&matchingModules
);
138 g_queue_clear(&analysisModules
);
139 g_queue_clear(&moduleOptions
);
144 * Calculate a traceset's drift and offset values based on network events
146 * The individual correction factors are written out to each trace.
149 * traceSetContext: traceset
152 * false if synchronization was not performed, true otherwise
154 bool syncTraceset(LttvTracesetContext
* const traceSetContext
)
156 SyncState
* syncState
;
157 struct timeval startTime
, endTime
;
158 struct rusage startUsage
, endUsage
;
163 if (!optionSync
.present
)
165 g_debug("Not synchronizing traceset because option is disabled");
169 if (optionSyncStats
.present
)
171 gettimeofday(&startTime
, 0);
172 getrusage(RUSAGE_SELF
, &startUsage
);
175 // Initialize data structures
176 syncState
= malloc(sizeof(SyncState
));
178 if (optionSyncStats
.present
)
180 syncState
->stats
= true;
184 syncState
->stats
= false;
187 if (!optionSyncNull
.present
&& optionSyncGraphs
.present
)
189 // Create the graph directory right away in case the module initialization
190 // functions have something to write in it.
191 syncState
->graphsDir
= optionSyncGraphsDir
.arg
;
192 syncState
->graphsStream
= createGraphsDir(syncState
->graphsDir
);
196 syncState
->graphsStream
= NULL
;
197 syncState
->graphsDir
= NULL
;
200 // Identify and initialize modules
201 syncState
->processingData
= NULL
;
202 if (optionSyncNull
.present
)
204 result
= g_queue_find_custom(&processingModules
, "LTTV-null",
205 &gcfCompareProcessing
);
209 result
= g_queue_find_custom(&processingModules
, "LTTV-standard",
210 &gcfCompareProcessing
);
212 g_assert(result
!= NULL
);
213 syncState
->processingModule
= (ProcessingModule
*) result
->data
;
215 syncState
->matchingData
= NULL
;
216 result
= g_queue_find_custom(&matchingModules
, "TCP", &gcfCompareMatching
);
217 g_assert(result
!= NULL
);
218 syncState
->matchingModule
= (MatchingModule
*) result
->data
;
220 syncState
->analysisData
= NULL
;
221 result
= g_queue_find_custom(&analysisModules
, optionSyncAnalysis
.arg
,
222 &gcfCompareAnalysis
);
225 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
229 g_error("Analysis module '%s' not found", optionSyncAnalysis
.arg
);
232 syncState
->processingModule
->initProcessing(syncState
, traceSetContext
);
233 if (!optionSyncNull
.present
)
235 syncState
->matchingModule
->initMatching(syncState
);
236 syncState
->analysisModule
->initAnalysis(syncState
);
240 lttv_process_traceset_seek_time(traceSetContext
, ltt_time_zero
);
241 lttv_process_traceset_middle(traceSetContext
, ltt_time_infinite
,
243 lttv_process_traceset_seek_time(traceSetContext
, ltt_time_zero
);
245 syncState
->processingModule
->finalizeProcessing(syncState
);
248 if (!optionSyncNull
.present
&& optionSyncGraphs
.present
)
250 writeGraphsScript(syncState
);
252 if (fclose(syncState
->graphsStream
) != 0)
254 g_error(strerror(errno
));
258 if (!optionSyncNull
.present
&& optionSyncStats
.present
)
260 printStats(syncState
);
262 printf("Resulting synchronization factors:\n");
263 for (i
= 0; i
< syncState
->traceNb
; i
++)
267 t
= traceSetContext
->traces
[i
]->t
;
269 printf("\ttrace %u drift= %g offset= %g (%f) start time= %ld.%09ld\n",
270 i
, t
->drift
, t
->offset
, (double) tsc_to_uint64(t
->freq_scale
,
271 t
->start_freq
, t
->offset
) / NANOSECONDS_PER_SECOND
,
272 t
->start_time_from_tsc
.tv_sec
,
273 t
->start_time_from_tsc
.tv_nsec
);
277 syncState
->processingModule
->destroyProcessing(syncState
);
278 if (syncState
->matchingModule
!= NULL
)
280 syncState
->matchingModule
->destroyMatching(syncState
);
282 if (syncState
->analysisModule
!= NULL
)
284 syncState
->analysisModule
->destroyAnalysis(syncState
);
289 if (optionSyncStats
.present
)
291 gettimeofday(&endTime
, 0);
292 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
294 timeDiff(&endTime
, &startTime
);
295 timeDiff(&endUsage
.ru_utime
, &startUsage
.ru_utime
);
296 timeDiff(&endUsage
.ru_stime
, &startUsage
.ru_stime
);
298 printf("Synchronization time:\n");
299 printf("\treal time: %ld.%06ld\n", endTime
.tv_sec
, endTime
.tv_usec
);
300 printf("\tuser time: %ld.%06ld\n", endUsage
.ru_utime
.tv_sec
,
301 endUsage
.ru_utime
.tv_usec
);
302 printf("\tsystem time: %ld.%06ld\n", endUsage
.ru_stime
.tv_sec
,
303 endUsage
.ru_stime
.tv_usec
);
311 * A GFunc for g_queue_foreach()
314 * data: ModuleOption*
317 static void gfAddModuleOption(gpointer data
, gpointer user_data
)
319 ModuleOption
* option
= data
;
320 LttvOptionType conversion
[]= {
321 [NO_ARG
]= LTTV_OPT_NONE
,
322 [OPTIONAL_ARG
]= LTTV_OPT_NONE
,
323 [REQUIRED_ARG
]= LTTV_OPT_STRING
,
325 size_t fieldOffset
[]= {
326 [NO_ARG
]= offsetof(ModuleOption
, present
),
327 [REQUIRED_ARG
]= offsetof(ModuleOption
, arg
),
329 static const char* argHelpNone
= "none";
331 g_assert_cmpuint(sizeof(conversion
) / sizeof(*conversion
), ==,
333 if (option
->hasArg
== OPTIONAL_ARG
)
335 g_warning("Parameters with optional arguments not supported by the "
336 "lttv option scheme, parameter '%s' will not be available",
341 lttv_option_add(option
->longName
, '\0', option
->optionHelp
,
342 option
->argHelp
? option
->argHelp
: argHelpNone
,
343 conversion
[option
->hasArg
], (void*) option
+ fieldOffset
[option
->hasArg
],
350 * A GFunc for g_queue_foreach()
353 * data: ModuleOption*
356 static void gfRemoveModuleOption(gpointer data
, gpointer user_data
)
358 lttv_option_remove(((ModuleOption
*) data
)->longName
);
362 LTTV_MODULE("sync", "Synchronize traces", \
363 "Synchronizes a traceset based on the correspondance of network events", \
364 init
, destroy
, "option")