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 Provides the Cpu class.
9 */
10base.require('base.range');
11base.require('tracing.trace_model.slice');
12base.require('tracing.trace_model.counter');
13base.exportTo('tracing.trace_model', function() {
14
15  var Counter = tracing.trace_model.Counter;
16
17  /**
18   * The Cpu represents a Cpu from the kernel's point of view.
19   * @constructor
20   */
21  function Cpu(number) {
22    this.cpuNumber = number;
23    this.slices = [];
24    this.counters = {};
25    this.bounds = new base.Range();
26  };
27
28  Cpu.prototype = {
29    /**
30     * @return {TimlineCounter} The counter on this process named 'name',
31     * creating it if it doesn't exist.
32     */
33    getOrCreateCounter: function(cat, name) {
34      var id;
35      if (cat.length)
36        id = cat + '.' + name;
37      else
38        id = name;
39      if (!this.counters[id])
40        this.counters[id] = new Counter(this, id, cat, name);
41      return this.counters[id];
42    },
43
44    /**
45     * Shifts all the timestamps inside this CPU forward by the amount
46     * specified.
47     */
48    shiftTimestampsForward: function(amount) {
49      for (var sI = 0; sI < this.slices.length; sI++)
50        this.slices[sI].start = (this.slices[sI].start + amount);
51      for (var id in this.counters)
52        this.counters[id].shiftTimestampsForward(amount);
53    },
54
55    /**
56     * Updates the range based on the current slices attached to the cpu.
57     */
58    updateBounds: function() {
59      this.bounds.reset();
60      if (this.slices.length) {
61        this.bounds.addValue(this.slices[0].start);
62        this.bounds.addValue(this.slices[this.slices.length - 1].end);
63      }
64      for (var id in this.counters) {
65        this.counters[id].updateBounds();
66        this.bounds.addRange(this.counters[id].bounds);
67      }
68    },
69
70    addCategoriesToDict: function(categoriesDict) {
71      for (var i = 0; i < this.slices.length; i++)
72        categoriesDict[this.slices[i].category] = true;
73      for (var id in this.counters)
74        categoriesDict[this.counters[id].category] = true;
75    }
76
77  };
78
79  /**
80   * Comparison between processes that orders by cpuNumber.
81   */
82  Cpu.compare = function(x, y) {
83    return x.cpuNumber - y.cpuNumber;
84  };
85
86
87  return {
88    Cpu: Cpu
89  };
90});
91