1// Copyright (C) 2019 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  Data,
24  HEAP_PROFILE_TRACK_KIND,
25} from './common';
26
27class HeapProfileTrackController extends TrackController<Config, Data> {
28  static readonly kind = HEAP_PROFILE_TRACK_KIND;
29  async onBoundsChange(start: number, end: number, resolution: number):
30      Promise<Data> {
31    if (this.config.upid === undefined) {
32      return {
33        start,
34        end,
35        resolution,
36        length: 0,
37        tsStarts: new Float64Array(),
38        types: new Array<string>()
39      };
40    }
41    const result = await this.query(`
42    select * from
43    (select distinct(ts) as ts, 'native' as type from heap_profile_allocation
44     where upid = ${this.config.upid}
45        union
46        select distinct(graph_sample_ts) as ts, 'graph' as type from
47        heap_graph_object
48        where upid = ${this.config.upid}) order by ts`);
49    const numRows = slowlyCountRows(result);
50    const data: Data = {
51      start,
52      end,
53      resolution,
54      length: numRows,
55      tsStarts: new Float64Array(numRows),
56      types: new Array<string>(numRows),
57    };
58
59    for (let row = 0; row < numRows; row++) {
60      data.tsStarts[row] = +result.columns[0].longValues![row];
61      data.types[row] = result.columns[1].stringValues![row];
62    }
63
64    return data;
65  }
66}
67
68trackControllerRegistry.register(HeapProfileTrackController);
69