Do not use __attribute__((constructor))
[lttv.git] / lttv / lttv / sync / sync_chain_lttv.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2009 Benjamin Poirier <benjamin.poirier@polymtl.ca>
3 *
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;
7 *
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.
12 *
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,
16 * MA 02111-1307, USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/resource.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #include <lttv/module.h>
34 #include <lttv/option.h>
35
36
37 #include "event_processing_lttng_standard.h"
38 #include "event_processing_lttng_null.h"
39 #include "event_matching_tcp.h"
40 #include "event_matching_broadcast.h"
41 #include "event_matching_distributor.h"
42 #include "event_analysis_chull.h"
43 #include "event_analysis_linreg.h"
44 #include "event_analysis_eval.h"
45 #include "sync_chain.h"
46 #include "sync_chain_lttv.h"
47
48
49 static void init();
50 static void destroy();
51
52 static void gfAddModuleOption(gpointer data, gpointer user_data);
53 static void gfRemoveModuleOption(gpointer data, gpointer user_data);
54
55 static ModuleOption optionSync= {
56 .longName= "sync",
57 .hasArg= NO_ARG,
58 .optionHelp= "synchronize the time between the traces",
59 };
60 static ModuleOption optionSyncStats= {
61 .longName= "sync-stats",
62 .hasArg= NO_ARG,
63 .optionHelp= "print statistics about the time synchronization",
64 };
65 static ModuleOption optionSyncNull= {
66 .longName= "sync-null",
67 .hasArg= NO_ARG,
68 .optionHelp= "read the events but do not perform any processing",
69 };
70 static GString* analysisModulesNames;
71 static ModuleOption optionSyncAnalysis= {
72 .longName= "sync-analysis",
73 .hasArg= REQUIRED_ARG,
74 .optionHelp= "specify the algorithm to use for event analysis",
75 };
76 static ModuleOption optionSyncGraphs= {
77 .longName= "sync-graphs",
78 .hasArg= NO_ARG,
79 .optionHelp= "output gnuplot graph showing synchronization points",
80 };
81 static char graphsDir[20];
82 static ModuleOption optionSyncGraphsDir= {
83 .longName= "sync-graphs-dir",
84 .hasArg= REQUIRED_ARG,
85 .optionHelp= "specify the directory where to store the graphs",
86 };
87
88
89 /*
90 * Module init function
91 *
92 * This function is declared to be the module initialization function.
93 */
94 static void init()
95 {
96 int retval;
97
98 g_debug("Sync init");
99
100 /*
101 * Initialize event modules
102 * Call the "constructor" or initialization function of each event module
103 * so it can register itself. This must be done before elements in
104 * processingModules, matchingModules, analysisModules or moduleOptions
105 * are accessed.
106 */
107 registerProcessingLTTVStandard();
108 registerProcessingLTTVNull();
109
110 registerMatchingTCP();
111 registerMatchingBroadcast();
112 registerMatchingDistributor();
113
114 registerAnalysisCHull();
115 registerAnalysisLinReg();
116 registerAnalysisEval();
117
118 g_assert(g_queue_get_length(&analysisModules) > 0);
119 optionSyncAnalysis.arg= ((AnalysisModule*)
120 g_queue_peek_head(&analysisModules))->name;
121 analysisModulesNames= g_string_new("");
122 g_queue_foreach(&analysisModules, &gfAppendAnalysisName,
123 analysisModulesNames);
124 // remove the last ", "
125 g_string_truncate(analysisModulesNames, analysisModulesNames->len - 2);
126 optionSyncAnalysis.argHelp= analysisModulesNames->str;
127
128 retval= snprintf(graphsDir, sizeof(graphsDir), "graphs-%d", getpid());
129 if (retval > sizeof(graphsDir) - 1)
130 {
131 graphsDir[sizeof(graphsDir) - 1]= '\0';
132 }
133 optionSyncGraphsDir.arg= graphsDir;
134 optionSyncGraphsDir.argHelp= graphsDir;
135
136 g_queue_push_head(&moduleOptions, &optionSyncGraphsDir);
137 g_queue_push_head(&moduleOptions, &optionSyncGraphs);
138 g_queue_push_head(&moduleOptions, &optionSyncAnalysis);
139 g_queue_push_head(&moduleOptions, &optionSyncNull);
140 g_queue_push_head(&moduleOptions, &optionSyncStats);
141 g_queue_push_head(&moduleOptions, &optionSync);
142
143 g_queue_foreach(&moduleOptions, &gfAddModuleOption, NULL);
144 }
145
146
147 /*
148 * Module unload function
149 */
150 static void destroy()
151 {
152 g_debug("Sync destroy");
153
154 g_queue_foreach(&moduleOptions, &gfRemoveModuleOption, NULL);
155 g_string_free(analysisModulesNames, TRUE);
156
157 g_queue_clear(&processingModules);
158 g_queue_clear(&matchingModules);
159 g_queue_clear(&analysisModules);
160 g_queue_clear(&moduleOptions);
161 }
162
163
164 /*
165 * Calculate a traceset's drift and offset values based on network events
166 *
167 * The individual correction factors are written out to each trace.
168 *
169 * Args:
170 * traceSetContext: traceset
171 *
172 * Returns:
173 * false if synchronization was not performed, true otherwise
174 */
175 bool syncTraceset(LttvTracesetContext* const traceSetContext)
176 {
177 SyncState* syncState;
178 struct timeval startTime, endTime;
179 struct rusage startUsage, endUsage;
180 GList* result;
181 unsigned int i;
182 int retval;
183
184 if (!optionSync.present)
185 {
186 g_debug("Not synchronizing traceset because option is disabled");
187 return false;
188 }
189
190 if (optionSyncStats.present)
191 {
192 gettimeofday(&startTime, 0);
193 getrusage(RUSAGE_SELF, &startUsage);
194 }
195
196 // Initialize data structures
197 syncState= malloc(sizeof(SyncState));
198
199 if (optionSyncStats.present)
200 {
201 syncState->stats= true;
202 }
203 else
204 {
205 syncState->stats= false;
206 }
207
208 if (!optionSyncNull.present && optionSyncGraphs.present)
209 {
210 // Create the graph directory right away in case the module initialization
211 // functions have something to write in it.
212 syncState->graphsDir= optionSyncGraphsDir.arg;
213 syncState->graphsStream= createGraphsDir(syncState->graphsDir);
214 }
215 else
216 {
217 syncState->graphsStream= NULL;
218 syncState->graphsDir= NULL;
219 }
220
221 // Identify and initialize modules
222 syncState->processingData= NULL;
223 if (optionSyncNull.present)
224 {
225 result= g_queue_find_custom(&processingModules, "LTTV-null",
226 &gcfCompareProcessing);
227 }
228 else
229 {
230 result= g_queue_find_custom(&processingModules, "LTTV-standard",
231 &gcfCompareProcessing);
232 }
233 g_assert(result != NULL);
234 syncState->processingModule= (ProcessingModule*) result->data;
235
236 syncState->matchingData= NULL;
237 result= g_queue_find_custom(&matchingModules, "TCP", &gcfCompareMatching);
238 g_assert(result != NULL);
239 syncState->matchingModule= (MatchingModule*) result->data;
240
241 syncState->analysisData= NULL;
242 result= g_queue_find_custom(&analysisModules, optionSyncAnalysis.arg,
243 &gcfCompareAnalysis);
244 if (result != NULL)
245 {
246 syncState->analysisModule= (AnalysisModule*) result->data;
247 }
248 else
249 {
250 g_error("Analysis module '%s' not found", optionSyncAnalysis.arg);
251 }
252
253 syncState->processingModule->initProcessing(syncState, traceSetContext);
254 if (!optionSyncNull.present)
255 {
256 syncState->matchingModule->initMatching(syncState);
257 syncState->analysisModule->initAnalysis(syncState);
258 }
259
260 // Process traceset
261 lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero);
262 lttv_process_traceset_middle(traceSetContext, ltt_time_infinite,
263 G_MAXULONG, NULL);
264 lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero);
265
266 syncState->processingModule->finalizeProcessing(syncState);
267
268 // Write graphs file
269 if (!optionSyncNull.present && optionSyncGraphs.present)
270 {
271 writeGraphsScript(syncState);
272
273 if (fclose(syncState->graphsStream) != 0)
274 {
275 g_error(strerror(errno));
276 }
277 }
278
279 if (!optionSyncNull.present && optionSyncStats.present)
280 {
281 printStats(syncState);
282
283 printf("Resulting synchronization factors:\n");
284 for (i= 0; i < syncState->traceNb; i++)
285 {
286 LttTrace* t;
287
288 t= traceSetContext->traces[i]->t;
289
290 printf("\ttrace %u drift= %g offset= %g (%f) start time= %ld.%09ld\n",
291 i, t->drift, t->offset, (double) tsc_to_uint64(t->freq_scale,
292 t->start_freq, t->offset) / NANOSECONDS_PER_SECOND,
293 t->start_time_from_tsc.tv_sec,
294 t->start_time_from_tsc.tv_nsec);
295 }
296 }
297
298 syncState->processingModule->destroyProcessing(syncState);
299 if (syncState->matchingModule != NULL)
300 {
301 syncState->matchingModule->destroyMatching(syncState);
302 }
303 if (syncState->analysisModule != NULL)
304 {
305 syncState->analysisModule->destroyAnalysis(syncState);
306 }
307
308 free(syncState);
309
310 if (optionSyncStats.present)
311 {
312 gettimeofday(&endTime, 0);
313 retval= getrusage(RUSAGE_SELF, &endUsage);
314
315 timeDiff(&endTime, &startTime);
316 timeDiff(&endUsage.ru_utime, &startUsage.ru_utime);
317 timeDiff(&endUsage.ru_stime, &startUsage.ru_stime);
318
319 printf("Synchronization time:\n");
320 printf("\treal time: %ld.%06ld\n", endTime.tv_sec, endTime.tv_usec);
321 printf("\tuser time: %ld.%06ld\n", endUsage.ru_utime.tv_sec,
322 endUsage.ru_utime.tv_usec);
323 printf("\tsystem time: %ld.%06ld\n", endUsage.ru_stime.tv_sec,
324 endUsage.ru_stime.tv_usec);
325 }
326
327 return true;
328 }
329
330
331 /*
332 * A GFunc for g_queue_foreach()
333 *
334 * Args:
335 * data: ModuleOption*
336 * user_data: NULL
337 */
338 static void gfAddModuleOption(gpointer data, gpointer user_data)
339 {
340 ModuleOption* option= data;
341 LttvOptionType conversion[]= {
342 [NO_ARG]= LTTV_OPT_NONE,
343 [OPTIONAL_ARG]= LTTV_OPT_NONE,
344 [REQUIRED_ARG]= LTTV_OPT_STRING,
345 };
346 size_t fieldOffset[]= {
347 [NO_ARG]= offsetof(ModuleOption, present),
348 [REQUIRED_ARG]= offsetof(ModuleOption, arg),
349 };
350 static const char* argHelpNone= "none";
351
352 g_assert_cmpuint(sizeof(conversion) / sizeof(*conversion), ==,
353 HAS_ARG_COUNT);
354 if (option->hasArg == OPTIONAL_ARG)
355 {
356 g_warning("Parameters with optional arguments not supported by the "
357 "lttv option scheme, parameter '%s' will not be available",
358 option->longName);
359 }
360 else
361 {
362 lttv_option_add(option->longName, '\0', option->optionHelp,
363 option->argHelp ? option->argHelp : argHelpNone,
364 conversion[option->hasArg], (void*) option + fieldOffset[option->hasArg],
365 NULL, NULL);
366 }
367 }
368
369
370 /*
371 * A GFunc for g_queue_foreach()
372 *
373 * Args:
374 * data: ModuleOption*
375 * user_data: NULL
376 */
377 static void gfRemoveModuleOption(gpointer data, gpointer user_data)
378 {
379 lttv_option_remove(((ModuleOption*) data)->longName);
380 }
381
382
383 LTTV_MODULE("sync", "Synchronize traces", \
384 "Synchronizes a traceset based on the correspondance of network events", \
385 init, destroy, "option")
This page took 0.037663 seconds and 4 git commands to generate.