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
;
105 const char* testCaseName
;
106 GString
* analysisModulesNames
;
110 * Initialize event modules
111 * Call the "constructor" or initialization function of each event module
112 * so it can register itself. This must be done before elements in
113 * processingModules, matchingModules, analysisModules or moduleOptions
116 registerProcessingText();
118 registerMatchingTCP();
119 registerMatchingBroadcast();
120 registerMatchingDistributor();
122 registerAnalysisCHull();
123 registerAnalysisLinReg();
124 registerAnalysisEval();
126 // Initialize data structures
127 syncState
= malloc(sizeof(SyncState
));
129 // Process command line arguments
130 g_assert(g_queue_get_length(&analysisModules
) > 0);
131 optionSyncAnalysis
.arg
= ((AnalysisModule
*)
132 g_queue_peek_head(&analysisModules
))->name
;
133 analysisModulesNames
= g_string_new("Available modules: ");
134 g_queue_foreach(&analysisModules
, &gfAppendAnalysisName
,
135 analysisModulesNames
);
136 // remove the last ", "
137 g_string_truncate(analysisModulesNames
, analysisModulesNames
->len
- 2);
138 optionSyncAnalysis
.argHelp
= analysisModulesNames
->str
;
140 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "graphs-%d", getpid());
141 if (retval
> sizeof(graphsDir
) - 1)
143 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
145 optionSyncGraphs
.arg
= graphsDir
;
147 g_queue_push_head(&moduleOptions
, &optionSyncAnalysis
);
148 g_queue_push_head(&moduleOptions
, &optionSyncGraphs
);
149 g_queue_push_head(&moduleOptions
, &optionSyncStats
);
151 testCaseName
= processOptions(argc
, argv
);
153 g_string_free(analysisModulesNames
, TRUE
);
155 if (optionSyncStats
.present
)
157 syncState
->stats
= true;
158 gettimeofday(&startTime
, 0);
159 getrusage(RUSAGE_SELF
, &startUsage
);
163 syncState
->stats
= false;
164 id
= g_log_set_handler(NULL
, G_LOG_LEVEL_DEBUG
, nullLog
, NULL
);
167 if (optionSyncGraphs
.present
)
169 // Create the graph directory right away in case the module initialization
170 // functions have something to write in it.
171 syncState
->graphsDir
= optionSyncGraphs
.arg
;
172 syncState
->graphsStream
= createGraphsDir(syncState
->graphsDir
);
176 syncState
->graphsStream
= NULL
;
177 syncState
->graphsDir
= NULL
;
181 syncState
->processingData
= NULL
;
182 result
= g_queue_find_custom(&processingModules
, "text",
183 &gcfCompareProcessing
);
184 g_assert(result
!= NULL
);
185 syncState
->processingModule
= (ProcessingModule
*) result
->data
;
187 syncState
->matchingData
= NULL
;
188 result
= g_queue_find_custom(&matchingModules
, "TCP", &gcfCompareMatching
);
189 g_assert(result
!= NULL
);
190 syncState
->matchingModule
= (MatchingModule
*) result
->data
;
192 syncState
->analysisData
= NULL
;
193 result
= g_queue_find_custom(&analysisModules
, optionSyncAnalysis
.arg
,
194 &gcfCompareAnalysis
);
197 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
201 g_error("Analysis module '%s' not found", optionSyncAnalysis
.arg
);
204 // Initialize modules
205 syncState
->processingModule
->initProcessing(syncState
, testCaseName
);
206 syncState
->matchingModule
->initMatching(syncState
);
207 syncState
->analysisModule
->initAnalysis(syncState
);
210 syncState
->processingModule
->finalizeProcessing(syncState
);
213 if (syncState
->graphsStream
)
215 writeGraphsScript(syncState
);
217 if (fclose(syncState
->graphsStream
) != 0)
219 g_error(strerror(errno
));
224 if (syncState
->stats
)
226 printStats(syncState
);
229 // Destroy modules and clean up
230 syncState
->processingModule
->destroyProcessing(syncState
);
231 syncState
->matchingModule
->destroyMatching(syncState
);
232 syncState
->analysisModule
->destroyAnalysis(syncState
);
234 stats
= syncState
->stats
;
239 gettimeofday(&endTime
, 0);
240 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
242 timeDiff(&endTime
, &startTime
);
243 timeDiff(&endUsage
.ru_utime
, &startUsage
.ru_utime
);
244 timeDiff(&endUsage
.ru_stime
, &startUsage
.ru_stime
);
246 printf("Synchronization time:\n");
247 printf("\treal time: %ld.%06ld\n", endTime
.tv_sec
, endTime
.tv_usec
);
248 printf("\tuser time: %ld.%06ld\n", endUsage
.ru_utime
.tv_sec
,
249 endUsage
.ru_utime
.tv_usec
);
250 printf("\tsystem time: %ld.%06ld\n", endUsage
.ru_stime
.tv_sec
,
251 endUsage
.ru_stime
.tv_usec
);
254 if (!optionSyncStats
.present
)
256 g_log_remove_handler(NULL
, id
);
264 * Read program arguments dans update ModuleOptions structures
267 * argc, argv: standard argument arrays
270 * Name of the test case file (first parameter)
272 const char* processOptions(const int argc
, char* const argv
[])
276 extern int optind
, opterr
, optopt
;
278 GString
* optionString
;
281 GHashTable
* shortIndex
;
283 longOptions
= g_array_sized_new(TRUE
, FALSE
, sizeof(struct option
),
284 g_queue_get_length(&moduleOptions
));
285 optionString
= g_string_new("");
286 longIndex
= g_queue_new();
287 shortIndex
= g_hash_table_new(&ghfCharHash
, &gefCharEqual
);
289 g_queue_foreach(&moduleOptions
, &gfAddModuleOption
, &(struct OptionsInfo
)
290 {longOptions
, optionString
, longIndex
, shortIndex
});
295 ModuleOption
* moduleOption
;
298 c
= getopt_long(argc
, argv
, optionString
->str
, (struct option
*)
299 longOptions
->data
, &optionIndex
);
301 if (longOption
>= 0 && longOption
< g_queue_get_length(longIndex
))
303 moduleOption
= g_queue_peek_nth(longIndex
, longOption
);
305 else if ((moduleOption
= g_hash_table_lookup(shortIndex
, &c
)) != NULL
)
319 g_error("Option parse error");
322 moduleOption
->present
= true;
324 if (moduleOption
->hasArg
== REQUIRED_ARG
)
326 moduleOption
->arg
= optarg
;
328 if (moduleOption
->hasArg
== OPTIONAL_ARG
&& optarg
)
330 moduleOption
->arg
= optarg
;
334 g_array_free(longOptions
, TRUE
);
335 g_string_free(optionString
, TRUE
);
336 g_queue_free(longIndex
);
337 g_hash_table_destroy(shortIndex
);
341 fprintf(stderr
, "Test file unspecified\n");
351 * Print information about program options and arguments.
354 * programName: name of the program, as contained in argv[0] for example
356 static void usage(const char* const programName
)
359 "%s [options] <test file>\n"
360 "Options:\n", programName
);
362 g_queue_foreach(&moduleOptions
, &gfPrintModuleOption
, NULL
);
367 * A GFunc for g_queue_foreach()
369 * Print analysis module names.
372 * data: ModuleOption*, option
375 static void gfPrintModuleOption(gpointer data
, gpointer user_data
)
377 ModuleOption
* option
= data
;
378 int width
= 0, sum
= 0;
379 const int colWidth
= 27;
383 if (option
->shortName
)
385 printf("-%c, %n", option
->shortName
, &width
);
389 printf("--%-s%n", option
->longName
, &width
);
392 if (option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
)
394 printf("=[..]%n", &width
);
398 if (option
->optionHelp
)
400 printf("%*s%s\n", colWidth
- sum
> 0 ? colWidth
- sum
: 0, "", option
->optionHelp
);
405 printf("\t%*s%s\n", colWidth
, "", option
->argHelp
);
408 if ((option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
) && option
->arg
)
410 printf("\t%*sDefault value: %s\n", colWidth
, "", option
->arg
);
416 * A Glib log function which does nothing.
418 static void nullLog(const gchar
*log_domain
, GLogLevelFlags log_level
, const
419 gchar
*message
, gpointer user_data
)
424 * A GFunc for g_queue_foreach()
427 * data: ModuleOption*, option
428 * user_data: struct OptionsInfo*, add option to this array of struct option
430 static void gfAddModuleOption(gpointer data
, gpointer user_data
)
432 ModuleOption
* option
= data
;
433 struct OptionsInfo
* optionsInfo
= user_data
;
434 struct option newOption
;
435 // "[mixing enumerations] can still be considered bad style even though it
436 // is not strictly illegal" c.faq 2.22
437 const int conversion
[]= {
438 [NO_ARG
]= no_argument
,
439 [OPTIONAL_ARG
]= optional_argument
,
440 [REQUIRED_ARG
]= required_argument
,
442 const char* colons
[]= {
444 [OPTIONAL_ARG
]= "::",
448 newOption
.name
= option
->longName
;
449 newOption
.has_arg
= conversion
[option
->hasArg
];
450 newOption
.flag
= NULL
;
451 newOption
.val
= g_queue_get_length(optionsInfo
->longIndex
);
453 g_array_append_val(optionsInfo
->longOptions
, newOption
);
454 if (option
->shortName
)
456 g_string_append_c(optionsInfo
->optionString
, option
->shortName
);
457 g_string_append(optionsInfo
->optionString
, colons
[option
->hasArg
]);
459 g_hash_table_insert(optionsInfo
->shortIndex
, &option
->shortName
,
462 g_queue_push_tail(optionsInfo
->longIndex
, option
);
467 * A GHashFunc for g_hash_table_new()
470 * key char*, just one character
472 static guint
ghfCharHash(gconstpointer key
)
479 * A GEqualFunc for g_hash_table_new()
482 * a, b char*, just one character each
485 * TRUE if both values are equal
487 static gboolean
gefCharEqual(gconstpointer a
, gconstpointer b
)
489 if (*(char*) a
== *(char*) b
)