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
.linuxtools
.internal
.lttng2
.ust
.ui
.views
.memusage
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.linuxtools
.internal
.lttng2
.ust
.core
.memoryusage
.UstMemoryStrings
;
22 import org
.eclipse
.linuxtools
.lttng2
.ust
.core
.analysis
.memory
.UstMemoryAnalysisModule
;
23 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystem
;
24 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
25 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
26 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateValueTypeException
;
27 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.TimeRangeException
;
28 import org
.eclipse
.linuxtools
.statesystem
.core
.statevalue
.ITmfStateValue
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
32 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
37 * @author Matthew Khouzam
39 @SuppressWarnings("restriction")
40 public class MemoryUsageViewer
extends TmfCommonXLineChartViewer
{
42 private TmfStateSystemAnalysisModule fModule
= null;
44 private final Map
<Integer
, double[]> fYValues
= new HashMap
<>();
45 private final Map
<Integer
, Integer
> fMemoryQuarks
= new HashMap
<>();
46 private final Map
<Integer
, String
> fSeriesName
= new HashMap
<>();
48 private static final int BYTES_TO_KB
= 1024;
50 // Timeout between updates in the updateData thread
51 private static final long BUILD_UPDATE_TIMEOUT
= 500;
59 public MemoryUsageViewer(Composite parent
) {
60 super(parent
, Messages
.MemoryUsageViewer_Title
, Messages
.MemoryUsageViewer_XAxis
, Messages
.MemoryUsageViewer_YAxis
);
64 protected void initializeDataSource() {
65 if (getTrace() != null) {
66 fModule
= getTrace().getAnalysisModuleOfClass(TmfStateSystemAnalysisModule
.class, UstMemoryAnalysisModule
.ID
);
67 if (fModule
== null) {
75 protected void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
) {
77 if (getTrace() == null || fModule
== null) {
80 fModule
.waitForInitialization();
81 ITmfStateSystem ss
= fModule
.getStateSystem();
82 /* Don't wait for the module completion, when it's ready, we'll know */
87 double[] xvalues
= getXAxis(start
, end
, nb
);
90 boolean complete
= false;
91 long currentEnd
= start
;
93 while (!complete
&& currentEnd
< end
) {
94 if (monitor
.isCanceled()) {
97 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
98 currentEnd
= ss
.getCurrentEndTime();
99 List
<Integer
> tidQuarks
= ss
.getSubAttributes(-1, false);
100 long traceStart
= getStartTime();
101 long traceEnd
= getEndTime();
102 long offset
= this.getTimeOffset();
104 /* Initialize quarks and series names */
105 for (int quark
: tidQuarks
) {
106 fYValues
.put(quark
, new double[xvalues
.length
]);
107 fMemoryQuarks
.put(quark
, ss
.getQuarkRelative(quark
, UstMemoryStrings
.UST_MEMORY_MEMORY_ATTRIBUTE
));
108 int procNameQuark
= ss
.getQuarkRelative(quark
, UstMemoryStrings
.UST_MEMORY_PROCNAME_ATTRIBUTE
);
110 ITmfStateValue procnameValue
= ss
.querySingleState(start
, procNameQuark
).getStateValue();
111 String procname
= new String();
112 if (!procnameValue
.isNull()) {
113 procname
= procnameValue
.unboxStr();
115 fSeriesName
.put(quark
, new String(procname
+ ' ' + '(' + ss
.getAttributeName(quark
) + ')').trim());
116 } catch (TimeRangeException e
) {
117 fSeriesName
.put(quark
, '(' + ss
.getAttributeName(quark
) + ')');
122 * TODO: It should only show active threads in the time range. If a
123 * tid does not have any memory value (only 1 interval in the time
124 * range with value null or 0), then its series should not be
128 for (int i
= 0; i
< xvalues
.length
; i
++) {
129 if (monitor
.isCanceled()) {
132 double x
= xvalues
[i
];
133 long time
= (long) x
+ offset
;
134 // make sure that time is in the trace range after double to
136 time
= time
< traceStart ? traceStart
: time
;
137 time
= time
> traceEnd ? traceEnd
: time
;
139 for (int quark
: tidQuarks
) {
141 yvalue
= ss
.querySingleState(time
, fMemoryQuarks
.get(quark
)).getStateValue().unboxLong() / BYTES_TO_KB
;
142 fYValues
.get(quark
)[i
] = yvalue
;
143 } catch (TimeRangeException e
) {
144 fYValues
.get(quark
)[i
] = 0;
148 for (int quark
: tidQuarks
) {
149 setSeries(fSeriesName
.get(quark
), fYValues
.get(quark
));
153 } catch (AttributeNotFoundException
| StateValueTypeException
| StateSystemDisposedException e
) {
154 Activator
.logError("Error updating the data of the Memory usage view", e
); //$NON-NLS-1$