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
;
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 factors
= syncState
->processingModule
->finalizeProcessing(syncState
);
212 g_array_free(factors
, TRUE
);
215 if (syncState
->graphsStream
)
217 writeGraphsScript(syncState
);
219 if (fclose(syncState
->graphsStream
) != 0)
221 g_error(strerror(errno
));
226 if (syncState
->stats
)
228 printStats(syncState
);
231 // Destroy modules and clean up
232 syncState
->processingModule
->destroyProcessing(syncState
);
233 syncState
->matchingModule
->destroyMatching(syncState
);
234 syncState
->analysisModule
->destroyAnalysis(syncState
);
236 stats
= syncState
->stats
;
241 gettimeofday(&endTime
, 0);
242 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
244 timeDiff(&endTime
, &startTime
);
245 timeDiff(&endUsage
.ru_utime
, &startUsage
.ru_utime
);
246 timeDiff(&endUsage
.ru_stime
, &startUsage
.ru_stime
);
248 printf("Synchronization time:\n");
249 printf("\treal time: %ld.%06ld\n", endTime
.tv_sec
, endTime
.tv_usec
);
250 printf("\tuser time: %ld.%06ld\n", endUsage
.ru_utime
.tv_sec
,
251 endUsage
.ru_utime
.tv_usec
);
252 printf("\tsystem time: %ld.%06ld\n", endUsage
.ru_stime
.tv_sec
,
253 endUsage
.ru_stime
.tv_usec
);
256 if (!optionSyncStats
.present
)
258 g_log_remove_handler(NULL
, id
);
266 * Read program arguments dans update ModuleOptions structures
269 * argc, argv: standard argument arrays
272 * Name of the test case file (first parameter)
274 const char* processOptions(const int argc
, char* const argv
[])
278 extern int optind
, opterr
, optopt
;
280 GString
* optionString
;
283 GHashTable
* shortIndex
;
285 longOptions
= g_array_sized_new(TRUE
, FALSE
, sizeof(struct option
),
286 g_queue_get_length(&moduleOptions
));
287 optionString
= g_string_new("");
288 longIndex
= g_queue_new();
289 shortIndex
= g_hash_table_new(&ghfCharHash
, &gefCharEqual
);
291 g_queue_foreach(&moduleOptions
, &gfAddModuleOption
, &(struct OptionsInfo
)
292 {longOptions
, optionString
, longIndex
, shortIndex
});
297 ModuleOption
* moduleOption
;
300 c
= getopt_long(argc
, argv
, optionString
->str
, (struct option
*)
301 longOptions
->data
, &optionIndex
);
303 if (longOption
>= 0 && longOption
< g_queue_get_length(longIndex
))
305 moduleOption
= g_queue_peek_nth(longIndex
, longOption
);
307 else if ((moduleOption
= g_hash_table_lookup(shortIndex
, &c
)) != NULL
)
321 g_error("Option parse error");
324 moduleOption
->present
= true;
326 if (moduleOption
->hasArg
== REQUIRED_ARG
)
328 moduleOption
->arg
= optarg
;
330 if (moduleOption
->hasArg
== OPTIONAL_ARG
&& optarg
)
332 moduleOption
->arg
= optarg
;
336 g_array_free(longOptions
, TRUE
);
337 g_string_free(optionString
, TRUE
);
338 g_queue_free(longIndex
);
339 g_hash_table_destroy(shortIndex
);
343 fprintf(stderr
, "Test file unspecified\n");
353 * Print information about program options and arguments.
356 * programName: name of the program, as contained in argv[0] for example
358 static void usage(const char* const programName
)
361 "%s [options] <test file>\n"
362 "Options:\n", programName
);
364 g_queue_foreach(&moduleOptions
, &gfPrintModuleOption
, NULL
);
369 * A GFunc for g_queue_foreach()
371 * Print analysis module names.
374 * data: ModuleOption*, option
377 static void gfPrintModuleOption(gpointer data
, gpointer user_data
)
379 ModuleOption
* option
= data
;
380 int width
= 0, sum
= 0;
381 const int colWidth
= 27;
385 if (option
->shortName
)
387 printf("-%c, %n", option
->shortName
, &width
);
391 printf("--%-s%n", option
->longName
, &width
);
394 if (option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
)
396 printf("=[..]%n", &width
);
400 if (option
->optionHelp
)
402 printf("%*s%s\n", colWidth
- sum
> 0 ? colWidth
- sum
: 0, "", option
->optionHelp
);
407 printf("\t%*s%s\n", colWidth
, "", option
->argHelp
);
410 if ((option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
) && option
->arg
)
412 printf("\t%*sDefault value: %s\n", colWidth
, "", option
->arg
);
418 * A Glib log function which does nothing.
420 static void nullLog(const gchar
*log_domain
, GLogLevelFlags log_level
, const
421 gchar
*message
, gpointer user_data
)
426 * A GFunc for g_queue_foreach()
429 * data: ModuleOption*, option
430 * user_data: struct OptionsInfo*, add option to this array of struct option
432 static void gfAddModuleOption(gpointer data
, gpointer user_data
)
434 ModuleOption
* option
= data
;
435 struct OptionsInfo
* optionsInfo
= user_data
;
436 struct option newOption
;
437 // "[mixing enumerations] can still be considered bad style even though it
438 // is not strictly illegal" c.faq 2.22
439 const int conversion
[]= {
440 [NO_ARG
]= no_argument
,
441 [OPTIONAL_ARG
]= optional_argument
,
442 [REQUIRED_ARG
]= required_argument
,
444 const char* colons
[]= {
446 [OPTIONAL_ARG
]= "::",
450 newOption
.name
= option
->longName
;
451 newOption
.has_arg
= conversion
[option
->hasArg
];
452 newOption
.flag
= NULL
;
453 newOption
.val
= g_queue_get_length(optionsInfo
->longIndex
);
455 g_array_append_val(optionsInfo
->longOptions
, newOption
);
456 if (option
->shortName
)
458 g_string_append_c(optionsInfo
->optionString
, option
->shortName
);
459 g_string_append(optionsInfo
->optionString
, colons
[option
->hasArg
]);
461 g_hash_table_insert(optionsInfo
->shortIndex
, &option
->shortName
,
464 g_queue_push_tail(optionsInfo
->longIndex
, option
);
469 * A GHashFunc for g_hash_table_new()
472 * key char*, just one character
474 static guint
ghfCharHash(gconstpointer key
)
481 * A GEqualFunc for g_hash_table_new()
484 * a, b char*, just one character each
487 * TRUE if both values are equal
489 static gboolean
gefCharEqual(gconstpointer a
, gconstpointer b
)
491 if (*(char*) a
== *(char*) b
)