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 {slowlyCountRows} from '../../common/query_iterator';
16import {
17  TrackController,
18  trackControllerRegistry
19} from '../../controller/track_controller';
20
21import {
22  Config,
23  CPU_PROFILE_TRACK_KIND,
24  Data,
25} from './common';
26
27class CpuProfileTrackController extends TrackController<Config, Data> {
28  static readonly kind = CPU_PROFILE_TRACK_KIND;
29  async onBoundsChange(start: number, end: number, resolution: number):
30      Promise<Data> {
31    const query = `select id, ts, callsite_id from cpu_profile_stack_sample
32        where utid = ${this.config.utid}
33        order by ts`;
34
35    const result = await this.query(query);
36
37    const numRows = slowlyCountRows(result);
38    const data: Data = {
39      start,
40      end,
41      resolution,
42      length: numRows,
43      ids: new Float64Array(numRows),
44      tsStarts: new Float64Array(numRows),
45      callsiteId: new Uint32Array(numRows),
46    };
47
48    for (let row = 0; row < numRows; row++) {
49      data.ids[row] = +result.columns[0].longValues![row];
50      data.tsStarts[row] = +result.columns[1].longValues![row];
51      data.callsiteId[row] = +result.columns[2].longValues![row];
52    }
53
54    return data;
55  }
56}
57
58trackControllerRegistry.register(CpuProfileTrackController);
59