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