Call the stats and graph functions from sync_chain
[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 FILE* graphsStream;
173 unsigned int i, j;
174 int retval;
175
176 if (!optionSync.present)
177 {
178 g_debug("Not synchronizing traceset because option is disabled");
179 return;
180 }
181
182 if (optionSyncStats.present)
183 {
184 gettimeofday(&startTime, 0);
185 getrusage(RUSAGE_SELF, &startUsage);
186 }
187
188 // Initialize data structures
189 syncState= malloc(sizeof(SyncState));
190 syncState->traceNb= lttv_traceset_number(traceSetContext->ts);
191
192 if (optionSyncStats.present)
193 {
194 syncState->stats= true;
195 }
196 else
197 {
198 syncState->stats= false;
199 }
200
201 if (optionSyncGraphs.present)
202 {
203 syncState->graphs= optionSyncGraphsDir.arg;
204 }
205 else
206 {
207 syncState->graphs= NULL;
208 }
209
210 graphsStream= NULL;
211 if (syncState->graphs)
212 {
213 char* cwd;
214 int graphsFp;
215
216 // Create the graph directory right away in case the module initialization
217 // functions have something to write in it.
218 cwd= changeToGraphDir(syncState->graphs);
219
220 if ((graphsFp= open("graphs.gnu", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
221 S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH
222 | S_IWOTH | S_IXOTH)) == -1)
223 {
224 g_error(strerror(errno));
225 }
226 if ((graphsStream= fdopen(graphsFp, "w")) == NULL)
227 {
228 g_error(strerror(errno));
229 }
230
231 retval= chdir(cwd);
232 if (retval == -1)
233 {
234 g_error(strerror(errno));
235 }
236 free(cwd);
237 }
238
239 // Identify and initialize modules
240 syncState->processingData= NULL;
241 if (optionSyncNull.present)
242 {
243 result= g_queue_find_custom(&processingModules, "LTTV-null",
244 &gcfCompareProcessing);
245 }
246 else
247 {
248 result= g_queue_find_custom(&processingModules, "LTTV-standard",
249 &gcfCompareProcessing);
250 }
251 g_assert(result != NULL);
252 syncState->processingModule= (ProcessingModule*) result->data;
253
254 syncState->matchingData= NULL;
255 result= g_queue_find_custom(&matchingModules, "TCP", &gcfCompareMatching);
256 g_assert(result != NULL);
257 syncState->matchingModule= (MatchingModule*) result->data;
258
259 syncState->analysisData= NULL;
260 result= g_queue_find_custom(&analysisModules, optionSyncAnalysis.arg,
261 &gcfCompareAnalysis);
262 if (result != NULL)
263 {
264 syncState->analysisModule= (AnalysisModule*) result->data;
265 }
266 else
267 {
268 g_error("Analysis module '%s' not found", optionSyncAnalysis.arg);
269 }
270
271 if (!optionSyncNull.present)
272 {
273 syncState->analysisModule->initAnalysis(syncState);
274 syncState->matchingModule->initMatching(syncState);
275 }
276 syncState->processingModule->initProcessing(syncState, traceSetContext);
277
278 // Process traceset
279 lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero);
280 lttv_process_traceset_middle(traceSetContext, ltt_time_infinite,
281 G_MAXULONG, NULL);
282 lttv_process_traceset_seek_time(traceSetContext, ltt_time_zero);
283
284 syncState->processingModule->finalizeProcessing(syncState);
285
286 // Write graphs file
287 if (graphsStream != NULL)
288 {
289 fprintf(graphsStream,
290 "#!/usr/bin/gnuplot\n\n"
291 "set terminal postscript eps color size 8in,6in\n");
292
293 // Cover the upper triangular matrix, i is the reference node.
294 for (i= 0; i < syncState->traceNb; i++)
295 {
296 for (j= i + 1; j < syncState->traceNb; j++)
297 {
298 long pos1, pos2, trunc;
299
300 fprintf(graphsStream,
301 "\nset output \"%03d-%03d.eps\"\n"
302 "plot \\\n", i, j);
303
304 if (syncState->processingModule->writeProcessingGraphsPlots)
305 {
306 syncState->processingModule->writeProcessingGraphsPlots(graphsStream,
307 syncState, i, j);
308 }
309 if (syncState->matchingModule->writeMatchingGraphsPlots)
310 {
311 syncState->matchingModule->writeMatchingGraphsPlots(graphsStream,
312 syncState, i, j);
313 }
314 if (syncState->analysisModule->writeAnalysisGraphsPlots)
315 {
316 syncState->analysisModule->writeAnalysisGraphsPlots(graphsStream,
317 syncState, i, j);
318 }
319
320 fflush(graphsStream);
321 pos2= ftell(graphsStream);
322 if (pos1 != pos2)
323 {
324 // Remove the ", \\\n" from the last graph plot line
325 trunc= pos2 - 4;
326 }
327 else
328 {
329 // Remove the "plot \\\n" line to avoid creating an invalid
330 // gnuplot script
331 trunc= pos2 - 7;
332 }
333
334 if (ftruncate(fileno(graphsStream), trunc) == -1)
335 {
336 g_error(strerror(errno));
337 }
338 if (fseek(graphsStream, 0, SEEK_END) == -1)
339 {
340 g_error(strerror(errno));
341 }
342
343 fprintf(graphsStream,
344 "\nset output \"%03d-%03d.eps\"\n"
345 "set title \"\"\n", i, j);
346
347 if (syncState->processingModule->writeProcessingGraphsOptions)
348 {
349 syncState->processingModule->writeProcessingGraphsOptions(graphsStream,
350 syncState, i, j);
351 }
352 if (syncState->matchingModule->writeMatchingGraphsOptions)
353 {
354 syncState->matchingModule->writeMatchingGraphsOptions(graphsStream,
355 syncState, i, j);
356 }
357 if (syncState->analysisModule->writeAnalysisGraphsOptions)
358 {
359 syncState->analysisModule->writeAnalysisGraphsOptions(graphsStream,
360 syncState, i, j);
361 }
362
363 if (pos1 != pos2)
364 {
365 fprintf(graphsStream, "replot\n");
366 }
367 }
368 }
369
370 if (fclose(graphsStream) != 0)
371 {
372 g_error(strerror(errno));
373 }
374 }
375
376 if (syncState->processingModule->printProcessingStats != NULL)
377 {
378 syncState->processingModule->printProcessingStats(syncState);
379 }
380 if (syncState->matchingModule->printMatchingStats != NULL)
381 {
382 syncState->matchingModule->printMatchingStats(syncState);
383 }
384 if (syncState->analysisModule->printAnalysisStats != NULL)
385 {
386 syncState->analysisModule->printAnalysisStats(syncState);
387 }
388
389 if (optionSyncStats.present)
390 {
391 printf("Resulting synchronization factors:\n");
392 for (i= 0; i < syncState->traceNb; i++)
393 {
394 LttTrace* t;
395
396 t= traceSetContext->traces[i]->t;
397
398 printf("\ttrace %u drift= %g offset= %g (%f) start time= %ld.%09ld\n",
399 i, t->drift, t->offset, (double) tsc_to_uint64(t->freq_scale,
400 t->start_freq, t->offset) / NANOSECONDS_PER_SECOND,
401 t->start_time_from_tsc.tv_sec,
402 t->start_time_from_tsc.tv_nsec);
403 }
404 }
405
406 syncState->processingModule->destroyProcessing(syncState);
407 if (syncState->matchingModule != NULL)
408 {
409 syncState->matchingModule->destroyMatching(syncState);
410 }
411 if (syncState->analysisModule != NULL)
412 {
413 syncState->analysisModule->destroyAnalysis(syncState);
414 }
415
416 free(syncState);
417
418 if (optionSyncStats.present)
419 {
420 gettimeofday(&endTime, 0);
421 retval= getrusage(RUSAGE_SELF, &endUsage);
422
423 timeDiff(&endTime, &startTime);
424 timeDiff(&endUsage.ru_utime, &startUsage.ru_utime);
425 timeDiff(&endUsage.ru_stime, &startUsage.ru_stime);
426
427 printf("Synchronization time:\n");
428 printf("\treal time: %ld.%06ld\n", endTime.tv_sec, endTime.tv_usec);
429 printf("\tuser time: %ld.%06ld\n", endUsage.ru_utime.tv_sec,
430 endUsage.ru_utime.tv_usec);
431 printf("\tsystem time: %ld.%06ld\n", endUsage.ru_stime.tv_sec,
432 endUsage.ru_stime.tv_usec);
433 }
434 }
435
436
437 /*
438 * Calculate the elapsed time between two timeval values
439 *
440 * Args:
441 * end: end time, result is also stored in this structure
442 * start: start time
443 */
444 void timeDiff(struct timeval* const end, const struct timeval* const start)
445 {
446 if (end->tv_usec >= start->tv_usec)
447 {
448 end->tv_sec-= start->tv_sec;
449 end->tv_usec-= start->tv_usec;
450 }
451 else
452 {
453 end->tv_sec= end->tv_sec - start->tv_sec - 1;
454 end->tv_usec= end->tv_usec - start->tv_usec + 1e6;
455 }
456 }
457
458
459 /*
460 * A GCompareFunc for g_slist_find_custom()
461 *
462 * Args:
463 * a: ProcessingModule*, element's data
464 * b: char*, user data to compare against
465 *
466 * Returns:
467 * 0 if the processing module a's name is b
468 */
469 gint gcfCompareProcessing(gconstpointer a, gconstpointer b)
470 {
471 const ProcessingModule* processingModule;
472 const char* name;
473
474 processingModule= (const ProcessingModule*) a;
475 name= (const char*) b;
476
477 return strncmp(processingModule->name, name,
478 strlen(processingModule->name) + 1);
479 }
480
481
482 /*
483 * A GCompareFunc for g_slist_find_custom()
484 *
485 * Args:
486 * a: MatchingModule*, element's data
487 * b: char*, user data to compare against
488 *
489 * Returns:
490 * 0 if the matching module a's name is b
491 */
492 gint gcfCompareMatching(gconstpointer a, gconstpointer b)
493 {
494 const MatchingModule* matchingModule;
495 const char* name;
496
497 matchingModule= (const MatchingModule*) a;
498 name= (const char*) b;
499
500 return strncmp(matchingModule->name, name, strlen(matchingModule->name) +
501 1);
502 }
503
504
505 /*
506 * A GCompareFunc for g_slist_find_custom()
507 *
508 * Args:
509 * a: AnalysisModule*, element's data
510 * b: char*, user data to compare against
511 *
512 * Returns:
513 * 0 if the analysis module a's name is b
514 */
515 gint gcfCompareAnalysis(gconstpointer a, gconstpointer b)
516 {
517 const AnalysisModule* analysisModule;
518 const char* name;
519
520 analysisModule= (const AnalysisModule*) a;
521 name= (const char*) b;
522
523 return strncmp(analysisModule->name, name, strlen(analysisModule->name) +
524 1);
525 }
526
527
528 /*
529 * A GFunc for g_queue_foreach()
530 *
531 * Concatenate analysis module names.
532 *
533 * Args:
534 * data: AnalysisModule*
535 * user_data: GString*, concatenated names
536 */
537 static void gfAppendAnalysisName(gpointer data, gpointer user_data)
538 {
539 g_string_append((GString*) user_data, ((AnalysisModule*) data)->name);
540 g_string_append((GString*) user_data, ", ");
541 }
542
543
544 /*
545 * Change to the directory used to hold graphs. Create it if necessary.
546 *
547 * Args:
548 * graph: name of directory
549 *
550 * Returns:
551 * The current working directory before the execution of the function. The
552 * string must be free'd by the caller.
553 */
554 char* changeToGraphDir(const char* const graphs)
555 {
556 int retval;
557 char* cwd;
558
559 cwd= getcwd(NULL, 0);
560 if (cwd == NULL)
561 {
562 g_error(strerror(errno));
563 }
564 while ((retval= chdir(graphs)) != 0)
565 {
566 if (errno == ENOENT)
567 {
568 retval= mkdir(graphs, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
569 S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
570 if (retval != 0)
571 {
572 g_error(strerror(errno));
573 }
574 }
575 else
576 {
577 g_error(strerror(errno));
578 }
579 }
580
581 return cwd;
582 }
583
584
585 /*
586 * A GFunc for g_queue_foreach()
587 *
588 * Args:
589 * data: ModuleOption*
590 * user_data: NULL
591 */
592 static void gfAddModuleOption(gpointer data, gpointer user_data)
593 {
594 ModuleOption* option;
595 LttvOptionType conversion[]= {
596 [NO_ARG]= LTTV_OPT_NONE,
597 [REQUIRED_ARG]= LTTV_OPT_STRING,
598 };
599
600 g_assert_cmpuint(sizeof(conversion) / sizeof(*conversion), ==,
601 HAS_ARG_COUNT);
602 option= (ModuleOption*) data;
603 lttv_option_add(option->longName, '\0', option->optionHelp,
604 option->argHelp ? option->argHelp : argHelpNone,
605 conversion[option->hasArg], &option->arg, NULL, NULL);
606 }
607
608
609 /*
610 * A GFunc for g_queue_foreach()
611 *
612 * Args:
613 * data: ModuleOption*
614 * user_data: NULL
615 */
616 static void gfRemoveModuleOption(gpointer data, gpointer user_data)
617 {
618 lttv_option_remove(((ModuleOption*) data)->longName);
619 }
620
621
622 LTTV_MODULE("sync", "Synchronize traces", \
623 "Synchronizes a traceset based on the correspondance of network events", \
624 init, destroy, "option")
This page took 0.041328 seconds and 4 git commands to generate.