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