-
Notifications
You must be signed in to change notification settings - Fork 10
/
AbstractOtf2StateProvider.java
366 lines (335 loc) · 11.9 KB
/
AbstractOtf2StateProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**********************************************************************
* Copyright (c) 2021 École Polytechnique de Montréal
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
package org.eclipse.tracecompass.incubator.internal.otf2.core.analysis;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.incubator.internal.otf2.core.Activator;
import org.eclipse.tracecompass.incubator.internal.otf2.core.analysis.callstack.Otf2CallStackStateProvider;
import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
/**
* Base state provider for the OTF2 analyses
*
* @author Yoann Heitz
*/
public abstract class AbstractOtf2StateProvider extends AbstractTmfStateProvider {
/**
* Unknown location ID
*/
protected static final long UNKNOWN_LOCATION_ID = -1;
/**
* Unknown rank
*/
protected static final int UNKNOWN_RANK = -1;
/**
* Unknown ID
*/
protected static final int UNKNOWN_ID = -1;
/**
* Unknown string
*/
protected static final String UNKNOWN = "UNKNOWN"; //$NON-NLS-1$
/**
* String id to string value mapping
*/
private final Map<Integer, String> fStringId;
/**
* Region id to string id mapping
*/
private final Map<Integer, Integer> fRegionStringId;
/**
* Communicator id to group id mapping
*/
private final Map<Integer, Integer> fCommunicatorGroup;
/**
* Group id to group members mapping
*/
private final Map<Integer, @Nullable ArrayList<Long>> fGroupMembers;
/**
* Constructor
*
* @param trace
* the trace
* @param id
* the ID of the corresponding analysis
*/
protected AbstractOtf2StateProvider(@Nullable ITmfTrace trace, String id) {
super(Objects.requireNonNull(trace), id);
fStringId = new HashMap<>();
fRegionStringId = new HashMap<>();
fCommunicatorGroup = new HashMap<>();
fGroupMembers = new HashMap<>();
}
@Override
public ITmfStateProvider getNewInstance() {
try {
return Objects.requireNonNull((ITmfStateProvider) this.getClass().getConstructor(ITmfTrace.class).newInstance(getTrace()));
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
Activator.getInstance().logError("Error instantiating state provider"); //$NON-NLS-1$
return new Otf2CallStackStateProvider(getTrace());
}
}
/**
* Basic check to figure out if further processing should be done with an
* event
*
* @param event
* the event
* @return true if the event should be handled, false otherwise
*/
protected static boolean considerEvent(ITmfEvent event) {
return true;
}
@Override
protected void eventHandle(ITmfEvent event) {
ITmfEventField content = event.getContent();
if (content == null) {
return;
}
ITmfStateSystemBuilder ssb = getStateSystemBuilder();
if (ssb == null) {
return;
}
String ctfEventName = event.getName();
Matcher matcher = IOtf2Constants.OTF2_EVENT_NAME_PATTERN.matcher(ctfEventName);
if (!matcher.matches()) {
return;
}
String eventType = matcher.group(IOtf2Constants.OTF2_TYPE_GROUP);
String otf2EventName = matcher.group(IOtf2Constants.OTF2_NAME_GROUP);
if (otf2EventName == null) {
return;
}
switch (eventType) {
case IOtf2Constants.OTF2_GLOBAL_DEFINITION: {
processGlobalDefinition(event, otf2EventName);
break;
}
case IOtf2Constants.OTF2_EVENT: {
processOtf2Event(event, otf2EventName, ssb);
break;
}
default:
return;
}
}
/**
* Process a global definition
*
* @param event
* The event to process
* @param name
* The name of the global definition
*/
protected abstract void processGlobalDefinition(ITmfEvent event, String name);
/**
* Default method to process a string definition (add it to the String id to
* String value mapping table)
*
* @param event
* The event to process
*/
protected void processStringDefinition(ITmfEvent event) {
ITmfEventField content = event.getContent();
Integer stringReference = content.getFieldValue(Integer.class, IOtf2Fields.OTF2_SELF);
String stringValue = content.getFieldValue(String.class, IOtf2Fields.OTF2_STRING_VALUE);
if (stringReference == null || stringValue == null) {
return;
}
fStringId.put(stringReference, stringValue);
}
/**
* Default method to process a region definition (add it to the Region to
* String id mapping table)
*
* @param event
* The event to process
*/
protected void processRegionDefinition(ITmfEvent event) {
ITmfEventField content = event.getContent();
Integer regionReference = content.getFieldValue(Integer.class, IOtf2Fields.OTF2_SELF);
Integer stringReference = content.getFieldValue(Integer.class, IOtf2Fields.OTF2_NAME);
if (regionReference == null || stringReference == null) {
return;
}
fRegionStringId.put(regionReference, stringReference);
}
/**
* Default method to process a communicator definition (add it to the
* Communicator to Group mapping table)
*
* @param event
* The event to process
*/
protected void processCommunicatorDefinition(ITmfEvent event) {
ITmfEventField content = event.getContent();
Integer communicatorReference = content.getFieldValue(Integer.class, IOtf2Fields.OTF2_SELF);
Integer groupReference = content.getFieldValue(Integer.class, IOtf2Fields.OTF2_GROUP);
if (communicatorReference == null || groupReference == null) {
return;
}
fCommunicatorGroup.put(communicatorReference, groupReference);
}
/**
* Default method to process a group definition (initialize the members
* array)
*
* @param event
* The event to process
*/
protected void processGroupDefinition(ITmfEvent event) {
ITmfEventField content = event.getContent();
Integer groupReference = content.getFieldValue(Integer.class, IOtf2Fields.OTF2_SELF);
Integer numberOfMembers = content.getFieldValue(Integer.class, IOtf2Fields.OTF2_NUMBER_OF_MEMBERS);
if (groupReference == null || numberOfMembers == null) {
return;
}
if (numberOfMembers == 0) {
fGroupMembers.put(groupReference, null);
return;
}
ArrayList<Long> membersArray = new ArrayList<>(numberOfMembers);
for (int i = 0; i < numberOfMembers; i++) {
membersArray.add(UNKNOWN_LOCATION_ID);
}
fGroupMembers.put(groupReference, membersArray);
}
/**
* Default method to process a group member definition (fill the
* corresponding members array)
*
* @param event
* The event to process
*/
protected void processGroupMemberDefinition(ITmfEvent event) {
ITmfEventField content = event.getContent();
Integer groupReference = content.getFieldValue(Integer.class, IOtf2Fields.OTF2_GROUP);
Integer rank = content.getFieldValue(Integer.class, IOtf2Fields.OTF2_RANK);
Long locationReference = content.getFieldValue(Long.class, IOtf2Fields.OTF2_LOCATION);
if (groupReference == null || rank == null || locationReference == null) {
return;
}
ArrayList<Long> membersArray = fGroupMembers.get(groupReference);
if (membersArray == null) {
return;
}
membersArray.set(rank, locationReference);
fGroupMembers.put(groupReference, membersArray);
}
/**
* Process an OTF2 event
*
* @param event
* The event to process
* @param name
* The name of the event
* @param ssb
* The state system builder
*/
protected abstract void processOtf2Event(ITmfEvent event, String name, ITmfStateSystemBuilder ssb);
/**
* Check if an event is of a given type
*
* @param event
* the event
* @param eventName
* the event name to check for
* @return true if the event is of the given type, false otherwise
*/
protected static boolean isEvent(ITmfEvent event, String eventName) {
return event.getName().equals(eventName);
}
/**
* Return the location id of an OTF2 event.
*
* @param event
* The event
* @return The location id
*/
protected static long getLocationId(ITmfEvent event) {
ITmfEventField content = event.getContent();
Long locationId = content.getFieldValue(Long.class, IOtf2Fields.OTF2_LOCATION_ID);
return (locationId != null) ? locationId : UNKNOWN_LOCATION_ID;
}
/**
* Return the rank of a location into an MPI communicator.
*
* @param location
* The location id
* @param communicatorReference
* The communicator id
* @return The rank of the location into the communicator
*/
protected int getRank(Long location, Integer communicatorReference) {
Integer groupReference = fCommunicatorGroup.get(communicatorReference);
ArrayList<Long> members = fGroupMembers.get(groupReference);
if (members == null) {
return UNKNOWN_RANK;
}
return members.indexOf(location);
}
/**
* @param stringId
* the id of the string
* @return the value of the string
*/
protected String getStringFromStringId(int stringId) {
String stringValue = fStringId.get(stringId);
if (stringValue == null) {
return UNKNOWN;
}
return stringValue;
}
/**
*
* @param regionId
* the id of the region of code
* @return the name of the region
*/
protected String getRegionNameFromRegionId(int regionId) {
Integer stringId = fRegionStringId.get(regionId);
if (stringId == null) {
return UNKNOWN;
}
return getStringFromStringId(stringId);
}
/**
*
* @param communicatorReference
* the id of the communicator
* @return the list of the members in the communicator
*/
protected ArrayList<Long> getMembersFromCommunicatorReference(int communicatorReference) {
Integer groupReference = fCommunicatorGroup.get(communicatorReference);
ArrayList<Long> members = fGroupMembers.get(groupReference);
if (members == null) {
return new ArrayList<>();
}
return members;
}
/**
* Getter for the fStringId field
*
* @return the fStringId field
*/
protected Map<Integer, String> getStringId() {
return fStringId;
}
}