analysis: bug 494786. Clear process selection when switching trace
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / internal / analysis / os / linux / ui / views / kernelmemoryusage / KernelMemoryUsageViewer.java
CommitLineData
aa19e48b
NA
1/**********************************************************************
2 * Copyright (c) 2016 É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 **********************************************************************/
9package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.kernelmemoryusage;
10
aa19e48b
NA
11import java.util.List;
12
13import org.eclipse.core.runtime.IProgressMonitor;
14import org.eclipse.swt.SWT;
15import org.eclipse.swt.widgets.Composite;
16import org.eclipse.tracecompass.analysis.os.linux.core.kernelmemoryusage.KernelMemoryAnalysisModule;
f937c19c 17import org.eclipse.tracecompass.common.core.format.DataSizeWithUnitFormat;
aa19e48b
NA
18import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator;
19import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
20import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
21import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
22import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
23import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
629bf3c1
JCK
24import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
25import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
26import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
aa19e48b
NA
27import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
28import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
29import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
30import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer;
31import org.swtchart.Chart;
32
33/**
34 * Memory usage view
35 *
36 * @author Samuel Gagnon
37 * @author Wassim Nasrallah
38 */
39public class KernelMemoryUsageViewer extends TmfCommonXLineChartViewer {
40
aa19e48b
NA
41 private static final String NOT_SELECTED = "-1"; //$NON-NLS-1$
42
43 private TmfStateSystemAnalysisModule fModule = null;
44 private String fSelectedThread = NOT_SELECTED;
45
46 /**
47 * Constructor
48 *
49 * @param parent
50 * parent view
51 */
52 public KernelMemoryUsageViewer(Composite parent) {
53 super(parent, Messages.MemoryUsageViewer_title, Messages.MemoryUsageViewer_xAxis, Messages.MemoryUsageViewer_yAxis);
54 Chart chart = getSwtChart();
c81aca6d 55 chart.getAxisSet().getYAxis(0).getTick().setFormat(DataSizeWithUnitFormat.getInstance());
aa19e48b
NA
56 chart.getLegend().setPosition(SWT.BOTTOM);
57 }
58
59 @Override
60 protected void initializeDataSource() {
61 ITmfTrace trace = getTrace();
62 if (trace != null) {
63 fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, TmfStateSystemAnalysisModule.class, KernelMemoryAnalysisModule.ID);
64 if (fModule == null) {
65 return;
66 }
67 fModule.schedule();
68 }
69 }
70
71 @Override
72 protected void updateData(long start, long end, int nb, IProgressMonitor monitor) {
73 TmfStateSystemAnalysisModule module = fModule;
74 if (getTrace() == null || module == null) {
75 return;
76 }
77
78 if (!module.waitForInitialization()) {
79 return;
80 }
81
82 ITmfStateSystem ss = module.getStateSystem();
83 if (ss == null) {
63949239 84 throw new IllegalStateException("No state system for the module " + module.toString()); //$NON-NLS-1$
aa19e48b
NA
85 }
86
87 double[] xvalues = getXAxis(start, end, nb);
88 if (xvalues.length == 0) {
89 return;
90 }
63949239
MK
91 long clampedEnd = Math.min(end, ss.getCurrentEndTime());
92 if (clampedEnd < ss.getStartTime()) {
93 return;
94 }
aa19e48b
NA
95 setXAxis(xvalues);
96
97 try {
98 /**
99 * For a given time range, we plot two lines representing the memory
100 * allocation. The first line represent the total memory allocation
101 * of every process. The second line represent the memory allocation
102 * of the selected thread.
103 */
104 double[] totalKernelMemoryValues = new double[xvalues.length];
105 double[] selectedThreadValues = new double[xvalues.length];
106 for (int i = 0; i < xvalues.length; i++) {
107 if (monitor.isCanceled()) {
108 return;
109 }
110
111 double x = xvalues[i];
112 long t = (long) x + getTimeOffset();
63949239
MK
113 if( ss.getCurrentEndTime() < t || ss.getStartTime() > t) {
114 selectedThreadValues[i] = 0;
115 continue;
116 }
aa19e48b
NA
117 List<ITmfStateInterval> kernelState = ss.queryFullState(t);
118
119 /* The subattributes of the root are the different threads */
120 List<Integer> threadQuarkList = ss.getSubAttributes(-1, false);
121 /* We add the value of each thread to the total quantity */
122 for (Integer threadQuark : threadQuarkList) {
123 ITmfStateInterval threadMemoryInterval = kernelState.get(threadQuark);
124 long value = threadMemoryInterval.getStateValue().unboxLong();
125 totalKernelMemoryValues[i] += value;
126
127 String tid = ss.getAttributeName(threadQuark);
128 if (tid.equals(fSelectedThread)) {
129 selectedThreadValues[i] = value;
130 }
131 }
132 }
133
134 /**
135 * For each thread, we look for its lowest value since the beginning
136 * of the trace. This way, we can avoid negative values in the plot.
137 */
138 double totalKernelMemoryValuesShift = 0;
139 double selectThreadValuesShift = 0;
140
141 /*
142 * The lowest value we are searching is at the end of the current
143 * selected zone
144 */
63949239 145 List<ITmfStateInterval> kernelState = ss.queryFullState(clampedEnd);
aa19e48b
NA
146 List<Integer> threadQuarkList = ss.getSubAttributes(-1, false);
147 /* We add the lowest value of each thread */
148 for (Integer threadQuark : threadQuarkList) {
149 int lowestMemoryQuark = ss.getQuarkRelative(threadQuark, KernelMemoryAnalysisModule.THREAD_LOWEST_MEMORY_VALUE);
150 ITmfStateInterval lowestMemoryInterval = kernelState.get(lowestMemoryQuark);
151 long lowestMemoryValue = lowestMemoryInterval.getStateValue().unboxLong();
152 // We want to add up a positive quantity.
153 totalKernelMemoryValuesShift -= lowestMemoryValue;
154
155 String tid = ss.getAttributeName(threadQuark);
156 if (tid.equals(fSelectedThread)) {
157 // We want to add up a positive quantity.
158 selectThreadValuesShift = -lowestMemoryValue;
159 }
160 }
161
162 /**
163 * We shift the two displayed lines up.
164 */
165 for (int i = 0; i < xvalues.length; i++) {
166 totalKernelMemoryValues[i] += totalKernelMemoryValuesShift;
167 selectedThreadValues[i] += selectThreadValuesShift;
168 }
169 setSeries(Messages.MemoryUsageViewer_Total, totalKernelMemoryValues);
170 if (fSelectedThread != NOT_SELECTED) {
171 setSeries(fSelectedThread, selectedThreadValues);
172 }
173 updateDisplay();
174
175 } catch (TimeRangeException | StateSystemDisposedException | AttributeNotFoundException e) {
176 Activator.getDefault().logError(e.getMessage(), e);
177 }
178 }
179
180 /**
181 * Set the selected thread ID, which will be graphed in this viewer
182 *
183 * @param tid
184 * The selected thread ID
185 */
186 public void setSelectedThread(String tid) {
187 cancelUpdate();
188 deleteSeries(fSelectedThread);
189 fSelectedThread = tid;
190 updateContent();
191 }
192
629bf3c1
JCK
193 @Override
194 @TmfSignalHandler
195 public void traceSelected(TmfTraceSelectedSignal signal) {
196 setSelectedThread(NOT_SELECTED);
197 super.traceSelected(signal);
198 }
199
200 @Override
201 @TmfSignalHandler
202 public void traceOpened(TmfTraceOpenedSignal signal) {
203 setSelectedThread(NOT_SELECTED);
204 super.traceOpened(signal);
205 }
206
aa19e48b 207}
This page took 0.035834 seconds and 5 git commands to generate.