1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2009 Benjamin Poirier
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,
30 #include <sys/resource.h>
32 #include <sys/types.h>
36 #include <lttv/attribute.h>
37 #include <lttv/filter.h>
38 #include <lttv/hook.h>
39 #include <lttv/iattribute.h>
40 #include <lttv/lttv.h>
41 #include <lttv/module.h>
42 #include <lttv/option.h>
43 #include <lttv/print.h>
44 #include <lttv/sync/sync_chain_lttv.h>
46 #include <ltt/event.h>
47 #include <ltt/trace.h>
50 struct TracesetChainState
{
51 // uint64_t* eventNbs[LttvTraceContext*]
55 struct timeval startTime
;
56 struct rusage startUsage
;
60 static LttvHooks
* before_traceset
, * before_trace
, * event_hook
, * after_traceset
;
64 static void destroy();
66 static gboolean
tracesetStart(void *hook_data
, void *call_data
);
67 static gboolean
traceStart(void *hook_data
, void *call_data
);
68 static int processEveryEvent(void *hook_data
, void *call_data
);
69 static gboolean
tracesetEnd(void *hook_data
, void *call_data
);
71 static void setupSyncChain(LttvTracesetContext
* const traceSetContext
);
72 static void teardownSyncChain(LttvTracesetContext
* const traceSetContext
);
74 void ghfPrintEventCount(gpointer key
, gpointer value
, gpointer user_data
);
75 void gdnDestroyUint64(gpointer data
);
77 // struct TracesetChainState* tracesetChainStates[LttvTracesetContext*]
78 static GHashTable
* tracesetChainStates
;
80 static LttvHooks
* before_traceset
, * before_trace
, * event_hook
, * after_traceset
;
82 const char const *path
;
85 } batchAnalysisHooks
[] = {
86 {"hooks/traceset/before", &before_traceset
, &tracesetStart
},
87 {"hooks/trace/before", &before_trace
, &traceStart
},
88 {"hooks/event", &event_hook
, &processEveryEvent
},
89 {"hooks/traceset/after", &after_traceset
, &tracesetEnd
},
92 static gboolean optionEvalGraphs
;
93 static char* optionEvalGraphsDir
;
94 static char graphsDir
[20];
98 * Module init function
104 LttvAttributeValue value
;
106 LttvIAttribute
* attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
108 tracesetChainStates
= g_hash_table_new(NULL
, NULL
);
110 for (i
= 0; i
< sizeof(batchAnalysisHooks
) / sizeof(*batchAnalysisHooks
);
113 result
= lttv_iattribute_find_by_path(attributes
,
114 batchAnalysisHooks
[i
].path
, LTTV_POINTER
, &value
);
116 *batchAnalysisHooks
[i
].hook
= *(value
.v_pointer
);
117 g_assert(*batchAnalysisHooks
[i
].hook
);
118 lttv_hooks_add(*batchAnalysisHooks
[i
].hook
,
119 batchAnalysisHooks
[i
].function
, NULL
, LTTV_PRIO_DEFAULT
);
122 optionEvalGraphs
= FALSE
;
123 lttv_option_add("eval-graphs", '\0', "output gnuplot graph showing "
124 "synchronization points", "none", LTTV_OPT_NONE
, &optionEvalGraphs
,
127 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "graphs-%d", getpid());
128 if (retval
> sizeof(graphsDir
) - 1)
130 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
132 optionEvalGraphsDir
= graphsDir
;
133 lttv_option_add("eval-graphs-dir", '\0', "specify the directory where to"
134 " store the graphs", graphsDir
, LTTV_OPT_STRING
, &optionEvalGraphsDir
,
140 * Module destroy function
142 static void destroy()
146 g_assert_cmpuint(g_hash_table_size(tracesetChainStates
), ==, 0);
147 g_hash_table_destroy(tracesetChainStates
);
149 for (i
= 0; i
< sizeof(batchAnalysisHooks
) / sizeof(*batchAnalysisHooks
);
152 lttv_hooks_remove_data(*batchAnalysisHooks
[i
].hook
,
153 batchAnalysisHooks
[i
].function
, NULL
);
156 lttv_option_remove("eval-graphs");
157 lttv_option_remove("eval-graphs-dir");
162 * Lttv hook function that will be called before a traceset is processed
166 * callData: LttvTracesetContext* at that moment
169 * FALSE Always returns FALSE, meaning to keep processing hooks
171 static gboolean
tracesetStart(void *hook_data
, void *call_data
)
173 struct TracesetChainState
* tracesetChainState
;
174 LttvTracesetContext
*tsc
= (LttvTracesetContext
*) call_data
;
176 tracesetChainState
= malloc(sizeof(struct TracesetChainState
));
177 g_hash_table_insert(tracesetChainStates
, tsc
, tracesetChainState
);
178 tracesetChainState
->eventNbs
= g_hash_table_new_full(&g_direct_hash
,
179 &g_direct_equal
, NULL
, &gdnDestroyUint64
);
181 gettimeofday(&tracesetChainState
->startTime
, 0);
182 getrusage(RUSAGE_SELF
, &tracesetChainState
->startUsage
);
191 * Lttv hook function that will be called before a trace is processed
195 * callData: LttvTraceContext* at that moment
198 * FALSE Always returns FALSE, meaning to keep processing hooks
200 static gboolean
traceStart(void *hook_data
, void *call_data
)
202 struct TracesetChainState
* tracesetChainState
;
204 LttvTraceContext
* tc
= (LttvTraceContext
*) call_data
;
205 LttvTracesetContext
* tsc
= tc
->ts_context
;
207 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, tsc
);
208 eventNb
= malloc(sizeof(uint64_t));
210 g_hash_table_insert(tracesetChainState
->eventNbs
, tc
, eventNb
);
217 * Lttv hook function that is called for every event
221 * callData: LttvTracefileContext* at the moment of the event
224 * FALSE Always returns FALSE, meaning to keep processing hooks for
227 static int processEveryEvent(void *hook_data
, void *call_data
)
229 LttvTracefileContext
* tfc
= (LttvTracefileContext
*) call_data
;
230 LttvTraceContext
* tc
= tfc
->t_context
;
231 LttvTracesetContext
* tsc
= tc
->ts_context
;
232 struct TracesetChainState
* tracesetChainState
;
235 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, tsc
);
236 eventNb
= g_hash_table_lookup(tracesetChainState
->eventNbs
, tc
);
245 * Lttv hook function that is called after a traceset has been processed
249 * callData: LttvTracefileContext* at that moment
252 * FALSE Always returns FALSE, meaning to keep processing hooks
254 static gboolean
tracesetEnd(void *hook_data
, void *call_data
)
256 struct TracesetChainState
* tracesetChainState
;
257 LttvTracesetContext
* tsc
= (LttvTracesetContext
*) call_data
;
260 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, tsc
);
261 printf("Event count (%u traces):\n",
262 g_hash_table_size(tracesetChainState
->eventNbs
));
263 g_hash_table_foreach(tracesetChainState
->eventNbs
, &ghfPrintEventCount
,
265 printf("\ttotal events: %" PRIu64
"\n", sum
);
266 g_hash_table_destroy(tracesetChainState
->eventNbs
);
268 teardownSyncChain(tsc
);
270 g_hash_table_remove(tracesetChainStates
, tsc
);
277 * Initialize modules in a sync chain. Use modules that will check
278 * the precision of time synchronization between a group of traces.
281 * traceSetContext: traceset
283 void setupSyncChain(LttvTracesetContext
* const traceSetContext
)
285 struct TracesetChainState
* tracesetChainState
;
286 SyncState
* syncState
;
290 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, traceSetContext
);
291 syncState
= malloc(sizeof(SyncState
));
292 tracesetChainState
->syncState
= syncState
;
293 syncState
->traceNb
= lttv_traceset_number(traceSetContext
->ts
);
295 syncState
->stats
= true;
297 if (optionEvalGraphs
)
299 syncState
->graphs
= optionEvalGraphsDir
;
303 syncState
->graphs
= NULL
;
306 syncState
->processingData
= NULL
;
307 result
= g_queue_find_custom(&processingModules
, "LTTV-standard",
308 &gcfCompareProcessing
);
309 syncState
->processingModule
= (ProcessingModule
*) result
->data
;
311 tracesetChainState
->graphsStream
= NULL
;
312 if (syncState
->graphs
&&
313 syncState
->processingModule
->writeProcessingGraphsPlots
!= NULL
)
318 // Create the graph directory right away in case the module initialization
319 // functions have something to write in it.
320 cwd
= changeToGraphDir(syncState
->graphs
);
322 if ((graphsFp
= open("graphs.gnu", O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
|
323 S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IWGRP
| S_IXGRP
| S_IROTH
324 | S_IWOTH
| S_IXOTH
)) == -1)
326 g_error(strerror(errno
));
328 if ((tracesetChainState
->graphsStream
= fdopen(graphsFp
, "w")) == NULL
)
330 g_error(strerror(errno
));
336 g_error(strerror(errno
));
341 syncState
->matchingData
= NULL
;
342 result
= g_queue_find_custom(&matchingModules
, "TCP", &gcfCompareMatching
);
343 syncState
->matchingModule
= (MatchingModule
*) result
->data
;
345 syncState
->analysisData
= NULL
;
346 result
= g_queue_find_custom(&analysisModules
, "chull", &gcfCompareAnalysis
);
347 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
349 syncState
->processingModule
->initProcessing(syncState
, traceSetContext
);
350 syncState
->matchingModule
->initMatching(syncState
);
351 syncState
->analysisModule
->initAnalysis(syncState
);
356 * Destroy modules in a sync chain
359 * traceSetContext: traceset
361 void teardownSyncChain(LttvTracesetContext
* const traceSetContext
)
363 struct TracesetChainState
* tracesetChainState
;
364 SyncState
* syncState
;
365 struct timeval endTime
;
366 struct rusage endUsage
;
369 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, traceSetContext
);
370 syncState
= tracesetChainState
->syncState
;
372 syncState
->processingModule
->finalizeProcessing(syncState
);
375 if (tracesetChainState
->graphsStream
!= NULL
)
379 fprintf(tracesetChainState
->graphsStream
,
380 "#!/usr/bin/gnuplot\n\n"
381 "set terminal postscript eps color size 8in,6in\n");
383 // Cover the upper triangular matrix, i is the reference node.
384 for (i
= 0; i
< syncState
->traceNb
; i
++)
386 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
390 fprintf(tracesetChainState
->graphsStream
,
391 "\nset output \"%03d-%03d.eps\"\n"
394 syncState
->processingModule
->writeProcessingGraphsPlots(tracesetChainState
->graphsStream
,
397 // Remove the ", \\\n" from the last graph plot line
398 fflush(tracesetChainState
->graphsStream
);
399 pos
= ftell(tracesetChainState
->graphsStream
);
400 if (ftruncate(fileno(tracesetChainState
->graphsStream
), pos
- 4) == -1)
402 g_error(strerror(errno
));
404 if (fseek(tracesetChainState
->graphsStream
, 0, SEEK_END
) == -1)
406 g_error(strerror(errno
));
409 fprintf(tracesetChainState
->graphsStream
,
410 "\nset output \"%1$03d-%2$03d.eps\"\n"
411 "set key inside right bottom\n"
413 "set xlabel \"Clock %1$u\"\n"
414 "set xtics nomirror\n"
415 "set ylabel \"Clock %2$u\"\n"
416 "set ytics nomirror\n", i
, j
);
418 syncState
->processingModule
->writeProcessingGraphsOptions(tracesetChainState
->graphsStream
,
421 fprintf(tracesetChainState
->graphsStream
,
426 if (fclose(tracesetChainState
->graphsStream
) != 0)
428 g_error(strerror(errno
));
432 if (syncState
->processingModule
->printProcessingStats
!= NULL
)
434 syncState
->processingModule
->printProcessingStats(syncState
);
437 syncState
->processingModule
->destroyProcessing(syncState
);
438 if (syncState
->matchingModule
!= NULL
)
440 syncState
->matchingModule
->destroyMatching(syncState
);
442 if (syncState
->analysisModule
!= NULL
)
444 syncState
->analysisModule
->destroyAnalysis(syncState
);
449 gettimeofday(&endTime
, 0);
450 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
452 timeDiff(&endTime
, &tracesetChainState
->startTime
);
453 timeDiff(&endUsage
.ru_utime
, &tracesetChainState
->startUsage
.ru_utime
);
454 timeDiff(&endUsage
.ru_stime
, &tracesetChainState
->startUsage
.ru_stime
);
456 printf("Evaluation time:\n");
457 printf("\treal time: %ld.%06ld\n", endTime
.tv_sec
, endTime
.tv_usec
);
458 printf("\tuser time: %ld.%06ld\n", endUsage
.ru_utime
.tv_sec
,
459 endUsage
.ru_utime
.tv_usec
);
460 printf("\tsystem time: %ld.%06ld\n", endUsage
.ru_stime
.tv_sec
,
461 endUsage
.ru_stime
.tv_usec
);
463 g_hash_table_remove(tracesetChainStates
, traceSetContext
);
464 free(tracesetChainState
);
470 * A GHFunc function for g_hash_table_foreach()
473 * key: LttvTraceContext *
474 * value: uint64_t *, event count for this trace
475 * user_data: uint64_t *, sum of the event counts
478 * Updates the sum in user_data
480 void ghfPrintEventCount(gpointer key
, gpointer value
, gpointer user_data
)
482 LttvTraceContext
*tc
= (LttvTraceContext
*) key
;
483 uint64_t *eventNb
= (uint64_t *)value
;
484 uint64_t *sum
= (uint64_t *)user_data
;
486 printf("\t%s: %" PRIu64
"\n", g_quark_to_string(ltt_trace_name(tc
->t
)),
493 * A GDestroyNotify function for g_hash_table_new_full()
498 void gdnDestroyUint64(gpointer data
)
500 free((uint64_t *) data
);
504 LTTV_MODULE("sync_chain_batch", "Execute synchronization modules in a "\
505 "post-processing step.", "This can be used to quantify the precision "\
506 "with which a group of trace is synchronized.", init
, destroy
,\
507 "batchAnalysis", "option", "sync")