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
.internal
.tmf
.core
.Activator
;
23 import org
.eclipse
.linuxtools
.lttng2
.ust
.core
.analysis
.memory
.UstMemoryAnalysisModule
;
24 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystem
;
25 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
26 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
27 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateValueTypeException
;
28 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.TimeRangeException
;
29 import org
.eclipse
.linuxtools
.statesystem
.core
.statevalue
.ITmfStateValue
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
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;
56 public MemoryUsageViewer(Composite parent
) {
57 super(parent
, Messages
.MemoryUsageViewer_Title
, Messages
.MemoryUsageViewer_XAxis
, Messages
.MemoryUsageViewer_YAxis
);
61 protected void initializeDataSource() {
62 if (getTrace() != null) {
63 fModule
= getTrace().getAnalysisModuleOfClass(TmfStateSystemAnalysisModule
.class, UstMemoryAnalysisModule
.ID
);
64 if (fModule
== null) {
72 protected void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
) {
74 if (getTrace() == null || fModule
== null) {
77 ITmfStateSystem ss
= fModule
.getStateSystem();
78 /* Don't wait for the module completion, when it's ready, we'll know */
82 double[] xvalues
= getXAxis(start
, end
, nb
);
84 List
<Integer
> tidQuarks
= ss
.getSubAttributes(-1, false);
85 long traceStart
= getStartTime();
86 long traceEnd
= getEndTime();
87 long offset
= this.getTimeOffset();
89 /* Initialize quarks and series names */
90 for (int quark
: tidQuarks
) {
91 fYValues
.put(quark
, new double[xvalues
.length
]);
92 fMemoryQuarks
.put(quark
, ss
.getQuarkRelative(quark
, UstMemoryStrings
.UST_MEMORY_MEMORY_ATTRIBUTE
));
93 int procNameQuark
= ss
.getQuarkRelative(quark
, UstMemoryStrings
.UST_MEMORY_PROCNAME_ATTRIBUTE
);
95 ITmfStateValue procnameValue
= ss
.querySingleState(start
, procNameQuark
).getStateValue();
96 String procname
= new String();
97 if (!procnameValue
.isNull()) {
98 procname
= procnameValue
.unboxStr();
100 fSeriesName
.put(quark
, new String(procname
+ ' ' + '(' + ss
.getAttributeName(quark
) + ')').trim());
101 } catch (TimeRangeException e
) {
102 fSeriesName
.put(quark
, '(' + ss
.getAttributeName(quark
) + ')');
107 * TODO: It should only show active threads in the time range. If a
108 * tid does not have any memory value (only 1 interval in the time
109 * range with value null or 0), then its series should not be
113 for (int i
= 0; i
< xvalues
.length
; i
++) {
114 if (monitor
.isCanceled()) {
117 double x
= xvalues
[i
];
118 long time
= (long) x
+ offset
;
119 // make sure that time is in the trace range after double to
121 time
= time
< traceStart ? traceStart
: time
;
122 time
= time
> traceEnd ? traceEnd
: time
;
124 for (int quark
: tidQuarks
) {
126 yvalue
= ss
.querySingleState(time
, fMemoryQuarks
.get(quark
)).getStateValue().unboxLong() / BYTES_TO_KB
;
127 fYValues
.get(quark
)[i
] = yvalue
;
128 } catch (TimeRangeException e
) {
129 fYValues
.get(quark
)[i
] = 0;
133 for (int quark
: tidQuarks
) {
134 setSeries(fSeriesName
.get(quark
), fYValues
.get(quark
));
137 } catch (AttributeNotFoundException
| StateValueTypeException
| StateSystemDisposedException e
) {
138 Activator
.logError("Error updating the data of the Memory usage view", e
); //$NON-NLS-1$