summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/tracing/tracing/metrics/system_health/cpu_time_metric.html
blob: f8a63e84ba7e8dfbbc434a26cb5c64c1a560c826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<!--
Copyright 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/metrics/metric_registry.html">
<link rel="import" href="/tracing/model/helpers/chrome_model_helper.html">
<link rel="import" href="/tracing/model/helpers/chrome_renderer_helper.html">
<link rel="import" href="/tracing/value/histogram.html">

<script>
'use strict';

tr.exportTo('tr.metrics.sh', function() {
  // Use a lower bound of 0.01 for the metric boundaries (when no CPU time
  // is consumed) and an upper bound of 50 (fifty cores are all active
  // for the entire time). We can't use zero exactly for the lower bound with an
  // exponential histogram.
  const CPU_TIME_PERCENTAGE_BOUNDARIES =
      tr.v.HistogramBinBoundaries.createExponential(0.01, 50, 200);

  /**
   * This metric measures total CPU time for Chrome processes, per second of
   *   clock time.
   * This metric requires only the 'toplevel' tracing category.
   *
   * @param {!tr.v.HistogramSet} histograms
   * @param {!tr.model.Model} model
   * @param {!Object=} opt_options
   */
  function cpuTimeMetric(histograms, model, opt_options) {
    let rangeOfInterest = model.bounds;

    if (opt_options && opt_options.rangeOfInterest) {
      rangeOfInterest = opt_options.rangeOfInterest;
    } else {
      // If no range of interest is provided, limit the relevant range to
      // Chrome processes. This prevents us from normalizing against non-Chrome
      // related slices in the trace.
      const chromeHelper = model.getOrCreateHelper(
          tr.model.helpers.ChromeModelHelper);
      if (chromeHelper) {
        const chromeBounds = chromeHelper.chromeBounds;
        if (chromeBounds) {
          rangeOfInterest = chromeBounds;
        }
      }
    }

    let allProcessCpuTime = 0;

    for (const pid in model.processes) {
      const process = model.processes[pid];
      if (tr.model.helpers.ChromeRendererHelper.isTracingProcess(process)) {
        continue;
      }

      let processCpuTime = 0;
      for (const tid in process.threads) {
        const thread = process.threads[tid];
        processCpuTime += thread.getCpuTimeForRange(rangeOfInterest);
      }
      allProcessCpuTime += processCpuTime;
    }

    // Normalize cpu time by clock time.
    let normalizedAllProcessCpuTime = 0;
    if (rangeOfInterest.duration > 0) {
      normalizedAllProcessCpuTime =
          allProcessCpuTime / rangeOfInterest.duration;
    }

    const unit = tr.b.Unit.byName.normalizedPercentage_smallerIsBetter;
    const cpuTimeHist = new tr.v.Histogram(
        'cpu_time_percentage', unit, CPU_TIME_PERCENTAGE_BOUNDARIES);
    cpuTimeHist.description =
        'Percent CPU utilization, normalized against a single core. Can be ' +
        'greater than 100% if machine has multiple cores.';
    cpuTimeHist.customizeSummaryOptions({
      avg: true,
      count: false,
      max: false,
      min: false,
      std: false,
      sum: false
    });
    cpuTimeHist.addSample(normalizedAllProcessCpuTime);
    histograms.addHistogram(cpuTimeHist);
  }

  tr.metrics.MetricRegistry.register(cpuTimeMetric, {
    supportsRangeOfInterest: true
  });

  return {
    cpuTimeMetric,
  };
});
</script>