Commit | Line | Data |
---|---|---|
70407e86 BP |
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 | ||
08365995 BP |
23 | #include <errno.h> |
24 | #include <fcntl.h> | |
25 | #include <stdio.h> | |
70407e86 | 26 | #include <stdlib.h> |
70407e86 | 27 | #include <sys/resource.h> |
08365995 BP |
28 | #include <sys/stat.h> |
29 | #include <sys/time.h> | |
30 | #include <sys/types.h> | |
31 | #include <sys/stat.h> | |
32 | #include <unistd.h> | |
70407e86 BP |
33 | |
34 | #include <lttv/module.h> | |
35 | #include <lttv/option.h> | |
36 | ||
37 | #include "sync_chain.h" | |
38 | ||
39 | ||
40 | #ifndef g_info | |
41 | #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format) | |
42 | #endif | |
43 | ||
44 | ||
45 | static void init(); | |
46 | static void destroy(); | |
47 | ||
48 | static void timeDiff(struct timeval* const end, const struct timeval* const start); | |
08365995 | 49 | |
70407e86 BP |
50 | static gint gcfCompareAnalysis(gconstpointer a, gconstpointer b); |
51 | static gint gcfCompareProcessing(gconstpointer a, gconstpointer b); | |
52 | static void gfAppendAnalysisName(gpointer data, gpointer user_data); | |
53 | ||
54 | static gboolean optionSync; | |
55 | static gboolean optionSyncStats; | |
56 | static gboolean optionSyncNull; | |
57 | static char* optionSyncAnalysis; | |
08365995 BP |
58 | static gboolean optionSyncGraphs; |
59 | static char* optionSyncGraphsDir; | |
60 | static char graphsDir[20]; | |
70407e86 BP |
61 | |
62 | GQueue processingModules= G_QUEUE_INIT; | |
63 | GQueue matchingModules= G_QUEUE_INIT; | |
64 | GQueue analysisModules= G_QUEUE_INIT; | |
65 | ||
66 | ||
67 | /* | |
68 | * Module init function | |
69 | * | |
70 | * This function is declared to be the module initialization function. Event | |
71 | * modules are registered with a "constructor (102)" attribute except one in | |
72 | * each class (processing, matching, analysis) which is chosen to be the | |
73 | * default and which is registered with a "constructor (101)" attribute. | |
74 | * Constructors with no priority are called after constructors with | |
75 | * priorities. The result is that the list of event modules is known when this | |
76 | * function is executed. | |
77 | */ | |
78 | static void init() | |
79 | { | |
80 | GString* analysisModulesNames; | |
08365995 | 81 | int retval; |
70407e86 BP |
82 | |
83 | g_debug("\t\t\tXXXX sync init\n"); | |
84 | ||
85 | optionSync= FALSE; | |
86 | lttv_option_add("sync", '\0', "synchronize the time between the traces" , | |
87 | "none", LTTV_OPT_NONE, &optionSync, NULL, NULL); | |
88 | ||
89 | optionSyncStats= FALSE; | |
90 | lttv_option_add("sync-stats", '\0', "print statistics about the time " | |
91 | "synchronization", "none", LTTV_OPT_NONE, &optionSyncStats, NULL, | |
92 | NULL); | |
93 | ||
94 | optionSyncNull= FALSE; | |
95 | lttv_option_add("sync-null", '\0', "read the events but do not perform " | |
96 | "any processing", "none", LTTV_OPT_NONE, &optionSyncNull, NULL, NULL); | |
97 | ||
98 | g_assert(g_queue_get_length(&analysisModules) > 0); | |
99 | optionSyncAnalysis= ((AnalysisModule*) | |
100 | g_queue_peek_head(&analysisModules))->name; | |
101 | analysisModulesNames= g_string_new(""); | |
102 | g_queue_foreach(&analysisModules, &gfAppendAnalysisName, | |
103 | analysisModulesNames); | |
104 | // remove the last ", " | |
105 | g_string_truncate(analysisModulesNames, analysisModulesNames->len - 2); | |
106 | lttv_option_add("sync-analysis", '\0', "specify the algorithm to use for " | |
107 | "event analysis" , analysisModulesNames->str, LTTV_OPT_STRING, | |
108 | &optionSyncAnalysis, NULL, NULL); | |
109 | g_string_free(analysisModulesNames, TRUE); | |
08365995 BP |
110 | |
111 | optionSyncGraphs= FALSE; | |
112 | lttv_option_add("sync-graphs", '\0', "output gnuplot graph showing " | |
113 | "synchronization points", "none", LTTV_OPT_NONE, &optionSyncGraphs, | |
114 | NULL, NULL); | |
115 | ||
116 | retval= snprintf(graphsDir, sizeof(graphsDir), "graphs-%d", getpid()); | |
117 | if (retval > sizeof(graphsDir) - 1) | |
118 | { | |
119 | graphsDir[sizeof(graphsDir) - 1]= '\0'; | |
120 | } | |
121 | optionSyncGraphsDir= graphsDir; | |
122 | lttv_option_add("sync-graphs-dir", '\0', "specify the directory where to" | |
123 | " store the graphs", graphsDir, LTTV_OPT_STRING, &optionSyncGraphsDir, | |
124 | NULL, NULL); | |
70407e86 BP |
125 | } |
126 | ||
127 | ||
128 | /* | |
129 | * Module unload function | |
130 | */ | |
131 | static void destroy() | |
132 | { | |
133 | g_debug("\t\t\tXXXX sync destroy\n"); | |
134 | ||
135 | lttv_option_remove("sync"); | |
136 | lttv_option_remove("sync-stats"); | |
137 | lttv_option_remove("sync-null"); | |
138 | lttv_option_remove("sync-analysis"); | |
08365995 BP |
139 | lttv_option_remove("sync-graphs"); |
140 | lttv_option_remove("sync-graphs-dir"); | |
70407e86 BP |
141 | } |
142 | ||
143 | ||
144 | /* | |
145 | * Calculate a traceset's drift and offset values based on network events | |
146 | * | |
147 | * The individual correction factors are written out to each trace. | |
148 | * | |
149 | * Args: | |
150 | * traceSetContext: traceset | |
151 | */ | |
152 | void syncTraceset(LttvTracesetContext* const traceSetContext) | |
153 | { | |
154 | SyncState* syncState; | |
155 | struct timeval startTime, endTime; | |
156 | struct rusage startUsage, endUsage; | |
157 | GList* result; | |
08365995 BP |
158 | char* cwd; |
159 | FILE* graphsStream; | |
160 | int graphsFp; | |
70407e86 BP |
161 | int retval; |
162 | ||
163 | if (optionSync == FALSE) | |
164 | { | |
165 | g_debug("Not synchronizing traceset because option is disabled"); | |
166 | return; | |
167 | } | |
168 | ||
169 | if (optionSyncStats) | |
170 | { | |
171 | gettimeofday(&startTime, 0); | |
172 | getrusage(RUSAGE_SELF, &startUsage); | |
173 | } | |
174 | ||
175 | // Initialize data structures | |
176 | syncState= malloc(sizeof(SyncState)); | |
177 | syncState->traceNb= lttv_traceset_number(traceSetContext->ts); | |
178 | ||
179 | if (optionSyncStats) | |
180 | { | |
181 | syncState->stats= true; | |
182 | } | |
183 | else | |
184 | { | |
185 | syncState->stats= false; | |
186 | } | |
187 | ||
08365995 BP |
188 | if (optionSyncGraphs) |
189 | { | |
190 | syncState->graphs= optionSyncGraphsDir; | |
191 | } | |
192 | else | |
193 | { | |
194 | syncState->graphs= NULL; | |
195 | } | |
196 | ||
197 | // Identify and initialize processing module | |
70407e86 BP |
198 | syncState->processingData= NULL; |
199 | if (optionSyncNull) | |
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; | |
08365995 BP |
211 | |
212 | graphsStream= NULL; | |
213 | if (syncState->graphs) | |
214 | { | |
215 | // Create the graph directory right away in case the module initialization | |
216 | // functions have something to write in it. | |
217 | cwd= changeToGraphDir(syncState->graphs); | |
218 | ||
219 | if (syncState->processingModule->writeProcessingGraphsPlots != NULL) | |
220 | { | |
221 | if ((graphsFp= open("graphs.gnu", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | | |
222 | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | |
223 | | S_IWOTH | S_IXOTH)) == -1) | |
224 | { | |
225 | g_error(strerror(errno)); | |
226 | } | |
227 | if ((graphsStream= fdopen(graphsFp, "w")) == NULL) | |
228 | { | |
229 | g_error(strerror(errno)); | |
230 | } | |
231 | } | |
232 | ||
233 | retval= chdir(cwd); | |
234 | if (retval == -1) | |
235 | { | |
236 | g_error(strerror(errno)); | |
237 | } | |
238 | free(cwd); | |
239 | } | |
240 | ||
70407e86 BP |
241 | syncState->processingModule->initProcessing(syncState, traceSetContext); |
242 | ||
08365995 | 243 | // Identify and initialize matching and analysis modules |
70407e86 BP |
244 | syncState->matchingData= NULL; |
245 | syncState->analysisData= NULL; | |
246 | if (optionSyncNull) | |
247 | { | |
248 | syncState->matchingModule= NULL; | |
249 | syncState->analysisModule= NULL; | |
250 | } | |
251 | else | |
252 | { | |
253 | g_assert(g_queue_get_length(&matchingModules) == 1); | |
254 | syncState->matchingModule= (MatchingModule*) | |
255 | g_queue_peek_head(&matchingModules); | |
256 | syncState->matchingModule->initMatching(syncState); | |
257 | ||
258 | result= g_queue_find_custom(&analysisModules, optionSyncAnalysis, | |
259 | &gcfCompareAnalysis); | |
260 | if (result != NULL) | |
261 | { | |
262 | syncState->analysisModule= (AnalysisModule*) result->data; | |
263 | syncState->analysisModule->initAnalysis(syncState); | |
264 | } | |
265 | else | |
266 | { | |
267 | g_error("Analysis module '%s' not found", optionSyncAnalysis); | |
268 | } | |
269 | } | |
270 | ||
271 | // Process traceset | |
272 | lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero); | |
273 | lttv_process_traceset_middle(traceSetContext, ltt_time_infinite, | |
274 | G_MAXULONG, NULL); | |
275 | lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero); | |
276 | ||
277 | syncState->processingModule->finalizeProcessing(syncState); | |
278 | ||
08365995 BP |
279 | // Write graphs file |
280 | if (graphsStream != NULL) | |
281 | { | |
282 | unsigned int i, j; | |
283 | ||
284 | fprintf(graphsStream, | |
285 | "#!/usr/bin/gnuplot\n\n" | |
286 | "set terminal postscript eps color size 8in,6in\n"); | |
287 | ||
288 | // Cover the upper triangular matrix, i is the reference node. | |
289 | for (i= 0; i < syncState->traceNb; i++) | |
290 | { | |
291 | for (j= i + 1; j < syncState->traceNb; j++) | |
292 | { | |
293 | long pos; | |
294 | ||
295 | fprintf(graphsStream, | |
296 | "\nset output \"%03d-%03d.eps\"\n" | |
297 | "plot \\\n", i, j); | |
298 | ||
299 | syncState->processingModule->writeProcessingGraphsPlots(graphsStream, | |
300 | syncState, i, j); | |
301 | ||
302 | // Remove the ", \\\n" from the last graph plot line | |
303 | fflush(graphsStream); | |
304 | pos= ftell(graphsStream); | |
305 | if (ftruncate(fileno(graphsStream), pos - 4) == -1) | |
306 | { | |
307 | g_error(strerror(errno)); | |
308 | } | |
309 | if (fseek(graphsStream, 0, SEEK_END) == -1) | |
310 | { | |
311 | g_error(strerror(errno)); | |
312 | } | |
313 | ||
314 | fprintf(graphsStream, | |
315 | "\nset output \"%1$03d-%2$03d.eps\"\n" | |
316 | "set key inside right bottom\n" | |
317 | "set title \"\"\n" | |
318 | "set xlabel \"Clock %1$u\"\n" | |
319 | "set xtics nomirror\n" | |
320 | "set ylabel \"Clock %2$u\"\n" | |
321 | "set ytics nomirror\n", i, j); | |
322 | ||
323 | syncState->processingModule->writeProcessingGraphsOptions(graphsStream, | |
324 | syncState, i, j); | |
325 | ||
326 | fprintf(graphsStream, | |
327 | "replot\n"); | |
328 | } | |
329 | } | |
330 | ||
331 | if (fclose(graphsStream) != 0) | |
332 | { | |
333 | g_error(strerror(errno)); | |
334 | } | |
335 | } | |
336 | ||
70407e86 BP |
337 | if (syncState->processingModule->printProcessingStats != NULL) |
338 | { | |
339 | syncState->processingModule->printProcessingStats(syncState); | |
340 | } | |
341 | ||
342 | syncState->processingModule->destroyProcessing(syncState); | |
343 | if (syncState->matchingModule != NULL) | |
344 | { | |
345 | syncState->matchingModule->destroyMatching(syncState); | |
346 | } | |
347 | if (syncState->analysisModule != NULL) | |
348 | { | |
349 | syncState->analysisModule->destroyAnalysis(syncState); | |
350 | } | |
351 | ||
352 | free(syncState); | |
353 | ||
354 | if (optionSyncStats) | |
355 | { | |
356 | gettimeofday(&endTime, 0); | |
357 | retval= getrusage(RUSAGE_SELF, &endUsage); | |
358 | ||
359 | timeDiff(&endTime, &startTime); | |
360 | timeDiff(&endUsage.ru_utime, &startUsage.ru_utime); | |
361 | timeDiff(&endUsage.ru_stime, &startUsage.ru_stime); | |
362 | ||
363 | printf("Synchronization time:\n"); | |
364 | printf("\treal time: %ld.%06ld\n", endTime.tv_sec, endTime.tv_usec); | |
365 | printf("\tuser time: %ld.%06ld\n", endUsage.ru_utime.tv_sec, | |
366 | endUsage.ru_utime.tv_usec); | |
367 | printf("\tsystem time: %ld.%06ld\n", endUsage.ru_stime.tv_sec, | |
368 | endUsage.ru_stime.tv_usec); | |
369 | } | |
370 | } | |
371 | ||
372 | ||
373 | /* | |
374 | * Calculate the elapsed time between two timeval values | |
375 | * | |
376 | * Args: | |
377 | * end: end time, result is also stored in this structure | |
378 | * start: start time | |
379 | */ | |
380 | static void timeDiff(struct timeval* const end, const struct timeval* const start) | |
381 | { | |
382 | if (end->tv_usec >= start->tv_usec) | |
383 | { | |
384 | end->tv_sec-= start->tv_sec; | |
385 | end->tv_usec-= start->tv_usec; | |
386 | } | |
387 | else | |
388 | { | |
389 | end->tv_sec= end->tv_sec - start->tv_sec - 1; | |
390 | end->tv_usec= end->tv_usec - start->tv_usec + 1e6; | |
391 | } | |
392 | } | |
393 | ||
394 | ||
395 | /* | |
396 | * A GCompareFunc for g_slist_find_custom() | |
397 | * | |
398 | * Args: | |
399 | * a: AnalysisModule*, element's data | |
400 | * b: char*, user data to compare against | |
401 | * | |
402 | * Returns: | |
403 | * 0 if the analysis module a's name is b | |
404 | */ | |
405 | static gint gcfCompareAnalysis(gconstpointer a, gconstpointer b) | |
406 | { | |
407 | const AnalysisModule* analysisModule; | |
408 | const char* name; | |
409 | ||
410 | analysisModule= (const AnalysisModule*)a; | |
411 | name= (const char*)b; | |
412 | ||
413 | return strncmp(analysisModule->name, name, strlen(analysisModule->name) + | |
414 | 1); | |
415 | } | |
416 | ||
417 | ||
418 | /* | |
419 | * A GCompareFunc for g_slist_find_custom() | |
420 | * | |
421 | * Args: | |
422 | * a: ProcessingModule*, element's data | |
423 | * b: char*, user data to compare against | |
424 | * | |
425 | * Returns: | |
426 | * 0 if the analysis module a's name is b | |
427 | */ | |
428 | static gint gcfCompareProcessing(gconstpointer a, gconstpointer b) | |
429 | { | |
430 | const ProcessingModule* processingModule; | |
431 | const char* name; | |
432 | ||
433 | processingModule= (const ProcessingModule*)a; | |
434 | name= (const char*)b; | |
435 | ||
436 | return strncmp(processingModule->name, name, | |
437 | strlen(processingModule->name) + 1); | |
438 | } | |
439 | ||
440 | ||
441 | /* | |
442 | * A GFunc for g_queue_foreach() | |
443 | * | |
444 | * Concatenate analysis module names. | |
445 | * | |
446 | * Args: | |
447 | * data: AnalysisModule* | |
448 | * user_data: GString*, concatenated names | |
449 | */ | |
450 | static void gfAppendAnalysisName(gpointer data, gpointer user_data) | |
451 | { | |
452 | g_string_append((GString*) user_data, ((AnalysisModule*) data)->name); | |
453 | g_string_append((GString*) user_data, ", "); | |
454 | } | |
455 | ||
456 | ||
08365995 BP |
457 | /* |
458 | * Change to the directory used to hold graphs. Create it if necessary. | |
459 | * | |
460 | * Args: | |
461 | * graph: name of directory | |
462 | * | |
463 | * Returns: | |
464 | * The current working directory before the execution of the function. The | |
465 | * string must be free'd by the caller. | |
466 | */ | |
467 | char* changeToGraphDir(char* const graphs) | |
468 | { | |
469 | int retval; | |
470 | char* cwd; | |
471 | ||
472 | cwd= getcwd(NULL, 0); | |
473 | if (cwd == NULL) | |
474 | { | |
475 | g_error(strerror(errno)); | |
476 | } | |
477 | while ((retval= chdir(graphs)) != 0) | |
478 | { | |
479 | if (errno == ENOENT) | |
480 | { | |
481 | retval= mkdir(graphs, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | | |
482 | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); | |
483 | if (retval != 0) | |
484 | { | |
485 | g_error(strerror(errno)); | |
486 | } | |
487 | } | |
488 | else | |
489 | { | |
490 | g_error(strerror(errno)); | |
491 | } | |
492 | } | |
493 | ||
494 | return cwd; | |
495 | } | |
496 | ||
497 | ||
70407e86 BP |
498 | LTTV_MODULE("sync", "Synchronize traces", \ |
499 | "Synchronizes a traceset based on the correspondance of network events", \ | |
500 | init, destroy, "option") |