tmf: Bug 490400: Leaking widgets due to incorrect cleanup in dispose()
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.ust.ui / src / org / eclipse / tracecompass / internal / lttng2 / ust / ui / views / memusage / MemoryUsageViewer.java
CommitLineData
5db8e1e9
GB
1/**********************************************************************
2 * Copyright (c) 2014 Ericsson, École Polytechnique de Montréal
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 * Bernd Hufmann - Initial API and implementation
11 * Geneviève Bastien - Create and use base class for XY plots
12 **********************************************************************/
13
9bc60be7 14package org.eclipse.tracecompass.internal.lttng2.ust.ui.views.memusage;
5db8e1e9 15
202956f1
AM
16import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
5db8e1e9
GB
18import java.util.HashMap;
19import java.util.List;
20import java.util.Map;
21
00968516 22import org.eclipse.core.runtime.IProgressMonitor;
5db8e1e9 23import org.eclipse.swt.widgets.Composite;
116738ad 24import org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.memory.UstMemoryStrings;
2bdf0193 25import org.eclipse.tracecompass.internal.tmf.core.Activator;
9bc60be7 26import org.eclipse.tracecompass.lttng2.ust.core.analysis.memory.UstMemoryAnalysisModule;
e894a508
AM
27import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
28import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
29import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
30import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
31import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
32import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
2bdf0193 33import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
1d83ed07 34import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
b8585c7c 35import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
2bdf0193 36import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer;
5db8e1e9
GB
37
38/**
39 * Memory usage view
40 *
41 * @author Matthew Khouzam
42 */
43@SuppressWarnings("restriction")
44public class MemoryUsageViewer extends TmfCommonXLineChartViewer {
45
46 private TmfStateSystemAnalysisModule fModule = null;
47
48 private final Map<Integer, double[]> fYValues = new HashMap<>();
49 private final Map<Integer, Integer> fMemoryQuarks = new HashMap<>();
50 private final Map<Integer, String> fSeriesName = new HashMap<>();
51
52 private static final int BYTES_TO_KB = 1024;
53
261af2c6
GB
54 // Timeout between updates in the updateData thread
55 private static final long BUILD_UPDATE_TIMEOUT = 500;
56
5db8e1e9
GB
57 /**
58 * Constructor
59 *
60 * @param parent
61 * parent view
62 */
63 public MemoryUsageViewer(Composite parent) {
64 super(parent, Messages.MemoryUsageViewer_Title, Messages.MemoryUsageViewer_XAxis, Messages.MemoryUsageViewer_YAxis);
65 }
66
67 @Override
68 protected void initializeDataSource() {
1d83ed07
AM
69 ITmfTrace trace = getTrace();
70 if (trace != null) {
71 fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, TmfStateSystemAnalysisModule.class, UstMemoryAnalysisModule.ID);
5db8e1e9
GB
72 if (fModule == null) {
73 return;
74 }
75 fModule.schedule();
76 }
77 }
78
79 @Override
00968516 80 protected void updateData(long start, long end, int nb, IProgressMonitor monitor) {
5db8e1e9
GB
81 try {
82 if (getTrace() == null || fModule == null) {
83 return;
84 }
c81ffdf2
JCK
85 if (!fModule.waitForInitialization()) {
86 return;
87 }
5db8e1e9
GB
88 ITmfStateSystem ss = fModule.getStateSystem();
89 /* Don't wait for the module completion, when it's ready, we'll know */
90 if (ss == null) {
91 return;
92 }
261af2c6 93
5db8e1e9
GB
94 double[] xvalues = getXAxis(start, end, nb);
95 setXAxis(xvalues);
5db8e1e9 96
261af2c6
GB
97 boolean complete = false;
98 long currentEnd = start;
99
100 while (!complete && currentEnd < end) {
00968516
GB
101 if (monitor.isCanceled()) {
102 return;
103 }
261af2c6
GB
104 complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
105 currentEnd = ss.getCurrentEndTime();
106 List<Integer> tidQuarks = ss.getSubAttributes(-1, false);
107 long traceStart = getStartTime();
108 long traceEnd = getEndTime();
109 long offset = this.getTimeOffset();
5db8e1e9 110
261af2c6 111 /* Initialize quarks and series names */
5db8e1e9 112 for (int quark : tidQuarks) {
261af2c6
GB
113 fYValues.put(quark, new double[xvalues.length]);
114 fMemoryQuarks.put(quark, ss.getQuarkRelative(quark, UstMemoryStrings.UST_MEMORY_MEMORY_ATTRIBUTE));
115 int procNameQuark = ss.getQuarkRelative(quark, UstMemoryStrings.UST_MEMORY_PROCNAME_ATTRIBUTE);
6206fd75
MAL
116 String oldSeriesName = fSeriesName.get(quark);
117 String seriesName = null;
5db8e1e9 118 try {
261af2c6 119 ITmfStateValue procnameValue = ss.querySingleState(start, procNameQuark).getStateValue();
1157a348 120 String procname = ""; //$NON-NLS-1$
261af2c6
GB
121 if (!procnameValue.isNull()) {
122 procname = procnameValue.unboxStr();
123 }
6206fd75 124 seriesName = (procname + ' ' + '(' + ss.getAttributeName(quark) + ')').trim();
5db8e1e9 125 } catch (TimeRangeException e) {
6206fd75 126 seriesName = '(' + ss.getAttributeName(quark) + ')';
5db8e1e9 127 }
6206fd75
MAL
128
129 if (oldSeriesName != null && !oldSeriesName.equals(seriesName)) {
130 deleteSeries(oldSeriesName);
131 }
132 fSeriesName.put(quark, seriesName);
5db8e1e9 133 }
261af2c6
GB
134
135 /*
1157a348
ACL
136 * TODO: It should only show active threads in the time range.
137 * If a tid does not have any memory value (only 1 interval in
138 * the time range with value null or 0), then its series should
139 * not be displayed.
261af2c6
GB
140 */
141 double yvalue = 0.0;
142 for (int i = 0; i < xvalues.length; i++) {
143 if (monitor.isCanceled()) {
144 return;
145 }
146 double x = xvalues[i];
147 long time = (long) x + offset;
148 // make sure that time is in the trace range after double to
149 // long conversion
150 time = time < traceStart ? traceStart : time;
151 time = time > traceEnd ? traceEnd : time;
152
153 for (int quark : tidQuarks) {
202956f1 154 double[] values = checkNotNull(fYValues.get(quark));
261af2c6 155 try {
202956f1
AM
156 Integer memQuark = checkNotNull(fMemoryQuarks.get(quark));
157 yvalue = ss.querySingleState(time, memQuark.intValue()).getStateValue().unboxLong() / BYTES_TO_KB;
158 values[i] = yvalue;
261af2c6 159 } catch (TimeRangeException e) {
202956f1 160 values[i] = 0;
261af2c6
GB
161 }
162 }
163 }
164 for (int quark : tidQuarks) {
165 setSeries(fSeriesName.get(quark), fYValues.get(quark));
166 }
167 updateDisplay();
5db8e1e9 168 }
e1c415b3 169 } catch (AttributeNotFoundException | StateValueTypeException e) {
5db8e1e9 170 Activator.logError("Error updating the data of the Memory usage view", e); //$NON-NLS-1$
e1c415b3
BH
171 } catch (StateSystemDisposedException e) {
172 /* State system is closing down, no point continuing */
5db8e1e9
GB
173 }
174 }
175
176}
This page took 0.066226 seconds and 5 git commands to generate.