Initialize traceNb in the processing modules
[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 #include "sync_chain.h"
37 #include "sync_chain_lttv.h"
38
39
40 static void init();
41 static void destroy();
42
43 static void gfAddModuleOption(gpointer data, gpointer user_data);
44 static void gfRemoveModuleOption(gpointer data, gpointer user_data);
45
46 static ModuleOption optionSync= {
47 .longName= "sync",
48 .hasArg= NO_ARG,
49 .optionHelp= "synchronize the time between the traces",
50 };
51 static ModuleOption optionSyncStats= {
52 .longName= "sync-stats",
53 .hasArg= NO_ARG,
54 .optionHelp= "print statistics about the time synchronization",
55 };
56 static ModuleOption optionSyncNull= {
57 .longName= "sync-null",
58 .hasArg= NO_ARG,
59 .optionHelp= "read the events but do not perform any processing",
60 };
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",
66 };
67 static ModuleOption optionSyncGraphs= {
68 .longName= "sync-graphs",
69 .hasArg= NO_ARG,
70 .optionHelp= "output gnuplot graph showing synchronization points",
71 };
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",
77 };
78
79
80 /*
81 * Module init function
82 *
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.
90 */
91 static void init()
92 {
93 int retval;
94
95 g_debug("Sync init");
96
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;
106
107 retval= snprintf(graphsDir, sizeof(graphsDir), "graphs-%d", getpid());
108 if (retval > sizeof(graphsDir) - 1)
109 {
110 graphsDir[sizeof(graphsDir) - 1]= '\0';
111 }
112 optionSyncGraphsDir.arg= graphsDir;
113 optionSyncGraphsDir.argHelp= graphsDir;
114
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);
121
122 g_queue_foreach(&moduleOptions, &gfAddModuleOption, NULL);
123 }
124
125
126 /*
127 * Module unload function
128 */
129 static void destroy()
130 {
131 g_debug("Sync destroy");
132
133 g_queue_foreach(&moduleOptions, &gfRemoveModuleOption, NULL);
134 g_string_free(analysisModulesNames, TRUE);
135
136 g_queue_clear(&processingModules);
137 g_queue_clear(&matchingModules);
138 g_queue_clear(&analysisModules);
139 g_queue_clear(&moduleOptions);
140 }
141
142
143 /*
144 * Calculate a traceset's drift and offset values based on network events
145 *
146 * The individual correction factors are written out to each trace.
147 *
148 * Args:
149 * traceSetContext: traceset
150 */
151 void syncTraceset(LttvTracesetContext* const traceSetContext)
152 {
153 SyncState* syncState;
154 struct timeval startTime, endTime;
155 struct rusage startUsage, endUsage;
156 GList* result;
157 unsigned int i;
158 int retval;
159
160 if (!optionSync.present)
161 {
162 g_debug("Not synchronizing traceset because option is disabled");
163 return;
164 }
165
166 if (optionSyncStats.present)
167 {
168 gettimeofday(&startTime, 0);
169 getrusage(RUSAGE_SELF, &startUsage);
170 }
171
172 // Initialize data structures
173 syncState= malloc(sizeof(SyncState));
174
175 if (optionSyncStats.present)
176 {
177 syncState->stats= true;
178 }
179 else
180 {
181 syncState->stats= false;
182 }
183
184 if (optionSyncGraphs.present)
185 {
186 // Create the graph directory right away in case the module initialization
187 // functions have something to write in it.
188 syncState->graphsDir= optionSyncGraphsDir.arg;
189 syncState->graphsStream= createGraphsDir(syncState->graphsDir);
190 }
191 else
192 {
193 syncState->graphsStream= NULL;
194 syncState->graphsDir= NULL;
195 }
196
197 // Identify and initialize modules
198 syncState->processingData= NULL;
199 if (optionSyncNull.present)
200 {
201 result= g_queue_find_custom(&processingModules, "LTTV-null",
202 &gcfCompareProcessing);
203 }
204 else
205 {
206 result= g_queue_find_custom(&processingModules, "LTTV-standard",
207 &gcfCompareProcessing);
208 }
209 g_assert(result != NULL);
210 syncState->processingModule= (ProcessingModule*) result->data;
211
212 syncState->matchingData= NULL;
213 result= g_queue_find_custom(&matchingModules, "TCP", &gcfCompareMatching);
214 g_assert(result != NULL);
215 syncState->matchingModule= (MatchingModule*) result->data;
216
217 syncState->analysisData= NULL;
218 result= g_queue_find_custom(&analysisModules, optionSyncAnalysis.arg,
219 &gcfCompareAnalysis);
220 if (result != NULL)
221 {
222 syncState->analysisModule= (AnalysisModule*) result->data;
223 }
224 else
225 {
226 g_error("Analysis module '%s' not found", optionSyncAnalysis.arg);
227 }
228
229 syncState->processingModule->initProcessing(syncState, traceSetContext);
230 if (!optionSyncNull.present)
231 {
232 syncState->matchingModule->initMatching(syncState);
233 syncState->analysisModule->initAnalysis(syncState);
234 }
235
236 // Process traceset
237 lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero);
238 lttv_process_traceset_middle(traceSetContext, ltt_time_infinite,
239 G_MAXULONG, NULL);
240 lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero);
241
242 syncState->processingModule->finalizeProcessing(syncState);
243
244 // Write graphs file
245 if (optionSyncGraphs.present)
246 {
247 writeGraphsScript(syncState);
248
249 if (fclose(syncState->graphsStream) != 0)
250 {
251 g_error(strerror(errno));
252 }
253 }
254
255 if (syncState->processingModule->printProcessingStats != NULL)
256 {
257 syncState->processingModule->printProcessingStats(syncState);
258 }
259 if (syncState->matchingModule->printMatchingStats != NULL)
260 {
261 syncState->matchingModule->printMatchingStats(syncState);
262 }
263 if (syncState->analysisModule->printAnalysisStats != NULL)
264 {
265 syncState->analysisModule->printAnalysisStats(syncState);
266 }
267
268 if (optionSyncStats.present)
269 {
270 printf("Resulting synchronization factors:\n");
271 for (i= 0; i < syncState->traceNb; i++)
272 {
273 LttTrace* t;
274
275 t= traceSetContext->traces[i]->t;
276
277 printf("\ttrace %u drift= %g offset= %g (%f) start time= %ld.%09ld\n",
278 i, t->drift, t->offset, (double) tsc_to_uint64(t->freq_scale,
279 t->start_freq, t->offset) / NANOSECONDS_PER_SECOND,
280 t->start_time_from_tsc.tv_sec,
281 t->start_time_from_tsc.tv_nsec);
282 }
283 }
284
285 syncState->processingModule->destroyProcessing(syncState);
286 if (syncState->matchingModule != NULL)
287 {
288 syncState->matchingModule->destroyMatching(syncState);
289 }
290 if (syncState->analysisModule != NULL)
291 {
292 syncState->analysisModule->destroyAnalysis(syncState);
293 }
294
295 free(syncState);
296
297 if (optionSyncStats.present)
298 {
299 gettimeofday(&endTime, 0);
300 retval= getrusage(RUSAGE_SELF, &endUsage);
301
302 timeDiff(&endTime, &startTime);
303 timeDiff(&endUsage.ru_utime, &startUsage.ru_utime);
304 timeDiff(&endUsage.ru_stime, &startUsage.ru_stime);
305
306 printf("Synchronization time:\n");
307 printf("\treal time: %ld.%06ld\n", endTime.tv_sec, endTime.tv_usec);
308 printf("\tuser time: %ld.%06ld\n", endUsage.ru_utime.tv_sec,
309 endUsage.ru_utime.tv_usec);
310 printf("\tsystem time: %ld.%06ld\n", endUsage.ru_stime.tv_sec,
311 endUsage.ru_stime.tv_usec);
312 }
313 }
314
315
316 /*
317 * A GFunc for g_queue_foreach()
318 *
319 * Args:
320 * data: ModuleOption*
321 * user_data: NULL
322 */
323 static void gfAddModuleOption(gpointer data, gpointer user_data)
324 {
325 ModuleOption* option= data;
326 LttvOptionType conversion[]= {
327 [NO_ARG]= LTTV_OPT_NONE,
328 [OPTIONAL_ARG]= LTTV_OPT_NONE,
329 [REQUIRED_ARG]= LTTV_OPT_STRING,
330 };
331 size_t fieldOffset[]= {
332 [NO_ARG]= offsetof(ModuleOption, present),
333 [REQUIRED_ARG]= offsetof(ModuleOption, arg),
334 };
335 static const char* argHelpNone= "none";
336
337 g_assert_cmpuint(sizeof(conversion) / sizeof(*conversion), ==,
338 HAS_ARG_COUNT);
339 if (option->hasArg == OPTIONAL_ARG)
340 {
341 g_warning("Parameters with optional arguments not supported by the "
342 "lttv option scheme, parameter '%s' will not be available",
343 option->longName);
344 }
345 else
346 {
347 lttv_option_add(option->longName, '\0', option->optionHelp,
348 option->argHelp ? option->argHelp : argHelpNone,
349 conversion[option->hasArg], (void*) option + fieldOffset[option->hasArg],
350 NULL, NULL);
351 }
352 }
353
354
355 /*
356 * A GFunc for g_queue_foreach()
357 *
358 * Args:
359 * data: ModuleOption*
360 * user_data: NULL
361 */
362 static void gfRemoveModuleOption(gpointer data, gpointer user_data)
363 {
364 lttv_option_remove(((ModuleOption*) data)->longName);
365 }
366
367
368 LTTV_MODULE("sync", "Synchronize traces", \
369 "Synchronizes a traceset based on the correspondance of network events", \
370 init, destroy, "option")
This page took 0.050438 seconds and 4 git commands to generate.