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>
30 #include <sys/types.h>
34 #include <lttv/module.h>
35 #include <lttv/option.h>
37 #include "sync_chain.h"
41 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
46 static void destroy();
48 static void timeDiff(struct timeval
* const end
, const struct timeval
* const start
);
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
);
54 static gboolean optionSync
;
55 static gboolean optionSyncStats
;
56 static gboolean optionSyncNull
;
57 static char* optionSyncAnalysis
;
58 static gboolean optionSyncGraphs
;
59 static char* optionSyncGraphsDir
;
60 static char graphsDir
[20];
62 GQueue processingModules
= G_QUEUE_INIT
;
63 GQueue matchingModules
= G_QUEUE_INIT
;
64 GQueue analysisModules
= G_QUEUE_INIT
;
68 * Module init function
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.
80 GString
* analysisModulesNames
;
83 g_debug("\t\t\tXXXX sync init\n");
86 lttv_option_add("sync", '\0', "synchronize the time between the traces" ,
87 "none", LTTV_OPT_NONE
, &optionSync
, NULL
, NULL
);
89 optionSyncStats
= FALSE
;
90 lttv_option_add("sync-stats", '\0', "print statistics about the time "
91 "synchronization", "none", LTTV_OPT_NONE
, &optionSyncStats
, NULL
,
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
);
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
);
111 optionSyncGraphs
= FALSE
;
112 lttv_option_add("sync-graphs", '\0', "output gnuplot graph showing "
113 "synchronization points", "none", LTTV_OPT_NONE
, &optionSyncGraphs
,
116 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "graphs-%d", getpid());
117 if (retval
> sizeof(graphsDir
) - 1)
119 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
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
,
129 * Module unload function
131 static void destroy()
133 g_debug("\t\t\tXXXX sync destroy\n");
135 lttv_option_remove("sync");
136 lttv_option_remove("sync-stats");
137 lttv_option_remove("sync-null");
138 lttv_option_remove("sync-analysis");
139 lttv_option_remove("sync-graphs");
140 lttv_option_remove("sync-graphs-dir");
145 * Calculate a traceset's drift and offset values based on network events
147 * The individual correction factors are written out to each trace.
150 * traceSetContext: traceset
152 void syncTraceset(LttvTracesetContext
* const traceSetContext
)
154 SyncState
* syncState
;
155 struct timeval startTime
, endTime
;
156 struct rusage startUsage
, endUsage
;
163 if (optionSync
== FALSE
)
165 g_debug("Not synchronizing traceset because option is disabled");
171 gettimeofday(&startTime
, 0);
172 getrusage(RUSAGE_SELF
, &startUsage
);
175 // Initialize data structures
176 syncState
= malloc(sizeof(SyncState
));
177 syncState
->traceNb
= lttv_traceset_number(traceSetContext
->ts
);
181 syncState
->stats
= true;
185 syncState
->stats
= false;
188 if (optionSyncGraphs
)
190 syncState
->graphs
= optionSyncGraphsDir
;
194 syncState
->graphs
= NULL
;
197 // Identify and initialize processing module
198 syncState
->processingData
= NULL
;
201 result
= g_queue_find_custom(&processingModules
, "LTTV-null",
202 &gcfCompareProcessing
);
206 result
= g_queue_find_custom(&processingModules
, "LTTV-standard",
207 &gcfCompareProcessing
);
209 g_assert(result
!= NULL
);
210 syncState
->processingModule
= (ProcessingModule
*) result
->data
;
213 if (syncState
->graphs
)
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
);
219 if (syncState
->processingModule
->writeProcessingGraphsPlots
!= NULL
)
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)
225 g_error(strerror(errno
));
227 if ((graphsStream
= fdopen(graphsFp
, "w")) == NULL
)
229 g_error(strerror(errno
));
236 g_error(strerror(errno
));
241 syncState
->processingModule
->initProcessing(syncState
, traceSetContext
);
243 // Identify and initialize matching and analysis modules
244 syncState
->matchingData
= NULL
;
245 syncState
->analysisData
= NULL
;
248 syncState
->matchingModule
= NULL
;
249 syncState
->analysisModule
= NULL
;
253 g_assert(g_queue_get_length(&matchingModules
) == 1);
254 syncState
->matchingModule
= (MatchingModule
*)
255 g_queue_peek_head(&matchingModules
);
256 syncState
->matchingModule
->initMatching(syncState
);
258 result
= g_queue_find_custom(&analysisModules
, optionSyncAnalysis
,
259 &gcfCompareAnalysis
);
262 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
263 syncState
->analysisModule
->initAnalysis(syncState
);
267 g_error("Analysis module '%s' not found", optionSyncAnalysis
);
272 lttv_process_traceset_seek_time(traceSetContext
, ltt_time_zero
);
273 lttv_process_traceset_middle(traceSetContext
, ltt_time_infinite
,
275 lttv_process_traceset_seek_time(traceSetContext
, ltt_time_zero
);
277 syncState
->processingModule
->finalizeProcessing(syncState
);
280 if (graphsStream
!= NULL
)
284 fprintf(graphsStream
,
285 "#!/usr/bin/gnuplot\n\n"
286 "set terminal postscript eps color size 8in,6in\n");
288 // Cover the upper triangular matrix, i is the reference node.
289 for (i
= 0; i
< syncState
->traceNb
; i
++)
291 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
295 fprintf(graphsStream
,
296 "\nset output \"%03d-%03d.eps\"\n"
299 syncState
->processingModule
->writeProcessingGraphsPlots(graphsStream
,
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)
307 g_error(strerror(errno
));
309 if (fseek(graphsStream
, 0, SEEK_END
) == -1)
311 g_error(strerror(errno
));
314 fprintf(graphsStream
,
315 "\nset output \"%1$03d-%2$03d.eps\"\n"
316 "set key inside right bottom\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
);
323 syncState
->processingModule
->writeProcessingGraphsOptions(graphsStream
,
326 fprintf(graphsStream
,
331 if (fclose(graphsStream
) != 0)
333 g_error(strerror(errno
));
337 if (syncState
->processingModule
->printProcessingStats
!= NULL
)
339 syncState
->processingModule
->printProcessingStats(syncState
);
342 syncState
->processingModule
->destroyProcessing(syncState
);
343 if (syncState
->matchingModule
!= NULL
)
345 syncState
->matchingModule
->destroyMatching(syncState
);
347 if (syncState
->analysisModule
!= NULL
)
349 syncState
->analysisModule
->destroyAnalysis(syncState
);
356 gettimeofday(&endTime
, 0);
357 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
359 timeDiff(&endTime
, &startTime
);
360 timeDiff(&endUsage
.ru_utime
, &startUsage
.ru_utime
);
361 timeDiff(&endUsage
.ru_stime
, &startUsage
.ru_stime
);
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
);
374 * Calculate the elapsed time between two timeval values
377 * end: end time, result is also stored in this structure
380 static void timeDiff(struct timeval
* const end
, const struct timeval
* const start
)
382 if (end
->tv_usec
>= start
->tv_usec
)
384 end
->tv_sec
-= start
->tv_sec
;
385 end
->tv_usec
-= start
->tv_usec
;
389 end
->tv_sec
= end
->tv_sec
- start
->tv_sec
- 1;
390 end
->tv_usec
= end
->tv_usec
- start
->tv_usec
+ 1e6
;
396 * A GCompareFunc for g_slist_find_custom()
399 * a: AnalysisModule*, element's data
400 * b: char*, user data to compare against
403 * 0 if the analysis module a's name is b
405 static gint
gcfCompareAnalysis(gconstpointer a
, gconstpointer b
)
407 const AnalysisModule
* analysisModule
;
410 analysisModule
= (const AnalysisModule
*)a
;
411 name
= (const char*)b
;
413 return strncmp(analysisModule
->name
, name
, strlen(analysisModule
->name
) +
419 * A GCompareFunc for g_slist_find_custom()
422 * a: ProcessingModule*, element's data
423 * b: char*, user data to compare against
426 * 0 if the analysis module a's name is b
428 static gint
gcfCompareProcessing(gconstpointer a
, gconstpointer b
)
430 const ProcessingModule
* processingModule
;
433 processingModule
= (const ProcessingModule
*)a
;
434 name
= (const char*)b
;
436 return strncmp(processingModule
->name
, name
,
437 strlen(processingModule
->name
) + 1);
442 * A GFunc for g_queue_foreach()
444 * Concatenate analysis module names.
447 * data: AnalysisModule*
448 * user_data: GString*, concatenated names
450 static void gfAppendAnalysisName(gpointer data
, gpointer user_data
)
452 g_string_append((GString
*) user_data
, ((AnalysisModule
*) data
)->name
);
453 g_string_append((GString
*) user_data
, ", ");
458 * Change to the directory used to hold graphs. Create it if necessary.
461 * graph: name of directory
464 * The current working directory before the execution of the function. The
465 * string must be free'd by the caller.
467 char* changeToGraphDir(char* const graphs
)
472 cwd
= getcwd(NULL
, 0);
475 g_error(strerror(errno
));
477 while ((retval
= chdir(graphs
)) != 0)
481 retval
= mkdir(graphs
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
|
482 S_IWGRP
| S_IXGRP
| S_IROTH
| S_IWOTH
| S_IXOTH
);
485 g_error(strerror(errno
));
490 g_error(strerror(errno
));
498 LTTV_MODULE("sync", "Synchronize traces", \
499 "Synchronizes a traceset based on the correspondance of network events", \
500 init
, destroy
, "option")