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