1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 'use strict'; 6 7 /** 8 * @fileoverview Parses power events in the Linux event trace format. 9 */ 10 base.require('tracing.importer.linux_perf.parser'); 11 base.require('tracing.trace_model.counter_series'); 12 13 base.exportTo('tracing.importer.linux_perf', function() { 14 15 var Parser = tracing.importer.linux_perf.Parser; 16 17 /** 18 * Parses linux power trace events. 19 * @constructor 20 */ 21 function PowerParser(importer) { 22 Parser.call(this, importer); 23 24 // NB: old-style power events, deprecated 25 importer.registerEventHandler('power_start', 26 PowerParser.prototype.powerStartEvent.bind(this)); 27 importer.registerEventHandler('power_frequency', 28 PowerParser.prototype.powerFrequencyEvent.bind(this)); 29 30 importer.registerEventHandler('cpu_frequency', 31 PowerParser.prototype.cpuFrequencyEvent.bind(this)); 32 importer.registerEventHandler('cpu_idle', 33 PowerParser.prototype.cpuIdleEvent.bind(this)); 34 } 35 36 PowerParser.prototype = { 37 __proto__: Parser.prototype, 38 39 cpuStateSlice: function(ts, targetCpuNumber, eventType, cpuState) { 40 var targetCpu = this.importer.getOrCreateCpuState(targetCpuNumber); 41 var powerCounter; 42 if (eventType != '1') { 43 this.importer.importError('Don\'t understand power_start events of ' + 44 'type ' + eventType); 45 return; 46 } 47 powerCounter = targetCpu.cpu.getOrCreateCounter('', 'C-State'); 48 if (powerCounter.numSeries === 0) { 49 powerCounter.addSeries(new tracing.trace_model.CounterSeries('state', 50 tracing.getStringColorId(powerCounter.name + '.' + 'state'))); 51 } 52 powerCounter.series.forEach(function(series) { 53 series.addSample(ts, cpuState); 54 }); 55 }, 56 57 cpuIdleSlice: function(ts, targetCpuNumber, cpuState) { 58 var targetCpu = this.importer.getOrCreateCpuState(targetCpuNumber); 59 var powerCounter = targetCpu.cpu.getOrCreateCounter('', 'C-State'); 60 if (powerCounter.numSeries === 0) { 61 powerCounter.addSeries(new tracing.trace_model.CounterSeries('state', 62 tracing.getStringColorId(powerCounter.name))); 63 } 64 // NB: 4294967295/-1 means an exit from the current state 65 var val = (cpuState != 4294967295 ? cpuState : 0); 66 powerCounter.series.forEach(function(series) { 67 series.addSample(ts, val); 68 }); 69 }, 70 71 cpuFrequencySlice: function(ts, targetCpuNumber, powerState) { 72 var targetCpu = this.importer.getOrCreateCpuState(targetCpuNumber); 73 var powerCounter = 74 targetCpu.cpu.getOrCreateCounter('', 'Clock Frequency'); 75 if (powerCounter.numSeries === 0) { 76 powerCounter.addSeries(new tracing.trace_model.CounterSeries('state', 77 tracing.getStringColorId(powerCounter.name + '.' + 'state'))); 78 } 79 powerCounter.series.forEach(function(series) { 80 series.addSample(ts, powerState); 81 }); 82 }, 83 84 /** 85 * Parses power events and sets up state in the importer. 86 */ 87 powerStartEvent: function(eventName, cpuNumber, pid, ts, eventBase) { 88 var event = /type=(\d+) state=(\d) cpu_id=(\d)+/.exec(eventBase.details); 89 if (!event) 90 return false; 91 92 var targetCpuNumber = parseInt(event[3]); 93 var cpuState = parseInt(event[2]); 94 this.cpuStateSlice(ts, targetCpuNumber, event[1], cpuState); 95 return true; 96 }, 97 98 powerFrequencyEvent: function(eventName, cpuNumber, pid, ts, eventBase) { 99 var event = /type=(\d+) state=(\d+) cpu_id=(\d)+/ 100 .exec(eventBase.details); 101 if (!event) 102 return false; 103 104 var targetCpuNumber = parseInt(event[3]); 105 var powerState = parseInt(event[2]); 106 this.cpuFrequencySlice(ts, targetCpuNumber, powerState); 107 return true; 108 }, 109 110 cpuFrequencyEvent: function(eventName, cpuNumber, pid, ts, eventBase) { 111 var event = /state=(\d+) cpu_id=(\d)+/.exec(eventBase.details); 112 if (!event) 113 return false; 114 115 var targetCpuNumber = parseInt(event[2]); 116 var powerState = parseInt(event[1]); 117 this.cpuFrequencySlice(ts, targetCpuNumber, powerState); 118 return true; 119 }, 120 121 cpuIdleEvent: function(eventName, cpuNumber, pid, ts, eventBase) { 122 var event = /state=(\d+) cpu_id=(\d)+/.exec(eventBase.details); 123 if (!event) 124 return false; 125 126 var targetCpuNumber = parseInt(event[2]); 127 var cpuState = parseInt(event[1]); 128 this.cpuIdleSlice(ts, targetCpuNumber, cpuState); 129 return true; 130 } 131 }; 132 133 Parser.registerSubtype(PowerParser); 134 135 return { 136 PowerParser: PowerParser 137 }; 138 }); 139