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