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