1<!DOCTYPE html>
2<!--
3Copyright (c) 2013 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/model/cpu.html">
9<link rel="import" href="/tracing/model/process_base.html">
10<link rel="import" href="/tracing/base/iteration_helpers.html">
11
12<script>
13'use strict';
14
15/**
16 * @fileoverview Provides the Process class.
17 */
18tr.exportTo('tr.model', function() {
19  var Cpu = tr.model.Cpu;
20  var ProcessBase = tr.model.ProcessBase;
21
22  /**
23   * The Kernel represents kernel-level objects in the model.
24   * @constructor
25   */
26  function Kernel(model) {
27    ProcessBase.call(this, model);
28
29    this.cpus = {};
30    this.softwareMeasuredCpuCount_ = undefined;
31  };
32
33  /**
34   * Comparison between kernels is pretty meaningless.
35   */
36  Kernel.compare = function(x, y) {
37    return 0;
38  };
39
40  Kernel.prototype = {
41    __proto__: ProcessBase.prototype,
42
43    compareTo: function(that) {
44      return Kernel.compare(this, that);
45    },
46
47    get userFriendlyName() {
48      return 'Kernel';
49    },
50
51    get userFriendlyDetails() {
52      return 'Kernel';
53    },
54
55    get stableId() {
56      return 'Kernel';
57    },
58
59    /**
60     * @return {Cpu} Gets a specific Cpu or creates one if
61     * it does not exist.
62     */
63    getOrCreateCpu: function(cpuNumber) {
64      if (!this.cpus[cpuNumber])
65        this.cpus[cpuNumber] = new Cpu(this, cpuNumber);
66      return this.cpus[cpuNumber];
67    },
68
69    get softwareMeasuredCpuCount() {
70      return this.softwareMeasuredCpuCount_;
71    },
72
73    set softwareMeasuredCpuCount(softwareMeasuredCpuCount) {
74      if (this.softwareMeasuredCpuCount_ !== undefined &&
75          this.softwareMeasuredCpuCount_ !== softwareMeasuredCpuCount) {
76        throw new Error(
77            'Cannot change the softwareMeasuredCpuCount once it is set');
78      }
79
80      this.softwareMeasuredCpuCount_ = softwareMeasuredCpuCount;
81    },
82
83    /**
84     * Estimates how many cpus are in the system, for use in system load
85     * estimation.
86     *
87     * If kernel trace was provided, uses that data. Otherwise, uses the
88     * software measured cpu count.
89     */
90    get bestGuessAtCpuCount() {
91      var realCpuCount = tr.b.dictionaryLength(this.cpus);
92      if (realCpuCount !== 0)
93        return realCpuCount;
94      return this.softwareMeasuredCpuCount;
95    },
96
97    updateBounds: function() {
98      ProcessBase.prototype.updateBounds.call(this);
99      for (var cpuNumber in this.cpus) {
100        var cpu = this.cpus[cpuNumber];
101        cpu.updateBounds();
102        this.bounds.addRange(cpu.bounds);
103      }
104    },
105
106    createSubSlices: function() {
107      ProcessBase.prototype.createSubSlices.call(this);
108      for (var cpuNumber in this.cpus) {
109        var cpu = this.cpus[cpuNumber];
110        cpu.createSubSlices();
111      }
112    },
113
114    addCategoriesToDict: function(categoriesDict) {
115      ProcessBase.prototype.addCategoriesToDict.call(this, categoriesDict);
116      for (var cpuNumber in this.cpus)
117        this.cpus[cpuNumber].addCategoriesToDict(categoriesDict);
118    },
119
120    getSettingsKey: function() {
121      return 'kernel';
122    },
123
124    iterateAllChildEventContainers: function(callback, opt_this) {
125      ProcessBase.prototype.iterateAllChildEventContainers.call(
126          this, callback, opt_this);
127      for (var cpuId in this.cpus)
128        callback.call(opt_this, this.cpus[cpuId]);
129    },
130
131    iterateAllEventsInThisContainer: function(eventTypePredicate,
132                                              callback, opt_this) {
133      ProcessBase.prototype.iterateAllEventsInThisContainer.call(
134          this, eventTypePredicate, callback, opt_this);
135    }
136  };
137
138  return {
139    Kernel: Kernel
140  };
141});
142</script>
143