tmf: Split the state system in a separate plugin
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ust.ui / src / org / eclipse / linuxtools / internal / lttng2 / ust / ui / views / memusage / MemoryUsageViewer.java
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
14 package org.eclipse.linuxtools.internal.lttng2.ust.ui.views.memusage;
15
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
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;
33
34 /**
35 * Memory usage view
36 *
37 * @author Matthew Khouzam
38 */
39 @SuppressWarnings("restriction")
40 public class MemoryUsageViewer extends TmfCommonXLineChartViewer {
41
42 private TmfStateSystemAnalysisModule fModule = null;
43
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<>();
47
48 private static final int BYTES_TO_KB = 1024;
49
50 /**
51 * Constructor
52 *
53 * @param parent
54 * parent view
55 */
56 public MemoryUsageViewer(Composite parent) {
57 super(parent, Messages.MemoryUsageViewer_Title, Messages.MemoryUsageViewer_XAxis, Messages.MemoryUsageViewer_YAxis);
58 }
59
60 @Override
61 protected void initializeDataSource() {
62 if (getTrace() != null) {
63 fModule = getTrace().getAnalysisModuleOfClass(TmfStateSystemAnalysisModule.class, UstMemoryAnalysisModule.ID);
64 if (fModule == null) {
65 return;
66 }
67 fModule.schedule();
68 }
69 }
70
71 @Override
72 protected void updateData(long start, long end, int nb, IProgressMonitor monitor) {
73 try {
74 if (getTrace() == null || fModule == null) {
75 return;
76 }
77 ITmfStateSystem ss = fModule.getStateSystem();
78 /* Don't wait for the module completion, when it's ready, we'll know */
79 if (ss == null) {
80 return;
81 }
82 double[] xvalues = getXAxis(start, end, nb);
83 setXAxis(xvalues);
84 List<Integer> tidQuarks = ss.getSubAttributes(-1, false);
85 long traceStart = getStartTime();
86 long traceEnd = getEndTime();
87 long offset = this.getTimeOffset();
88
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);
94 try {
95 ITmfStateValue procnameValue = ss.querySingleState(start, procNameQuark).getStateValue();
96 String procname = new String();
97 if (!procnameValue.isNull()) {
98 procname = procnameValue.unboxStr();
99 }
100 fSeriesName.put(quark, new String(procname + ' ' + '(' + ss.getAttributeName(quark) + ')').trim());
101 } catch (TimeRangeException e) {
102 fSeriesName.put(quark, '(' + ss.getAttributeName(quark) + ')');
103 }
104 }
105
106 /*
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
110 * displayed.
111 */
112 double yvalue = 0.0;
113 for (int i = 0; i < xvalues.length; i++) {
114 if (monitor.isCanceled()) {
115 return;
116 }
117 double x = xvalues[i];
118 long time = (long) x + offset;
119 // make sure that time is in the trace range after double to
120 // long conversion
121 time = time < traceStart ? traceStart : time;
122 time = time > traceEnd ? traceEnd : time;
123
124 for (int quark : tidQuarks) {
125 try {
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;
130 }
131 }
132 }
133 for (int quark : tidQuarks) {
134 setSeries(fSeriesName.get(quark), fYValues.get(quark));
135 }
136 updateDisplay();
137 } catch (AttributeNotFoundException | StateValueTypeException | StateSystemDisposedException e) {
138 Activator.logError("Error updating the data of the Memory usage view", e); //$NON-NLS-1$
139 }
140 }
141
142 }
This page took 0.035041 seconds and 5 git commands to generate.