1 /**********************************************************************
2 * Copyright (c) 2014 Ericsson, École Polytechnique de Montréal
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
10 * Bernd Hufmann - Initial API and implementation
11 * Geneviève Bastien - Create and use base class for XY plots
12 **********************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.lttng2
.ust
.ui
.views
.memusage
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.tracecompass
.internal
.lttng2
.ust
.core
.analysis
.memory
.UstMemoryStrings
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
26 import org
.eclipse
.tracecompass
.lttng2
.ust
.core
.analysis
.memory
.UstMemoryAnalysisModule
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
41 * @author Matthew Khouzam
43 @SuppressWarnings("restriction")
44 public class MemoryUsageViewer
extends TmfCommonXLineChartViewer
{
46 private TmfStateSystemAnalysisModule fModule
= null;
48 private final Map
<Integer
, double[]> fYValues
= new HashMap
<>();
49 private final Map
<Integer
, Integer
> fMemoryQuarks
= new HashMap
<>();
50 private final Map
<Integer
, String
> fSeriesName
= new HashMap
<>();
52 private static final int BYTES_TO_KB
= 1024;
54 // Timeout between updates in the updateData thread
55 private static final long BUILD_UPDATE_TIMEOUT
= 500;
63 public MemoryUsageViewer(Composite parent
) {
64 super(parent
, Messages
.MemoryUsageViewer_Title
, Messages
.MemoryUsageViewer_XAxis
, Messages
.MemoryUsageViewer_YAxis
);
68 protected void initializeDataSource() {
69 ITmfTrace trace
= getTrace();
71 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, TmfStateSystemAnalysisModule
.class, UstMemoryAnalysisModule
.ID
);
72 if (fModule
== null) {
80 protected void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
) {
82 if (getTrace() == null || fModule
== null) {
85 fModule
.waitForInitialization();
86 ITmfStateSystem ss
= fModule
.getStateSystem();
87 /* Don't wait for the module completion, when it's ready, we'll know */
92 double[] xvalues
= getXAxis(start
, end
, nb
);
95 boolean complete
= false;
96 long currentEnd
= start
;
98 while (!complete
&& currentEnd
< end
) {
99 if (monitor
.isCanceled()) {
102 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
103 currentEnd
= ss
.getCurrentEndTime();
104 List
<Integer
> tidQuarks
= ss
.getSubAttributes(-1, false);
105 long traceStart
= getStartTime();
106 long traceEnd
= getEndTime();
107 long offset
= this.getTimeOffset();
109 /* Initialize quarks and series names */
110 for (int quark
: tidQuarks
) {
111 fYValues
.put(quark
, new double[xvalues
.length
]);
112 fMemoryQuarks
.put(quark
, ss
.getQuarkRelative(quark
, UstMemoryStrings
.UST_MEMORY_MEMORY_ATTRIBUTE
));
113 int procNameQuark
= ss
.getQuarkRelative(quark
, UstMemoryStrings
.UST_MEMORY_PROCNAME_ATTRIBUTE
);
115 ITmfStateValue procnameValue
= ss
.querySingleState(start
, procNameQuark
).getStateValue();
116 String procname
= ""; //$NON-NLS-1$
117 if (!procnameValue
.isNull()) {
118 procname
= procnameValue
.unboxStr();
120 fSeriesName
.put(quark
, new String(procname
+ ' ' + '(' + ss
.getAttributeName(quark
) + ')').trim());
121 } catch (TimeRangeException e
) {
122 fSeriesName
.put(quark
, '(' + ss
.getAttributeName(quark
) + ')');
127 * TODO: It should only show active threads in the time range.
128 * If a tid does not have any memory value (only 1 interval in
129 * the time range with value null or 0), then its series should
133 for (int i
= 0; i
< xvalues
.length
; i
++) {
134 if (monitor
.isCanceled()) {
137 double x
= xvalues
[i
];
138 long time
= (long) x
+ offset
;
139 // make sure that time is in the trace range after double to
141 time
= time
< traceStart ? traceStart
: time
;
142 time
= time
> traceEnd ? traceEnd
: time
;
144 for (int quark
: tidQuarks
) {
145 double[] values
= checkNotNull(fYValues
.get(quark
));
147 Integer memQuark
= checkNotNull(fMemoryQuarks
.get(quark
));
148 yvalue
= ss
.querySingleState(time
, memQuark
.intValue()).getStateValue().unboxLong() / BYTES_TO_KB
;
150 } catch (TimeRangeException e
) {
155 for (int quark
: tidQuarks
) {
156 setSeries(fSeriesName
.get(quark
), fYValues
.get(quark
));
160 } catch (AttributeNotFoundException
| StateValueTypeException e
) {
161 Activator
.logError("Error updating the data of the Memory usage view", e
); //$NON-NLS-1$
162 } catch (StateSystemDisposedException e
) {
163 /* State system is closing down, no point continuing */