Commit | Line | Data |
---|---|---|
dffc234f GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 É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 | ||
9bc60be7 | 13 | package org.eclipse.tracecompass.internal.lttng2.kernel.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; |
9bc60be7 | 23 | import org.eclipse.tracecompass.internal.lttng2.kernel.ui.Activator; |
42d5b5f2 | 24 | import org.eclipse.tracecompass.lttng2.kernel.core.analysis.cpuusage.LttngKernelCpuUsageAnalysis; |
e894a508 AM |
25 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
26 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
b8585c7c | 27 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
2bdf0193 | 28 | import 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 | */ | |
36 | public class CpuUsageXYViewer extends TmfCommonXLineChartViewer { | |
37 | ||
38 | private LttngKernelCpuUsageAnalysis fModule = null; | |
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) { | |
b8585c7c | 67 | fModule = TmfTraceUtils.getAnalysisModuleOfClass(getTrace(), LttngKernelCpuUsageAnalysis.class, LttngKernelCpuUsageAnalysis.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 | } | |
133 | if (!entry.getKey().startsWith(LttngKernelCpuUsageAnalysis.TOTAL)) { | |
134 | continue; | |
135 | } | |
136 | String[] strings = entry.getKey().split(LttngKernelCpuUsageAnalysis.SPLIT_STRING, 2); | |
137 | ||
138 | if ((strings.length > 1) && !(strings[1].equals(LttngKernelCpuUsageAnalysis.TID_ZERO))) { | |
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 | } |