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 java
.util
.HashMap
;
17 import java
.util
.List
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.swt
.widgets
.Composite
;
22 import org
.eclipse
.tracecompass
.internal
.lttng2
.ust
.core
.memoryusage
.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
.TmfTraceUtils
;
33 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
38 * @author Matthew Khouzam
40 @SuppressWarnings("restriction")
41 public class MemoryUsageViewer
extends TmfCommonXLineChartViewer
{
43 private TmfStateSystemAnalysisModule fModule
= null;
45 private final Map
<Integer
, double[]> fYValues
= new HashMap
<>();
46 private final Map
<Integer
, Integer
> fMemoryQuarks
= new HashMap
<>();
47 private final Map
<Integer
, String
> fSeriesName
= new HashMap
<>();
49 private static final int BYTES_TO_KB
= 1024;
51 // Timeout between updates in the updateData thread
52 private static final long BUILD_UPDATE_TIMEOUT
= 500;
60 public MemoryUsageViewer(Composite parent
) {
61 super(parent
, Messages
.MemoryUsageViewer_Title
, Messages
.MemoryUsageViewer_XAxis
, Messages
.MemoryUsageViewer_YAxis
);
65 protected void initializeDataSource() {
66 if (getTrace() != null) {
67 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(getTrace(), TmfStateSystemAnalysisModule
.class, UstMemoryAnalysisModule
.ID
);
68 if (fModule
== null) {
76 protected void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
) {
78 if (getTrace() == null || fModule
== null) {
81 fModule
.waitForInitialization();
82 ITmfStateSystem ss
= fModule
.getStateSystem();
83 /* Don't wait for the module completion, when it's ready, we'll know */
88 double[] xvalues
= getXAxis(start
, end
, nb
);
91 boolean complete
= false;
92 long currentEnd
= start
;
94 while (!complete
&& currentEnd
< end
) {
95 if (monitor
.isCanceled()) {
98 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
99 currentEnd
= ss
.getCurrentEndTime();
100 List
<Integer
> tidQuarks
= ss
.getSubAttributes(-1, false);
101 long traceStart
= getStartTime();
102 long traceEnd
= getEndTime();
103 long offset
= this.getTimeOffset();
105 /* Initialize quarks and series names */
106 for (int quark
: tidQuarks
) {
107 fYValues
.put(quark
, new double[xvalues
.length
]);
108 fMemoryQuarks
.put(quark
, ss
.getQuarkRelative(quark
, UstMemoryStrings
.UST_MEMORY_MEMORY_ATTRIBUTE
));
109 int procNameQuark
= ss
.getQuarkRelative(quark
, UstMemoryStrings
.UST_MEMORY_PROCNAME_ATTRIBUTE
);
111 ITmfStateValue procnameValue
= ss
.querySingleState(start
, procNameQuark
).getStateValue();
112 String procname
= new String();
113 if (!procnameValue
.isNull()) {
114 procname
= procnameValue
.unboxStr();
116 fSeriesName
.put(quark
, new String(procname
+ ' ' + '(' + ss
.getAttributeName(quark
) + ')').trim());
117 } catch (TimeRangeException e
) {
118 fSeriesName
.put(quark
, '(' + ss
.getAttributeName(quark
) + ')');
123 * TODO: It should only show active threads in the time range. If a
124 * tid does not have any memory value (only 1 interval in the time
125 * range with value null or 0), then its series should not be
129 for (int i
= 0; i
< xvalues
.length
; i
++) {
130 if (monitor
.isCanceled()) {
133 double x
= xvalues
[i
];
134 long time
= (long) x
+ offset
;
135 // make sure that time is in the trace range after double to
137 time
= time
< traceStart ? traceStart
: time
;
138 time
= time
> traceEnd ? traceEnd
: time
;
140 for (int quark
: tidQuarks
) {
142 yvalue
= ss
.querySingleState(time
, fMemoryQuarks
.get(quark
)).getStateValue().unboxLong() / BYTES_TO_KB
;
143 fYValues
.get(quark
)[i
] = yvalue
;
144 } catch (TimeRangeException e
) {
145 fYValues
.get(quark
)[i
] = 0;
149 for (int quark
: tidQuarks
) {
150 setSeries(fSeriesName
.get(quark
), fYValues
.get(quark
));
154 } catch (AttributeNotFoundException
| StateValueTypeException e
) {
155 Activator
.logError("Error updating the data of the Memory usage view", e
); //$NON-NLS-1$
156 } catch (StateSystemDisposedException e
) {
157 /* State system is closing down, no point continuing */