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,
27 #include <sys/resource.h>
29 #include <sys/types.h>
33 #include <lttv/module.h>
34 #include <lttv/option.h>
36 #include "sync_chain.h"
40 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
45 static void destroy();
47 static void gfAppendAnalysisName(gpointer data
, gpointer user_data
);
48 static void gfAddModuleOption(gpointer data
, gpointer user_data
);
49 static void gfRemoveModuleOption(gpointer data
, gpointer user_data
);
51 GQueue processingModules
= G_QUEUE_INIT
;
52 GQueue matchingModules
= G_QUEUE_INIT
;
53 GQueue analysisModules
= G_QUEUE_INIT
;
54 GQueue moduleOptions
= G_QUEUE_INIT
;
56 static char* argHelpNone
= "none";
57 static ModuleOption optionSync
= {
61 .optionHelp
= "synchronize the time between the traces",
63 static char graphsDir
[20];
64 static ModuleOption optionSyncStats
= {
65 .longName
= "sync-stats",
68 .optionHelp
= "print statistics about the time synchronization",
70 static ModuleOption optionSyncNull
= {
71 .longName
= "sync-null",
74 .optionHelp
= "read the events but do not perform any processing",
76 static GString
* analysisModulesNames
;
77 static ModuleOption optionSyncAnalysis
= {
78 .longName
= "sync-analysis",
79 .hasArg
= REQUIRED_ARG
,
80 .optionHelp
= "specify the algorithm to use for event analysis",
82 static ModuleOption optionSyncGraphs
= {
83 .longName
= "sync-graphs",
86 .optionHelp
= "output gnuplot graph showing synchronization points",
88 static ModuleOption optionSyncGraphsDir
= {
89 .longName
= "sync-graphs-dir",
90 .hasArg
= REQUIRED_ARG
,
91 .optionHelp
= "specify the directory where to store the graphs",
95 * Module init function
97 * This function is declared to be the module initialization function. Event
98 * modules are registered with a "constructor (102)" attribute except one in
99 * each class (processing, matching, analysis) which is chosen to be the
100 * default and which is registered with a "constructor (101)" attribute.
101 * Constructors with no priority are called after constructors with
102 * priorities. The result is that the list of event modules is known when this
103 * function is executed.
109 g_debug("\t\t\tXXXX sync init\n");
111 g_assert(g_queue_get_length(&analysisModules
) > 0);
112 optionSyncAnalysis
.arg
= ((AnalysisModule
*)
113 g_queue_peek_head(&analysisModules
))->name
;
114 analysisModulesNames
= g_string_new("");
115 g_queue_foreach(&analysisModules
, &gfAppendAnalysisName
,
116 analysisModulesNames
);
117 // remove the last ", "
118 g_string_truncate(analysisModulesNames
, analysisModulesNames
->len
- 2);
119 optionSyncAnalysis
.argHelp
= analysisModulesNames
->str
;
121 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "graphs-%d", getpid());
122 if (retval
> sizeof(graphsDir
) - 1)
124 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
126 optionSyncGraphsDir
.arg
= graphsDir
;
127 optionSyncGraphsDir
.argHelp
= graphsDir
;
129 g_queue_push_head(&moduleOptions
, &optionSyncGraphsDir
);
130 g_queue_push_head(&moduleOptions
, &optionSyncGraphs
);
131 g_queue_push_head(&moduleOptions
, &optionSyncAnalysis
);
132 g_queue_push_head(&moduleOptions
, &optionSyncNull
);
133 g_queue_push_head(&moduleOptions
, &optionSyncStats
);
134 g_queue_push_head(&moduleOptions
, &optionSync
);
136 g_queue_foreach(&moduleOptions
, &gfAddModuleOption
, NULL
);
142 * Module unload function
144 static void destroy()
146 g_debug("\t\t\tXXXX sync destroy\n");
148 g_queue_foreach(&moduleOptions
, &gfRemoveModuleOption
, NULL
);
149 g_string_free(analysisModulesNames
, TRUE
);
151 g_queue_clear(&processingModules
);
152 g_queue_clear(&matchingModules
);
153 g_queue_clear(&analysisModules
);
154 g_queue_clear(&moduleOptions
);
159 * Calculate a traceset's drift and offset values based on network events
161 * The individual correction factors are written out to each trace.
164 * traceSetContext: traceset
166 void syncTraceset(LttvTracesetContext
* const traceSetContext
)
168 SyncState
* syncState
;
169 struct timeval startTime
, endTime
;
170 struct rusage startUsage
, endUsage
;
175 if (!optionSync
.present
)
177 g_debug("Not synchronizing traceset because option is disabled");
181 if (optionSyncStats
.present
)
183 gettimeofday(&startTime
, 0);
184 getrusage(RUSAGE_SELF
, &startUsage
);
187 // Initialize data structures
188 syncState
= malloc(sizeof(SyncState
));
189 syncState
->traceNb
= lttv_traceset_number(traceSetContext
->ts
);
191 if (optionSyncStats
.present
)
193 syncState
->stats
= true;
197 syncState
->stats
= false;
200 if (optionSyncGraphs
.present
)
205 // Create the graph directory right away in case the module initialization
206 // functions have something to write in it.
207 syncState
->graphsDir
= optionSyncGraphsDir
.arg
;
208 cwd
= changeToGraphDir(optionSyncGraphsDir
.arg
);
210 if ((graphsFp
= open("graphs.gnu", O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
|
211 S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IWGRP
| S_IXGRP
| S_IROTH
212 | S_IWOTH
| S_IXOTH
)) == -1)
214 g_error(strerror(errno
));
216 if ((syncState
->graphsStream
= fdopen(graphsFp
, "w")) == NULL
)
218 g_error(strerror(errno
));
221 fprintf(syncState
->graphsStream
,
222 "#!/usr/bin/gnuplot\n\n"
223 "set terminal postscript eps color size 8in,6in\n");
228 g_error(strerror(errno
));
234 syncState
->graphsStream
= NULL
;
235 syncState
->graphsDir
= NULL
;
238 // Identify and initialize modules
239 syncState
->processingData
= NULL
;
240 if (optionSyncNull
.present
)
242 result
= g_queue_find_custom(&processingModules
, "LTTV-null",
243 &gcfCompareProcessing
);
247 result
= g_queue_find_custom(&processingModules
, "LTTV-standard",
248 &gcfCompareProcessing
);
250 g_assert(result
!= NULL
);
251 syncState
->processingModule
= (ProcessingModule
*) result
->data
;
253 syncState
->matchingData
= NULL
;
254 result
= g_queue_find_custom(&matchingModules
, "TCP", &gcfCompareMatching
);
255 g_assert(result
!= NULL
);
256 syncState
->matchingModule
= (MatchingModule
*) result
->data
;
258 syncState
->analysisData
= NULL
;
259 result
= g_queue_find_custom(&analysisModules
, optionSyncAnalysis
.arg
,
260 &gcfCompareAnalysis
);
263 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
267 g_error("Analysis module '%s' not found", optionSyncAnalysis
.arg
);
270 if (!optionSyncNull
.present
)
272 syncState
->analysisModule
->initAnalysis(syncState
);
273 syncState
->matchingModule
->initMatching(syncState
);
275 syncState
->processingModule
->initProcessing(syncState
, traceSetContext
);
278 lttv_process_traceset_seek_time(traceSetContext
, ltt_time_zero
);
279 lttv_process_traceset_middle(traceSetContext
, ltt_time_infinite
,
281 lttv_process_traceset_seek_time(traceSetContext
, ltt_time_zero
);
283 syncState
->processingModule
->finalizeProcessing(syncState
);
286 if (optionSyncGraphs
.present
)
288 writeGraphsScript(syncState
);
290 if (fclose(syncState
->graphsStream
) != 0)
292 g_error(strerror(errno
));
296 if (syncState
->processingModule
->printProcessingStats
!= NULL
)
298 syncState
->processingModule
->printProcessingStats(syncState
);
300 if (syncState
->matchingModule
->printMatchingStats
!= NULL
)
302 syncState
->matchingModule
->printMatchingStats(syncState
);
304 if (syncState
->analysisModule
->printAnalysisStats
!= NULL
)
306 syncState
->analysisModule
->printAnalysisStats(syncState
);
309 if (optionSyncStats
.present
)
311 printf("Resulting synchronization factors:\n");
312 for (i
= 0; i
< syncState
->traceNb
; i
++)
316 t
= traceSetContext
->traces
[i
]->t
;
318 printf("\ttrace %u drift= %g offset= %g (%f) start time= %ld.%09ld\n",
319 i
, t
->drift
, t
->offset
, (double) tsc_to_uint64(t
->freq_scale
,
320 t
->start_freq
, t
->offset
) / NANOSECONDS_PER_SECOND
,
321 t
->start_time_from_tsc
.tv_sec
,
322 t
->start_time_from_tsc
.tv_nsec
);
326 syncState
->processingModule
->destroyProcessing(syncState
);
327 if (syncState
->matchingModule
!= NULL
)
329 syncState
->matchingModule
->destroyMatching(syncState
);
331 if (syncState
->analysisModule
!= NULL
)
333 syncState
->analysisModule
->destroyAnalysis(syncState
);
338 if (optionSyncStats
.present
)
340 gettimeofday(&endTime
, 0);
341 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
343 timeDiff(&endTime
, &startTime
);
344 timeDiff(&endUsage
.ru_utime
, &startUsage
.ru_utime
);
345 timeDiff(&endUsage
.ru_stime
, &startUsage
.ru_stime
);
347 printf("Synchronization time:\n");
348 printf("\treal time: %ld.%06ld\n", endTime
.tv_sec
, endTime
.tv_usec
);
349 printf("\tuser time: %ld.%06ld\n", endUsage
.ru_utime
.tv_sec
,
350 endUsage
.ru_utime
.tv_usec
);
351 printf("\tsystem time: %ld.%06ld\n", endUsage
.ru_stime
.tv_sec
,
352 endUsage
.ru_stime
.tv_usec
);
358 * Calculate the elapsed time between two timeval values
361 * end: end time, result is also stored in this structure
364 void timeDiff(struct timeval
* const end
, const struct timeval
* const start
)
366 if (end
->tv_usec
>= start
->tv_usec
)
368 end
->tv_sec
-= start
->tv_sec
;
369 end
->tv_usec
-= start
->tv_usec
;
373 end
->tv_sec
= end
->tv_sec
- start
->tv_sec
- 1;
374 end
->tv_usec
= end
->tv_usec
- start
->tv_usec
+ 1e6
;
380 * A GCompareFunc for g_slist_find_custom()
383 * a: ProcessingModule*, element's data
384 * b: char*, user data to compare against
387 * 0 if the processing module a's name is b
389 gint
gcfCompareProcessing(gconstpointer a
, gconstpointer b
)
391 const ProcessingModule
* processingModule
;
394 processingModule
= (const ProcessingModule
*) a
;
395 name
= (const char*) b
;
397 return strncmp(processingModule
->name
, name
,
398 strlen(processingModule
->name
) + 1);
403 * A GCompareFunc for g_slist_find_custom()
406 * a: MatchingModule*, element's data
407 * b: char*, user data to compare against
410 * 0 if the matching module a's name is b
412 gint
gcfCompareMatching(gconstpointer a
, gconstpointer b
)
414 const MatchingModule
* matchingModule
;
417 matchingModule
= (const MatchingModule
*) a
;
418 name
= (const char*) b
;
420 return strncmp(matchingModule
->name
, name
, strlen(matchingModule
->name
) +
426 * A GCompareFunc for g_slist_find_custom()
429 * a: AnalysisModule*, element's data
430 * b: char*, user data to compare against
433 * 0 if the analysis module a's name is b
435 gint
gcfCompareAnalysis(gconstpointer a
, gconstpointer b
)
437 const AnalysisModule
* analysisModule
;
440 analysisModule
= (const AnalysisModule
*) a
;
441 name
= (const char*) b
;
443 return strncmp(analysisModule
->name
, name
, strlen(analysisModule
->name
) +
449 * A GFunc for g_queue_foreach()
451 * Concatenate analysis module names.
454 * data: AnalysisModule*
455 * user_data: GString*, concatenated names
457 static void gfAppendAnalysisName(gpointer data
, gpointer user_data
)
459 g_string_append((GString
*) user_data
, ((AnalysisModule
*) data
)->name
);
460 g_string_append((GString
*) user_data
, ", ");
465 * Change to the directory used to hold graphs. Create it if necessary.
468 * graph: name of directory
471 * The current working directory before the execution of the function. The
472 * string must be free'd by the caller.
474 char* changeToGraphDir(const char* const graphs
)
479 cwd
= getcwd(NULL
, 0);
482 g_error(strerror(errno
));
484 while ((retval
= chdir(graphs
)) != 0)
488 retval
= mkdir(graphs
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
|
489 S_IWGRP
| S_IXGRP
| S_IROTH
| S_IWOTH
| S_IXOTH
);
492 g_error(strerror(errno
));
497 g_error(strerror(errno
));
506 * A GFunc for g_queue_foreach()
509 * data: ModuleOption*
512 static void gfAddModuleOption(gpointer data
, gpointer user_data
)
514 ModuleOption
* option
;
515 LttvOptionType conversion
[]= {
516 [NO_ARG
]= LTTV_OPT_NONE
,
517 [REQUIRED_ARG
]= LTTV_OPT_STRING
,
520 g_assert_cmpuint(sizeof(conversion
) / sizeof(*conversion
), ==,
522 option
= (ModuleOption
*) data
;
523 lttv_option_add(option
->longName
, '\0', option
->optionHelp
,
524 option
->argHelp
? option
->argHelp
: argHelpNone
,
525 conversion
[option
->hasArg
], &option
->arg
, NULL
, NULL
);
530 * A GFunc for g_queue_foreach()
533 * data: ModuleOption*
536 static void gfRemoveModuleOption(gpointer data
, gpointer user_data
)
538 lttv_option_remove(((ModuleOption
*) data
)->longName
);
542 LTTV_MODULE("sync", "Synchronize traces", \
543 "Synchronizes a traceset based on the correspondance of network events", \
544 init
, destroy
, "option")