lttng: Move plugins to their own sub-directory
[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 java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.memory.UstMemoryStrings;
23 import org.eclipse.tracecompass.internal.tmf.core.Activator;
24 import org.eclipse.tracecompass.lttng2.ust.core.analysis.memory.UstMemoryAnalysisModule;
25 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
26 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
27 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
28 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
29 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
30 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
31 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
32 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
33 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
34 import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer;
35
36 /**
37 * Memory usage view
38 *
39 * @author Matthew Khouzam
40 */
41 @SuppressWarnings("restriction")
42 public 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
52 // Timeout between updates in the updateData thread
53 private static final long BUILD_UPDATE_TIMEOUT = 500;
54
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() {
67 ITmfTrace trace = getTrace();
68 if (trace != null) {
69 fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, TmfStateSystemAnalysisModule.class, UstMemoryAnalysisModule.ID);
70 if (fModule == null) {
71 return;
72 }
73 fModule.schedule();
74 }
75 }
76
77 @Override
78 protected void updateData(long start, long end, int nb, IProgressMonitor monitor) {
79 try {
80 if (getTrace() == null || fModule == null) {
81 return;
82 }
83 fModule.waitForInitialization();
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 }
89
90 double[] xvalues = getXAxis(start, end, nb);
91 setXAxis(xvalues);
92
93 boolean complete = false;
94 long currentEnd = start;
95
96 while (!complete && currentEnd < end) {
97 if (monitor.isCanceled()) {
98 return;
99 }
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();
106
107 /* Initialize quarks and series names */
108 for (int quark : tidQuarks) {
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);
112 try {
113 ITmfStateValue procnameValue = ss.querySingleState(start, procNameQuark).getStateValue();
114 String procname = ""; //$NON-NLS-1$
115 if (!procnameValue.isNull()) {
116 procname = procnameValue.unboxStr();
117 }
118 fSeriesName.put(quark, new String(procname + ' ' + '(' + ss.getAttributeName(quark) + ')').trim());
119 } catch (TimeRangeException e) {
120 fSeriesName.put(quark, '(' + ss.getAttributeName(quark) + ')');
121 }
122 }
123
124 /*
125 * TODO: It should only show active threads in the time range.
126 * If a tid does not have any memory value (only 1 interval in
127 * the time range with value null or 0), then its series should
128 * not be 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();
155 }
156 } catch (AttributeNotFoundException | StateValueTypeException e) {
157 Activator.logError("Error updating the data of the Memory usage view", e); //$NON-NLS-1$
158 } catch (StateSystemDisposedException e) {
159 /* State system is closing down, no point continuing */
160 }
161 }
162
163 }
This page took 0.039455 seconds and 6 git commands to generate.