Commit | Line | Data |
---|---|---|
5d10d135 ASL |
1 | /** |
2 | * | |
3 | */ | |
4 | package org.eclipse.linuxtools.lttng.state.evProcessor.state; | |
5 | ||
63eecb47 FC |
6 | import java.util.List; |
7 | ||
5d10d135 ASL |
8 | import org.eclipse.linuxtools.lttng.TraceDebug; |
9 | import org.eclipse.linuxtools.lttng.event.LttngEvent; | |
10 | import org.eclipse.linuxtools.lttng.event.LttngEventContent; | |
11 | import org.eclipse.linuxtools.lttng.event.LttngEventField; | |
12 | import org.eclipse.linuxtools.lttng.state.StateStrings.Fields; | |
13 | import org.eclipse.linuxtools.lttng.state.model.LttngProcessState; | |
14 | import org.eclipse.linuxtools.lttng.state.model.LttngTraceState; | |
15 | import org.eclipse.linuxtools.tmf.event.TmfEventField; | |
16 | ||
17 | /** | |
18 | * Common utility methods for all state processing handlers, not intended to be | |
19 | * instantiated on its own | |
20 | * | |
21 | * @author alvaro | |
22 | * | |
23 | */ | |
24 | public abstract class AbsStateProcessing { | |
25 | ||
26 | /** | |
27 | * protected method used when a Field is requested among several available | |
28 | * fields and the expected type is Long | |
29 | * | |
30 | * @param trcEvent | |
31 | * @param traceSt | |
32 | * @param expectedNumFields | |
33 | * @return | |
34 | */ | |
63eecb47 FC |
35 | protected Long getAFieldLong(LttngEvent trcEvent, LttngTraceState traceSt, |
36 | Fields expectedField) { | |
37 | Long fieldVal = null; | |
5d10d135 ASL |
38 | // TmfEventField[] fields = trcEvent.getContent().getFields(); |
39 | TmfEventField[] fields = ((LttngEventContent) trcEvent.getContent()) | |
40 | .getFields(trcEvent); | |
41 | ||
42 | // At least one field expected | |
43 | if (fields.length == 0) { | |
44 | TraceDebug.debug("Unexpected number of fields received: " | |
45 | + fields.length); | |
46 | return null; | |
47 | } | |
48 | ||
49 | LttngEventField field; | |
50 | String fieldname; | |
51 | String expectedFieldName = expectedField.getInName(); | |
52 | for (int i = 0; i < fields.length; i++) { | |
53 | field = (LttngEventField) fields[i]; | |
54 | fieldname = field.getName(); | |
55 | if (fieldname.equals(expectedFieldName)) { | |
56 | Object fieldObj = field.getValue(); | |
57 | if (fieldObj instanceof Long) { | |
58 | // Expected value found | |
59 | fieldVal = (Long) field.getValue(); | |
60 | // if (expectedField == Fields.LTT_FIELD_TYPE) { | |
61 | // TraceDebug.debug("Field Type value is: " + fieldVal); | |
62 | // } | |
63 | break; | |
64 | } else { | |
65 | if (TraceDebug.isDEBUG()) { | |
66 | TraceDebug | |
67 | .debug("Unexpected field Type. Expected: Long, Received: " | |
68 | + fieldObj.getClass().getSimpleName()); | |
69 | } | |
70 | return null; | |
71 | } | |
72 | } | |
73 | } | |
5d10d135 | 74 | |
63eecb47 FC |
75 | if (fieldVal == null) { |
76 | if (TraceDebug.isDEBUG()) { | |
77 | sendNoFieldFoundMsg(fields, expectedFieldName); | |
78 | } | |
79 | } | |
5d10d135 ASL |
80 | return fieldVal; |
81 | } | |
82 | ||
83 | /** | |
84 | * protected method used when a Field is requested among several available | |
85 | * fields and the expected type is String | |
86 | * | |
87 | * @param trcEvent | |
88 | * @param traceSt | |
89 | * @param expectedNumFields | |
90 | * @return | |
91 | */ | |
92 | protected String getAFieldString(LttngEvent trcEvent, | |
93 | LttngTraceState traceSt, Fields expectedField) { | |
63eecb47 | 94 | String fieldVal = null; |
5d10d135 ASL |
95 | // TmfEventField[] fields = trcEvent.getContent().getFields(); |
96 | TmfEventField[] fields = ((LttngEventContent) trcEvent.getContent()) | |
97 | .getFields(trcEvent); | |
98 | ||
99 | // Only one field expected | |
100 | if (fields.length == 0) { | |
101 | TraceDebug.debug("Unexpected number of fields received: " | |
102 | + fields.length); | |
103 | return null; | |
104 | } | |
105 | ||
106 | LttngEventField field; | |
107 | String fieldname; | |
108 | String expectedFieldName = expectedField.getInName(); | |
109 | for (int i = 0; i < fields.length; i++) { | |
110 | field = (LttngEventField) fields[i]; | |
111 | fieldname = field.getName(); | |
112 | if (fieldname.equals(expectedFieldName)) { | |
113 | Object fieldObj = field.getValue(); | |
114 | if (fieldObj instanceof String) { | |
115 | // Expected value found | |
116 | fieldVal = (String) field.getValue(); | |
117 | break; | |
118 | } else { | |
119 | if (TraceDebug.isDEBUG()) { | |
120 | TraceDebug | |
121 | .debug("Unexpected field Type. Expected: String, Received: " | |
122 | + fieldObj.getClass().getSimpleName()); | |
123 | } | |
124 | return null; | |
125 | } | |
126 | } | |
127 | } | |
63eecb47 FC |
128 | |
129 | if (fieldVal == null) { | |
130 | if (TraceDebug.isDEBUG()) { | |
131 | sendNoFieldFoundMsg(fields, expectedFieldName); | |
132 | } | |
133 | } | |
5d10d135 ASL |
134 | return fieldVal; |
135 | } | |
136 | ||
137 | /** | |
138 | * Find the process matching the given pid and cpu | |
139 | * | |
140 | * If cpu is 0, the cpu value is not matched and the selection is based on | |
141 | * pid value only | |
142 | * | |
143 | * @param traceState | |
144 | * @param cpu | |
145 | * @param pid | |
146 | * @return | |
147 | */ | |
148 | protected LttngProcessState lttv_state_find_process( | |
63eecb47 FC |
149 | LttngTraceState traceState, Long cpu, Long pid) { |
150 | // Define the return value | |
151 | LttngProcessState returnedProcess = null; | |
152 | ||
153 | // Obtain the list of available processes | |
154 | List<LttngProcessState> processList = traceState.getProcesses(); | |
155 | ||
156 | // FIXME: This needs be more efficient e.g. introduce a class with a | |
157 | // overriden hash and equals to consider the key values pid, cpu and | |
158 | // traceid and iterate over a collection of this new type. | |
159 | int pos = 0; | |
160 | while ((pos < processList.size()) && (returnedProcess == null)) { | |
161 | if (processList.get(pos).getPid().equals(pid)) { | |
162 | if ((processList.get(pos).getCpu().equals(cpu)) | |
163 | || (cpu.longValue() == 0L)) { | |
164 | returnedProcess = processList.get(pos); | |
165 | } | |
166 | } | |
167 | ||
168 | pos++; | |
169 | } | |
5d10d135 | 170 | |
63eecb47 | 171 | return returnedProcess; |
5d10d135 ASL |
172 | } |
173 | ||
174 | protected void sendNoFieldFoundMsg(TmfEventField[] fields, | |
175 | String expectedFieldName) { | |
176 | LttngEventField field; | |
177 | StringBuilder sb = new StringBuilder("Field not found, requested: " | |
178 | + expectedFieldName); | |
179 | sb.append(" number of fields: " + fields.length + "Fields: "); | |
180 | for (int i = 0; i < fields.length; i++) { | |
181 | field = (LttngEventField) fields[i]; | |
63eecb47 | 182 | sb.append(field.getName() + " "); |
5d10d135 ASL |
183 | } |
184 | ||
185 | TraceDebug.debug(sb.toString(), 5); | |
186 | } | |
187 | ||
188 | } |