Show the latency histograms by IP address rather than by trace
[lttv.git] / lttv / lttv / sync / sync_chain_lttv.c
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 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/resource.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #include <lttv/module.h>
34 #include <lttv/option.h>
35
36 #include "sync_chain.h"
37
38
39 #ifndef g_info
40 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
41 #endif
42
43
44 static void init();
45 static void destroy();
46
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);
50
51 GQueue processingModules= G_QUEUE_INIT;
52 GQueue matchingModules= G_QUEUE_INIT;
53 GQueue analysisModules= G_QUEUE_INIT;
54 GQueue moduleOptions= G_QUEUE_INIT;
55
56 static char* argHelpNone= "none";
57 static ModuleOption optionSync= {
58 .longName= "sync",
59 .hasArg= NO_ARG,
60 {.present= false},
61 .optionHelp= "synchronize the time between the traces",
62 };
63 static char graphsDir[20];
64 static ModuleOption optionSyncStats= {
65 .longName= "sync-stats",
66 .hasArg= NO_ARG,
67 {.present= false},
68 .optionHelp= "print statistics about the time synchronization",
69 };
70 static ModuleOption optionSyncNull= {
71 .longName= "sync-null",
72 .hasArg= NO_ARG,
73 {.present= false},
74 .optionHelp= "read the events but do not perform any processing",
75 };
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",
81 };
82 static ModuleOption optionSyncGraphs= {
83 .longName= "sync-graphs",
84 .hasArg= NO_ARG,
85 {.present= false},
86 .optionHelp= "output gnuplot graph showing synchronization points",
87 };
88 static ModuleOption optionSyncGraphsDir= {
89 .longName= "sync-graphs-dir",
90 .hasArg= REQUIRED_ARG,
91 .optionHelp= "specify the directory where to store the graphs",
92 };
93
94 /*
95 * Module init function
96 *
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.
104 */
105 static void init()
106 {
107 int retval;
108
109 g_debug("\t\t\tXXXX sync init\n");
110
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;
120
121 retval= snprintf(graphsDir, sizeof(graphsDir), "graphs-%d", getpid());
122 if (retval > sizeof(graphsDir) - 1)
123 {
124 graphsDir[sizeof(graphsDir) - 1]= '\0';
125 }
126 optionSyncGraphsDir.arg= graphsDir;
127 optionSyncGraphsDir.argHelp= graphsDir;
128
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);
135
136 g_queue_foreach(&moduleOptions, &gfAddModuleOption, NULL);
137
138 }
139
140
141 /*
142 * Module unload function
143 */
144 static void destroy()
145 {
146 g_debug("\t\t\tXXXX sync destroy\n");
147
148 g_queue_foreach(&moduleOptions, &gfRemoveModuleOption, NULL);
149 g_string_free(analysisModulesNames, TRUE);
150
151 g_queue_clear(&processingModules);
152 g_queue_clear(&matchingModules);
153 g_queue_clear(&analysisModules);
154 g_queue_clear(&moduleOptions);
155 }
156
157
158 /*
159 * Calculate a traceset's drift and offset values based on network events
160 *
161 * The individual correction factors are written out to each trace.
162 *
163 * Args:
164 * traceSetContext: traceset
165 */
166 void syncTraceset(LttvTracesetContext* const traceSetContext)
167 {
168 SyncState* syncState;
169 struct timeval startTime, endTime;
170 struct rusage startUsage, endUsage;
171 GList* result;
172 unsigned int i, j;
173 int retval;
174
175 if (!optionSync.present)
176 {
177 g_debug("Not synchronizing traceset because option is disabled");
178 return;
179 }
180
181 if (optionSyncStats.present)
182 {
183 gettimeofday(&startTime, 0);
184 getrusage(RUSAGE_SELF, &startUsage);
185 }
186
187 // Initialize data structures
188 syncState= malloc(sizeof(SyncState));
189 syncState->traceNb= lttv_traceset_number(traceSetContext->ts);
190
191 if (optionSyncStats.present)
192 {
193 syncState->stats= true;
194 }
195 else
196 {
197 syncState->stats= false;
198 }
199
200 if (optionSyncGraphs.present)
201 {
202 char* cwd;
203 int graphsFp;
204
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);
209
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)
213 {
214 g_error(strerror(errno));
215 }
216 if ((syncState->graphsStream= fdopen(graphsFp, "w")) == NULL)
217 {
218 g_error(strerror(errno));
219 }
220
221 fprintf(syncState->graphsStream,
222 "#!/usr/bin/gnuplot\n\n"
223 "set terminal postscript eps color size 8in,6in\n");
224
225 retval= chdir(cwd);
226 if (retval == -1)
227 {
228 g_error(strerror(errno));
229 }
230 free(cwd);
231 }
232 else
233 {
234 syncState->graphsStream= NULL;
235 syncState->graphsDir= NULL;
236 }
237
238 // Identify and initialize modules
239 syncState->processingData= NULL;
240 if (optionSyncNull.present)
241 {
242 result= g_queue_find_custom(&processingModules, "LTTV-null",
243 &gcfCompareProcessing);
244 }
245 else
246 {
247 result= g_queue_find_custom(&processingModules, "LTTV-standard",
248 &gcfCompareProcessing);
249 }
250 g_assert(result != NULL);
251 syncState->processingModule= (ProcessingModule*) result->data;
252
253 syncState->matchingData= NULL;
254 result= g_queue_find_custom(&matchingModules, "TCP", &gcfCompareMatching);
255 g_assert(result != NULL);
256 syncState->matchingModule= (MatchingModule*) result->data;
257
258 syncState->analysisData= NULL;
259 result= g_queue_find_custom(&analysisModules, optionSyncAnalysis.arg,
260 &gcfCompareAnalysis);
261 if (result != NULL)
262 {
263 syncState->analysisModule= (AnalysisModule*) result->data;
264 }
265 else
266 {
267 g_error("Analysis module '%s' not found", optionSyncAnalysis.arg);
268 }
269
270 if (!optionSyncNull.present)
271 {
272 syncState->analysisModule->initAnalysis(syncState);
273 syncState->matchingModule->initMatching(syncState);
274 }
275 syncState->processingModule->initProcessing(syncState, traceSetContext);
276
277 // Process traceset
278 lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero);
279 lttv_process_traceset_middle(traceSetContext, ltt_time_infinite,
280 G_MAXULONG, NULL);
281 lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero);
282
283 syncState->processingModule->finalizeProcessing(syncState);
284
285 // Write graphs file
286 if (optionSyncGraphs.present)
287 {
288 // Cover the upper triangular matrix, i is the reference node.
289 for (i= 0; i < syncState->traceNb; i++)
290 {
291 for (j= i + 1; j < syncState->traceNb; j++)
292 {
293 long pos1, pos2, trunc;
294
295 fprintf(syncState->graphsStream,
296 "\nreset\n"
297 "set output \"%03d-%03d.eps\"\n"
298 "plot \\\n", i, j);
299
300 if (syncState->processingModule->writeProcessingGraphsPlots)
301 {
302 syncState->processingModule->writeProcessingGraphsPlots(syncState,
303 i, j);
304 }
305 if (syncState->matchingModule->writeMatchingGraphsPlots)
306 {
307 syncState->matchingModule->writeMatchingGraphsPlots(syncState,
308 i, j);
309 }
310 if (syncState->analysisModule->writeAnalysisGraphsPlots)
311 {
312 syncState->analysisModule->writeAnalysisGraphsPlots(syncState,
313 i, j);
314 }
315
316 fflush(syncState->graphsStream);
317 pos2= ftell(syncState->graphsStream);
318 if (pos1 != pos2)
319 {
320 // Remove the ", \\\n" from the last graph plot line
321 trunc= pos2 - 4;
322 }
323 else
324 {
325 // Remove the "plot \\\n" line to avoid creating an invalid
326 // gnuplot script
327 trunc= pos2 - 7;
328 }
329
330 if (ftruncate(fileno(syncState->graphsStream), trunc) == -1)
331 {
332 g_error(strerror(errno));
333 }
334 if (fseek(syncState->graphsStream, 0, SEEK_END) == -1)
335 {
336 g_error(strerror(errno));
337 }
338
339 fprintf(syncState->graphsStream,
340 "\nset output \"%03d-%03d.eps\"\n"
341 "set title \"\"\n", i, j);
342
343 if (syncState->processingModule->writeProcessingGraphsOptions)
344 {
345 syncState->processingModule->writeProcessingGraphsOptions(syncState,
346 i, j);
347 }
348 if (syncState->matchingModule->writeMatchingGraphsOptions)
349 {
350 syncState->matchingModule->writeMatchingGraphsOptions(syncState,
351 i, j);
352 }
353 if (syncState->analysisModule->writeAnalysisGraphsOptions)
354 {
355 syncState->analysisModule->writeAnalysisGraphsOptions(syncState,
356 i, j);
357 }
358
359 if (pos1 != pos2)
360 {
361 fprintf(syncState->graphsStream, "replot\n");
362 }
363 }
364 }
365
366 if (fclose(syncState->graphsStream) != 0)
367 {
368 g_error(strerror(errno));
369 }
370 }
371
372 if (syncState->processingModule->printProcessingStats != NULL)
373 {
374 syncState->processingModule->printProcessingStats(syncState);
375 }
376 if (syncState->matchingModule->printMatchingStats != NULL)
377 {
378 syncState->matchingModule->printMatchingStats(syncState);
379 }
380 if (syncState->analysisModule->printAnalysisStats != NULL)
381 {
382 syncState->analysisModule->printAnalysisStats(syncState);
383 }
384
385 if (optionSyncStats.present)
386 {
387 printf("Resulting synchronization factors:\n");
388 for (i= 0; i < syncState->traceNb; i++)
389 {
390 LttTrace* t;
391
392 t= traceSetContext->traces[i]->t;
393
394 printf("\ttrace %u drift= %g offset= %g (%f) start time= %ld.%09ld\n",
395 i, t->drift, t->offset, (double) tsc_to_uint64(t->freq_scale,
396 t->start_freq, t->offset) / NANOSECONDS_PER_SECOND,
397 t->start_time_from_tsc.tv_sec,
398 t->start_time_from_tsc.tv_nsec);
399 }
400 }
401
402 syncState->processingModule->destroyProcessing(syncState);
403 if (syncState->matchingModule != NULL)
404 {
405 syncState->matchingModule->destroyMatching(syncState);
406 }
407 if (syncState->analysisModule != NULL)
408 {
409 syncState->analysisModule->destroyAnalysis(syncState);
410 }
411
412 free(syncState);
413
414 if (optionSyncStats.present)
415 {
416 gettimeofday(&endTime, 0);
417 retval= getrusage(RUSAGE_SELF, &endUsage);
418
419 timeDiff(&endTime, &startTime);
420 timeDiff(&endUsage.ru_utime, &startUsage.ru_utime);
421 timeDiff(&endUsage.ru_stime, &startUsage.ru_stime);
422
423 printf("Synchronization time:\n");
424 printf("\treal time: %ld.%06ld\n", endTime.tv_sec, endTime.tv_usec);
425 printf("\tuser time: %ld.%06ld\n", endUsage.ru_utime.tv_sec,
426 endUsage.ru_utime.tv_usec);
427 printf("\tsystem time: %ld.%06ld\n", endUsage.ru_stime.tv_sec,
428 endUsage.ru_stime.tv_usec);
429 }
430 }
431
432
433 /*
434 * Calculate the elapsed time between two timeval values
435 *
436 * Args:
437 * end: end time, result is also stored in this structure
438 * start: start time
439 */
440 void timeDiff(struct timeval* const end, const struct timeval* const start)
441 {
442 if (end->tv_usec >= start->tv_usec)
443 {
444 end->tv_sec-= start->tv_sec;
445 end->tv_usec-= start->tv_usec;
446 }
447 else
448 {
449 end->tv_sec= end->tv_sec - start->tv_sec - 1;
450 end->tv_usec= end->tv_usec - start->tv_usec + 1e6;
451 }
452 }
453
454
455 /*
456 * A GCompareFunc for g_slist_find_custom()
457 *
458 * Args:
459 * a: ProcessingModule*, element's data
460 * b: char*, user data to compare against
461 *
462 * Returns:
463 * 0 if the processing module a's name is b
464 */
465 gint gcfCompareProcessing(gconstpointer a, gconstpointer b)
466 {
467 const ProcessingModule* processingModule;
468 const char* name;
469
470 processingModule= (const ProcessingModule*) a;
471 name= (const char*) b;
472
473 return strncmp(processingModule->name, name,
474 strlen(processingModule->name) + 1);
475 }
476
477
478 /*
479 * A GCompareFunc for g_slist_find_custom()
480 *
481 * Args:
482 * a: MatchingModule*, element's data
483 * b: char*, user data to compare against
484 *
485 * Returns:
486 * 0 if the matching module a's name is b
487 */
488 gint gcfCompareMatching(gconstpointer a, gconstpointer b)
489 {
490 const MatchingModule* matchingModule;
491 const char* name;
492
493 matchingModule= (const MatchingModule*) a;
494 name= (const char*) b;
495
496 return strncmp(matchingModule->name, name, strlen(matchingModule->name) +
497 1);
498 }
499
500
501 /*
502 * A GCompareFunc for g_slist_find_custom()
503 *
504 * Args:
505 * a: AnalysisModule*, element's data
506 * b: char*, user data to compare against
507 *
508 * Returns:
509 * 0 if the analysis module a's name is b
510 */
511 gint gcfCompareAnalysis(gconstpointer a, gconstpointer b)
512 {
513 const AnalysisModule* analysisModule;
514 const char* name;
515
516 analysisModule= (const AnalysisModule*) a;
517 name= (const char*) b;
518
519 return strncmp(analysisModule->name, name, strlen(analysisModule->name) +
520 1);
521 }
522
523
524 /*
525 * A GFunc for g_queue_foreach()
526 *
527 * Concatenate analysis module names.
528 *
529 * Args:
530 * data: AnalysisModule*
531 * user_data: GString*, concatenated names
532 */
533 static void gfAppendAnalysisName(gpointer data, gpointer user_data)
534 {
535 g_string_append((GString*) user_data, ((AnalysisModule*) data)->name);
536 g_string_append((GString*) user_data, ", ");
537 }
538
539
540 /*
541 * Change to the directory used to hold graphs. Create it if necessary.
542 *
543 * Args:
544 * graph: name of directory
545 *
546 * Returns:
547 * The current working directory before the execution of the function. The
548 * string must be free'd by the caller.
549 */
550 char* changeToGraphDir(const char* const graphs)
551 {
552 int retval;
553 char* cwd;
554
555 cwd= getcwd(NULL, 0);
556 if (cwd == NULL)
557 {
558 g_error(strerror(errno));
559 }
560 while ((retval= chdir(graphs)) != 0)
561 {
562 if (errno == ENOENT)
563 {
564 retval= mkdir(graphs, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
565 S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
566 if (retval != 0)
567 {
568 g_error(strerror(errno));
569 }
570 }
571 else
572 {
573 g_error(strerror(errno));
574 }
575 }
576
577 return cwd;
578 }
579
580
581 /*
582 * A GFunc for g_queue_foreach()
583 *
584 * Args:
585 * data: ModuleOption*
586 * user_data: NULL
587 */
588 static void gfAddModuleOption(gpointer data, gpointer user_data)
589 {
590 ModuleOption* option;
591 LttvOptionType conversion[]= {
592 [NO_ARG]= LTTV_OPT_NONE,
593 [REQUIRED_ARG]= LTTV_OPT_STRING,
594 };
595
596 g_assert_cmpuint(sizeof(conversion) / sizeof(*conversion), ==,
597 HAS_ARG_COUNT);
598 option= (ModuleOption*) data;
599 lttv_option_add(option->longName, '\0', option->optionHelp,
600 option->argHelp ? option->argHelp : argHelpNone,
601 conversion[option->hasArg], &option->arg, NULL, NULL);
602 }
603
604
605 /*
606 * A GFunc for g_queue_foreach()
607 *
608 * Args:
609 * data: ModuleOption*
610 * user_data: NULL
611 */
612 static void gfRemoveModuleOption(gpointer data, gpointer user_data)
613 {
614 lttv_option_remove(((ModuleOption*) data)->longName);
615 }
616
617
618 LTTV_MODULE("sync", "Synchronize traces", \
619 "Synchronizes a traceset based on the correspondance of network events", \
620 init, destroy, "option")
This page took 0.042502 seconds and 4 git commands to generate.