• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {colorForTid} from '../../common/colorizer';
16import {TrackState} from '../../common/state';
17import {checkerboardExcept} from '../../frontend/checkerboard';
18import {globals} from '../../frontend/globals';
19import {Track} from '../../frontend/track';
20import {trackRegistry} from '../../frontend/track_registry';
21
22import {
23  Config,
24  Data,
25  PROCESS_SUMMARY_TRACK,
26} from './common';
27
28const MARGIN_TOP = 5;
29const RECT_HEIGHT = 30;
30const TRACK_HEIGHT = MARGIN_TOP * 2 + RECT_HEIGHT;
31const SUMMARY_HEIGHT = TRACK_HEIGHT - MARGIN_TOP;
32
33class ProcessSummaryTrack extends Track<Config, Data> {
34  static readonly kind = PROCESS_SUMMARY_TRACK;
35  static create(trackState: TrackState): ProcessSummaryTrack {
36    return new ProcessSummaryTrack(trackState);
37  }
38
39  constructor(trackState: TrackState) {
40    super(trackState);
41  }
42
43  getHeight(): number {
44    return TRACK_HEIGHT;
45  }
46
47  renderCanvas(ctx: CanvasRenderingContext2D): void {
48    const {timeScale, visibleWindowTime} = globals.frontendLocalState;
49    const data = this.data();
50    if (data === undefined) return;  // Can't possibly draw anything.
51
52    checkerboardExcept(
53        ctx,
54        this.getHeight(),
55        timeScale.timeToPx(visibleWindowTime.start),
56        timeScale.timeToPx(visibleWindowTime.end),
57        timeScale.timeToPx(data.start),
58        timeScale.timeToPx(data.end));
59
60    this.renderSummary(ctx, data);
61  }
62
63  // TODO(dproy): Dedup with CPU slices.
64  renderSummary(ctx: CanvasRenderingContext2D, data: Data): void {
65    const {timeScale, visibleWindowTime} = globals.frontendLocalState;
66    const startPx = Math.floor(timeScale.timeToPx(visibleWindowTime.start));
67    const bottomY = TRACK_HEIGHT;
68
69    let lastX = startPx;
70    let lastY = bottomY;
71
72    // TODO(hjd): Dedupe this math.
73    const color = colorForTid(this.config.pidForColor);
74    color.l = Math.min(color.l + 10, 60);
75    color.s -= 20;
76
77    ctx.fillStyle = `hsl(${color.h}, ${color.s}%, ${color.l}%)`;
78    ctx.beginPath();
79    ctx.moveTo(lastX, lastY);
80    for (let i = 0; i < data.utilizations.length; i++) {
81      // TODO(dproy): Investigate why utilization is > 1 sometimes.
82      const utilization = Math.min(data.utilizations[i], 1);
83      const startTime = i * data.bucketSizeSeconds + data.start;
84
85      lastX = Math.floor(timeScale.timeToPx(startTime));
86
87      ctx.lineTo(lastX, lastY);
88      lastY = MARGIN_TOP + Math.round(SUMMARY_HEIGHT * (1 - utilization));
89      ctx.lineTo(lastX, lastY);
90    }
91    ctx.lineTo(lastX, bottomY);
92    ctx.closePath();
93    ctx.fill();
94  }
95}
96
97trackRegistry.register(ProcessSummaryTrack);
98