tmf: TmfTraceManager improvements
[deliverable/tracecompass.git] / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / analysis / os / linux / ui / views / cpuusage / CpuUsageXYViewer.java
CommitLineData
dffc234f 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
dffc234f
GB
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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
e363eae1 13package org.eclipse.tracecompass.analysis.os.linux.ui.views.cpuusage;
dffc234f
GB
14
15import java.util.Arrays;
16import java.util.HashMap;
17import java.util.LinkedHashMap;
18import java.util.Map;
19import java.util.Map.Entry;
20
21import org.eclipse.core.runtime.IProgressMonitor;
dffc234f 22import org.eclipse.swt.widgets.Composite;
e363eae1
AM
23import org.eclipse.tracecompass.analysis.os.linux.core.cpuusage.KernelCpuUsageAnalysis;
24import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator;
e894a508
AM
25import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
26import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
b8585c7c 27import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
2bdf0193 28import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer;
dffc234f
GB
29
30/**
31 * CPU usage viewer with XY line chart. It displays the total CPU usage and that
32 * of the threads selected in the CPU usage tree viewer.
33 *
34 * @author Geneviève Bastien
35 */
36public class CpuUsageXYViewer extends TmfCommonXLineChartViewer {
37
e363eae1 38 private KernelCpuUsageAnalysis fModule = null;
dffc234f
GB
39
40 /* Maps a thread ID to a list of y values */
41 private final Map<String, double[]> fYValues = new LinkedHashMap<>();
42 /*
43 * To avoid up and downs CPU usage when process is in and out of CPU
44 * frequently, use a smaller resolution to get better averages.
45 */
46 private static final double RESOLUTION = 0.4;
47
d48661f2
GB
48 // Timeout between updates in the updateData thread
49 private static final long BUILD_UPDATE_TIMEOUT = 500;
50
dffc234f
GB
51 private long fSelectedThread = -1;
52
53 /**
54 * Constructor
55 *
56 * @param parent
57 * parent composite
dffc234f 58 */
e9a0d1cb 59 public CpuUsageXYViewer(Composite parent) {
dffc234f
GB
60 super(parent, Messages.CpuUsageXYViewer_Title, Messages.CpuUsageXYViewer_TimeXAxis, Messages.CpuUsageXYViewer_CpuYAxis);
61 setResolution(RESOLUTION);
dffc234f
GB
62 }
63
64 @Override
65 protected void initializeDataSource() {
66 if (getTrace() != null) {
e363eae1 67 fModule = TmfTraceUtils.getAnalysisModuleOfClass(getTrace(), KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID);
dffc234f
GB
68 if (fModule == null) {
69 return;
70 }
71 fModule.schedule();
72 }
73 }
74
75 private static double[] zeroFill(int nb) {
76 double[] arr = new double[nb];
77 Arrays.fill(arr, 0.0);
78 return arr;
79 }
80
81 @Override
82 protected void updateData(long start, long end, int nb, IProgressMonitor monitor) {
83 try {
84 if (getTrace() == null || fModule == null) {
85 return;
86 }
d48661f2 87 fModule.waitForInitialization();
dffc234f 88 ITmfStateSystem ss = fModule.getStateSystem();
dffc234f
GB
89 if (ss == null) {
90 return;
91 }
92 double[] xvalues = getXAxis(start, end, nb);
93 if (xvalues.length == 0) {
94 return;
95 }
96 setXAxis(xvalues);
97
d48661f2
GB
98 boolean complete = false;
99 long currentEnd = start;
dffc234f 100
d48661f2 101 while (!complete && currentEnd < end) {
dffc234f 102
dffc234f
GB
103 if (monitor.isCanceled()) {
104 return;
105 }
dffc234f 106
d48661f2
GB
107 long traceStart = getStartTime();
108 long traceEnd = getEndTime();
109 long offset = getTimeOffset();
110 long selectedThread = fSelectedThread;
111
112 complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
113 currentEnd = ss.getCurrentEndTime();
114
115 /* Initialize the data */
116 Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(Math.max(start, traceStart), Math.min(end, traceEnd));
117 Map<String, String> totalEntries = new HashMap<>();
118 fYValues.clear();
119 fYValues.put(Messages.CpuUsageXYViewer_Total, zeroFill(xvalues.length));
120 String stringSelectedThread = Long.toString(selectedThread);
121 if (selectedThread != -1) {
122 fYValues.put(stringSelectedThread, zeroFill(xvalues.length));
123 }
124
125 for (Entry<String, Long> entry : cpuUsageMap.entrySet()) {
126 /*
127 * Process only entries representing the total of all CPUs
128 * and that have time on CPU
129 */
130 if (entry.getValue() == 0) {
131 continue;
132 }
e363eae1 133 if (!entry.getKey().startsWith(KernelCpuUsageAnalysis.TOTAL)) {
d48661f2
GB
134 continue;
135 }
e363eae1 136 String[] strings = entry.getKey().split(KernelCpuUsageAnalysis.SPLIT_STRING, 2);
d48661f2 137
e363eae1 138 if ((strings.length > 1) && !(strings[1].equals(KernelCpuUsageAnalysis.TID_ZERO))) {
d48661f2
GB
139 /* This is the total cpu usage for a thread */
140 totalEntries.put(strings[1], entry.getKey());
141 }
142 }
dffc234f 143
d48661f2
GB
144 double prevX = xvalues[0];
145 long prevTime = (long) prevX + offset;
dffc234f 146 /*
d48661f2
GB
147 * make sure that time is in the trace range after double to
148 * long conversion
dffc234f 149 */
d48661f2
GB
150 prevTime = Math.max(traceStart, prevTime);
151 prevTime = Math.min(traceEnd, prevTime);
152 /* Get CPU usage statistics for each x value */
153 for (int i = 1; i < xvalues.length; i++) {
154 if (monitor.isCanceled()) {
155 return;
156 }
157 long totalCpu = 0;
158 double x = xvalues[i];
159 long time = (long) x + offset;
160 time = Math.max(traceStart, time);
161 time = Math.min(traceEnd, time);
dffc234f 162
d48661f2 163 cpuUsageMap = fModule.getCpuUsageInRange(prevTime, time);
dffc234f 164
d48661f2
GB
165 /*
166 * Calculate the sum of all total entries, and add a data
167 * point to the selected one
168 */
169 for (Entry<String, String> entry : totalEntries.entrySet()) {
170 Long cpuEntry = cpuUsageMap.get(entry.getValue());
171 cpuEntry = cpuEntry != null ? cpuEntry : 0L;
172
173 totalCpu += cpuEntry;
dffc234f 174
d48661f2
GB
175 if (entry.getKey().equals(stringSelectedThread)) {
176 /* This is the total cpu usage for a thread */
177 fYValues.get(entry.getKey())[i] = (double) cpuEntry / (double) (time - prevTime) * 100;
178 }
179
180 }
181 fYValues.get(Messages.CpuUsageXYViewer_Total)[i] = (double) totalCpu / (double) (time - prevTime) * 100;
182 prevTime = time;
dffc234f 183 }
d48661f2
GB
184 for (Entry<String, double[]> entry : fYValues.entrySet()) {
185 setSeries(entry.getKey(), entry.getValue());
186 }
187 if (monitor.isCanceled()) {
188 return;
189 }
190 updateDisplay();
dffc234f 191 }
dffc234f
GB
192 } catch (StateValueTypeException e) {
193 Activator.getDefault().logError("Error updating the data of the CPU usage view", e); //$NON-NLS-1$
194 }
195
196 }
197
198 /**
199 * Set the selected thread ID, which will be graphed in this viewer
200 *
201 * @param tid
202 * The selected thread ID
203 */
204 public void setSelectedThread(long tid) {
205 cancelUpdate();
206 deleteSeries(Long.toString(fSelectedThread));
207 fSelectedThread = tid;
208 updateContent();
209 }
210
211}
This page took 0.051013 seconds and 5 git commands to generate.