Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: chrome/browser/resources/chromeos/power.js

Issue 149973002: [chromeos/about:power] Collect cpuidle and cpufreq stats (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * Plot a line graph of data versus time on a HTML canvas element. 6 * Plot a line graph of data versus time on a HTML canvas element.
7 * 7 *
8 * @param {HTMLCanvasElement} canvas The canvas on which the line graph is 8 * @param {HTMLCanvasElement} plotCanvas The canvas on which the line graph is
9 * drawn. 9 * drawn.
10 * @param {HTMLCanvasElement} legendCanvas The canvas on which the legend for
11 * the line graph is drawn.
10 * @param {Array.<number>} tData The time (in seconds) in the past when the 12 * @param {Array.<number>} tData The time (in seconds) in the past when the
11 * corresponding data in plots was sampled. 13 * corresponding data in plots was sampled.
12 * @param {Array.<{data: Array.<number>, color: string}>} plots An 14 * @param {Array.<{data: Array.<number>, color: string}>} plots An
13 * array of plots to plot on the canvas. The field 'data' of a plot is an 15 * array of plots to plot on the canvas. The field 'data' of a plot is an
14 * array of samples to be plotted as a line graph with color speficied by 16 * array of samples to be plotted as a line graph with color speficied by
15 * the field 'color'. The elements in the 'data' array are ordered 17 * the field 'color'. The elements in the 'data' array are ordered
16 * corresponding to their sampling time in the argument 'tData'. Also, the 18 * corresponding to their sampling time in the argument 'tData'. Also, the
17 * number of elements in the 'data' array should be the same as in the time 19 * number of elements in the 'data' array should be the same as in the time
18 * array 'tData' above. 20 * array 'tData' above.
19 * @param {number} yMin Minimum bound of y-axis 21 * @param {number} yMin Minimum bound of y-axis
20 * @param {number} yMax Maximum bound of y-axis. 22 * @param {number} yMax Maximum bound of y-axis.
21 * @param {integer} yPrecision An integer value representing the number of 23 * @param {integer} yPrecision An integer value representing the number of
22 * digits of precision the y-axis data should be printed with. 24 * digits of precision the y-axis data should be printed with.
23 */ 25 */
24 function plotLineGraph(canvas, tData, plots, yMin, yMax, yPrecision) { 26 function plotLineGraph(
27 plotCanvas, legendCanvas, tData, plots, yMin, yMax, yPrecision) {
25 var textFont = '12px Arial'; 28 var textFont = '12px Arial';
26 var textHeight = 12; 29 var textHeight = 12;
27 var padding = 5; // Pixels 30 var padding = 5; // Pixels
28 var errorOffsetPixels = 15; 31 var errorOffsetPixels = 15;
29 var gridColor = '#ccc'; 32 var gridColor = '#ccc';
30 var ctx = canvas.getContext('2d'); 33 var plotCtx = plotCanvas.getContext('2d');
31 var size = tData.length; 34 var size = tData.length;
32 35
33 function drawText(text, x, y) { 36 function drawText(ctx, text, x, y) {
34 ctx.font = textFont; 37 ctx.font = textFont;
35 ctx.fillStyle = '#000'; 38 ctx.fillStyle = '#000';
36 ctx.fillText(text, x, y); 39 ctx.fillText(text, x, y);
37 } 40 }
38 41
39 function printErrorText(text) { 42 function printErrorText(ctx, text) {
40 ctx.clearRect(0, 0, canvas.width, canvas.height); 43 ctx.clearRect(0, 0, plotCanvas.width, plotCanvas.height);
41 drawText(text, errorOffsetPixels, errorOffsetPixels); 44 drawText(ctx, text, errorOffsetPixels, errorOffsetPixels);
42 } 45 }
43 46
44 if (size < 2) { 47 if (size < 2) {
45 printErrorText(loadTimeData.getString('notEnoughDataAvailableYet')); 48 printErrorText(plotCtx,
49 loadTimeData.getString('notEnoughDataAvailableYet'));
46 return; 50 return;
47 } 51 }
48 52
49 for (var count = 0; count < plots.length; count++) { 53 for (var count = 0; count < plots.length; count++) {
50 if (plots[count].data.length != size) { 54 if (plots[count].data.length != size) {
51 throw new Error('Mismatch in time and plot data.'); 55 throw new Error('Mismatch in time and plot data.');
52 } 56 }
53 } 57 }
54 58
55 function valueToString(value) { 59 function valueToString(value) {
56 return Number(value).toPrecision(yPrecision); 60 if (Math.abs(value) < 1) {
61 return Number(value).toFixed(yPrecision - 1);
62 } else {
63 return Number(value).toPrecision(yPrecision);
64 }
57 } 65 }
58 66
59 function getTextWidth(text) { 67 function getTextWidth(ctx, text) {
60 ctx.font = textFont; 68 ctx.font = textFont;
61 // For now, all text is drawn to the left of vertical lines, or centered. 69 // For now, all text is drawn to the left of vertical lines, or centered.
62 // Add a 2 pixel padding so that there is some spacing between the text 70 // Add a 2 pixel padding so that there is some spacing between the text
63 // and the vertical line. 71 // and the vertical line.
64 return Math.round(ctx.measureText(text).width) + 2; 72 return Math.round(ctx.measureText(text).width) + 2;
65 } 73 }
66 74
67 function drawHighlightText(text, x, y, color) { 75 function drawHighlightText(ctx, text, x, y, color) {
68 ctx.strokeStyle = '#000'; 76 ctx.strokeStyle = '#000';
69 ctx.strokeRect(x, y - textHeight, getTextWidth(text), textHeight); 77 ctx.strokeRect(x, y - textHeight, getTextWidth(ctx, text), textHeight);
70 ctx.fillStyle = color; 78 ctx.fillStyle = color;
71 ctx.fillRect(x, y - textHeight, getTextWidth(text), textHeight); 79 ctx.fillRect(x, y - textHeight, getTextWidth(ctx, text), textHeight);
72 ctx.fillStyle = '#fff'; 80 ctx.fillStyle = '#fff';
73 ctx.fillText(text, x, y); 81 ctx.fillText(text, x, y);
74 } 82 }
75 83
76 function drawLine(x1, y1, x2, y2, color) { 84 function drawLine(ctx, x1, y1, x2, y2, color) {
77 ctx.save(); 85 ctx.save();
78 ctx.beginPath(); 86 ctx.beginPath();
79 ctx.moveTo(x1, y1); 87 ctx.moveTo(x1, y1);
80 ctx.lineTo(x2, y2); 88 ctx.lineTo(x2, y2);
81 ctx.strokeStyle = color; 89 ctx.strokeStyle = color;
82 ctx.stroke(); 90 ctx.stroke();
83 ctx.restore(); 91 ctx.restore();
84 } 92 }
85 93
86 // The strokeRect method of the 2d context of a canvas draws a bounding 94 // The strokeRect method of the 2d context of a plotCanvas draws a bounding
87 // rectangle with an offset origin and greater dimensions. Hence, use this 95 // rectangle with an offset origin and greater dimensions. Hence, use this
88 // function to draw a rect at the desired location with desired dimensions. 96 // function to draw a rect at the desired location with desired dimensions.
89 function drawRect(x, y, width, height, color) { 97 function drawRect(ctx, x, y, width, height, color) {
90 drawLine(x, y, x + width - 1, y, color); 98 drawLine(ctx, x, y, x + width - 1, y, color);
91 drawLine(x, y, x, y + height - 1, color); 99 drawLine(ctx, x, y, x, y + height - 1, color);
92 drawLine(x, y + height - 1, x + width - 1, y + height - 1, color); 100 drawLine(ctx, x, y + height - 1, x + width - 1, y + height - 1, color);
93 drawLine(x + width - 1, y, x + width - 1, y + height - 1, color); 101 drawLine(ctx, x + width - 1, y, x + width - 1, y + height - 1, color);
102 }
103
104 function drawLegend() {
105 // Show a legend only if atleast one individual plot have a name.
106 var valid = false;
107 for (var i = 0; i < plots.length; i++) {
108 if (plots[i].name != null) {
109 valid = true;
110 break;
111 }
112 }
113 if (!valid) {
114 legendCanvas.hidden = true;
115 return;
116 }
117
118 var padding = 2;
119 var legendSquareSide = 12;
120 var legendCtx = legendCanvas.getContext('2d');
121 var xLoc = padding;
122 var yLoc = padding;
123 // Adjust the height of the canvas before drawing on it.
124 for (var i = 0; i < plots.length; i++) {
125 if (plots[i].name == null) {
126 continue;
127 }
128 var legendText = ' - ' + plots[i].name;
129 xLoc += legendSquareSide + getTextWidth(legendCtx, legendText) +
130 2 * padding;
131 if (i < plots.length - 1) {
132 var xLocNext = xLoc +
133 getTextWidth(legendCtx, ' - ' + plots[i + 1].name) +
134 legendSquareSide;
135 if (xLocNext >= legendCanvas.width) {
136 xLoc = padding;
137 yLoc = yLoc + 2 * padding + textHeight;
138 }
139 }
140 }
141
142 legendCanvas.height = yLoc + textHeight + padding;
143
144 xLoc = padding;
145 yLoc = padding;
146 // Go over the plots again, this time drawing the legends.
147 for (var i = 0; i < plots.length; i++) {
148 legendCtx.fillStyle = plots[i].color;
149 legendCtx.fillRect(xLoc, yLoc, legendSquareSide, legendSquareSide);
150 xLoc += legendSquareSide;
151
152 var legendText = ' - ' + plots[i].name;
153 drawText(legendCtx, legendText, xLoc, yLoc + textHeight - 1);
154 xLoc += getTextWidth(legendCtx, legendText) + 2 * padding;
155
156 if (i < plots.length - 1) {
157 var xLocNext = xLoc +
158 getTextWidth(legendCtx, ' - ' + plots[i + 1].name) +
159 legendSquareSide;
160 if (xLocNext >= legendCanvas.width) {
161 xLoc = padding;
162 yLoc = yLoc + 2 * padding + textHeight;
163 }
164 }
165 }
94 } 166 }
95 167
96 var yMinStr = valueToString(yMin); 168 var yMinStr = valueToString(yMin);
97 var yMaxStr = valueToString(yMax); 169 var yMaxStr = valueToString(yMax);
98 var yHalfStr = valueToString((yMax + yMin) / 2); 170 var yHalfStr = valueToString((yMax + yMin) / 2);
99 var yMinWidth = getTextWidth(yMinStr); 171 var yMinWidth = getTextWidth(plotCtx, yMinStr);
100 var yMaxWidth = getTextWidth(yMaxStr); 172 var yMaxWidth = getTextWidth(plotCtx, yMaxStr);
101 var yHalfWidth = getTextWidth(yHalfStr); 173 var yHalfWidth = getTextWidth(plotCtx, yHalfStr);
102 174
103 var xMinStr = tData[0]; 175 var xMinStr = tData[0];
104 var xMaxStr = tData[size - 1]; 176 var xMaxStr = tData[size - 1];
105 var xMinWidth = getTextWidth(xMinStr); 177 var xMinWidth = getTextWidth(plotCtx, xMinStr);
106 var xMaxWidth = getTextWidth(xMaxStr); 178 var xMaxWidth = getTextWidth(plotCtx, xMaxStr);
107 179
108 var xOrigin = padding + Math.max(yMinWidth, 180 var xOrigin = padding + Math.max(yMinWidth,
109 yMaxWidth, 181 yMaxWidth,
110 Math.round(xMinWidth / 2)); 182 Math.round(xMinWidth / 2));
111 var yOrigin = padding + textHeight; 183 var yOrigin = padding + textHeight;
112 var width = canvas.width - xOrigin - Math.floor(xMaxWidth / 2) - padding; 184 var width = plotCanvas.width - xOrigin - Math.floor(xMaxWidth / 2) - padding;
113 if (width < size) { 185 if (width < size) {
114 canvas.width += size - width; 186 plotCanvas.width += size - width;
115 width = size; 187 width = size;
116 } 188 }
117 var height = canvas.height - yOrigin - textHeight - padding; 189 var height = plotCanvas.height - yOrigin - textHeight - padding;
118 190
119 function drawPlots() { 191 function drawPlots() {
120 // Start fresh. 192 // Start fresh.
121 ctx.clearRect(0, 0, canvas.width, canvas.height); 193 plotCtx.clearRect(0, 0, plotCanvas.width, plotCanvas.height);
122 194
123 // Draw the bounding rectangle. 195 // Draw the bounding rectangle.
124 drawRect(xOrigin, yOrigin, width, height, gridColor); 196 drawRect(plotCtx, xOrigin, yOrigin, width, height, gridColor);
125 197
126 // Draw the x and y bound values. 198 // Draw the x and y bound values.
127 drawText(yMaxStr, xOrigin - yMaxWidth, yOrigin + textHeight); 199 drawText(plotCtx, yMaxStr, xOrigin - yMaxWidth, yOrigin + textHeight);
128 drawText(yMinStr, xOrigin - yMinWidth, yOrigin + height); 200 drawText(plotCtx, yMinStr, xOrigin - yMinWidth, yOrigin + height);
129 drawText(xMinStr, xOrigin - xMinWidth / 2, yOrigin + height + textHeight); 201 drawText(plotCtx,
130 drawText(xMaxStr, 202 xMinStr,
203 xOrigin - xMinWidth / 2,
204 yOrigin + height + textHeight);
205 drawText(plotCtx,
206 xMaxStr,
131 xOrigin + width - xMaxWidth / 2, 207 xOrigin + width - xMaxWidth / 2,
132 yOrigin + height + textHeight); 208 yOrigin + height + textHeight);
133 209
134 // Draw y-level (horizontal) lines. 210 // Draw y-level (horizontal) lines.
135 drawLine(xOrigin + 1, yOrigin + height / 4, 211 drawLine(plotCtx,
212 xOrigin + 1, yOrigin + height / 4,
136 xOrigin + width - 2, yOrigin + height / 4, 213 xOrigin + width - 2, yOrigin + height / 4,
137 gridColor); 214 gridColor);
138 drawLine(xOrigin + 1, yOrigin + height / 2, 215 drawLine(plotCtx,
216 xOrigin + 1, yOrigin + height / 2,
139 xOrigin + width - 2, yOrigin + height / 2, gridColor); 217 xOrigin + width - 2, yOrigin + height / 2, gridColor);
140 drawLine(xOrigin + 1, yOrigin + 3 * height / 4, 218 drawLine(plotCtx,
219 xOrigin + 1, yOrigin + 3 * height / 4,
141 xOrigin + width - 2, yOrigin + 3 * height / 4, 220 xOrigin + width - 2, yOrigin + 3 * height / 4,
142 gridColor); 221 gridColor);
143 222
144 // Draw half-level value. 223 // Draw half-level value.
145 drawText(yHalfStr, 224 drawText(plotCtx,
225 yHalfStr,
146 xOrigin - yHalfWidth, 226 xOrigin - yHalfWidth,
147 yOrigin + height / 2 + textHeight / 2); 227 yOrigin + height / 2 + textHeight / 2);
148 228
149 // Draw the plots. 229 // Draw the plots.
150 var yValRange = yMax - yMin; 230 var yValRange = yMax - yMin;
151 for (var count = 0; count < plots.length; count++) { 231 for (var count = 0; count < plots.length; count++) {
152 var plot = plots[count]; 232 var plot = plots[count];
153 var yData = plot.data; 233 var yData = plot.data;
154 ctx.strokeStyle = plot.color; 234 plotCtx.strokeStyle = plot.color;
155 ctx.beginPath(); 235 plotCtx.beginPath();
156 var beginPath = true; 236 var beginPath = true;
157 for (var i = 0; i < size; i++) { 237 for (var i = 0; i < size; i++) {
158 var val = yData[i]; 238 var val = yData[i];
159 if (typeof val === 'string') { 239 if (typeof val === 'string') {
160 // Stroke the plot drawn so far and begin a fresh plot. 240 // Stroke the plot drawn so far and begin a fresh plot.
161 ctx.stroke(); 241 plotCtx.stroke();
162 ctx.beginPath(); 242 plotCtx.beginPath();
163 beginPath = true; 243 beginPath = true;
164 continue; 244 continue;
165 } 245 }
166 var xPos = xOrigin + Math.floor(i / (size - 1) * (width - 1)); 246 var xPos = xOrigin + Math.floor(i / (size - 1) * (width - 1));
167 var yPos = yOrigin + height - 1 - 247 var yPos = yOrigin + height - 1 -
168 Math.round((val - yMin) / yValRange * (height - 1)); 248 Math.round((val - yMin) / yValRange * (height - 1));
169 if (beginPath) { 249 if (beginPath) {
170 ctx.moveTo(xPos, yPos); 250 plotCtx.moveTo(xPos, yPos);
171 // A simple move to does not print anything. Hence, draw a little 251 // A simple move to does not print anything. Hence, draw a little
172 // square here to mark a beginning. 252 // square here to mark a beginning.
173 ctx.fillStyle = '#000'; 253 plotCtx.fillStyle = '#000';
174 ctx.fillRect(xPos - 1, yPos - 1, 2, 2); 254 plotCtx.fillRect(xPos - 1, yPos - 1, 2, 2);
175 beginPath = false; 255 beginPath = false;
176 } else { 256 } else {
177 ctx.lineTo(xPos, yPos); 257 plotCtx.lineTo(xPos, yPos);
178 if (i === size - 1 || typeof yData[i + 1] === 'string') { 258 if (i === size - 1 || typeof yData[i + 1] === 'string') {
179 // Draw a little square to mark an end to go with the start 259 // Draw a little square to mark an end to go with the start
180 // markers from above. 260 // markers from above.
181 ctx.fillStyle = '#000'; 261 plotCtx.fillStyle = '#000';
182 ctx.fillRect(xPos - 1, yPos - 1, 2, 2); 262 plotCtx.fillRect(xPos - 1, yPos - 1, 2, 2);
183 } 263 }
184 } 264 }
185 } 265 }
186 ctx.stroke(); 266 plotCtx.stroke();
187 } 267 }
188 268
189 // Paint the missing time intervals with |gridColor|. 269 // Paint the missing time intervals with |gridColor|.
190 // Pick one of the plots to look for missing time intervals. 270 // Pick one of the plots to look for missing time intervals.
271 function drawMissingRect(start, end) {
272 var xLeft = xOrigin + Math.floor(start / (size - 1) * (width - 1));
273 var xRight = xOrigin + Math.floor(end / (size - 1) * (width - 1));
274 plotCtx.fillStyle = gridColor;
275 // The x offsets below are present so that the blank space starts
276 // and ends between two valid samples.
277 plotCtx.fillRect(xLeft + 1, yOrigin, xRight - xLeft - 2, height - 1);
278 }
191 var inMissingInterval = false; 279 var inMissingInterval = false;
192 var intervalStart; 280 var intervalStart;
193 for (var i = 0; i < size; i++) { 281 for (var i = 0; i < size; i++) {
194 if (typeof plots[0].data[i] === 'string') { 282 if (typeof plots[0].data[i] === 'string') {
195 if (!inMissingInterval) { 283 if (!inMissingInterval) {
196 inMissingInterval = true; 284 inMissingInterval = true;
197 // The missing interval should actually start from the previous 285 // The missing interval should actually start from the previous
198 // sample. 286 // sample.
199 intervalStart = Math.max(i - 1, 0); 287 intervalStart = Math.max(i - 1, 0);
200 } 288 }
289
290 if (i == size - 1) {
291 // If this is the last sample, just draw missing rect.
292 drawMissingRect(intervalStart, i);
293 }
201 } else if (inMissingInterval) { 294 } else if (inMissingInterval) {
202 inMissingInterval = false; 295 inMissingInterval = false;
203 var xLeft = xOrigin + 296 drawMissingRect(intervalStart, i);
204 Math.floor(intervalStart / (size - 1) * (width - 1));
205 var xRight = xOrigin + Math.floor(i / (size - 1) * (width - 1));
206 ctx.fillStyle = gridColor;
207 // The x offsets below are present so that the blank space starts
208 // and ends between two valid samples.
209 ctx.fillRect(xLeft + 1, yOrigin, xRight - xLeft - 2, height - 1);
210 } 297 }
211 } 298 }
212 } 299 }
213 300
214 function drawTimeGuide(tDataIndex) { 301 function drawTimeGuide(tDataIndex) {
215 var x = xOrigin + tDataIndex / (size - 1) * (width - 1); 302 var x = xOrigin + tDataIndex / (size - 1) * (width - 1);
216 drawLine(x, yOrigin, x, yOrigin + height - 1, '#000'); 303 drawLine(plotCtx, x, yOrigin, x, yOrigin + height - 1, '#000');
217 drawText(tData[tDataIndex], 304 drawText(plotCtx,
218 x - getTextWidth(tData[tDataIndex]) / 2, 305 tData[tDataIndex],
306 x - getTextWidth(plotCtx, tData[tDataIndex]) / 2,
219 yOrigin - 2); 307 yOrigin - 2);
220 308
221 for (var count = 0; count < plots.length; count++) { 309 for (var count = 0; count < plots.length; count++) {
222 var yData = plots[count].data; 310 var yData = plots[count].data;
223 311
224 // Draw small black square on the plot where the time guide intersects 312 // Draw small black square on the plot where the time guide intersects
225 // it. 313 // it.
226 var val = yData[tDataIndex]; 314 var val = yData[tDataIndex];
227 var yPos, valStr; 315 var yPos, valStr;
228 if (typeof val === 'string') { 316 if (typeof val === 'string') {
229 yPos = yOrigin + Math.round(height / 2); 317 yPos = yOrigin + Math.round(height / 2);
230 valStr = val; 318 valStr = val;
231 } else { 319 } else {
232 yPos = yOrigin + height - 1 - 320 yPos = yOrigin + height - 1 -
233 Math.round((val - yMin) / (yMax - yMin) * (height - 1)); 321 Math.round((val - yMin) / (yMax - yMin) * (height - 1));
234 valStr = valueToString(val); 322 valStr = valueToString(val);
235 } 323 }
236 ctx.fillStyle = '#000'; 324 plotCtx.fillStyle = '#000';
237 ctx.fillRect(x - 2, yPos - 2, 4, 4); 325 plotCtx.fillRect(x - 2, yPos - 2, 4, 4);
238 326
239 // Draw the val to right of the intersection. 327 // Draw the val to right of the intersection.
240 var yLoc; 328 var yLoc;
241 if (yPos - textHeight / 2 < yOrigin) { 329 if (yPos - textHeight / 2 < yOrigin) {
242 yLoc = yOrigin + textHeight; 330 yLoc = yOrigin + textHeight;
243 } else if (yPos + textHeight / 2 >= yPos + height) { 331 } else if (yPos + textHeight / 2 >= yPos + height) {
244 yLoc = yOrigin + height - 1; 332 yLoc = yOrigin + height - 1;
245 } else { 333 } else {
246 yLoc = yPos + textHeight / 2; 334 yLoc = yPos + textHeight / 2;
247 } 335 }
248 drawHighlightText(valStr, x + 5, yLoc, plots[count].color); 336 drawHighlightText(plotCtx, valStr, x + 5, yLoc, plots[count].color);
249 } 337 }
250 } 338 }
251 339
252 function onMouseOverOrMove(event) { 340 function onMouseOverOrMove(event) {
253 drawPlots(); 341 drawPlots();
254 342
255 var boundingRect = canvas.getBoundingClientRect(); 343 var boundingRect = plotCanvas.getBoundingClientRect();
256 var x = event.clientX - boundingRect.left; 344 var x = event.clientX - boundingRect.left;
257 var y = event.clientY - boundingRect.top; 345 var y = event.clientY - boundingRect.top;
258 if (x < xOrigin || x >= xOrigin + width || 346 if (x < xOrigin || x >= xOrigin + width ||
259 y < yOrigin || y >= yOrigin + height) { 347 y < yOrigin || y >= yOrigin + height) {
260 return; 348 return;
261 } 349 }
262 350
263 if (width == size) { 351 if (width == size) {
264 drawTimeGuide(x - xOrigin); 352 drawTimeGuide(x - xOrigin);
265 } else { 353 } else {
266 drawTimeGuide(Math.round((x - xOrigin) / (width - 1) * (size - 1))); 354 drawTimeGuide(Math.round((x - xOrigin) / (width - 1) * (size - 1)));
267 } 355 }
268 } 356 }
269 357
270 function onMouseOut(event) { 358 function onMouseOut(event) {
271 drawPlots(); 359 drawPlots();
272 } 360 }
273 361
362 drawLegend();
274 drawPlots(); 363 drawPlots();
275 canvas.addEventListener('mouseover', onMouseOverOrMove); 364 plotCanvas.addEventListener('mouseover', onMouseOverOrMove);
276 canvas.addEventListener('mousemove', onMouseOverOrMove); 365 plotCanvas.addEventListener('mousemove', onMouseOverOrMove);
277 canvas.addEventListener('mouseout', onMouseOut); 366 plotCanvas.addEventListener('mouseout', onMouseOut);
278 } 367 }
279 368
280 var sleepSampleInterval = 30 * 1000; // in milliseconds. 369 var sleepSampleInterval = 30 * 1000; // in milliseconds.
281 var sleepText = loadTimeData.getString('systemSuspended'); 370 var sleepText = loadTimeData.getString('systemSuspended');
371 var invalidDataText = loadTimeData.getString('invalidData');
372 var offlineText = loadTimeData.getString('offlineText');
373
374 var plotColors = ['Red', 'Blue', 'Green', 'Gold', 'CadetBlue', 'LightCoral',
375 'LightSlateGray', 'Peru', 'DarkRed', 'LawnGreen', 'Tan'];
376
377 /**
378 * Add canvases for plotting to |plotsDiv|. For every header in |headerArray|,
379 * one canvas for the plot and one for its legend are added.
380 *
381 * @param {Array.<string>} headerArray Headers for the different plots to be
382 * added to |plotsDiv|.
383 * @param {HTMLDivElement} plotsDiv The div element into which the canvases
384 * are added.
385 * @return {<string>: {plotCanvas: <HTMLCanvasElement>,
386 * legendCanvas: <HTMLCanvasElement>} Returns an object
387 * with the headers as 'keys'. Each element is an object containing the
388 * legend canvas and the plot canvas that have been added to |plotsDiv|.
389 */
390 function addCanvases(headerArray, plotsDiv) {
391 // Remove the contents before adding new ones.
392 while (plotsDiv.firstChild != null) {
393 plotsDiv.removeChild(plotsDiv.firstChild);
394 }
395
396 canvases = {};
397 for (var i = 0; i < headerArray.length; i++) {
398 header = document.createElement('h4');
399 header.textContent = headerArray[i];
400 plotsDiv.appendChild(header);
401
402 legendCanvas = document.createElement('canvas');
403 legendCanvas.setAttribute('width', '600');
404 legendCanvas.setAttribute('height', '5');
405 plotsDiv.appendChild(legendCanvas);
406
407 br = document.createElement('br');
408 plotsDiv.appendChild(br);
409
410 plotCanvasDiv = document.createElement('div');
411 plotCanvasDiv.className = 'single-plot-div';
412 plotsDiv.appendChild(plotCanvasDiv);
413
414 plotCanvas = document.createElement('canvas');
415 plotCanvas.setAttribute('width', '600');
416 plotCanvas.setAttribute('height', '200');
417 plotCanvasDiv.appendChild(plotCanvas);
418
419 canvases[headerArray[i]] = {plot: plotCanvas, legend: legendCanvas};
420 }
421 return canvases;
422 }
282 423
283 /** 424 /**
284 * Add samples in |sampleArray| to individual plots in |plots|. If the system 425 * Add samples in |sampleArray| to individual plots in |plots|. If the system
285 * resumed from a sleep/suspend, then "suspended" sleep samples are added to 426 * resumed from a sleep/suspend, then "suspended" sleep samples are added to
286 * the plot for the sleep duration. 427 * the plot for the sleep duration.
287 * 428 *
288 * @param {Array.<{data: Array.<number>, color: string}>} plots An 429 * @param {Array.<{data: Array.<number>, color: string}>} plots An
289 * array of plots to plot on the canvas. The field 'data' of a plot is an 430 * array of plots to plot on the canvas. The field 'data' of a plot is an
290 * array of samples to be plotted as a line graph with color speficied by 431 * array of samples to be plotted as a line graph with color speficied by
291 * the field 'color'. The elements in the 'data' array are ordered 432 * the field 'color'. The elements in the 'data' array are ordered
292 * corresponding to their sampling time in the argument 'tData'. Also, the 433 * corresponding to their sampling time in the argument 'tData'. Also, the
293 * number of elements in the 'data' array should be the same as in the time 434 * number of elements in the 'data' array should be the same as in the time
294 * array 'tData' below. 435 * array 'tData' below.
295 * @param {Array.<number>} tData The time (in seconds) in the past when the 436 * @param {Array.<number>} tData The time (in seconds) in the past when the
296 * corresponding data in plots was sampled. 437 * corresponding data in plots was sampled.
297 * @param {Array.<number>} sampleArray The array of samples wherein each 438 * @param {Array.<number>} sampleArray The array of samples wherein each
298 * element corresponds to the individual plot in |plots|. 439 * element corresponds to the individual plot in |plots|.
299 * @param {number} sampleTime Time in milliseconds since the epoch when the 440 * @param {number} sampleTime Time in milliseconds since the epoch when the
300 * samples in |sampleArray| were captured. 441 * samples in |sampleArray| were captured.
301 * @param {number} previousSampleTime Time in milliseconds since the epoch 442 * @param {number} previousSampleTime Time in milliseconds since the epoch
302 * when the sample prior to the current sample was captured. 443 * when the sample prior to the current sample was captured.
303 * @param {Array.<{time: number, sleepDuration: number}>} systemResumedArray An 444 * @param {Array.<{time: number, sleepDuration: number}>} systemResumedArray An
304 * array objects corresponding to system resume events. The 'time' field is 445 * array objects corresponding to system resume events. The 'time' field is
305 * for the time in milliseconds since the epoch when the system resumed. The 446 * for the time in milliseconds since the epoch when the system resumed. The
306 * 'sleepDuration' field is for the time in milliseconds the system spent 447 * 'sleepDuration' field is for the time in milliseconds the system spent
307 * in sleep/suspend state. 448 * in sleep/suspend state.
308 */ 449 */
309 function addTimeDataSample(plots, tData, sampleArray, 450 function addTimeDataSample(plots, tData, absTime, sampleArray,
310 sampleTime, previousSampleTime, 451 sampleTime, previousSampleTime,
311 systemResumedArray) { 452 systemResumedArray) {
312 for (var i = 0; i < plots.length; i++) { 453 for (var i = 0; i < plots.length; i++) {
313 if (plots[i].data.length != tData.length) { 454 if (plots[i].data.length != tData.length) {
314 throw new Error('Mismatch in time and plot data.'); 455 throw new Error('Mismatch in time and plot data.');
315 } 456 }
316 } 457 }
317 458
318 var time; 459 var time;
319 if (tData.length == 0) { 460 if (tData.length == 0) {
320 time = new Date(sampleTime); 461 time = new Date(sampleTime);
462 absTime[0] = sampleTime;
321 tData[0] = time.toLocaleTimeString(); 463 tData[0] = time.toLocaleTimeString();
322 for (var i = 0; i < plots.length; i++) { 464 for (var i = 0; i < plots.length; i++) {
323 plots[i].data[0] = sampleArray[i]; 465 plots[i].data[0] = sampleArray[i];
324 } 466 }
325 return; 467 return;
326 } 468 }
327 469
328 for (var i = 0; i < systemResumedArray.length; i++) { 470 for (var i = 0; i < systemResumedArray.length; i++) {
329 var resumeTime = systemResumedArray[i].time; 471 var resumeTime = systemResumedArray[i].time;
330 var sleepDuration = systemResumedArray[i].sleepDuration; 472 var sleepDuration = systemResumedArray[i].sleepDuration;
331 var sleepStartTime = resumeTime - sleepDuration; 473 var sleepStartTime = resumeTime - sleepDuration;
332 if (resumeTime < sampleTime && sleepStartTime > previousSampleTime) { 474 if (resumeTime < sampleTime) {
475 if (sleepStartTime < previousSampleTime) {
476 // This can happen if pending callbacks were handled before actually
477 // suspending.
478 sleepStartTime = previousSampleTime + 1000;
479 }
333 // Add sleep samples for every |sleepSampleInterval|. 480 // Add sleep samples for every |sleepSampleInterval|.
334 var sleepSampleTime = sleepStartTime; 481 var sleepSampleTime = sleepStartTime;
335 while (sleepSampleTime < resumeTime) { 482 while (sleepSampleTime < resumeTime) {
336 time = new Date(sleepSampleTime); 483 time = new Date(sleepSampleTime);
484 absTime.push(sleepSampleTime);
337 tData.push(time.toLocaleTimeString()); 485 tData.push(time.toLocaleTimeString());
338 for (var j = 0; j < plots.length; j++) { 486 for (var j = 0; j < plots.length; j++) {
339 plots[j].data.push(sleepText); 487 plots[j].data.push(sleepText);
340 } 488 }
341 sleepSampleTime += sleepSampleInterval; 489 sleepSampleTime += sleepSampleInterval;
342 } 490 }
343 } 491 }
344 } 492 }
345 493
346 time = new Date(sampleTime); 494 time = new Date(sampleTime);
495 absTime.push(sampleTime);
347 tData.push(time.toLocaleTimeString()); 496 tData.push(time.toLocaleTimeString());
348 for (var i = 0; i < plots.length; i++) { 497 for (var i = 0; i < plots.length; i++) {
349 plots[i].data.push(sampleArray[i]); 498 plots[i].data.push(sampleArray[i]);
350 } 499 }
351 } 500 }
352 501
353 /** 502 /**
354 * Display the battery charge vs time on a line graph. 503 * Display the battery charge vs time on a line graph.
355 * 504 *
356 * @param {Array.<{time: number, 505 * @param {Array.<{time: number,
357 * batteryPercent: number, 506 * batteryPercent: number,
358 * batteryDischargeRate: number, 507 * batteryDischargeRate: number,
359 * externalPower: number}>} powerSupplyArray An array of objects 508 * externalPower: number}>} powerSupplyArray An array of objects
360 * with fields representing the battery charge, time when the charge 509 * with fields representing the battery charge, time when the charge
361 * measurement was taken, and whether there was external power connected at 510 * measurement was taken, and whether there was external power connected at
362 * that time. 511 * that time.
363 * @param {Array.<{time: ?, sleepDuration: ?}>} systemResumedArray An array 512 * @param {Array.<{time: ?, sleepDuration: ?}>} systemResumedArray An array
364 * objects with fields 'time' and 'sleepDuration'. Each object corresponds 513 * objects with fields 'time' and 'sleepDuration'. Each object corresponds
365 * to a system resume event. The 'time' field is for the time in 514 * to a system resume event. The 'time' field is for the time in
366 * milliseconds since the epoch when the system resumed. The 'sleepDuration' 515 * milliseconds since the epoch when the system resumed. The 'sleepDuration'
367 * field is for the time in milliseconds the system spent in sleep/suspend 516 * field is for the time in milliseconds the system spent in sleep/suspend
368 * state. 517 * state.
369 */ 518 */
370 function showBatteryChargeData(powerSupplyArray, systemResumedArray) { 519 function showBatteryChargeData(powerSupplyArray, systemResumedArray) {
371 var chargeTimeData = []; 520 var chargeTimeData = [];
521 var chargeAbsTime = [];
372 var chargePlot = [ 522 var chargePlot = [
373 { 523 {
524 name: loadTimeData.getString('batteryChargePercentageHeader'),
374 color: '#0000FF', 525 color: '#0000FF',
375 data: [] 526 data: []
376 } 527 }
377 ]; 528 ];
378 var dischargeRateTimeData = []; 529 var dischargeRateTimeData = [];
530 var dischargeRateAbsTime = [];
379 var dischargeRatePlot = [ 531 var dischargeRatePlot = [
380 { 532 {
533 name: loadTimeData.getString('dischargeRateLegendText'),
381 color: '#FF0000', 534 color: '#FF0000',
382 data: [] 535 data: []
383 } 536 }
384 ]; 537 ];
385 var minDischargeRate = 1000; // A high unrealistic number to begin with. 538 var minDischargeRate = 1000; // A high unrealistic number to begin with.
386 var maxDischargeRate = -1000; // A low unrealistic number to begin with. 539 var maxDischargeRate = -1000; // A low unrealistic number to begin with.
387 for (var i = 0; i < powerSupplyArray.length; i++) { 540 for (var i = 0; i < powerSupplyArray.length; i++) {
388 var j = Math.max(i - 1, 0); 541 var j = Math.max(i - 1, 0);
389 542
390 addTimeDataSample(chargePlot, chargeTimeData, 543 addTimeDataSample(chargePlot,
544 chargeTimeData,
545 chargeAbsTime,
391 [powerSupplyArray[i].batteryPercent], 546 [powerSupplyArray[i].batteryPercent],
392 powerSupplyArray[i].time, 547 powerSupplyArray[i].time,
393 powerSupplyArray[j].time, 548 powerSupplyArray[j].time,
394 systemResumedArray); 549 systemResumedArray);
395 550
396 var dischargeRate = powerSupplyArray[i].batteryDischargeRate; 551 var dischargeRate = powerSupplyArray[i].batteryDischargeRate;
397 minDischargeRate = Math.min(dischargeRate, minDischargeRate); 552 minDischargeRate = Math.min(dischargeRate, minDischargeRate);
398 maxDischargeRate = Math.max(dischargeRate, maxDischargeRate); 553 maxDischargeRate = Math.max(dischargeRate, maxDischargeRate);
399 addTimeDataSample(dischargeRatePlot, 554 addTimeDataSample(dischargeRatePlot,
400 dischargeRateTimeData, 555 dischargeRateTimeData,
556 dischargeRateAbsTime,
401 [dischargeRate], 557 [dischargeRate],
402 powerSupplyArray[i].time, 558 powerSupplyArray[i].time,
403 powerSupplyArray[j].time, 559 powerSupplyArray[j].time,
404 systemResumedArray); 560 systemResumedArray);
405 } 561 }
406 if (minDischargeRate == maxDischargeRate) { 562 if (minDischargeRate == maxDischargeRate) {
407 // This means that all the samples had the same value. Hence, offset the 563 // This means that all the samples had the same value. Hence, offset the
408 // extremes by a bit so that the plot looks good. 564 // extremes by a bit so that the plot looks good.
409 minDischargeRate -= 1; 565 minDischargeRate -= 1;
410 maxDischargeRate += 1; 566 maxDischargeRate += 1;
411 } 567 }
412 568
413 var chargeCanvas = $('battery-charge-percentage-canvas'); 569 plotsDiv = $('battery-charge-plots-div');
414 var dischargeRateCanvas = $('battery-discharge-rate-canvas'); 570
415 plotLineGraph(chargeCanvas, chargeTimeData, chargePlot, 0.00, 100.00, 3); 571 canvases = addCanvases(
416 plotLineGraph(dischargeRateCanvas, 572 [loadTimeData.getString('batteryChargePercentageHeader'),
417 dischargeRateTimeData, 573 loadTimeData.getString('batteryDischargeRateHeader')],
418 dischargeRatePlot, 574 plotsDiv);
419 minDischargeRate, 575
420 maxDischargeRate, 576 batteryChargeCanvases = canvases[
421 3); 577 loadTimeData.getString('batteryChargePercentageHeader')];
578 plotLineGraph(
579 batteryChargeCanvases['plot'],
580 batteryChargeCanvases['legend'],
581 chargeTimeData,
582 chargePlot,
583 0.00,
584 100.00,
585 3);
586
587 dischargeRateCanvases = canvases[
588 loadTimeData.getString('batteryDischargeRateHeader')];
589 plotLineGraph(
590 dischargeRateCanvases['plot'],
591 dischargeRateCanvases['legend'],
592 dischargeRateTimeData,
593 dischargeRatePlot,
594 minDischargeRate,
595 maxDischargeRate,
596 3);
597 }
598
599 /**
600 * Shows state occupancy data (CPU idle or CPU freq state occupancy) on a set of
601 * plots on the about:power UI.
602 *
603 * @param {Array.<{Array.<{
604 * time: number,
605 * cpuOnline:boolean,
606 * stateOccupancy: {<string>: number}>}>} stateOccupancyData Array of arrays
607 * where each array corresponds to a CPU on the system. The elements of the
608 * individual arrays contain state occupancy samples.
609 * @param {Array.<{time: ?, sleepDuration: ?}>} systemResumedArray An array
610 * objects with fields 'time' and 'sleepDuration'. Each object corresponds
611 * to a system resume event. The 'time' field is for the time in
612 * milliseconds since the epoch when the system resumed. The 'sleepDuration'
613 * field is for the time in milliseconds the system spent in sleep/suspend
614 * state.
615 * @param {string} i18nHeaderString The header string to be displayed with each
616 * plot. For example, CPU idle data will have its own header format, and CPU
617 * freq data will have its header format.
618 * @param {string} unitString This is the string capturing the unit, if any,
619 * for the different states. Note that this is not the unit of the data
620 * being plotted.
621 * @param {HTMLDivElement} plotsDivId The div element in which the plots should
622 * be added.
623 */
624 function showStateOccupancyData(stateOccupancyData,
625 systemResumedArray,
626 i18nHeaderString,
627 unitString,
628 plotsDivId) {
629 var cpuPlots = [];
630 for (var cpu = 0; cpu < stateOccupancyData.length; cpu++) {
631 var cpuData = stateOccupancyData[cpu];
632 if (cpuData.length == 0) {
633 cpuPlots[cpu] = {plots: [], tData: []};
634 continue;
635 }
636 tData = [];
637 absTime = [];
638 // Each element of |plots| is an array of samples, one for each of the CPU
639 // states. The number of states is dicovered by looking at the first
640 // sample for which the CPU is online.
641 var plots = [];
642 var stateIndexMap = [];
643 var stateCount = 0;
644 for (var i = 0; i < cpuData.length; i++) {
645 if (cpuData[i].cpuOnline) {
646 for (var state in cpuData[i].stateOccupancy) {
647 var stateName = state;
648 if (unitString != null) {
649 stateName += ' ' + unitString;
650 }
651 plots.push({
652 name: stateName,
653 data: [],
654 color: plotColors[stateCount]
655 });
656 stateIndexMap.push(state);
657 stateCount += 1;
658 }
659 break;
660 }
661 }
662 // If stateCount is 0, then it means the CPU has been offline
663 // throughout. Just add a single plot for such a case.
664 if (stateCount == 0) {
665 plots.push({
666 name: null,
667 data: [],
668 color: null
669 });
670 stateCount = 1; // Some invalid state!
671 }
672
673 // Pass the samples through the function addTimeDataSample to add 'sleep'
674 // samples.
675 for (var i = 0; i < cpuData.length; i++) {
676 var sample = cpuData[i];
677 var valArray = [];
678 for (var j = 0; j < stateCount; j++) {
679 if (sample.cpuOnline) {
680 valArray[j] = sample.stateOccupancy[stateIndexMap[j]];
681 } else {
682 valArray[j] = offlineText;
683 }
684 }
685
686 var k = Math.max(i - 1, 0);
687 addTimeDataSample(plots,
688 tData,
689 absTime,
690 valArray,
691 sample.time,
692 cpuData[k].time,
693 systemResumedArray);
694 }
695
696 // Calculate the percentage occupancy of each state. A valid number is
697 // possible only if two consecutive samples are valid/numbers.
698 for (var k = 0; k < stateCount; k++) {
699 var stateData = plots[k].data;
700 // Skip the first sample as there is no previous sample.
701 for (var i = stateData.length - 1; i > 0; i--) {
702 if (typeof stateData[i] === 'number') {
703 if (typeof stateData[i - 1] === 'number') {
704 stateData[i] = (stateData[i] - stateData[i - 1]) /
705 (absTime[i] - absTime[i - 1]) * 100;
706 } else {
707 stateData[i] = invalidDataText;
708 }
709 }
710 }
711 }
712
713 // Remove the first sample from the time and data arrays.
714 tData.shift();
715 for (var k = 0; k < stateCount; k++) {
716 plots[k].data.shift();
717 }
718 cpuPlots[cpu] = {plots: plots, tData: tData};
719 }
720
721 headers = [];
722 for (var cpu = 0; cpu < stateOccupancyData.length; cpu++) {
723 headers[cpu] =
724 'CPU ' + cpu + ' ' + loadTimeData.getString(i18nHeaderString);
725 }
726
727 canvases = addCanvases(headers, $(plotsDivId));
728 for (var cpu = 0; cpu < stateOccupancyData.length; cpu++) {
729 cpuCanvases = canvases[headers[cpu]];
730 plotLineGraph(cpuCanvases['plot'],
731 cpuCanvases['legend'],
732 cpuPlots[cpu]['tData'],
733 cpuPlots[cpu]['plots'],
734 0,
735 100,
736 3);
737 }
738 }
739
740 function showCpuIdleData(idleStateData, systemResumedArray) {
741 showStateOccupancyData(idleStateData,
742 systemResumedArray,
743 'idleStateOccupancyPercentageHeader',
744 null,
745 'cpu-idle-plots-div');
746 }
747
748 function showCpuFreqData(freqStateData, systemResumedArray) {
749 showStateOccupancyData(freqStateData,
750 systemResumedArray,
751 'frequencyStateOccupancyPercentageHeader',
752 'MHz',
753 'cpu-freq-plots-div');
422 } 754 }
423 755
424 function requestBatteryChargeData() { 756 function requestBatteryChargeData() {
425 chrome.send('requestBatteryChargeData'); 757 chrome.send('requestBatteryChargeData');
426 } 758 }
427 759
760 function requestCpuIdleData() {
761 chrome.send('requestCpuIdleData');
762 }
763
764 function requestCpuFreqData() {
765 chrome.send('requestCpuFreqData');
766 }
767
768 /**
769 * Return a callback for the 'Show'/'Hide' buttons for each section of the
770 * about:power page.
771 *
772 * @param {string} sectionId The ID of the section which is to be shown or
773 * hidden.
774 * @param {string} buttonId The ID of the 'Show'/'Hide' button.
775 * @param {function} requestFunction The function which should be invoked on
776 * 'Show' to request for data from chrome.
777 * @return {function} The button callback function.
778 */
779 function showHideCallback(sectionId, buttonId, requestFunction) {
780 return function() {
781 if ($(sectionId).hidden) {
782 $(sectionId).hidden = false;
783 $(buttonId).textContent = loadTimeData.getString('hideButton');
784 requestFunction();
785 } else {
786 $(sectionId).hidden = true;
787 $(buttonId).textContent = loadTimeData.getString('showButton');
788 }
789 }
790 }
791
428 var powerUI = { 792 var powerUI = {
429 showBatteryChargeData: showBatteryChargeData 793 showBatteryChargeData: showBatteryChargeData,
794 showCpuIdleData: showCpuIdleData,
795 showCpuFreqData: showCpuFreqData
430 }; 796 };
431 797
432 document.addEventListener('DOMContentLoaded', function() { 798 document.addEventListener('DOMContentLoaded', function() {
433 requestBatteryChargeData(); 799 $('battery-charge-section').hidden = true;
800 $('battery-charge-show-button').onclick = showHideCallback(
801 'battery-charge-section',
802 'battery-charge-show-button',
803 requestBatteryChargeData);
434 $('battery-charge-reload-button').onclick = requestBatteryChargeData; 804 $('battery-charge-reload-button').onclick = requestBatteryChargeData;
805
806 $('cpu-idle-section').hidden = true;
807 $('cpu-idle-show-button').onclick = showHideCallback(
808 'cpu-idle-section', 'cpu-idle-show-button', requestCpuIdleData);
809 $('cpu-idle-reload-button').onclick = requestCpuIdleData;
810
811
812 $('cpu-freq-section').hidden = true;
813 $('cpu-freq-show-button').onclick = showHideCallback(
814 'cpu-freq-section', 'cpu-freq-show-button', requestCpuFreqData);
815 $('cpu-freq-reload-button').onclick = requestCpuFreqData;
435 }); 816 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698