| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014, 2015 É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 | * Geneviève Bastien - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage; |
| 14 | |
| 15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 16 | |
| 17 | import java.util.Arrays; |
| 18 | import java.util.HashMap; |
| 19 | import java.util.LinkedHashMap; |
| 20 | import java.util.Map; |
| 21 | import java.util.Map.Entry; |
| 22 | import java.util.Set; |
| 23 | import java.util.TreeSet; |
| 24 | |
| 25 | import org.eclipse.core.runtime.IProgressMonitor; |
| 26 | import org.eclipse.jdt.annotation.NonNull; |
| 27 | import org.eclipse.swt.widgets.Composite; |
| 28 | import org.eclipse.tracecompass.analysis.os.linux.core.cpuusage.KernelCpuUsageAnalysis; |
| 29 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator; |
| 30 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
| 31 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; |
| 32 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 33 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
| 34 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer; |
| 35 | |
| 36 | import com.google.common.base.Joiner; |
| 37 | |
| 38 | /** |
| 39 | * CPU usage viewer with XY line chart. It displays the total CPU usage and that |
| 40 | * of the threads selected in the CPU usage tree viewer. |
| 41 | * |
| 42 | * @author Geneviève Bastien |
| 43 | */ |
| 44 | public class CpuUsageXYViewer extends TmfCommonXLineChartViewer { |
| 45 | |
| 46 | private KernelCpuUsageAnalysis fModule = null; |
| 47 | |
| 48 | /* Maps a thread ID to a list of y values */ |
| 49 | private final Map<String, double[]> fYValues = new LinkedHashMap<>(); |
| 50 | /* |
| 51 | * To avoid up and downs CPU usage when process is in and out of CPU |
| 52 | * frequently, use a smaller resolution to get better averages. |
| 53 | */ |
| 54 | private static final double RESOLUTION = 0.4; |
| 55 | |
| 56 | // Timeout between updates in the updateData thread |
| 57 | private static final long BUILD_UPDATE_TIMEOUT = 500; |
| 58 | |
| 59 | private long fSelectedThread = -1; |
| 60 | |
| 61 | private final @NonNull Set<@NonNull Integer> fCpus = new TreeSet<>(); |
| 62 | |
| 63 | /** |
| 64 | * Constructor |
| 65 | * |
| 66 | * @param parent |
| 67 | * parent composite |
| 68 | */ |
| 69 | public CpuUsageXYViewer(Composite parent) { |
| 70 | super(parent, Messages.CpuUsageXYViewer_Title, Messages.CpuUsageXYViewer_TimeXAxis, Messages.CpuUsageXYViewer_CpuYAxis); |
| 71 | setResolution(RESOLUTION); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected void initializeDataSource() { |
| 76 | ITmfTrace trace = getTrace(); |
| 77 | if (trace != null) { |
| 78 | fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID); |
| 79 | if (fModule == null) { |
| 80 | return; |
| 81 | } |
| 82 | fModule.schedule(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private static double[] zeroFill(int nb) { |
| 87 | double[] arr = new double[nb]; |
| 88 | Arrays.fill(arr, 0.0); |
| 89 | return arr; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | protected void updateData(long start, long end, int nb, IProgressMonitor monitor) { |
| 94 | try { |
| 95 | if (getTrace() == null || fModule == null) { |
| 96 | return; |
| 97 | } |
| 98 | fModule.waitForInitialization(); |
| 99 | ITmfStateSystem ss = fModule.getStateSystem(); |
| 100 | if (ss == null) { |
| 101 | return; |
| 102 | } |
| 103 | double[] xvalues = getXAxis(start, end, nb); |
| 104 | if (xvalues.length == 0) { |
| 105 | return; |
| 106 | } |
| 107 | setXAxis(xvalues); |
| 108 | |
| 109 | boolean complete = false; |
| 110 | long currentEnd = Math.max(ss.getStartTime(), start); |
| 111 | |
| 112 | while (!complete && currentEnd < end) { |
| 113 | |
| 114 | if (monitor.isCanceled()) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | long traceStart = Math.max(getStartTime(), ss.getStartTime()); |
| 119 | long traceEnd = getEndTime(); |
| 120 | long offset = getTimeOffset(); |
| 121 | long selectedThread = fSelectedThread; |
| 122 | |
| 123 | complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT); |
| 124 | currentEnd = ss.getCurrentEndTime(); |
| 125 | |
| 126 | /* Initialize the data */ |
| 127 | Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(fCpus, Math.max(start, traceStart), Math.min(end, traceEnd)); |
| 128 | Map<String, String> totalEntries = new HashMap<>(); |
| 129 | fYValues.clear(); |
| 130 | fYValues.put(Messages.CpuUsageXYViewer_Total, zeroFill(xvalues.length)); |
| 131 | String stringSelectedThread = Long.toString(selectedThread); |
| 132 | if (selectedThread != -1) { |
| 133 | fYValues.put(stringSelectedThread, zeroFill(xvalues.length)); |
| 134 | } |
| 135 | |
| 136 | for (Entry<String, Long> entry : cpuUsageMap.entrySet()) { |
| 137 | /* |
| 138 | * Process only entries representing the total of all CPUs |
| 139 | * and that have time on CPU |
| 140 | */ |
| 141 | if (entry.getValue() == 0) { |
| 142 | continue; |
| 143 | } |
| 144 | if (!entry.getKey().startsWith(KernelCpuUsageAnalysis.TOTAL)) { |
| 145 | continue; |
| 146 | } |
| 147 | String[] strings = entry.getKey().split(KernelCpuUsageAnalysis.SPLIT_STRING, 2); |
| 148 | |
| 149 | if ((strings.length > 1) && !(strings[1].equals(KernelCpuUsageAnalysis.TID_ZERO))) { |
| 150 | /* This is the total cpu usage for a thread */ |
| 151 | totalEntries.put(strings[1], entry.getKey()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | double prevX = xvalues[0] - 1; |
| 156 | long prevTime = (long) prevX + offset; |
| 157 | /* |
| 158 | * make sure that time is in the trace range after double to |
| 159 | * long conversion |
| 160 | */ |
| 161 | prevTime = Math.max(traceStart, prevTime); |
| 162 | prevTime = Math.min(traceEnd, prevTime); |
| 163 | /* Get CPU usage statistics for each x value */ |
| 164 | for (int i = 0; i < xvalues.length; i++) { |
| 165 | if (monitor.isCanceled()) { |
| 166 | return; |
| 167 | } |
| 168 | long totalCpu = 0; |
| 169 | double x = xvalues[i]; |
| 170 | long time = (long) x + offset; |
| 171 | time = Math.max(traceStart, time); |
| 172 | time = Math.min(traceEnd, time); |
| 173 | if (time == prevTime) { |
| 174 | /* |
| 175 | * we need at least 1 time unit to be able to get cpu |
| 176 | * usage when zoomed in |
| 177 | */ |
| 178 | prevTime = time - 1; |
| 179 | } |
| 180 | |
| 181 | cpuUsageMap = fModule.getCpuUsageInRange(fCpus, prevTime, time); |
| 182 | |
| 183 | /* |
| 184 | * Calculate the sum of all total entries, and add a data |
| 185 | * point to the selected one |
| 186 | */ |
| 187 | for (Entry<String, String> entry : totalEntries.entrySet()) { |
| 188 | Long cpuEntry = cpuUsageMap.get(entry.getValue()); |
| 189 | cpuEntry = cpuEntry != null ? cpuEntry : 0L; |
| 190 | |
| 191 | totalCpu += cpuEntry; |
| 192 | |
| 193 | if (entry.getKey().equals(stringSelectedThread)) { |
| 194 | /* This is the total cpu usage for a thread */ |
| 195 | double[] key = checkNotNull(fYValues.get(entry.getKey())); |
| 196 | key[i] = (double) cpuEntry / (double) (time - prevTime) * 100; |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | double[] key = checkNotNull(fYValues.get(Messages.CpuUsageXYViewer_Total)); |
| 201 | key[i] = (double) totalCpu / (double) (time - prevTime) * 100; |
| 202 | prevTime = time; |
| 203 | } |
| 204 | for (Entry<String, double[]> entry : fYValues.entrySet()) { |
| 205 | setSeries(entry.getKey(), entry.getValue()); |
| 206 | } |
| 207 | if (monitor.isCanceled()) { |
| 208 | return; |
| 209 | } |
| 210 | updateDisplay(); |
| 211 | } |
| 212 | } catch (StateValueTypeException e) { |
| 213 | Activator.getDefault().logError("Error updating the data of the CPU usage view", e); //$NON-NLS-1$ |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Set the selected thread ID, which will be graphed in this viewer |
| 220 | * |
| 221 | * @param tid |
| 222 | * The selected thread ID |
| 223 | */ |
| 224 | public void setSelectedThread(long tid) { |
| 225 | cancelUpdate(); |
| 226 | deleteSeries(Long.toString(fSelectedThread)); |
| 227 | fSelectedThread = tid; |
| 228 | updateContent(); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Gets the analysis module |
| 233 | * |
| 234 | * @return the {@link KernelCpuUsageAnalysis} |
| 235 | * |
| 236 | * @since 2.0 |
| 237 | */ |
| 238 | public KernelCpuUsageAnalysis getModule() { |
| 239 | return fModule; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Add a core |
| 244 | * |
| 245 | * @param core |
| 246 | * the core to add |
| 247 | * @since 2.0 |
| 248 | */ |
| 249 | public void addCpu(int core) { |
| 250 | fCpus.add(core); |
| 251 | cancelUpdate(); |
| 252 | updateContent(); |
| 253 | getSwtChart().getTitle().setText(Messages.CpuUsageView_Title + ' ' + getCpuList()); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Remove a core |
| 258 | * |
| 259 | * @param core |
| 260 | * the core to remove |
| 261 | * @since 2.0 |
| 262 | */ |
| 263 | public void removeCpu(int core) { |
| 264 | fCpus.remove(core); |
| 265 | cancelUpdate(); |
| 266 | updateContent(); |
| 267 | getSwtChart().getTitle().setText(Messages.CpuUsageView_Title + ' ' + getCpuList()); |
| 268 | } |
| 269 | |
| 270 | private String getCpuList() { |
| 271 | return Joiner.on(", ").join(fCpus); //$NON-NLS-1$ |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Clears the cores |
| 276 | * |
| 277 | * @since 2.0 |
| 278 | */ |
| 279 | public void clearCpu() { |
| 280 | fCpus.clear(); |
| 281 | cancelUpdate(); |
| 282 | updateContent(); |
| 283 | getSwtChart().getTitle().setText(Messages.CpuUsageView_Title); |
| 284 | } |
| 285 | |
| 286 | } |