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 "event_analysis.h"
28 #include "sync_chain.h"
30 #include "event_matching_distributor.h"
36 GQueue
* matchingModules
;
42 /* Offset whithin Matching module of the field* containing the function
49 // Functions common to all matching modules
50 static void initMatchingDistributor(SyncState
* const syncState
);
51 static void destroyMatchingDistributor(SyncState
* const syncState
);
53 static void matchEventDistributor(SyncState
* const syncState
, Event
* const
55 static GArray
* finalizeMatchingDistributor(SyncState
* const syncState
);
56 static void printMatchingStatsDistributor(SyncState
* const syncState
);
57 static void writeMatchingGraphsPlotsDistributor(SyncState
* const syncState
,
58 const unsigned int i
, const unsigned int j
);
59 static void writeMatchingGraphsOptionsDistributor(SyncState
* const syncState
,
60 const unsigned int i
, const unsigned int j
);
62 // Functions specific to this module
63 static void registerMatchingDistributor() __attribute__((constructor (101)));
65 void gfInitModule(gpointer data
, gpointer user_data
);
66 void gfDestroyModule(gpointer data
, gpointer user_data
);
67 void gfMatchEvent(gpointer data
, gpointer user_data
);
68 void gfFinalize(gpointer data
, gpointer user_data
);
69 void gfPrintStats(gpointer data
, gpointer user_data
);
70 void gfGraphFunctionCall(gpointer data
, gpointer user_data
);
73 static MatchingModule matchingModuleDistributor
= {
77 .initMatching
= &initMatchingDistributor
,
78 .destroyMatching
= &destroyMatchingDistributor
,
79 .matchEvent
= &matchEventDistributor
,
80 .finalizeMatching
= &finalizeMatchingDistributor
,
81 .printMatchingStats
= &printMatchingStatsDistributor
,
82 .writeMatchingGraphsPlots
= &writeMatchingGraphsPlotsDistributor
,
83 .writeMatchingGraphsOptions
= &writeMatchingGraphsOptionsDistributor
,
88 * Matching module registering function
90 static void registerMatchingDistributor()
92 g_queue_push_tail(&matchingModules
, &matchingModuleDistributor
);
97 * Matching init function
99 * This function is called at the beginning of a synchronization run for a set
102 * Build the list and initialize other matching Modules
105 * syncState container for synchronization data.
107 static void initMatchingDistributor(SyncState
* const syncState
)
109 MatchingDataDistributor
* matchingData
;
111 matchingData
= malloc(sizeof(MatchingDataDistributor
));
112 syncState
->matchingData
= matchingData
;
114 matchingData
->distributedModules
= g_queue_new();
115 g_queue_foreach(&matchingModules
, &gfInitModule
, &(struct InitAggregate
)
116 {syncState
, matchingData
->distributedModules
});
121 * Matching destroy function
123 * Destroy other modules and free the matching specific data structures
126 * syncState container for synchronization data.
128 static void destroyMatchingDistributor(SyncState
* const syncState
)
130 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
132 g_queue_foreach(matchingData
->distributedModules
, &gfDestroyModule
, NULL
);
134 g_queue_clear(matchingData
->distributedModules
);
135 free(syncState
->matchingData
);
136 syncState
->matchingData
= NULL
;
142 * Copy event and distribute to matching modules
145 * syncState container for synchronization data.
146 * event new event to match
148 static void matchEventDistributor(SyncState
* const syncState
, Event
* const event
)
150 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
152 g_queue_foreach(matchingData
->distributedModules
, &gfMatchEvent
, event
);
153 event
->destroy(event
);
158 * Call the distributed finalization functions and return identity factors
161 * syncState container for synchronization data.
164 * Factors[traceNb] identity factors for each trace
166 static GArray
* finalizeMatchingDistributor(SyncState
* const syncState
)
170 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
172 g_queue_foreach(matchingData
->distributedModules
, &gfFinalize
, NULL
);
174 factors
= g_array_sized_new(FALSE
, FALSE
, sizeof(Factors
),
176 g_array_set_size(factors
, syncState
->traceNb
);
177 for (i
= 0; i
< syncState
->traceNb
; i
++)
181 e
= &g_array_index(factors
, Factors
, i
);
191 * Call the distributed statistics functions (when they exist). Must be called
192 * after finalizeMatching.
195 * syncState container for synchronization data.
197 static void printMatchingStatsDistributor(SyncState
* const syncState
)
199 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
201 g_queue_foreach(matchingData
->distributedModules
, &gfPrintStats
, NULL
);
206 * Call the distributed graph lines functions (when they exist).
209 * syncState: container for synchronization data
210 * i: first trace number
211 * j: second trace number, garanteed to be larger than i
213 static void writeMatchingGraphsPlotsDistributor(SyncState
* const syncState
,
214 const unsigned int i
, const unsigned int j
)
216 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
218 g_queue_foreach(matchingData
->distributedModules
, &gfGraphFunctionCall
,
219 &(struct GraphAggregate
) {offsetof(MatchingModule
,
220 writeMatchingGraphsPlots
), i
, j
});
225 * Call the distributed graph options functions (when they exist).
228 * syncState: container for synchronization data
229 * i: first trace number
230 * j: second trace number, garanteed to be larger than i
232 static void writeMatchingGraphsOptionsDistributor(SyncState
* const syncState
,
233 const unsigned int i
, const unsigned int j
)
235 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
237 g_queue_foreach(matchingData
->distributedModules
, &gfGraphFunctionCall
,
238 &(struct GraphAggregate
) {offsetof(MatchingModule
,
239 writeMatchingGraphsOptions
), i
, j
});
244 * A GFunc for g_queue_foreach()
246 * Add and initialize matching module
249 * data MatchingModule*, module to add
250 * user_data InitAggregate*
252 void gfInitModule(gpointer data
, gpointer user_data
)
254 SyncState
* parallelSS
;
255 struct InitAggregate
* aggregate
= user_data
;
256 MatchingModule
* matchingModule
= data
;
258 if (strcmp(matchingModule
->name
, matchingModuleDistributor
.name
) == 0)
263 parallelSS
= malloc(sizeof(SyncState
));
264 memcpy(parallelSS
, aggregate
->syncState
, sizeof(SyncState
));
265 g_queue_push_tail(aggregate
->matchingModules
, parallelSS
);
267 parallelSS
->matchingModule
= matchingModule
;
268 parallelSS
->matchingModule
->initMatching(parallelSS
);
273 * A GFunc for g_queue_foreach()
275 * Destroy and remove matching module
278 * data SyncState* containing the module to destroy
281 void gfDestroyModule(gpointer data
, gpointer user_data
)
283 SyncState
* parallelSS
= data
;
285 parallelSS
->matchingModule
->destroyMatching(parallelSS
);
291 * A GFunc for g_queue_foreach()
294 * data SyncState* containing the distributed matching module
295 * user_data Event* original event
297 void gfMatchEvent(gpointer data
, gpointer user_data
)
299 SyncState
* parallelSS
= data
;
300 const Event
* event
= user_data
;
303 if (parallelSS
->matchingModule
->canMatch
[event
->type
])
305 event
->copy(event
, &newEvent
);
306 parallelSS
->matchingModule
->matchEvent(parallelSS
, newEvent
);
312 * A GFunc for g_queue_foreach()
315 * data SyncState* containing the distributed matching module
318 void gfFinalize(gpointer data
, gpointer user_data
)
321 SyncState
* parallelSS
= data
;
323 factors
= parallelSS
->matchingModule
->finalizeMatching(parallelSS
);
324 g_array_free(factors
, TRUE
);
329 * A GFunc for g_queue_foreach()
332 * data SyncState* containing the distributed matching module
335 void gfPrintStats(gpointer data
, gpointer user_data
)
337 SyncState
* parallelSS
= data
;
339 if (parallelSS
->matchingModule
->printMatchingStats
!= NULL
)
341 parallelSS
->matchingModule
->printMatchingStats(parallelSS
);
347 * A GFunc for g_queue_foreach()
349 * Call a certain matching function
352 * data SyncState* containing the distributed matching module
355 void gfGraphFunctionCall(gpointer data
, gpointer user_data
)
357 SyncState
* parallelSS
= data
;
358 struct GraphAggregate
* aggregate
= user_data
;
359 void (*graphFunction
)(struct _SyncState
*, const unsigned int, const
360 unsigned int)= (void*) data
+ (size_t) aggregate
->offset
;
362 if (graphFunction
!= NULL
)
364 graphFunction(parallelSS
, aggregate
->i
, aggregate
->j
);