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 {slowlyCountRows} from '../../common/query_iterator';
16import {fromNs, toNsCeil, toNsFloor} from '../../common/time';
17import {LIMIT} from '../../common/track_data';
18import {
19  TrackController,
20  trackControllerRegistry,
21} from '../../controller/track_controller';
22
23import {ANDROID_LOGS_TRACK_KIND, Config, Data} from './common';
24
25class AndroidLogTrackController extends TrackController<Config, Data> {
26  static readonly kind = ANDROID_LOGS_TRACK_KIND;
27
28  async onBoundsChange(start: number, end: number, resolution: number):
29      Promise<Data> {
30    const startNs = toNsFloor(start);
31    const endNs = toNsCeil(end);
32
33    // |resolution| is in s/px the frontend wants.
34    const quantNs = toNsCeil(resolution);
35
36    const rawResult = await this.query(`
37      select
38        cast(ts / ${quantNs} as integer) * ${quantNs} as ts_quant,
39        prio,
40        count(prio)
41      from android_logs
42      where ts >= ${startNs} and ts <= ${endNs}
43      group by ts_quant, prio
44      order by ts_quant, prio limit ${LIMIT};`);
45
46    const rowCount = slowlyCountRows(rawResult);
47    const result = {
48      start,
49      end,
50      resolution,
51      length: rowCount,
52      numEvents: 0,
53      timestamps: new Float64Array(rowCount),
54      priorities: new Uint8Array(rowCount),
55    };
56    const cols = rawResult.columns;
57    for (let i = 0; i < rowCount; i++) {
58      result.timestamps[i] = fromNs(+cols[0].longValues![i]);
59      const prio = Math.min(+cols[1].longValues![i], 7);
60      result.priorities[i] |= (1 << prio);
61      result.numEvents += +cols[2].longValues![i];
62    }
63    return result;
64  }
65}
66
67trackControllerRegistry.register(AndroidLogTrackController);
68