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,
31 #include <sys/resource.h>
34 #include <sys/types.h>
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"
45 #include "sync_chain.h"
51 GString
* optionString
;
53 GHashTable
* shortIndex
;
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
);
63 static guint
ghfCharHash(gconstpointer key
);
64 static gboolean
gefCharEqual(gconstpointer a
, gconstpointer b
);
67 static ModuleOption optionSyncStats
= {
69 .longName
= "sync-stats",
71 .optionHelp
= "Print statistics and debug messages",
73 static char graphsDir
[20];
74 static ModuleOption optionSyncGraphs
= {
76 .longName
= "sync-graphs",
77 .hasArg
= OPTIONAL_ARG
,
78 .optionHelp
= "Output gnuplot graph showing synchronization points",
80 static ModuleOption optionSyncAnalysis
= {
82 .longName
= "sync-analysis",
83 .hasArg
= REQUIRED_ARG
,
84 .optionHelp
= "Specify which algorithm to use for event analysis",
89 * Implement a sync chain, it is mostly for unittest and it does not depend on
93 * argc, argv: standard argument arrays
96 * exit status from main() is always EXIT_SUCCESS
98 int main(const int argc
, char* const argv
[])
100 SyncState
* syncState
;
101 struct timeval startTime
, endTime
;
102 struct rusage startUsage
, endUsage
;
106 const char* testCaseName
;
107 GString
* analysisModulesNames
;
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
117 registerProcessingText();
119 registerMatchingTCP();
120 registerMatchingBroadcast();
121 registerMatchingDistributor();
123 registerAnalysisCHull();
124 registerAnalysisLinReg();
125 registerAnalysisEval();
127 // Initialize data structures
128 syncState
= malloc(sizeof(SyncState
));
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
;
141 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "graphs-%d", getpid());
142 if (retval
> sizeof(graphsDir
) - 1)
144 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
146 optionSyncGraphs
.arg
= graphsDir
;
148 g_queue_push_head(&moduleOptions
, &optionSyncAnalysis
);
149 g_queue_push_head(&moduleOptions
, &optionSyncGraphs
);
150 g_queue_push_head(&moduleOptions
, &optionSyncStats
);
152 testCaseName
= processOptions(argc
, argv
);
154 g_string_free(analysisModulesNames
, TRUE
);
156 if (optionSyncStats
.present
)
158 syncState
->stats
= true;
159 gettimeofday(&startTime
, 0);
160 getrusage(RUSAGE_SELF
, &startUsage
);
164 syncState
->stats
= false;
165 id
= g_log_set_handler(NULL
, G_LOG_LEVEL_DEBUG
, nullLog
, NULL
);
168 if (optionSyncGraphs
.present
)
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
);
177 syncState
->graphsStream
= NULL
;
178 syncState
->graphsDir
= NULL
;
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
;
188 syncState
->matchingData
= NULL
;
189 result
= g_queue_find_custom(&matchingModules
, "TCP", &gcfCompareMatching
);
190 g_assert(result
!= NULL
);
191 syncState
->matchingModule
= (MatchingModule
*) result
->data
;
193 syncState
->analysisData
= NULL
;
194 result
= g_queue_find_custom(&analysisModules
, optionSyncAnalysis
.arg
,
195 &gcfCompareAnalysis
);
198 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
202 g_error("Analysis module '%s' not found", optionSyncAnalysis
.arg
);
205 // Initialize modules
206 syncState
->processingModule
->initProcessing(syncState
, testCaseName
);
207 syncState
->matchingModule
->initMatching(syncState
);
208 syncState
->analysisModule
->initAnalysis(syncState
);
211 syncState
->processingModule
->finalizeProcessing(syncState
);
214 if (syncState
->graphsStream
)
216 writeGraphsScript(syncState
);
218 if (fclose(syncState
->graphsStream
) != 0)
220 g_error(strerror(errno
));
225 if (syncState
->stats
)
227 printStats(syncState
);
230 // Destroy modules and clean up
231 syncState
->processingModule
->destroyProcessing(syncState
);
232 syncState
->matchingModule
->destroyMatching(syncState
);
233 syncState
->analysisModule
->destroyAnalysis(syncState
);
235 stats
= syncState
->stats
;
240 gettimeofday(&endTime
, 0);
241 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
243 timeDiff(&endTime
, &startTime
);
244 timeDiff(&endUsage
.ru_utime
, &startUsage
.ru_utime
);
245 timeDiff(&endUsage
.ru_stime
, &startUsage
.ru_stime
);
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
);
255 if (!optionSyncStats
.present
)
257 g_log_remove_handler(NULL
, id
);
265 * Read program arguments dans update ModuleOptions structures
268 * argc, argv: standard argument arrays
271 * Name of the test case file (first parameter)
273 const char* processOptions(const int argc
, char* const argv
[])
277 extern int optind
, opterr
, optopt
;
279 GString
* optionString
;
282 GHashTable
* shortIndex
;
284 longOptions
= g_array_sized_new(TRUE
, FALSE
, sizeof(struct option
),
285 g_queue_get_length(&moduleOptions
));
286 optionString
= g_string_new("");
287 longIndex
= g_queue_new();
288 shortIndex
= g_hash_table_new(&ghfCharHash
, &gefCharEqual
);
290 g_queue_foreach(&moduleOptions
, &gfAddModuleOption
, &(struct OptionsInfo
)
291 {longOptions
, optionString
, longIndex
, shortIndex
});
296 ModuleOption
* moduleOption
;
299 c
= getopt_long(argc
, argv
, optionString
->str
, (struct option
*)
300 longOptions
->data
, &optionIndex
);
302 if (longOption
>= 0 && longOption
< g_queue_get_length(longIndex
))
304 moduleOption
= g_queue_peek_nth(longIndex
, longOption
);
306 else if ((moduleOption
= g_hash_table_lookup(shortIndex
, &c
)) != NULL
)
320 g_error("Option parse error");
323 moduleOption
->present
= true;
325 if (moduleOption
->hasArg
== REQUIRED_ARG
)
327 moduleOption
->arg
= optarg
;
329 if (moduleOption
->hasArg
== OPTIONAL_ARG
&& optarg
)
331 moduleOption
->arg
= optarg
;
335 g_array_free(longOptions
, TRUE
);
336 g_string_free(optionString
, TRUE
);
337 g_queue_free(longIndex
);
338 g_hash_table_destroy(shortIndex
);
342 fprintf(stderr
, "Test file unspecified\n");
352 * Print information about program options and arguments.
355 * programName: name of the program, as contained in argv[0] for example
357 static void usage(const char* const programName
)
360 "%s [options] <test file>\n"
361 "Options:\n", programName
);
363 g_queue_foreach(&moduleOptions
, &gfPrintModuleOption
, NULL
);
368 * A GFunc for g_queue_foreach()
370 * Print analysis module names.
373 * data: ModuleOption*, option
376 static void gfPrintModuleOption(gpointer data
, gpointer user_data
)
378 ModuleOption
* option
= data
;
379 int width
= 0, sum
= 0;
380 const int colWidth
= 27;
384 if (option
->shortName
)
386 printf("-%c, %n", option
->shortName
, &width
);
390 printf("--%-s%n", option
->longName
, &width
);
393 if (option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
)
395 printf("=[..]%n", &width
);
399 if (option
->optionHelp
)
401 printf("%*s%s\n", colWidth
- sum
> 0 ? colWidth
- sum
: 0, "", option
->optionHelp
);
406 printf("\t%*s%s\n", colWidth
, "", option
->argHelp
);
409 if ((option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
) && option
->arg
)
411 printf("\t%*sDefault value: %s\n", colWidth
, "", option
->arg
);
417 * A Glib log function which does nothing.
419 static void nullLog(const gchar
*log_domain
, GLogLevelFlags log_level
, const
420 gchar
*message
, gpointer user_data
)
425 * A GFunc for g_queue_foreach()
428 * data: ModuleOption*, option
429 * user_data: struct OptionsInfo*, add option to this array of struct option
431 static void gfAddModuleOption(gpointer data
, gpointer user_data
)
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
,
443 const char* colons
[]= {
445 [OPTIONAL_ARG
]= "::",
449 newOption
.name
= option
->longName
;
450 newOption
.has_arg
= conversion
[option
->hasArg
];
451 newOption
.flag
= NULL
;
452 newOption
.val
= g_queue_get_length(optionsInfo
->longIndex
);
454 g_array_append_val(optionsInfo
->longOptions
, newOption
);
455 if (option
->shortName
)
457 g_string_append_c(optionsInfo
->optionString
, option
->shortName
);
458 g_string_append(optionsInfo
->optionString
, colons
[option
->hasArg
]);
460 g_hash_table_insert(optionsInfo
->shortIndex
, &option
->shortName
,
463 g_queue_push_tail(optionsInfo
->longIndex
, option
);
468 * A GHashFunc for g_hash_table_new()
471 * key char*, just one character
473 static guint
ghfCharHash(gconstpointer key
)
480 * A GEqualFunc for g_hash_table_new()
483 * a, b char*, just one character each
486 * TRUE if both values are equal
488 static gboolean
gefCharEqual(gconstpointer a
, gconstpointer b
)
490 if (*(char*) a
== *(char*) b
)