summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/tracing/tracing/metrics/rendering/cpu_utilization.html
blob: e6d55984e2774f5a6b26a11c60625c6fd636e1a6 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE html>
<!--
Copyright 2018 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/base/math/statistics.html">
<link rel="import" href="/tracing/base/unit.html">
<link rel="import" href="/tracing/model/helpers/chrome_model_helper.html">
<link rel="import" href="/tracing/model/user_model/segment.html">

<link rel="import" href="/tracing/value/histogram.html">

<script>
'use strict';

/**
 * @fileoverview This file contains implementations of the following metrics.
 *
 * The addCpuSegmentCostHistograms method can generate the following sets of
 * metrics, dependeing on the input values for segments, and segmentCostFunc.
 *
 * thread_{thread group}_cpu_time_per_frame
 * ========================================
 * segments: display compositor's frames
 * segmentCostFunc: thread.getCpuTimeForRange
 *
 * This set of metrics show the distribution of CPU usage of a thread
 * group in each display compositor's frame.
 *
 * tasks_per_frame_{thread group}
 * ==============================
 * segments: display compositor's frames
 * segmentCostFunc: thread.getNumToplevelSlicesForRange
 *
 * This set of metrics show the distribution of the number of task in each
 * display compositor's frame of a thread group.
 *
 * The addCpuWallTimeHistogram method generates the metric:
 * cpu_wall_time_ratio
 * ==================
 * segments: display compositor's frames
 *
 * This metric shows the ratio of cpu usage to wall time.
 *
 * Note: the CPU usage in all above-mentioned metrics, is approximated from
 * top-level trace events in each thread; it does not come from the OS. So, the
 * metric may be noisy and not be very meaningful for threads that do not have a
 * message loop.
 */
tr.exportTo('tr.metrics.rendering', function() {
  const UNKNOWN_THREAD_NAME = 'Unknown';

  const CATEGORY_THREAD_MAP = new Map();
  CATEGORY_THREAD_MAP.set('total_all', [/.*/]);
  CATEGORY_THREAD_MAP.set(
      'browser', [/^Browser Compositor$/, /^CrBrowserMain$/]);
  CATEGORY_THREAD_MAP.set('display_compositor', [/^VizCompositorThread$/]);
  CATEGORY_THREAD_MAP.set('GPU', [/^Chrome_InProcGpuThread$/, /^CrGpuMain$/]);
  CATEGORY_THREAD_MAP.set('IO', [/IOThread/]);
  CATEGORY_THREAD_MAP.set('raster', [/CompositorTileWorker/]);
  CATEGORY_THREAD_MAP.set('renderer_compositor', [/^Compositor$/]);
  CATEGORY_THREAD_MAP.set('renderer_main', [/^CrRendererMain$/]);
  CATEGORY_THREAD_MAP.set(
      'total_rendering', [
        /^Browser Compositor$/, /^Chrome_InProcGpuThread$/, /^Compositor$/,
        /CompositorTileWorker/, /^CrBrowserMain$/, /^CrGpuMain$/,
        /^CrRendererMain$/, /IOThread/, /^VizCompositorThread$/]);


  const ALL_CATEGORIES = [...CATEGORY_THREAD_MAP.keys(), 'other'];

  function addValueToMap_(map, key, value) {
    const oldValue = map.get(key) || 0;
    map.set(key, oldValue + value);
  }

  function addToArrayInMap_(map, key, value) {
    const arr = map.get(key) || [];
    arr.push(value);
    map.set(key, arr);
  }

  function* getCategories_(threadName) {
    let isOther = true;
    for (const [category, regexps] of CATEGORY_THREAD_MAP) {
      for (const regexp of regexps) {
        if (regexp.test(threadName)) {
          if (category !== 'total_all') isOther = false;
          yield category;
          break;
        }
      }
    }
    if (isOther) yield 'other';
  }

  function addCpuSegmentCostHistograms(
      histograms, model, segments, segmentCostFunc, histogramNameFunc,
      description) {
    const categoryValues = new Map();
    for (const segment of segments) {
      const threadValues = new Map();
      // Compute and store CPU times per categories and thread name.
      for (const thread of model.getAllThreads()) {
        addValueToMap_(
            threadValues,
            thread.name || UNKNOWN_THREAD_NAME,
            segmentCostFunc(thread, segment));
      }

      for (const [threadName, coresPerSec] of threadValues) {
        for (const category of getCategories_(threadName)) {
          addToArrayInMap_(categoryValues, category, coresPerSec);
        }
      }
    }

    const unit = tr.b.Unit.byName.unitlessNumber_smallerIsBetter;
    for (const category of ALL_CATEGORIES) {
      const values = categoryValues.get(category) || 0;
      if (!values) continue;
      const avg = values.reduce((sum, e) => sum + e, 0) / segments.length;
      histograms.createHistogram(
          histogramNameFunc(category), unit, avg, {
            description,
            summaryOptions: {},
          });
    }
  }

  function addCpuWallTimeHistogram(histograms, model, segments) {
    let totalWallTime = 0;
    let totalCpuTime = 0;
    for (const segment of segments) {
      for (const thread of model.getAllThreads()) {
        totalCpuTime += thread.getCpuTimeForRange(segment.boundsRange);
        totalWallTime += thread.getWallTimeForRange(segment.boundsRange);
      }
    }
    histograms.createHistogram('cpu_wall_time_ratio',
        tr.b.Unit.byName.unitlessNumber_biggerIsBetter,
        totalWallTime ? totalCpuTime / totalWallTime : NaN,
        { description: 'Ratio of total cpu-time vs. wall-time.',
          summaryOptions: {},
        });
  }

  return {
    addCpuSegmentCostHistograms,
    addCpuWallTimeHistogram,
  };
});
</script>