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