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.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
;
59 static LttvHooks
* before_traceset
, * before_trace
, * event_hook
, * after_traceset
;
63 static void destroy();
65 static gboolean
tracesetStart(void *hook_data
, void *call_data
);
66 static gboolean
traceStart(void *hook_data
, void *call_data
);
67 static int processEveryEvent(void *hook_data
, void *call_data
);
68 static gboolean
tracesetEnd(void *hook_data
, void *call_data
);
70 static void setupSyncChain(LttvTracesetContext
* const traceSetContext
);
71 static void teardownSyncChain(LttvTracesetContext
* const traceSetContext
);
73 void ghfPrintEventCount(gpointer key
, gpointer value
, gpointer user_data
);
74 void gdnDestroyUint64(gpointer data
);
76 // struct TracesetChainState* tracesetChainStates[LttvTracesetContext*]
77 static GHashTable
* tracesetChainStates
;
79 static LttvHooks
* before_traceset
, * before_trace
, * event_hook
, * after_traceset
;
81 const char const *path
;
84 } batchAnalysisHooks
[] = {
85 {"hooks/traceset/before", &before_traceset
, &tracesetStart
},
86 {"hooks/trace/before", &before_trace
, &traceStart
},
87 {"hooks/event", &event_hook
, &processEveryEvent
},
88 {"hooks/traceset/after", &after_traceset
, &tracesetEnd
},
91 static gboolean optionEvalGraphs
;
92 static char* optionEvalGraphsDir
;
93 static char graphsDir
[20];
97 * Module init function
103 LttvAttributeValue value
;
105 LttvIAttribute
* attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
107 tracesetChainStates
= g_hash_table_new(NULL
, NULL
);
109 for (i
= 0; i
< sizeof(batchAnalysisHooks
) / sizeof(*batchAnalysisHooks
);
112 result
= lttv_iattribute_find_by_path(attributes
,
113 batchAnalysisHooks
[i
].path
, LTTV_POINTER
, &value
);
115 *batchAnalysisHooks
[i
].hook
= *(value
.v_pointer
);
116 g_assert(*batchAnalysisHooks
[i
].hook
);
117 lttv_hooks_add(*batchAnalysisHooks
[i
].hook
,
118 batchAnalysisHooks
[i
].function
, NULL
, LTTV_PRIO_DEFAULT
);
121 optionEvalGraphs
= FALSE
;
122 lttv_option_add("eval-graphs", '\0', "output gnuplot graph showing "
123 "synchronization points", "none", LTTV_OPT_NONE
, &optionEvalGraphs
,
126 retval
= snprintf(graphsDir
, sizeof(graphsDir
), "eval-graphs-%d", getpid());
127 if (retval
> sizeof(graphsDir
) - 1)
129 graphsDir
[sizeof(graphsDir
) - 1]= '\0';
131 optionEvalGraphsDir
= graphsDir
;
132 lttv_option_add("eval-graphs-dir", '\0', "specify the directory where to"
133 " store the graphs", graphsDir
, LTTV_OPT_STRING
, &optionEvalGraphsDir
,
139 * Module destroy function
141 static void destroy()
145 g_assert_cmpuint(g_hash_table_size(tracesetChainStates
), ==, 0);
146 g_hash_table_destroy(tracesetChainStates
);
148 for (i
= 0; i
< sizeof(batchAnalysisHooks
) / sizeof(*batchAnalysisHooks
);
151 lttv_hooks_remove_data(*batchAnalysisHooks
[i
].hook
,
152 batchAnalysisHooks
[i
].function
, NULL
);
155 lttv_option_remove("eval-graphs");
156 lttv_option_remove("eval-graphs-dir");
161 * Lttv hook function that will be called before a traceset is processed
165 * callData: LttvTracesetContext* at that moment
168 * FALSE Always returns FALSE, meaning to keep processing hooks
170 static gboolean
tracesetStart(void *hook_data
, void *call_data
)
172 struct TracesetChainState
* tracesetChainState
;
173 LttvTracesetContext
*tsc
= (LttvTracesetContext
*) call_data
;
175 tracesetChainState
= malloc(sizeof(struct TracesetChainState
));
176 g_hash_table_insert(tracesetChainStates
, tsc
, tracesetChainState
);
177 tracesetChainState
->eventNbs
= g_hash_table_new_full(&g_direct_hash
,
178 &g_direct_equal
, NULL
, &gdnDestroyUint64
);
180 gettimeofday(&tracesetChainState
->startTime
, 0);
181 getrusage(RUSAGE_SELF
, &tracesetChainState
->startUsage
);
190 * Lttv hook function that will be called before a trace is processed
194 * callData: LttvTraceContext* at that moment
197 * FALSE Always returns FALSE, meaning to keep processing hooks
199 static gboolean
traceStart(void *hook_data
, void *call_data
)
201 struct TracesetChainState
* tracesetChainState
;
203 LttvTraceContext
* tc
= (LttvTraceContext
*) call_data
;
204 LttvTracesetContext
* tsc
= tc
->ts_context
;
206 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, tsc
);
207 eventNb
= malloc(sizeof(uint64_t));
209 g_hash_table_insert(tracesetChainState
->eventNbs
, tc
, eventNb
);
216 * Lttv hook function that is called for every event
220 * callData: LttvTracefileContext* at the moment of the event
223 * FALSE Always returns FALSE, meaning to keep processing hooks for
226 static int processEveryEvent(void *hook_data
, void *call_data
)
228 LttvTracefileContext
* tfc
= (LttvTracefileContext
*) call_data
;
229 LttvTraceContext
* tc
= tfc
->t_context
;
230 LttvTracesetContext
* tsc
= tc
->ts_context
;
231 struct TracesetChainState
* tracesetChainState
;
234 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, tsc
);
235 eventNb
= g_hash_table_lookup(tracesetChainState
->eventNbs
, tc
);
244 * Lttv hook function that is called after a traceset has been processed
248 * callData: LttvTracefileContext* at that moment
251 * FALSE Always returns FALSE, meaning to keep processing hooks
253 static gboolean
tracesetEnd(void *hook_data
, void *call_data
)
255 struct TracesetChainState
* tracesetChainState
;
256 LttvTracesetContext
* tsc
= (LttvTracesetContext
*) call_data
;
259 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, tsc
);
260 printf("Event count (%u traces):\n",
261 g_hash_table_size(tracesetChainState
->eventNbs
));
262 g_hash_table_foreach(tracesetChainState
->eventNbs
, &ghfPrintEventCount
,
264 printf("\ttotal events: %" PRIu64
"\n", sum
);
265 g_hash_table_destroy(tracesetChainState
->eventNbs
);
267 teardownSyncChain(tsc
);
269 g_hash_table_remove(tracesetChainStates
, tsc
);
276 * Initialize modules in a sync chain. Use modules that will check
277 * the precision of time synchronization between a group of traces.
280 * traceSetContext: traceset
282 void setupSyncChain(LttvTracesetContext
* const traceSetContext
)
284 struct TracesetChainState
* tracesetChainState
;
285 SyncState
* syncState
;
288 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, traceSetContext
);
289 syncState
= malloc(sizeof(SyncState
));
290 tracesetChainState
->syncState
= syncState
;
291 syncState
->traceNb
= lttv_traceset_number(traceSetContext
->ts
);
293 // Statistics are always on with eval
294 syncState
->stats
= true;
296 if (optionEvalGraphs
)
298 // Create the graph directory right away in case the module initialization
299 // functions have something to write in it.
300 syncState
->graphsDir
= optionEvalGraphsDir
;
301 syncState
->graphsStream
= createGraphsDir(syncState
->graphsDir
);
305 syncState
->graphsStream
= NULL
;
306 syncState
->graphsDir
= NULL
;
309 syncState
->reductionData
= NULL
;
310 syncState
->reductionModule
= NULL
;
312 syncState
->analysisData
= NULL
;
313 result
= g_queue_find_custom(&analysisModules
, "eval",
314 &gcfCompareAnalysis
);
315 syncState
->analysisModule
= (AnalysisModule
*) result
->data
;
316 syncState
->analysisModule
->initAnalysis(syncState
);
318 syncState
->matchingData
= NULL
;
319 result
= g_queue_find_custom(&matchingModules
, "distributor",
320 &gcfCompareMatching
);
321 syncState
->matchingModule
= (MatchingModule
*) result
->data
;
322 syncState
->matchingModule
->initMatching(syncState
);
324 syncState
->processingData
= NULL
;
325 result
= g_queue_find_custom(&processingModules
, "LTTV-standard",
326 &gcfCompareProcessing
);
327 syncState
->processingModule
= (ProcessingModule
*) result
->data
;
328 syncState
->processingModule
->initProcessing(syncState
, traceSetContext
);
333 * Destroy modules in a sync chain
336 * traceSetContext: traceset
338 void teardownSyncChain(LttvTracesetContext
* const traceSetContext
)
340 struct TracesetChainState
* tracesetChainState
;
341 SyncState
* syncState
;
342 struct timeval endTime
;
343 struct rusage endUsage
;
346 tracesetChainState
= g_hash_table_lookup(tracesetChainStates
, traceSetContext
);
347 syncState
= tracesetChainState
->syncState
;
349 freeAllFactors(syncState
->processingModule
->finalizeProcessing(syncState
),
353 if (optionEvalGraphs
)
355 writeGraphsScript(syncState
);
357 if (fclose(syncState
->graphsStream
) != 0)
359 g_error(strerror(errno
));
363 printStats(syncState
);
365 syncState
->processingModule
->destroyProcessing(syncState
);
366 if (syncState
->matchingModule
!= NULL
)
368 syncState
->matchingModule
->destroyMatching(syncState
);
370 if (syncState
->analysisModule
!= NULL
)
372 syncState
->analysisModule
->destroyAnalysis(syncState
);
374 if (syncState
->reductionModule
!= NULL
)
376 syncState
->reductionModule
->destroyReduction(syncState
);
381 gettimeofday(&endTime
, 0);
382 retval
= getrusage(RUSAGE_SELF
, &endUsage
);
384 timeDiff(&endTime
, &tracesetChainState
->startTime
);
385 timeDiff(&endUsage
.ru_utime
, &tracesetChainState
->startUsage
.ru_utime
);
386 timeDiff(&endUsage
.ru_stime
, &tracesetChainState
->startUsage
.ru_stime
);
388 printf("Evaluation time:\n");
389 printf("\treal time: %ld.%06ld\n", endTime
.tv_sec
, endTime
.tv_usec
);
390 printf("\tuser time: %ld.%06ld\n", endUsage
.ru_utime
.tv_sec
,
391 endUsage
.ru_utime
.tv_usec
);
392 printf("\tsystem time: %ld.%06ld\n", endUsage
.ru_stime
.tv_sec
,
393 endUsage
.ru_stime
.tv_usec
);
395 g_hash_table_remove(tracesetChainStates
, traceSetContext
);
396 free(tracesetChainState
);
402 * A GHFunc function for g_hash_table_foreach()
405 * key: LttvTraceContext *
406 * value: uint64_t *, event count for this trace
407 * user_data: uint64_t *, sum of the event counts
410 * Updates the sum in user_data
412 void ghfPrintEventCount(gpointer key
, gpointer value
, gpointer user_data
)
414 LttvTraceContext
*tc
= (LttvTraceContext
*) key
;
415 uint64_t *eventNb
= (uint64_t *)value
;
416 uint64_t *sum
= (uint64_t *)user_data
;
418 printf("\t%s: %" PRIu64
"\n", g_quark_to_string(ltt_trace_name(tc
->t
)),
425 * A GDestroyNotify function for g_hash_table_new_full()
430 void gdnDestroyUint64(gpointer data
)
432 free((uint64_t *) data
);
436 LTTV_MODULE("sync_chain_batch", "Execute synchronization modules in a "\
437 "post-processing step.", "This can be used to quantify the precision "\
438 "with which a group of trace is synchronized.", init
, destroy
,\
439 "batchAnalysis", "option", "sync")