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
50 // Functions common to all matching modules
51 static void initMatchingDistributor(SyncState
* const syncState
);
52 static void destroyMatchingDistributor(SyncState
* const syncState
);
54 static void matchEventDistributor(SyncState
* const syncState
, Event
* const
56 static GArray
* finalizeMatchingDistributor(SyncState
* const syncState
);
57 static void printMatchingStatsDistributor(SyncState
* const syncState
);
58 static void writeMatchingGraphsPlotsDistributor(FILE* stream
, SyncState
* const
59 syncState
, const unsigned int i
, const unsigned int j
);
60 static void writeMatchingGraphsOptionsDistributor(FILE* stream
, SyncState
*
61 const syncState
, const unsigned int i
, const unsigned int j
);
63 // Functions specific to this module
64 static void registerMatchingDistributor() __attribute__((constructor (101)));
66 void gfInitModule(gpointer data
, gpointer user_data
);
67 void gfDestroyModule(gpointer data
, gpointer user_data
);
68 void gfMatchEvent(gpointer data
, gpointer user_data
);
69 void gfFinalize(gpointer data
, gpointer user_data
);
70 void gfPrintStats(gpointer data
, gpointer user_data
);
71 void gfGraphFunctionCall(gpointer data
, gpointer user_data
);
74 static MatchingModule matchingModuleDistributor
= {
78 .initMatching
= &initMatchingDistributor
,
79 .destroyMatching
= &destroyMatchingDistributor
,
80 .matchEvent
= &matchEventDistributor
,
81 .finalizeMatching
= &finalizeMatchingDistributor
,
82 .printMatchingStats
= &printMatchingStatsDistributor
,
83 .writeMatchingGraphsPlots
= &writeMatchingGraphsPlotsDistributor
,
84 .writeMatchingGraphsOptions
= &writeMatchingGraphsOptionsDistributor
,
89 * Matching module registering function
91 static void registerMatchingDistributor()
93 g_queue_push_tail(&matchingModules
, &matchingModuleDistributor
);
98 * Matching init function
100 * This function is called at the beginning of a synchronization run for a set
103 * Build the list and initialize other matching Modules
106 * syncState container for synchronization data.
108 static void initMatchingDistributor(SyncState
* const syncState
)
110 MatchingDataDistributor
* matchingData
;
112 matchingData
= malloc(sizeof(MatchingDataDistributor
));
113 syncState
->matchingData
= matchingData
;
115 matchingData
->distributedModules
= g_queue_new();
116 g_queue_foreach(&matchingModules
, &gfInitModule
, &(struct InitAggregate
)
117 {syncState
, matchingData
->distributedModules
});
122 * Matching destroy function
124 * Destroy other modules and free the matching specific data structures
127 * syncState container for synchronization data.
129 static void destroyMatchingDistributor(SyncState
* const syncState
)
131 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
133 g_queue_foreach(matchingData
->distributedModules
, &gfDestroyModule
, NULL
);
135 g_queue_clear(matchingData
->distributedModules
);
136 free(syncState
->matchingData
);
137 syncState
->matchingData
= NULL
;
143 * Copy event and distribute to matching modules
146 * syncState container for synchronization data.
147 * event new event to match
149 static void matchEventDistributor(SyncState
* const syncState
, Event
* const event
)
151 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
153 g_queue_foreach(matchingData
->distributedModules
, &gfMatchEvent
, event
);
154 event
->destroy(event
);
159 * Call the distributed finalization functions and return identity factors
162 * syncState container for synchronization data.
165 * Factors[traceNb] identity factors for each trace
167 static GArray
* finalizeMatchingDistributor(SyncState
* const syncState
)
171 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
173 g_queue_foreach(matchingData
->distributedModules
, &gfFinalize
, NULL
);
175 factors
= g_array_sized_new(FALSE
, FALSE
, sizeof(Factors
),
177 g_array_set_size(factors
, syncState
->traceNb
);
178 for (i
= 0; i
< syncState
->traceNb
; i
++)
182 e
= &g_array_index(factors
, Factors
, i
);
192 * Call the distributed statistics functions (when they exist). Must be called
193 * after finalizeMatching.
196 * syncState container for synchronization data.
198 static void printMatchingStatsDistributor(SyncState
* const syncState
)
200 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
202 g_queue_foreach(matchingData
->distributedModules
, &gfPrintStats
, NULL
);
207 * Call the distributed graph lines functions (when they exist).
210 * stream: stream where to write the data
211 * syncState: container for synchronization data
212 * i: first trace number
213 * j: second trace number, garanteed to be larger than i
215 static void writeMatchingGraphsPlotsDistributor(FILE* stream
, SyncState
* const
216 syncState
, const unsigned int i
, const unsigned int j
)
218 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
220 g_queue_foreach(matchingData
->distributedModules
, &gfGraphFunctionCall
,
221 &(struct GraphAggregate
) {offsetof(MatchingModule
,
222 writeMatchingGraphsPlots
), stream
, i
, j
});
227 * Call the distributed graph options functions (when they exist).
230 * stream: stream where to write the data
231 * syncState: container for synchronization data
232 * i: first trace number
233 * j: second trace number, garanteed to be larger than i
235 static void writeMatchingGraphsOptionsDistributor(FILE* stream
, SyncState
*
236 const syncState
, const unsigned int i
, const unsigned int j
)
238 MatchingDataDistributor
* matchingData
= syncState
->matchingData
;
240 g_queue_foreach(matchingData
->distributedModules
, &gfGraphFunctionCall
,
241 &(struct GraphAggregate
) {offsetof(MatchingModule
,
242 writeMatchingGraphsOptions
), stream
, i
, j
});
247 * A GFunc for g_queue_foreach()
249 * Add and initialize matching module
252 * data MatchingModule*, module to add
253 * user_data InitAggregate*
255 void gfInitModule(gpointer data
, gpointer user_data
)
257 SyncState
* parallelSS
;
258 struct InitAggregate
* aggregate
= user_data
;
259 MatchingModule
* matchingModule
= data
;
261 if (strcmp(matchingModule
->name
, matchingModuleDistributor
.name
) == 0)
266 parallelSS
= malloc(sizeof(SyncState
));
267 memcpy(parallelSS
, aggregate
->syncState
, sizeof(SyncState
));
268 g_queue_push_tail(aggregate
->matchingModules
, parallelSS
);
270 parallelSS
->matchingModule
= matchingModule
;
271 parallelSS
->matchingModule
->initMatching(parallelSS
);
276 * A GFunc for g_queue_foreach()
278 * Destroy and remove matching module
281 * data SyncState* containing the module to destroy
284 void gfDestroyModule(gpointer data
, gpointer user_data
)
286 SyncState
* parallelSS
= data
;
288 parallelSS
->matchingModule
->destroyMatching(parallelSS
);
294 * A GFunc for g_queue_foreach()
297 * data SyncState* containing the distributed matching module
298 * user_data Event* original event
300 void gfMatchEvent(gpointer data
, gpointer user_data
)
302 SyncState
* parallelSS
= data
;
303 const Event
* event
= user_data
;
306 if (parallelSS
->matchingModule
->canMatch
[event
->type
])
308 event
->copy(event
, &newEvent
);
309 parallelSS
->matchingModule
->matchEvent(parallelSS
, newEvent
);
315 * A GFunc for g_queue_foreach()
318 * data SyncState* containing the distributed matching module
321 void gfFinalize(gpointer data
, gpointer user_data
)
324 SyncState
* parallelSS
= data
;
326 factors
= parallelSS
->matchingModule
->finalizeMatching(parallelSS
);
327 g_array_free(factors
, TRUE
);
332 * A GFunc for g_queue_foreach()
335 * data SyncState* containing the distributed matching module
338 void gfPrintStats(gpointer data
, gpointer user_data
)
340 SyncState
* parallelSS
= data
;
342 if (parallelSS
->matchingModule
->printMatchingStats
!= NULL
)
344 parallelSS
->matchingModule
->printMatchingStats(parallelSS
);
350 * A GFunc for g_queue_foreach()
352 * Call a certain matching function
355 * data SyncState* containing the distributed matching module
358 void gfGraphFunctionCall(gpointer data
, gpointer user_data
)
360 SyncState
* parallelSS
= data
;
361 struct GraphAggregate
* aggregate
= user_data
;
362 void (*graphFunction
)(FILE* , struct _SyncState
*, const unsigned int,
363 const unsigned int)= (void*) data
+ (size_t) aggregate
->offset
;
365 if (graphFunction
!= NULL
)
367 graphFunction(aggregate
->stream
, parallelSS
, aggregate
->i
, aggregate
->j
);