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 "sync_chain.h"
44 GString
* optionString
;
46 GHashTable
* shortIndex
;
50 const char* processOptions(const int argc
, char* const argv
[]);
51 static void usage(const char* const programName
);
52 static void gfPrintModuleOption(gpointer data
, gpointer user_data
);
53 static void nullLog(const gchar
*log_domain
, GLogLevelFlags log_level
, const
54 gchar
*message
, gpointer user_data
);
55 static void gfAddModuleOption(gpointer data
, gpointer user_data
);
56 static guint
ghfCharHash(gconstpointer key
);
57 static gboolean
gefCharEqual(gconstpointer a
, gconstpointer b
);
60 static ModuleOption optionSyncStats
= {
62 .longName
= "sync-stats",
64 .optionHelp
= "Print statistics and debug messages",
66 static char graphsDir
[20];
67 static ModuleOption optionSyncGraphs
= {
69 .longName
= "sync-graphs",
70 .hasArg
= OPTIONAL_ARG
,
71 .optionHelp
= "Output gnuplot graph showing synchronization points",
73 static ModuleOption optionSyncAnalysis
= {
75 .longName
= "sync-analysis",
76 .hasArg
= REQUIRED_ARG
,
77 .optionHelp
= "Specify which algorithm to use for event analysis",
82 * Implement a sync chain, it is mostly for unittest and it does not depend on
86 * argc, argv: standard argument arrays
89 * exit status from main() is always EXIT_SUCCESS
91 int main(const int argc
, char* const argv
[])
94 struct timeval startTime
, endTime
;
95 struct rusage startUsage
, endUsage
;
99 const char* testCaseName
;
100 GString
* analysisModulesNames
;
102 // Initialize data structures
103 syncState
= malloc(sizeof(SyncState
));
105 // Process command line arguments
106 g_assert(g_queue_get_length(&analysisModules
) > 0);
107 optionSyncAnalysis
.arg
= ((AnalysisModule
*)
108 g_queue_peek_head(&analysisModules
))->name
;
109 analysisModulesNames
= g_string_new("Available modules: ");
110 g_queue_foreach(&analysisModules
, &gfAppendAnalysisName
,
111 analysisModulesNames
);
112 // remove the last ", "
113 g_string_truncate(analysisModulesNames
, analysisModulesNames
->len
- 2);
114 optionSyncAnalysis
.argHelp
= analysisModulesNames
->str
;
116 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "graphs-%d", getpid());
117 if (retval
> sizeof(graphsDir
) - 1)
119 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
121 optionSyncGraphs
.arg
= graphsDir
;
123 g_queue_push_head(&moduleOptions
, &optionSyncAnalysis
);
124 g_queue_push_head(&moduleOptions
, &optionSyncGraphs
);
125 g_queue_push_head(&moduleOptions
, &optionSyncStats
);
127 testCaseName
= processOptions(argc
, argv
);
129 g_string_free(analysisModulesNames
, TRUE
);
131 if (optionSyncStats
.present
)
133 syncState
->stats
= true;
134 gettimeofday(&startTime
, 0);
135 getrusage(RUSAGE_SELF
, &startUsage
);
139 syncState
->stats
= false;
140 g_log_set_handler(NULL
, G_LOG_LEVEL_DEBUG
, nullLog
, NULL
);
143 if (optionSyncGraphs
.present
)
145 // Create the graph directory right away in case the module initialization
146 // functions have something to write in it.
147 syncState
->graphsDir
= optionSyncGraphs
.arg
;
148 syncState
->graphsStream
= createGraphsDir(syncState
->graphsDir
);
152 syncState
->graphsStream
= NULL
;
153 syncState
->graphsDir
= NULL
;
157 syncState
->processingData
= NULL
;
158 result
= g_queue_find_custom(&processingModules
, "text",
159 &gcfCompareProcessing
);
160 g_assert(result
!= NULL
);
161 syncState
->processingModule
= (ProcessingModule
*) result
->data
;
163 syncState
->matchingData
= NULL
;
164 result
= g_queue_find_custom(&matchingModules
, "TCP", &gcfCompareMatching
);
165 g_assert(result
!= NULL
);
166 syncState
->matchingModule
= (MatchingModule
*) result
->data
;
168 syncState
->analysisData
= NULL
;
169 result
= g_queue_find_custom(&analysisModules
, optionSyncAnalysis
.arg
,
170 &gcfCompareAnalysis
);
173 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
177 g_error("Analysis module '%s' not found", optionSyncAnalysis
.arg
);
180 // Initialize modules
181 syncState
->processingModule
->initProcessing(syncState
, testCaseName
);
182 syncState
->matchingModule
->initMatching(syncState
);
183 syncState
->analysisModule
->initAnalysis(syncState
);
186 syncState
->processingModule
->finalizeProcessing(syncState
);
189 if (syncState
->graphsStream
)
191 writeGraphsScript(syncState
);
193 if (fclose(syncState
->graphsStream
) != 0)
195 g_error(strerror(errno
));
200 if (syncState
->stats
)
202 printStats(syncState
);
205 // Destroy modules and clean up
206 syncState
->processingModule
->destroyProcessing(syncState
);
207 syncState
->matchingModule
->destroyMatching(syncState
);
208 syncState
->analysisModule
->destroyAnalysis(syncState
);
210 stats
= syncState
->stats
;
215 gettimeofday(&endTime
, 0);
216 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
218 timeDiff(&endTime
, &startTime
);
219 timeDiff(&endUsage
.ru_utime
, &startUsage
.ru_utime
);
220 timeDiff(&endUsage
.ru_stime
, &startUsage
.ru_stime
);
222 printf("Synchronization time:\n");
223 printf("\treal time: %ld.%06ld\n", endTime
.tv_sec
, endTime
.tv_usec
);
224 printf("\tuser time: %ld.%06ld\n", endUsage
.ru_utime
.tv_sec
,
225 endUsage
.ru_utime
.tv_usec
);
226 printf("\tsystem time: %ld.%06ld\n", endUsage
.ru_stime
.tv_sec
,
227 endUsage
.ru_stime
.tv_usec
);
235 * Read program arguments dans update ModuleOptions structures
238 * argc, argv: standard argument arrays
241 * Name of the test case file (first parameter)
243 const char* processOptions(const int argc
, char* const argv
[])
247 extern int optind
, opterr
, optopt
;
249 GString
* optionString
;
252 GHashTable
* shortIndex
;
254 longOptions
= g_array_sized_new(TRUE
, FALSE
, sizeof(struct option
),
255 g_queue_get_length(&moduleOptions
));
256 optionString
= g_string_new("");
257 longIndex
= g_queue_new();
258 shortIndex
= g_hash_table_new(&ghfCharHash
, &gefCharEqual
);
260 g_queue_foreach(&moduleOptions
, &gfAddModuleOption
, &(struct OptionsInfo
)
261 {longOptions
, optionString
, longIndex
, shortIndex
});
266 ModuleOption
* moduleOption
;
269 c
= getopt_long(argc
, argv
, optionString
->str
, (struct option
*)
270 longOptions
->data
, &optionIndex
);
272 if (longOption
>= 0 && longOption
< g_queue_get_length(longIndex
))
274 moduleOption
= g_queue_peek_nth(longIndex
, longOption
);
276 else if ((moduleOption
= g_hash_table_lookup(shortIndex
, &c
)) != NULL
)
290 g_error("Option parse error");
293 moduleOption
->present
= true;
295 if (moduleOption
->hasArg
== REQUIRED_ARG
)
297 moduleOption
->arg
= optarg
;
299 if (moduleOption
->hasArg
== OPTIONAL_ARG
&& optarg
)
301 moduleOption
->arg
= optarg
;
305 g_array_free(longOptions
, TRUE
);
306 g_string_free(optionString
, TRUE
);
307 g_queue_free(longIndex
);
308 g_hash_table_destroy(shortIndex
);
312 fprintf(stderr
, "Test file unspecified\n");
322 * Print information about program options and arguments.
325 * programName: name of the program, as contained in argv[0] for example
327 static void usage(const char* const programName
)
330 "%s [options] <test file>\n"
331 "Options:\n", programName
);
333 g_queue_foreach(&moduleOptions
, &gfPrintModuleOption
, NULL
);
338 * A GFunc for g_queue_foreach()
340 * Print analysis module names.
343 * data: ModuleOption*, option
346 static void gfPrintModuleOption(gpointer data
, gpointer user_data
)
348 ModuleOption
* option
= data
;
349 int width
= 0, sum
= 0;
350 const int colWidth
= 27;
354 if (option
->shortName
)
356 printf("-%c, %n", option
->shortName
, &width
);
360 printf("--%-s%n", option
->longName
, &width
);
363 if (option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
)
365 printf("=[..]%n", &width
);
369 if (option
->optionHelp
)
371 printf("%*s%s\n", colWidth
- sum
> 0 ? colWidth
- sum
: 0, "", option
->optionHelp
);
376 printf("\t%*s%s\n", colWidth
, "", option
->argHelp
);
379 if ((option
->hasArg
== REQUIRED_ARG
|| option
->hasArg
== OPTIONAL_ARG
) && option
->arg
)
381 printf("\t%*sDefault value: %s\n", colWidth
, "", option
->arg
);
387 * A Glib log function which does nothing.
389 static void nullLog(const gchar
*log_domain
, GLogLevelFlags log_level
, const
390 gchar
*message
, gpointer user_data
)
395 * A GFunc for g_queue_foreach()
398 * data: ModuleOption*, option
399 * user_data: struct OptionsInfo*, add option to this array of struct option
401 static void gfAddModuleOption(gpointer data
, gpointer user_data
)
403 ModuleOption
* option
= data
;
404 struct OptionsInfo
* optionsInfo
= user_data
;
405 struct option newOption
;
406 // "[mixing enumerations] can still be considered bad style even though it
407 // is not strictly illegal" c.faq 2.22
408 const int conversion
[]= {
409 [NO_ARG
]= no_argument
,
410 [OPTIONAL_ARG
]= optional_argument
,
411 [REQUIRED_ARG
]= required_argument
,
413 const char* colons
[]= {
415 [OPTIONAL_ARG
]= "::",
419 newOption
.name
= option
->longName
;
420 newOption
.has_arg
= conversion
[option
->hasArg
];
421 newOption
.flag
= NULL
;
422 newOption
.val
= g_queue_get_length(optionsInfo
->longIndex
);
424 g_array_append_val(optionsInfo
->longOptions
, newOption
);
425 if (option
->shortName
)
427 g_string_append_c(optionsInfo
->optionString
, option
->shortName
);
428 g_string_append(optionsInfo
->optionString
, colons
[option
->hasArg
]);
430 g_hash_table_insert(optionsInfo
->shortIndex
, &option
->shortName
,
433 g_queue_push_tail(optionsInfo
->longIndex
, option
);
438 * A GHashFunc for g_hash_table_new()
441 * key char*, just one character
443 static guint
ghfCharHash(gconstpointer key
)
450 * A GEqualFunc for g_hash_table_new()
453 * a, b char*, just one character each
456 * TRUE if both values are equal
458 static gboolean
gefCharEqual(gconstpointer a
, gconstpointer b
)
460 if (*(char*) a
== *(char*) b
)