Commit | Line | Data |
---|---|---|
b670bb7c 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 | #define _GNU_SOURCE | |
20 | ||
21 | #ifdef HAVE_CONFIG_H | |
22 | #include <config.h> | |
23 | #endif | |
24 | ||
25 | #include <errno.h> | |
26 | #include <fcntl.h> | |
27 | #include <getopt.h> | |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <sys/resource.h> | |
32 | #include <sys/stat.h> | |
33 | #include <sys/time.h> | |
34 | #include <sys/types.h> | |
35 | #include <sys/stat.h> | |
36 | #include <unistd.h> | |
37 | ||
2f961b65 BP |
38 | #include "event_processing_text.h" |
39 | #include "event_matching_tcp.h" | |
40 | #include "event_matching_broadcast.h" | |
41 | #include "event_matching_distributor.h" | |
42 | #include "event_analysis_chull.h" | |
43 | #include "event_analysis_linreg.h" | |
44 | #include "event_analysis_eval.h" | |
b670bb7c BP |
45 | #include "sync_chain.h" |
46 | ||
47 | ||
48 | struct OptionsInfo | |
49 | { | |
50 | GArray* longOptions; | |
51 | GString* optionString; | |
48b641c1 BP |
52 | GQueue* longIndex; |
53 | GHashTable* shortIndex; | |
b670bb7c BP |
54 | }; |
55 | ||
56 | ||
57 | const char* processOptions(const int argc, char* const argv[]); | |
58 | static void usage(const char* const programName); | |
59 | static void gfPrintModuleOption(gpointer data, gpointer user_data); | |
60 | static void nullLog(const gchar *log_domain, GLogLevelFlags log_level, const | |
61 | gchar *message, gpointer user_data); | |
62 | static void gfAddModuleOption(gpointer data, gpointer user_data); | |
48b641c1 BP |
63 | static guint ghfCharHash(gconstpointer key); |
64 | static gboolean gefCharEqual(gconstpointer a, gconstpointer b); | |
b670bb7c BP |
65 | |
66 | ||
67 | static ModuleOption optionSyncStats= { | |
68 | .shortName= 's', | |
69 | .longName= "sync-stats", | |
70 | .hasArg= NO_ARG, | |
71 | .optionHelp= "Print statistics and debug messages", | |
72 | }; | |
73 | static char graphsDir[20]; | |
74 | static ModuleOption optionSyncGraphs= { | |
75 | .shortName= 'g', | |
76 | .longName= "sync-graphs", | |
77 | .hasArg= OPTIONAL_ARG, | |
78 | .optionHelp= "Output gnuplot graph showing synchronization points", | |
79 | }; | |
80 | static ModuleOption optionSyncAnalysis= { | |
81 | .shortName= 'a', | |
82 | .longName= "sync-analysis", | |
83 | .hasArg= REQUIRED_ARG, | |
84 | .optionHelp= "Specify which algorithm to use for event analysis", | |
85 | }; | |
86 | ||
87 | ||
88 | /* | |
89 | * Implement a sync chain, it is mostly for unittest and it does not depend on | |
90 | * lttv | |
91 | * | |
92 | * Args: | |
93 | * argc, argv: standard argument arrays | |
94 | * | |
95 | * Returns: | |
96 | * exit status from main() is always EXIT_SUCCESS | |
97 | */ | |
98 | int main(const int argc, char* const argv[]) | |
99 | { | |
100 | SyncState* syncState; | |
101 | struct timeval startTime, endTime; | |
102 | struct rusage startUsage, endUsage; | |
103 | GList* result; | |
104 | int retval; | |
105 | bool stats; | |
106 | const char* testCaseName; | |
107 | GString* analysisModulesNames; | |
6ce8ceac | 108 | unsigned int id; |
b670bb7c | 109 | |
2f961b65 BP |
110 | /* |
111 | * Initialize event modules | |
112 | * Call the "constructor" or initialization function of each event module | |
113 | * so it can register itself. This must be done before elements in | |
114 | * processingModules, matchingModules, analysisModules or moduleOptions | |
115 | * are accessed. | |
116 | */ | |
117 | registerProcessingText(); | |
118 | ||
119 | registerMatchingTCP(); | |
120 | registerMatchingBroadcast(); | |
121 | registerMatchingDistributor(); | |
122 | ||
123 | registerAnalysisCHull(); | |
124 | registerAnalysisLinReg(); | |
125 | registerAnalysisEval(); | |
126 | ||
b670bb7c BP |
127 | // Initialize data structures |
128 | syncState= malloc(sizeof(SyncState)); | |
129 | ||
130 | // Process command line arguments | |
131 | g_assert(g_queue_get_length(&analysisModules) > 0); | |
132 | optionSyncAnalysis.arg= ((AnalysisModule*) | |
133 | g_queue_peek_head(&analysisModules))->name; | |
134 | analysisModulesNames= g_string_new("Available modules: "); | |
135 | g_queue_foreach(&analysisModules, &gfAppendAnalysisName, | |
136 | analysisModulesNames); | |
137 | // remove the last ", " | |
138 | g_string_truncate(analysisModulesNames, analysisModulesNames->len - 2); | |
139 | optionSyncAnalysis.argHelp= analysisModulesNames->str; | |
140 | ||
141 | retval= snprintf(graphsDir, sizeof(graphsDir), "graphs-%d", getpid()); | |
142 | if (retval > sizeof(graphsDir) - 1) | |
143 | { | |
144 | graphsDir[sizeof(graphsDir) - 1]= '\0'; | |
145 | } | |
146 | optionSyncGraphs.arg= graphsDir; | |
147 | ||
148 | g_queue_push_head(&moduleOptions, &optionSyncAnalysis); | |
149 | g_queue_push_head(&moduleOptions, &optionSyncGraphs); | |
150 | g_queue_push_head(&moduleOptions, &optionSyncStats); | |
151 | ||
152 | testCaseName= processOptions(argc, argv); | |
153 | ||
154 | g_string_free(analysisModulesNames, TRUE); | |
155 | ||
156 | if (optionSyncStats.present) | |
157 | { | |
158 | syncState->stats= true; | |
159 | gettimeofday(&startTime, 0); | |
160 | getrusage(RUSAGE_SELF, &startUsage); | |
161 | } | |
162 | else | |
163 | { | |
164 | syncState->stats= false; | |
6ce8ceac | 165 | id= g_log_set_handler(NULL, G_LOG_LEVEL_DEBUG, nullLog, NULL); |
b670bb7c BP |
166 | } |
167 | ||
168 | if (optionSyncGraphs.present) | |
169 | { | |
170 | // Create the graph directory right away in case the module initialization | |
171 | // functions have something to write in it. | |
172 | syncState->graphsDir= optionSyncGraphs.arg; | |
173 | syncState->graphsStream= createGraphsDir(syncState->graphsDir); | |
174 | } | |
175 | else | |
176 | { | |
177 | syncState->graphsStream= NULL; | |
178 | syncState->graphsDir= NULL; | |
179 | } | |
180 | ||
181 | // Identify modules | |
182 | syncState->processingData= NULL; | |
183 | result= g_queue_find_custom(&processingModules, "text", | |
184 | &gcfCompareProcessing); | |
185 | g_assert(result != NULL); | |
186 | syncState->processingModule= (ProcessingModule*) result->data; | |
187 | ||
188 | syncState->matchingData= NULL; | |
189 | result= g_queue_find_custom(&matchingModules, "TCP", &gcfCompareMatching); | |
190 | g_assert(result != NULL); | |
191 | syncState->matchingModule= (MatchingModule*) result->data; | |
192 | ||
193 | syncState->analysisData= NULL; | |
194 | result= g_queue_find_custom(&analysisModules, optionSyncAnalysis.arg, | |
195 | &gcfCompareAnalysis); | |
196 | if (result != NULL) | |
197 | { | |
198 | syncState->analysisModule= (AnalysisModule*) result->data; | |
199 | } | |
200 | else | |
201 | { | |
202 | g_error("Analysis module '%s' not found", optionSyncAnalysis.arg); | |
203 | } | |
204 | ||
205 | // Initialize modules | |
206 | syncState->processingModule->initProcessing(syncState, testCaseName); | |
207 | syncState->matchingModule->initMatching(syncState); | |
208 | syncState->analysisModule->initAnalysis(syncState); | |
209 | ||
210 | // Process traceset | |
211 | syncState->processingModule->finalizeProcessing(syncState); | |
212 | ||
213 | // Write graphs file | |
214 | if (syncState->graphsStream) | |
215 | { | |
216 | writeGraphsScript(syncState); | |
217 | ||
218 | if (fclose(syncState->graphsStream) != 0) | |
219 | { | |
220 | g_error(strerror(errno)); | |
221 | } | |
222 | } | |
223 | ||
224 | // Print statistics | |
225 | if (syncState->stats) | |
226 | { | |
227 | printStats(syncState); | |
228 | } | |
229 | ||
230 | // Destroy modules and clean up | |
231 | syncState->processingModule->destroyProcessing(syncState); | |
232 | syncState->matchingModule->destroyMatching(syncState); | |
233 | syncState->analysisModule->destroyAnalysis(syncState); | |
234 | ||
235 | stats= syncState->stats; | |
236 | free(syncState); | |
237 | ||
238 | if (stats) | |
239 | { | |
240 | gettimeofday(&endTime, 0); | |
241 | retval= getrusage(RUSAGE_SELF, &endUsage); | |
242 | ||
243 | timeDiff(&endTime, &startTime); | |
244 | timeDiff(&endUsage.ru_utime, &startUsage.ru_utime); | |
245 | timeDiff(&endUsage.ru_stime, &startUsage.ru_stime); | |
246 | ||
247 | printf("Synchronization time:\n"); | |
248 | printf("\treal time: %ld.%06ld\n", endTime.tv_sec, endTime.tv_usec); | |
249 | printf("\tuser time: %ld.%06ld\n", endUsage.ru_utime.tv_sec, | |
250 | endUsage.ru_utime.tv_usec); | |
251 | printf("\tsystem time: %ld.%06ld\n", endUsage.ru_stime.tv_sec, | |
252 | endUsage.ru_stime.tv_usec); | |
253 | } | |
254 | ||
6ce8ceac BP |
255 | if (!optionSyncStats.present) |
256 | { | |
257 | g_log_remove_handler(NULL, id); | |
258 | } | |
259 | ||
b670bb7c BP |
260 | return EXIT_SUCCESS; |
261 | } | |
262 | ||
263 | ||
264 | /* | |
265 | * Read program arguments dans update ModuleOptions structures | |
266 | * | |
267 | * Args: | |
268 | * argc, argv: standard argument arrays | |
269 | * | |
270 | * Returns: | |
271 | * Name of the test case file (first parameter) | |
272 | */ | |
273 | const char* processOptions(const int argc, char* const argv[]) | |
274 | { | |
275 | int c; | |
276 | extern char* optarg; | |
277 | extern int optind, opterr, optopt; | |
278 | GArray* longOptions; | |
279 | GString* optionString; | |
48b641c1 BP |
280 | GQueue* longIndex; |
281 | int longOption; | |
282 | GHashTable* shortIndex; | |
b670bb7c BP |
283 | |
284 | longOptions= g_array_sized_new(TRUE, FALSE, sizeof(struct option), | |
285 | g_queue_get_length(&moduleOptions)); | |
286 | optionString= g_string_new(""); | |
48b641c1 BP |
287 | longIndex= g_queue_new(); |
288 | shortIndex= g_hash_table_new(&ghfCharHash, &gefCharEqual); | |
b670bb7c BP |
289 | |
290 | g_queue_foreach(&moduleOptions, &gfAddModuleOption, &(struct OptionsInfo) | |
48b641c1 | 291 | {longOptions, optionString, longIndex, shortIndex}); |
b670bb7c BP |
292 | |
293 | do | |
294 | { | |
295 | int optionIndex= 0; | |
48b641c1 | 296 | ModuleOption* moduleOption; |
b670bb7c | 297 | |
48b641c1 | 298 | longOption= -1; |
b670bb7c BP |
299 | c= getopt_long(argc, argv, optionString->str, (struct option*) |
300 | longOptions->data, &optionIndex); | |
301 | ||
48b641c1 BP |
302 | if (longOption >= 0 && longOption < g_queue_get_length(longIndex)) |
303 | { | |
304 | moduleOption= g_queue_peek_nth(longIndex, longOption); | |
305 | } | |
306 | else if ((moduleOption= g_hash_table_lookup(shortIndex, &c)) != NULL) | |
b670bb7c | 307 | { |
b670bb7c BP |
308 | } |
309 | else if (c == -1) | |
310 | { | |
311 | break; | |
312 | } | |
313 | else if (c == '?') | |
314 | { | |
315 | usage(argv[0]); | |
316 | abort(); | |
317 | } | |
318 | else | |
319 | { | |
320 | g_error("Option parse error"); | |
321 | } | |
48b641c1 BP |
322 | |
323 | moduleOption->present= true; | |
324 | ||
325 | if (moduleOption->hasArg == REQUIRED_ARG) | |
326 | { | |
327 | moduleOption->arg= optarg; | |
328 | } | |
329 | if (moduleOption->hasArg == OPTIONAL_ARG && optarg) | |
330 | { | |
331 | moduleOption->arg= optarg; | |
332 | } | |
b670bb7c BP |
333 | } while (c != -1); |
334 | ||
335 | g_array_free(longOptions, TRUE); | |
336 | g_string_free(optionString, TRUE); | |
48b641c1 BP |
337 | g_queue_free(longIndex); |
338 | g_hash_table_destroy(shortIndex); | |
b670bb7c BP |
339 | |
340 | if (argc <= optind) | |
341 | { | |
342 | fprintf(stderr, "Test file unspecified\n"); | |
343 | usage(argv[0]); | |
344 | abort(); | |
345 | } | |
346 | ||
347 | return argv[optind]; | |
348 | } | |
349 | ||
350 | ||
351 | /* | |
352 | * Print information about program options and arguments. | |
353 | * | |
354 | * Args: | |
355 | * programName: name of the program, as contained in argv[0] for example | |
356 | */ | |
357 | static void usage(const char* const programName) | |
358 | { | |
359 | printf( | |
360 | "%s [options] <test file>\n" | |
361 | "Options:\n", programName); | |
362 | ||
363 | g_queue_foreach(&moduleOptions, &gfPrintModuleOption, NULL); | |
364 | } | |
365 | ||
366 | ||
367 | /* | |
368 | * A GFunc for g_queue_foreach() | |
369 | * | |
370 | * Print analysis module names. | |
371 | * | |
372 | * Args: | |
373 | * data: ModuleOption*, option | |
374 | * user_data: NULL | |
375 | */ | |
376 | static void gfPrintModuleOption(gpointer data, gpointer user_data) | |
377 | { | |
378 | ModuleOption* option= data; | |
379 | int width= 0, sum= 0; | |
380 | const int colWidth= 27; | |
381 | ||
382 | printf("\t"); | |
383 | ||
384 | if (option->shortName) | |
385 | { | |
386 | printf("-%c, %n", option->shortName, &width); | |
387 | sum+= width; | |
388 | } | |
389 | ||
390 | printf("--%-s%n", option->longName, &width); | |
391 | sum+= width; | |
392 | ||
393 | if (option->hasArg == REQUIRED_ARG || option->hasArg == OPTIONAL_ARG) | |
394 | { | |
395 | printf("=[..]%n", &width); | |
396 | sum+= width; | |
397 | } | |
398 | ||
399 | if (option->optionHelp) | |
400 | { | |
401 | printf("%*s%s\n", colWidth - sum > 0 ? colWidth - sum : 0, "", option->optionHelp); | |
402 | } | |
403 | ||
404 | if (option->argHelp) | |
405 | { | |
406 | printf("\t%*s%s\n", colWidth, "", option->argHelp); | |
407 | } | |
408 | ||
409 | if ((option->hasArg == REQUIRED_ARG || option->hasArg == OPTIONAL_ARG) && option->arg) | |
410 | { | |
411 | printf("\t%*sDefault value: %s\n", colWidth, "", option->arg); | |
412 | } | |
413 | } | |
414 | ||
415 | ||
416 | /* | |
417 | * A Glib log function which does nothing. | |
418 | */ | |
419 | static void nullLog(const gchar *log_domain, GLogLevelFlags log_level, const | |
420 | gchar *message, gpointer user_data) | |
421 | {} | |
422 | ||
423 | ||
424 | /* | |
425 | * A GFunc for g_queue_foreach() | |
426 | * | |
427 | * Args: | |
428 | * data: ModuleOption*, option | |
429 | * user_data: struct OptionsInfo*, add option to this array of struct option | |
430 | */ | |
431 | static void gfAddModuleOption(gpointer data, gpointer user_data) | |
432 | { | |
433 | ModuleOption* option= data; | |
434 | struct OptionsInfo* optionsInfo= user_data; | |
435 | struct option newOption; | |
436 | // "[mixing enumerations] can still be considered bad style even though it | |
437 | // is not strictly illegal" c.faq 2.22 | |
438 | const int conversion[]= { | |
439 | [NO_ARG]= no_argument, | |
440 | [OPTIONAL_ARG]= optional_argument, | |
441 | [REQUIRED_ARG]= required_argument, | |
442 | }; | |
443 | const char* colons[]= { | |
444 | [NO_ARG]= "", | |
445 | [OPTIONAL_ARG]= "::", | |
446 | [REQUIRED_ARG]= ":", | |
447 | }; | |
448 | ||
449 | newOption.name= option->longName; | |
450 | newOption.has_arg= conversion[option->hasArg]; | |
451 | newOption.flag= NULL; | |
48b641c1 | 452 | newOption.val= g_queue_get_length(optionsInfo->longIndex); |
b670bb7c BP |
453 | |
454 | g_array_append_val(optionsInfo->longOptions, newOption); | |
48b641c1 BP |
455 | if (option->shortName) |
456 | { | |
457 | g_string_append_c(optionsInfo->optionString, option->shortName); | |
458 | g_string_append(optionsInfo->optionString, colons[option->hasArg]); | |
459 | ||
460 | g_hash_table_insert(optionsInfo->shortIndex, &option->shortName, | |
461 | option); | |
462 | } | |
463 | g_queue_push_tail(optionsInfo->longIndex, option); | |
464 | } | |
465 | ||
466 | ||
467 | /* | |
468 | * A GHashFunc for g_hash_table_new() | |
469 | * | |
470 | * Args: | |
471 | * key char*, just one character | |
472 | */ | |
473 | static guint ghfCharHash(gconstpointer key) | |
474 | { | |
475 | return *(char*) key; | |
476 | } | |
477 | ||
478 | ||
479 | /* | |
480 | * A GEqualFunc for g_hash_table_new() | |
481 | * | |
482 | * Args: | |
483 | * a, b char*, just one character each | |
484 | * | |
485 | * Returns: | |
486 | * TRUE if both values are equal | |
487 | */ | |
488 | static gboolean gefCharEqual(gconstpointer a, gconstpointer b) | |
489 | { | |
490 | if (*(char*) a == *(char*) b) | |
491 | { | |
492 | return TRUE; | |
493 | } | |
494 | else | |
495 | { | |
496 | return FALSE; | |
497 | } | |
b670bb7c | 498 | } |