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