1<!DOCTYPE html>
2<!--
3Copyright (c) 2012 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/extras/importer/linux_perf/parser.html">
9
10<script>
11'use strict';
12
13/**
14 * @fileoverview Parses cpufreq events in the Linux event trace format.
15 */
16tr.exportTo('tr.e.importer.linux_perf', function() {
17
18  var ColorScheme = tr.b.ColorScheme;
19  var Parser = tr.e.importer.linux_perf.Parser;
20
21  /**
22   * Parses linux cpufreq trace events.
23   * @constructor
24   */
25  function CpufreqParser(importer) {
26    Parser.call(this, importer);
27
28    importer.registerEventHandler('cpufreq_interactive_up',
29        CpufreqParser.prototype.cpufreqUpDownEvent.bind(this));
30    importer.registerEventHandler('cpufreq_interactive_down',
31        CpufreqParser.prototype.cpufreqUpDownEvent.bind(this));
32    importer.registerEventHandler('cpufreq_interactive_already',
33        CpufreqParser.prototype.cpufreqTargetEvent.bind(this));
34    importer.registerEventHandler('cpufreq_interactive_notyet',
35        CpufreqParser.prototype.cpufreqTargetEvent.bind(this));
36    importer.registerEventHandler('cpufreq_interactive_setspeed',
37        CpufreqParser.prototype.cpufreqTargetEvent.bind(this));
38    importer.registerEventHandler('cpufreq_interactive_target',
39        CpufreqParser.prototype.cpufreqTargetEvent.bind(this));
40    importer.registerEventHandler('cpufreq_interactive_boost',
41        CpufreqParser.prototype.cpufreqBoostUnboostEvent.bind(this));
42    importer.registerEventHandler('cpufreq_interactive_unboost',
43        CpufreqParser.prototype.cpufreqBoostUnboostEvent.bind(this));
44  }
45
46  function splitData(input) {
47    // TODO(sleffler) split by cpu
48    var data = {};
49    var args = input.split(/\s+/);
50    var len = args.length;
51    for (var i = 0; i < len; i++) {
52      var item = args[i].split('=');
53      data[item[0]] = parseInt(item[1]);
54    }
55    return data;
56  }
57
58  CpufreqParser.prototype = {
59    __proto__: Parser.prototype,
60
61    cpufreqSlice: function(ts, eventName, cpu, args) {
62      // TODO(sleffler) should be per-cpu
63      var kthread = this.importer.getOrCreatePseudoThread('cpufreq');
64      kthread.openSlice = eventName;
65      var slice = new tr.model.Slice('', kthread.openSlice,
66          ColorScheme.getColorIdForGeneralPurposeString(kthread.openSlice),
67          ts, args, 0);
68
69      kthread.thread.sliceGroup.pushSlice(slice);
70    },
71
72    cpufreqBoostSlice: function(ts, eventName, args) {
73      var kthread = this.importer.getOrCreatePseudoThread('cpufreq_boost');
74      kthread.openSlice = eventName;
75      var slice = new tr.model.Slice('', kthread.openSlice,
76          ColorScheme.getColorIdForGeneralPurposeString(kthread.openSlice),
77          ts, args, 0);
78
79      kthread.thread.sliceGroup.pushSlice(slice);
80    },
81
82    /**
83     * Parses cpufreq events and sets up state in the importer.
84     */
85    cpufreqUpDownEvent: function(eventName, cpuNumber, pid, ts, eventBase) {
86      var data = splitData(eventBase.details);
87      this.cpufreqSlice(ts, eventName, data.cpu, data);
88      return true;
89    },
90
91    cpufreqTargetEvent: function(eventName, cpuNumber, pid, ts,
92                                 eventBase) {
93      var data = splitData(eventBase.details);
94      this.cpufreqSlice(ts, eventName, data.cpu, data);
95      return true;
96    },
97
98    cpufreqBoostUnboostEvent: function(eventName, cpuNumber, pid, ts,
99                                       eventBase) {
100      this.cpufreqBoostSlice(ts, eventName,
101          {
102            type: eventBase.details
103          });
104      return true;
105    }
106  };
107
108  Parser.register(CpufreqParser);
109
110  return {
111    CpufreqParser: CpufreqParser
112  };
113});
114</script>
115
116