1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2009, 2010 Benjamin Poirier <benjamin.poirier@polymtl.ca>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 2.1 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 * License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/resource.h>
33 #include <sys/types.h>
37 #include "event_processing_text.h"
38 #include "event_matching_tcp.h"
39 #include "event_matching_broadcast.h"
40 #include "event_matching_distributor.h"
41 #include "event_analysis_chull.h"
42 #include "event_analysis_linreg.h"
43 #include "event_analysis_eval.h"
44 #include "sync_chain.h"
50 GString
* optionString
;
52 GHashTable
* shortIndex
;
56 const char* processOptions(const int argc
, char* const argv
[]);
57 static void usage(const char* const programName
);
58 static void gfPrintModuleOption(gpointer data
, gpointer user_data
);
59 static void nullLog(const gchar
*log_domain
, GLogLevelFlags log_level
, const
60 gchar
*message
, gpointer user_data
);
61 static void gfAddModuleOption(gpointer data
, gpointer user_data
);
62 static guint
ghfCharHash(gconstpointer key
);
63 static gboolean
gefCharEqual(gconstpointer a
, gconstpointer b
);
66 static ModuleOption optionSyncStats
= {
68 .longName
= "sync-stats",
70 .optionHelp
= "Print statistics and debug messages",
72 static char graphsDir
[20];
73 static ModuleOption optionSyncGraphs
= {
75 .longName
= "sync-graphs",
76 .hasArg
= OPTIONAL_ARG
,
77 .optionHelp
= "Output gnuplot graph showing synchronization points",
79 static ModuleOption optionSyncAnalysis
= {
81 .longName
= "sync-analysis",
82 .hasArg
= REQUIRED_ARG
,
83 .optionHelp
= "Specify which algorithm to use for event analysis",
88 * Implement a sync chain, it is mostly for unittest and it does not depend on
92 * argc, argv: standard argument arrays
95 * exit status from main() is always EXIT_SUCCESS
97 int main(const int argc
, char* const argv
[])
100 struct timeval startTime
, endTime
;
101 struct rusage startUsage
, endUsage
;
106 const char* testCaseName
;
107 GString
* analysisModulesNames
;
109 AllFactors
* allFactors
;
112 * Initialize event modules
113 * Call the "constructor" or initialization function of each event module
114 * so it can register itself. This must be done before elements in
115 * processingModules, matchingModules, analysisModules or moduleOptions
118 registerProcessingText();
120 registerMatchingTCP();
121 registerMatchingBroadcast();
122 registerMatchingDistributor();
124 registerAnalysisCHull();
125 registerAnalysisLinReg();
126 registerAnalysisEval();
128 // Initialize data structures
129 syncState
= malloc(sizeof(SyncState
));
131 // Process command line arguments
132 g_assert(g_queue_get_length(&analysisModules
) > 0);
133 optionSyncAnalysis
.arg
= ((AnalysisModule
*)
134 g_queue_peek_head(&analysisModules
))->name
;
135 analysisModulesNames
= g_string_new("Available modules: ");
136 g_queue_foreach(&analysisModules
, &gfAppendAnalysisName
,
137 analysisModulesNames
);
138 // remove the last ", "
139 g_string_truncate(analysisModulesNames
, analysisModulesNames
->len
- 2);
140 optionSyncAnalysis
.argHelp
= analysisModulesNames
->str
;
142 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "graphs-%d", getpid());
143 if (retval
> sizeof(graphsDir
) - 1)
145 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
147 optionSyncGraphs
.arg
= graphsDir
;
149 g_queue_push_head(&moduleOptions
, &optionSyncAnalysis
);
150 g_queue_push_head(&moduleOptions
, &optionSyncGraphs
);
151 g_queue_push_head(&moduleOptions
, &optionSyncStats
);
153 testCaseName
= processOptions(argc
, argv
);
155 g_string_free(analysisModulesNames
, TRUE
);
157 if (optionSyncStats
.present
)
159 syncState
->stats
= true;
160 gettimeofday(&startTime
, 0);
161 getrusage(RUSAGE_SELF
, &startUsage
);
165 syncState
->stats
= false;
166 id
= g_log_set_handler(NULL
, G_LOG_LEVEL_DEBUG
, nullLog
, NULL
);
169 if (optionSyncGraphs
.present
)
171 // Create the graph directory right away in case the module initialization
172 // functions have something to write in it.
173 syncState
->graphsDir
= optionSyncGraphs
.arg
;
174 syncState
->graphsStream
= createGraphsDir(syncState
->graphsDir
);
178 syncState
->graphsStream
= NULL
;
179 syncState
->graphsDir
= NULL
;
183 syncState
->processingData
= NULL
;
184 result
= g_queue_find_custom(&processingModules
, "text",
185 &gcfCompareProcessing
);
186 g_assert(result
!= NULL
);
187 syncState
->processingModule
= (ProcessingModule
*) result
->data
;
189 syncState
->matchingData
= NULL
;
190 result
= g_queue_find_custom(&matchingModules
, "TCP", &gcfCompareMatching
);
191 g_assert(result
!= NULL
);
192 syncState
->matchingModule
= (MatchingModule
*) result
->data
;
194 syncState
->analysisData
= NULL
;
195 result
= g_queue_find_custom(&analysisModules
, optionSyncAnalysis
.arg
,
196 &gcfCompareAnalysis
);
199 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
203 g_error("Analysis module '%s' not found", optionSyncAnalysis
.arg
);
206 // Initialize modules
207 syncState
->processingModule
->initProcessing(syncState
, testCaseName
);
208 syncState
->matchingModule
->initMatching(syncState
);
209 syncState
->analysisModule
->initAnalysis(syncState
);
212 allFactors
= syncState
->processingModule
->finalizeProcessing(syncState
);
213 factors
= reduceFactors(allFactors
);
214 freeAllFactors(allFactors
);
217 if (syncState
->graphsStream
)
219 writeGraphsScript(syncState
);
221 if (fclose(syncState
->graphsStream
) != 0)
223 g_error(strerror(errno
));
228 if (optionSyncStats
.present
)
232 printStats(syncState
);
234 printf("Resulting synchronization factors:\n");
235 for (i
= 0; i
< factors
->len
; i
++)
237 Factors
* traceFactors
= &g_array_index(factors
, Factors
, i
);
238 printf("\ttrace %u drift= %g offset= %g\n", i
,
239 traceFactors
->drift
, traceFactors
->offset
);
243 // Destroy modules and clean up
244 syncState
->processingModule
->destroyProcessing(syncState
);
245 syncState
->matchingModule
->destroyMatching(syncState
);
246 syncState
->analysisModule
->destroyAnalysis(syncState
);
248 stats
= syncState
->stats
;
253 gettimeofday(&endTime
, 0);
254 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
256 timeDiff(&endTime
, &startTime
);
257 timeDiff(&endUsage
.ru_utime
, &startUsage
.ru_utime
);
258 timeDiff(&endUsage
.ru_stime
, &startUsage
.ru_stime
);
260 printf("Synchronization time:\n");
261 printf("\treal time: %ld.%06ld\n", endTime
.tv_sec
, endTime
.tv_usec
);
262 printf("\tuser time: %ld.%06ld\n", endUsage
.ru_utime
.tv_sec
,
263 endUsage
.ru_utime
.tv_usec
);
264 printf("\tsystem time: %ld.%06ld\n", endUsage
.ru_stime
.tv_sec
,
265 endUsage
.ru_stime
.tv_usec
);
268 if (!optionSyncStats
.present
)
270 g_log_remove_handler(NULL
, id
);
278 * Read program arguments and update ModuleOptions structures
281 * argc, argv: standard argument arrays
284 * Name of the test case file (first parameter)
286 const char* processOptions(const int argc
, char* const argv
[])
290 extern int optind
, opterr
, optopt
;
292 GString
* optionString
;
295 GHashTable
* shortIndex
;
297 longOptions
= g_array_sized_new(TRUE
, FALSE
, sizeof(struct option
),
298 g_queue_get_length(&moduleOptions
));
299 optionString
= g_string_new("");
300 longIndex
= g_queue_new();
301 shortIndex
= g_hash_table_new(&ghfCharHash
, &gefCharEqual
);
303 g_queue_foreach(&moduleOptions
, &gfAddModuleOption
, &(struct OptionsInfo
)
304 {longOptions
, optionString
, longIndex
, shortIndex
});
309 ModuleOption
* moduleOption
;
312 c
= getopt_long(argc
, argv
, optionString
->str
, (struct option
*)
313 longOptions
->data
, &optionIndex
);
315 if (longOption
>= 0 && longOption
< g_queue_get_length(longIndex
))
317 moduleOption
= g_queue_peek_nth(longIndex
, longOption
);
319 else if ((moduleOption
= g_hash_table_lookup(shortIndex
, &c
)) != NULL
)
333 g_error("Option parse error");
336 moduleOption
->present
= true;
338 if (moduleOption
->hasArg
== REQUIRED_ARG
)
340 moduleOption
->arg
= optarg
;
342 if (moduleOption
->hasArg
== OPTIONAL_ARG
&& optarg
)
344 moduleOption
->arg
= optarg
;
348 g_array_free(longOptions
, TRUE
);
349 g_string_free(optionString
, TRUE
);
350 g_queue_free(longIndex
);
351 g_hash_table_destroy(shortIndex
);
355 fprintf(stderr
, "Test file unspecified\n");
365 * Print information about program options and arguments.
368 * programName: name of the program, as contained in argv[0] for example
370 static void usage(const char* const programName
)
373 "%s [options] <test file>\n"
374 "Options:\n", programName
);
376 g_queue_foreach(&moduleOptions
, &gfPrintModuleOption
, NULL
);
381 * A GFunc for g_queue_foreach()
383 * Print analysis module names.
386 * data: ModuleOption*, option
389 static void gfPrintModuleOption(gpointer data
, gpointer user_data
)
391 ModuleOption
* option
= data
;
392 int width
= 0, sum
= 0;
393 const int colWidth
= 27;
397 if (option
->shortName
)
399 printf("-%c, %n", option
->shortName
, &width
);
403 printf("--%-s%n", option
->longName
, &width
);
406 if (option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
)
408 printf("=[..]%n", &width
);
412 if (option
->optionHelp
)
414 printf("%*s%s\n", colWidth
- sum
> 0 ? colWidth
- sum
: 0, "", option
->optionHelp
);
419 printf("\t%*s%s\n", colWidth
, "", option
->argHelp
);
422 if ((option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
) && option
->arg
)
424 printf("\t%*sDefault value: %s\n", colWidth
, "", option
->arg
);
430 * A Glib log function which does nothing.
432 static void nullLog(const gchar
*log_domain
, GLogLevelFlags log_level
, const
433 gchar
*message
, gpointer user_data
)
438 * A GFunc for g_queue_foreach()
441 * data: ModuleOption*, option
442 * user_data: struct OptionsInfo*, add option to this array of struct option
444 static void gfAddModuleOption(gpointer data
, gpointer user_data
)
446 ModuleOption
* option
= data
;
447 struct OptionsInfo
* optionsInfo
= user_data
;
448 struct option newOption
;
449 // "[mixing enumerations] can still be considered bad style even though it
450 // is not strictly illegal" c.faq 2.22
451 const int conversion
[]= {
452 [NO_ARG
]= no_argument
,
453 [OPTIONAL_ARG
]= optional_argument
,
454 [REQUIRED_ARG
]= required_argument
,
456 const char* colons
[]= {
458 [OPTIONAL_ARG
]= "::",
462 newOption
.name
= option
->longName
;
463 newOption
.has_arg
= conversion
[option
->hasArg
];
464 newOption
.flag
= NULL
;
465 newOption
.val
= g_queue_get_length(optionsInfo
->longIndex
);
467 g_array_append_val(optionsInfo
->longOptions
, newOption
);
468 if (option
->shortName
)
470 g_string_append_c(optionsInfo
->optionString
, option
->shortName
);
471 g_string_append(optionsInfo
->optionString
, colons
[option
->hasArg
]);
473 g_hash_table_insert(optionsInfo
->shortIndex
, &option
->shortName
,
476 g_queue_push_tail(optionsInfo
->longIndex
, option
);
481 * A GHashFunc for g_hash_table_new()
484 * key char*, just one character
486 static guint
ghfCharHash(gconstpointer key
)
493 * A GEqualFunc for g_hash_table_new()
496 * a, b char*, just one character each
499 * TRUE if both values are equal
501 static gboolean
gefCharEqual(gconstpointer a
, gconstpointer b
)
503 if (*(char*) a
== *(char*) b
)