TMF: Add memory usage chart for UST traces with libc events enabled
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ust.ui / src / org / eclipse / linuxtools / internal / lttng2 / ust / ui / views / memusage / MemoryUsageViewer.java
CommitLineData
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
14package org.eclipse.linuxtools.internal.lttng2.ust.ui.views.memusage;
15
16import java.util.HashMap;
17import java.util.List;
18import java.util.Map;
19
20import org.eclipse.linuxtools.internal.lttng2.ust.core.memoryusage.UstMemoryStrings;
21import org.eclipse.linuxtools.internal.tmf.core.Activator;
22import org.eclipse.linuxtools.lttng2.ust.ui.analysis.memory.UstMemoryAnalysisModule;
23import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
24import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
25import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
26import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
27import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
28import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
29import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
30import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer;
31import org.eclipse.swt.widgets.Composite;
32
33/**
34 * Memory usage view
35 *
36 * @author Matthew Khouzam
37 */
38@SuppressWarnings("restriction")
39public class MemoryUsageViewer extends TmfCommonXLineChartViewer {
40
41 private TmfStateSystemAnalysisModule fModule = null;
42
43 private final Map<Integer, double[]> fYValues = new HashMap<>();
44 private final Map<Integer, Integer> fMemoryQuarks = new HashMap<>();
45 private final Map<Integer, String> fSeriesName = new HashMap<>();
46
47 private static final int BYTES_TO_KB = 1024;
48
49 /**
50 * Constructor
51 *
52 * @param parent
53 * parent view
54 */
55 public MemoryUsageViewer(Composite parent) {
56 super(parent, Messages.MemoryUsageViewer_Title, Messages.MemoryUsageViewer_XAxis, Messages.MemoryUsageViewer_YAxis);
57 }
58
59 @Override
60 protected void initializeDataSource() {
61 if (getTrace() != null) {
62 fModule = getTrace().getAnalysisModuleOfClass(TmfStateSystemAnalysisModule.class, UstMemoryAnalysisModule.ID);
63 if (fModule == null) {
64 return;
65 }
66 fModule.schedule();
67 }
68 }
69
70 @Override
71 protected void updateData(long start, long end, int nb) {
72 try {
73 if (getTrace() == null || fModule == null) {
74 return;
75 }
76 ITmfStateSystem ss = fModule.getStateSystem();
77 /* Don't wait for the module completion, when it's ready, we'll know */
78 if (ss == null) {
79 return;
80 }
81 double[] xvalues = getXAxis(start, end, nb);
82 setXAxis(xvalues);
83 List<Integer> tidQuarks = ss.getSubAttributes(-1, false);
84 long traceStart = getStartTime();
85 long traceEnd = getEndTime();
86 long offset = this.getTimeOffset();
87
88 /* Initialize quarks and series names */
89 for (int quark : tidQuarks) {
90 fYValues.put(quark, new double[xvalues.length]);
91 fMemoryQuarks.put(quark, ss.getQuarkRelative(quark, UstMemoryStrings.UST_MEMORY_MEMORY_ATTRIBUTE));
92 int procNameQuark = ss.getQuarkRelative(quark, UstMemoryStrings.UST_MEMORY_PROCNAME_ATTRIBUTE);
93 try {
94 ITmfStateValue procnameValue = ss.querySingleState(start, procNameQuark).getStateValue();
95 String procname = new String();
96 if (!procnameValue.isNull()) {
97 procname = procnameValue.unboxStr();
98 }
99 fSeriesName.put(quark, new String(procname + ' ' + '(' + ss.getAttributeName(quark) + ')').trim());
100 } catch (TimeRangeException e) {
101 fSeriesName.put(quark, '(' + ss.getAttributeName(quark) + ')');
102 }
103 }
104
105 /*
106 * TODO: It should only show active threads in the time range. If a
107 * tid does not have any memory value (only 1 interval in the time
108 * range with value null or 0), then its series should not be
109 * displayed.
110 */
111 double yvalue = 0.0;
112 for (int i = 0; i < xvalues.length; i++) {
113 double x = xvalues[i];
114 long time = (long) x + offset;
115 // make sure that time is in the trace range after double to
116 // long conversion
117 time = time < traceStart ? traceStart : time;
118 time = time > traceEnd ? traceEnd : time;
119
120 for (int quark : tidQuarks) {
121 try {
122 yvalue = ss.querySingleState(time, fMemoryQuarks.get(quark)).getStateValue().unboxLong() / BYTES_TO_KB;
123 fYValues.get(quark)[i] = yvalue;
124 } catch (TimeRangeException e) {
125 fYValues.get(quark)[i] = 0;
126 }
127 }
128 }
129 for (int quark : tidQuarks) {
130 setSeries(fSeriesName.get(quark), fYValues.get(quark));
131 }
132 updateDisplay();
133 } catch (AttributeNotFoundException | StateValueTypeException | StateSystemDisposedException e) {
134 Activator.logError("Error updating the data of the Memory usage view", e); //$NON-NLS-1$
135 }
136 }
137
138}
This page took 0.029904 seconds and 5 git commands to generate.