Commit | Line | Data |
---|---|---|
5db8e1e9 GB |
1 | /********************************************************************** |
2 | * Copyright (c) 2014 Ericsson, É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 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Initial API and implementation | |
11 | * Geneviève Bastien - Create and use base class for XY plots | |
12 | **********************************************************************/ | |
13 | ||
9bc60be7 | 14 | package org.eclipse.tracecompass.internal.lttng2.ust.ui.views.memusage; |
5db8e1e9 GB |
15 | |
16 | import java.util.HashMap; | |
17 | import java.util.List; | |
18 | import java.util.Map; | |
19 | ||
00968516 | 20 | import org.eclipse.core.runtime.IProgressMonitor; |
5db8e1e9 | 21 | import org.eclipse.swt.widgets.Composite; |
9bc60be7 | 22 | import org.eclipse.tracecompass.internal.lttng2.ust.core.memoryusage.UstMemoryStrings; |
2bdf0193 | 23 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
9bc60be7 | 24 | import org.eclipse.tracecompass.lttng2.ust.core.analysis.memory.UstMemoryAnalysisModule; |
e894a508 AM |
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; | |
2bdf0193 | 31 | import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule; |
b8585c7c | 32 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
2bdf0193 | 33 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer; |
5db8e1e9 GB |
34 | |
35 | /** | |
36 | * Memory usage view | |
37 | * | |
38 | * @author Matthew Khouzam | |
39 | */ | |
40 | @SuppressWarnings("restriction") | |
41 | public class MemoryUsageViewer extends TmfCommonXLineChartViewer { | |
42 | ||
43 | private TmfStateSystemAnalysisModule fModule = null; | |
44 | ||
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<>(); | |
48 | ||
49 | private static final int BYTES_TO_KB = 1024; | |
50 | ||
261af2c6 GB |
51 | // Timeout between updates in the updateData thread |
52 | private static final long BUILD_UPDATE_TIMEOUT = 500; | |
53 | ||
5db8e1e9 GB |
54 | /** |
55 | * Constructor | |
56 | * | |
57 | * @param parent | |
58 | * parent view | |
59 | */ | |
60 | public MemoryUsageViewer(Composite parent) { | |
61 | super(parent, Messages.MemoryUsageViewer_Title, Messages.MemoryUsageViewer_XAxis, Messages.MemoryUsageViewer_YAxis); | |
62 | } | |
63 | ||
64 | @Override | |
65 | protected void initializeDataSource() { | |
66 | if (getTrace() != null) { | |
b8585c7c | 67 | fModule = TmfTraceUtils.getAnalysisModuleOfClass(getTrace(), TmfStateSystemAnalysisModule.class, UstMemoryAnalysisModule.ID); |
5db8e1e9 GB |
68 | if (fModule == null) { |
69 | return; | |
70 | } | |
71 | fModule.schedule(); | |
72 | } | |
73 | } | |
74 | ||
75 | @Override | |
00968516 | 76 | protected void updateData(long start, long end, int nb, IProgressMonitor monitor) { |
5db8e1e9 GB |
77 | try { |
78 | if (getTrace() == null || fModule == null) { | |
79 | return; | |
80 | } | |
261af2c6 | 81 | fModule.waitForInitialization(); |
5db8e1e9 GB |
82 | ITmfStateSystem ss = fModule.getStateSystem(); |
83 | /* Don't wait for the module completion, when it's ready, we'll know */ | |
84 | if (ss == null) { | |
85 | return; | |
86 | } | |
261af2c6 | 87 | |
5db8e1e9 GB |
88 | double[] xvalues = getXAxis(start, end, nb); |
89 | setXAxis(xvalues); | |
5db8e1e9 | 90 | |
261af2c6 GB |
91 | boolean complete = false; |
92 | long currentEnd = start; | |
93 | ||
94 | while (!complete && currentEnd < end) { | |
00968516 GB |
95 | if (monitor.isCanceled()) { |
96 | return; | |
97 | } | |
261af2c6 GB |
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(); | |
5db8e1e9 | 104 | |
261af2c6 | 105 | /* Initialize quarks and series names */ |
5db8e1e9 | 106 | for (int quark : tidQuarks) { |
261af2c6 GB |
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); | |
5db8e1e9 | 110 | try { |
261af2c6 GB |
111 | ITmfStateValue procnameValue = ss.querySingleState(start, procNameQuark).getStateValue(); |
112 | String procname = new String(); | |
113 | if (!procnameValue.isNull()) { | |
114 | procname = procnameValue.unboxStr(); | |
115 | } | |
116 | fSeriesName.put(quark, new String(procname + ' ' + '(' + ss.getAttributeName(quark) + ')').trim()); | |
5db8e1e9 | 117 | } catch (TimeRangeException e) { |
261af2c6 | 118 | fSeriesName.put(quark, '(' + ss.getAttributeName(quark) + ')'); |
5db8e1e9 GB |
119 | } |
120 | } | |
261af2c6 GB |
121 | |
122 | /* | |
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 | |
126 | * displayed. | |
127 | */ | |
128 | double yvalue = 0.0; | |
129 | for (int i = 0; i < xvalues.length; i++) { | |
130 | if (monitor.isCanceled()) { | |
131 | return; | |
132 | } | |
133 | double x = xvalues[i]; | |
134 | long time = (long) x + offset; | |
135 | // make sure that time is in the trace range after double to | |
136 | // long conversion | |
137 | time = time < traceStart ? traceStart : time; | |
138 | time = time > traceEnd ? traceEnd : time; | |
139 | ||
140 | for (int quark : tidQuarks) { | |
141 | try { | |
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; | |
146 | } | |
147 | } | |
148 | } | |
149 | for (int quark : tidQuarks) { | |
150 | setSeries(fSeriesName.get(quark), fYValues.get(quark)); | |
151 | } | |
152 | updateDisplay(); | |
5db8e1e9 | 153 | } |
e1c415b3 | 154 | } catch (AttributeNotFoundException | StateValueTypeException e) { |
5db8e1e9 | 155 | Activator.logError("Error updating the data of the Memory usage view", e); //$NON-NLS-1$ |
e1c415b3 BH |
156 | } catch (StateSystemDisposedException e) { |
157 | /* State system is closing down, no point continuing */ | |
5db8e1e9 GB |
158 | } |
159 | } | |
160 | ||
161 | } |