1// Copyright (C) 2020 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 {assertTrue} from '../../base/logging';
16import {Actions} from '../../common/actions';
17import {slowlyCountRows} from '../../common/query_iterator';
18import {fromNs, toNs} from '../../common/time';
19import {globals} from '../../controller/globals';
20import {
21  TrackController,
22  trackControllerRegistry,
23} from '../../controller/track_controller';
24
25import {Config, Data, DEBUG_SLICE_TRACK_KIND} from './common';
26
27class DebugSliceTrackController extends TrackController<Config, Data> {
28  static readonly kind = DEBUG_SLICE_TRACK_KIND;
29
30  async onReload() {
31    const rawResult = await this.query(`select max(depth) from debug_slices`);
32    const maxDepth = (slowlyCountRows(rawResult) === 0) ?
33        1 :
34        rawResult.columns[0].longValues![0];
35    globals.dispatch(
36        Actions.updateTrackConfig({id: this.trackId, config: {maxDepth}}));
37  }
38
39  async onBoundsChange(start: number, end: number, resolution: number):
40      Promise<Data> {
41    const rawResult = await this.query(`select id, name, ts,
42        iif(dur = -1, (SELECT end_ts FROM trace_bounds) - ts, dur),
43        depth from debug_slices where
44        (ts + dur) >= ${toNs(start)} and ts <= ${toNs(end)}`);
45
46    assertTrue(rawResult.columns.length === 5);
47    const [idCol, nameCol, tsCol, durCol, depthCol] = rawResult.columns;
48    const idValues = idCol.longValues! || idCol.doubleValues!;
49    const tsValues = tsCol.longValues! || tsCol.doubleValues!;
50    const durValues = durCol.longValues! || durCol.doubleValues!;
51
52    const numRows = slowlyCountRows(rawResult);
53    const slices: Data = {
54      start,
55      end,
56      resolution,
57      length: numRows,
58      strings: [],
59      sliceIds: new Float64Array(numRows),
60      starts: new Float64Array(numRows),
61      ends: new Float64Array(numRows),
62      depths: new Uint16Array(numRows),
63      titles: new Uint16Array(numRows),
64      isInstant: new Uint16Array(numRows),
65      isIncomplete: new Uint16Array(numRows),
66    };
67
68    const stringIndexes = new Map<string, number>();
69    function internString(str: string) {
70      let idx = stringIndexes.get(str);
71      if (idx !== undefined) return idx;
72      idx = slices.strings.length;
73      slices.strings.push(str);
74      stringIndexes.set(str, idx);
75      return idx;
76    }
77
78    for (let i = 0; i < slowlyCountRows(rawResult); i++) {
79      let sliceStart: number, sliceEnd: number;
80      if (tsCol.isNulls![i] || durCol.isNulls![i]) {
81        sliceStart = sliceEnd = -1;
82      } else {
83        sliceStart = tsValues[i];
84        const sliceDur = durValues[i];
85        sliceEnd = sliceStart + sliceDur;
86      }
87      slices.sliceIds[i] = idCol.isNulls![i] ? -1 : idValues[i];
88      slices.starts[i] = fromNs(sliceStart);
89      slices.ends[i] = fromNs(sliceEnd);
90      slices.depths[i] = depthCol.isNulls![i] ? 0 : depthCol.longValues![i];
91      const sliceName =
92          nameCol.isNulls![i] ? '[null]' : nameCol.stringValues![i];
93      slices.titles[i] = internString(sliceName);
94      slices.isInstant[i] = 0;
95      slices.isIncomplete[i] = 0;
96    }
97
98    return slices;
99  }
100}
101
102trackControllerRegistry.register(DebugSliceTrackController);
103