Commit | Line | Data |
---|---|---|
2237fe8a | 1 | /********************************************************************** |
ed902a2b | 2 | * Copyright (c) 2014, 2015 Ericsson, École Polytechnique de Montréal |
2237fe8a GB |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | * Geneviève Bastien - Memory is per thread and only total is kept | |
12 | **********************************************************************/ | |
13 | ||
116738ad | 14 | package org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.memory; |
2237fe8a | 15 | |
d0c7e4ba AM |
16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
17 | ||
2237fe8a GB |
18 | import java.util.HashMap; |
19 | import java.util.Map; | |
20 | ||
d0c7e4ba | 21 | import org.eclipse.jdt.annotation.NonNull; |
9bc60be7 | 22 | import org.eclipse.tracecompass.lttng2.ust.core.trace.LttngUstTrace; |
7e452c97 | 23 | import org.eclipse.tracecompass.lttng2.ust.core.trace.layout.ILttngUstEventLayout; |
d0c7e4ba | 24 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder; |
e894a508 AM |
25 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
26 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
27 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; | |
28 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
29 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; | |
2bdf0193 AM |
30 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
31 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; | |
32 | import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider; | |
33 | import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider; | |
2237fe8a | 34 | |
7e452c97 AM |
35 | import com.google.common.collect.ImmutableMap; |
36 | ||
2237fe8a GB |
37 | /** |
38 | * State provider to track the memory of the threads using the UST libc wrapper | |
39 | * memory events. | |
40 | * | |
41 | * @author Matthew Khouzam | |
42 | * @author Geneviève Bastien | |
43 | */ | |
116738ad | 44 | public class UstMemoryStateProvider extends AbstractTmfStateProvider { |
2237fe8a GB |
45 | |
46 | /* Version of this state provider */ | |
47 | private static final int VERSION = 1; | |
48 | ||
2237fe8a GB |
49 | private static final Long MINUS_ONE = Long.valueOf(-1); |
50 | private static final Long ZERO = Long.valueOf(0); | |
51 | private static final String EMPTY_STRING = ""; //$NON-NLS-1$ | |
52 | ||
7e452c97 AM |
53 | private static final int MALLOC_INDEX = 1; |
54 | private static final int FREE_INDEX = 2; | |
55 | private static final int CALLOC_INDEX = 3; | |
56 | private static final int REALLOC_INDEX = 4; | |
57 | private static final int MEMALIGN_INDEX = 5; | |
58 | private static final int POSIX_MEMALIGN_INDEX = 6; | |
59 | ||
60 | /** Map of a pointer to a memory zone to the size of the memory */ | |
61 | private final Map<Long, Long> fMemory = new HashMap<>(); | |
62 | ||
63 | private final @NonNull ILttngUstEventLayout fLayout; | |
64 | private final @NonNull Map<String, Integer> fEventNames; | |
65 | ||
2237fe8a GB |
66 | /** |
67 | * Constructor | |
68 | * | |
69 | * @param trace | |
70 | * trace | |
71 | */ | |
116738ad | 72 | public UstMemoryStateProvider(@NonNull LttngUstTrace trace) { |
e2bcc8a5 | 73 | super(trace, "Ust:Memory"); //$NON-NLS-1$ |
7e452c97 AM |
74 | fLayout = trace.getEventLayout(); |
75 | fEventNames = buildEventNames(fLayout); | |
76 | } | |
77 | ||
78 | private static @NonNull Map<String, Integer> buildEventNames(ILttngUstEventLayout layout) { | |
79 | ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); | |
80 | builder.put(layout.eventLibcMalloc(), MALLOC_INDEX); | |
81 | builder.put(layout.eventLibcFree(), FREE_INDEX); | |
82 | builder.put(layout.eventLibcCalloc(), CALLOC_INDEX); | |
83 | builder.put(layout.eventLibcRealloc(), REALLOC_INDEX); | |
84 | builder.put(layout.eventLibcMemalign(), MEMALIGN_INDEX); | |
85 | builder.put(layout.eventLibcPosixMemalign(), POSIX_MEMALIGN_INDEX); | |
0e4f957e | 86 | return builder.build(); |
2237fe8a GB |
87 | } |
88 | ||
89 | @Override | |
90 | protected void eventHandle(ITmfEvent event) { | |
578716e6 | 91 | String name = event.getName(); |
7e452c97 AM |
92 | Integer index = fEventNames.get(name); |
93 | int intIndex = (index == null ? -1 : index.intValue()); | |
94 | ||
95 | switch (intIndex) { | |
96 | case MALLOC_INDEX: { | |
97 | Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue(); | |
2237fe8a GB |
98 | if (ZERO.equals(ptr)) { |
99 | return; | |
100 | } | |
7e452c97 | 101 | Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue(); |
2237fe8a GB |
102 | setMem(event, ptr, size); |
103 | } | |
104 | break; | |
7e452c97 AM |
105 | case FREE_INDEX: { |
106 | Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue(); | |
2237fe8a GB |
107 | if (ZERO.equals(ptr)) { |
108 | return; | |
109 | } | |
110 | setMem(event, ptr, ZERO); | |
111 | } | |
112 | break; | |
7e452c97 AM |
113 | case CALLOC_INDEX: { |
114 | Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue(); | |
2237fe8a GB |
115 | if (ZERO.equals(ptr)) { |
116 | return; | |
117 | } | |
7e452c97 AM |
118 | Long nmemb = (Long) event.getContent().getField(fLayout.fieldNmemb()).getValue(); |
119 | Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue(); | |
2237fe8a GB |
120 | setMem(event, ptr, size * nmemb); |
121 | } | |
122 | break; | |
7e452c97 AM |
123 | case REALLOC_INDEX: { |
124 | Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue(); | |
2237fe8a GB |
125 | if (ZERO.equals(ptr)) { |
126 | return; | |
127 | } | |
7e452c97 AM |
128 | Long newPtr = (Long) event.getContent().getField(fLayout.fieldInPtr()).getValue(); |
129 | Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue(); | |
2237fe8a GB |
130 | setMem(event, ptr, ZERO); |
131 | setMem(event, newPtr, size); | |
132 | } | |
133 | break; | |
7e452c97 AM |
134 | case MEMALIGN_INDEX: { |
135 | Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue(); | |
2237fe8a GB |
136 | if (ZERO.equals(ptr)) { |
137 | return; | |
138 | } | |
7e452c97 | 139 | Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue(); |
2237fe8a GB |
140 | setMem(event, ptr, size); |
141 | } | |
142 | break; | |
7e452c97 AM |
143 | case POSIX_MEMALIGN_INDEX: { |
144 | Long ptr = (Long) event.getContent().getField(fLayout.fieldOutPtr()).getValue(); | |
2237fe8a GB |
145 | if (ZERO.equals(ptr)) { |
146 | return; | |
147 | } | |
7e452c97 | 148 | Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue(); |
2237fe8a GB |
149 | setMem(event, ptr, size); |
150 | } | |
151 | break; | |
152 | default: | |
7e452c97 | 153 | /* Ignore other event types */ |
2237fe8a GB |
154 | break; |
155 | } | |
156 | ||
157 | } | |
158 | ||
159 | @Override | |
160 | public ITmfStateProvider getNewInstance() { | |
116738ad | 161 | return new UstMemoryStateProvider(getTrace()); |
2237fe8a GB |
162 | } |
163 | ||
164 | @Override | |
165 | public LttngUstTrace getTrace() { | |
166 | return (LttngUstTrace) super.getTrace(); | |
167 | } | |
168 | ||
169 | @Override | |
170 | public int getVersion() { | |
171 | return VERSION; | |
172 | } | |
173 | ||
7e452c97 AM |
174 | private Long getVtid(ITmfEvent event) { |
175 | ITmfEventField field = event.getContent().getField(fLayout.contextVtid()); | |
2237fe8a GB |
176 | if (field == null) { |
177 | return MINUS_ONE; | |
178 | } | |
179 | return (Long) field.getValue(); | |
180 | } | |
181 | ||
7e452c97 AM |
182 | private String getProcname(ITmfEvent event) { |
183 | ITmfEventField field = event.getContent().getField(fLayout.contextProcname()); | |
2237fe8a GB |
184 | if (field == null) { |
185 | return EMPTY_STRING; | |
186 | } | |
187 | return (String) field.getValue(); | |
188 | } | |
189 | ||
190 | private void setMem(ITmfEvent event, Long ptr, Long size) { | |
d0c7e4ba | 191 | ITmfStateSystemBuilder ss = checkNotNull(getStateSystemBuilder()); |
2237fe8a GB |
192 | long ts = event.getTimestamp().getValue(); |
193 | Long tid = getVtid(event); | |
194 | ||
195 | Long memoryDiff = size; | |
196 | /* Size is 0, it means it was deleted */ | |
197 | if (ZERO.equals(size)) { | |
198 | Long memSize = fMemory.remove(ptr); | |
199 | if (memSize == null) { | |
200 | return; | |
201 | } | |
202 | memoryDiff = -memSize; | |
203 | } else { | |
204 | fMemory.put(ptr, size); | |
205 | } | |
206 | try { | |
207 | int tidQuark = ss.getQuarkAbsoluteAndAdd(tid.toString()); | |
208 | int tidMemQuark = ss.getQuarkRelativeAndAdd(tidQuark, UstMemoryStrings.UST_MEMORY_MEMORY_ATTRIBUTE); | |
209 | ||
210 | ITmfStateValue prevMem = ss.queryOngoingState(tidMemQuark); | |
211 | /* First time we set this value */ | |
212 | if (prevMem.isNull()) { | |
213 | int procNameQuark = ss.getQuarkRelativeAndAdd(tidQuark, UstMemoryStrings.UST_MEMORY_PROCNAME_ATTRIBUTE); | |
214 | String procName = getProcname(event); | |
215 | /* | |
216 | * No tid/procname for the event for the event, added to a | |
217 | * 'others' thread | |
218 | */ | |
219 | if (tid.equals(MINUS_ONE)) { | |
220 | procName = UstMemoryStrings.OTHERS; | |
221 | } | |
222 | ss.modifyAttribute(ts, TmfStateValue.newValueString(procName), procNameQuark); | |
223 | prevMem = TmfStateValue.newValueLong(0); | |
224 | } | |
225 | ||
226 | long prevMemValue = prevMem.unboxLong(); | |
227 | prevMemValue += memoryDiff.longValue(); | |
228 | ss.modifyAttribute(ts, TmfStateValue.newValueLong(prevMemValue), tidMemQuark); | |
229 | } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) { | |
230 | throw new IllegalStateException(e); | |
231 | } | |
232 | } | |
233 | ||
234 | } |