• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2021 Huawei Device Co., Ltd.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14import { TraficEnum } from './utils/QueryEnum';
15import { filterDataByGroup } from './utils/DataFilter';
16import { clockList } from './utils/AllMemoryCache';
17import { Args } from './CommonArgs';
18
19export const chartClockDataSql = (args: Args): string => {
20  if (args.sqlType === 'clockFrequency') {
21    return `
22    with freq as (
23        select measure.filter_id,
24               measure.value,
25               measure.ts,
26               measure.type
27        from measure
28        where measure.filter_id in (select id
29                                    from clock_event_filter
30                                    where clock_event_filter.name = '${args.clockName}'
31                                      and clock_event_filter.type = 'clock_set_rate')
32                                      --and startNs >= ${Math.floor(args.startNS)}
33                                      --and startNs <= ${Math.floor(args.endNS)}
34    )
35    select freq.filter_id as filterId, freq.value, freq.ts - ${args.recordStartNS} as startNs, freq.type
36    from freq
37    order by startNs;
38    `;
39  } else if (args.sqlType === 'clockState') {
40    return `select measure.filter_id                                     as filterId,
41                   measure.value                                         as value,
42                   measure.ts - ${args.recordStartNS}                    as startNs,
43                   (lead(ts, 1, null) over ( order by measure.ts)) - ts as dur,
44                   measure.type                                          as type
45            from measure
46            where measure.filter_id in (select id
47                from clock_event_filter
48                where clock_event_filter.name = '${args.clockName}'
49                and clock_event_filter.type != 'clock_set_rate')
50            --and startNs + dur >= ${Math.floor(args.startNS)}
51            --and startNs <= ${Math.floor(args.endNS)}`;
52  } else if (args.sqlType === 'screenState') {
53    return `select filter_id as filterId,value,  m.ts - ${args.recordStartNS} as startNs, m.type
54            from measure m
55            where filter_id in (select id from process_measure_filter where name = 'ScreenState')
56            --and startNs >= ${Math.floor(args.startNS)}
57            --and startNs <= ${Math.floor(args.endNS)};`;
58  } else {
59    return '';
60  }
61};
62
63export const chartClockDataSqlMem = (args: Args): string => {
64  if (args.sqlType === 'clockFrequency') {
65    return `
66        with freq as (  select measure.filter_id, measure.ts, measure.type, measure.value from clock_event_filter
67                                                                                                   left join measure
68                        where clock_event_filter.name = '${args.clockName}' and clock_event_filter.type = 'clock_set_rate' and clock_event_filter.id = measure.filter_id
69                        order by measure.ts)
70        select freq.filter_id as filterId,freq.ts - r.start_ts as startNs,freq.type,freq.value from freq,trace_range r order by startNs;
71    `;
72  } else if (args.sqlType === 'clockState') {
73    return `with state as (
74        select filter_id, ts, endts, endts-ts as dur, type, value from
75            (select measure.filter_id, measure.ts, lead(ts, 1, null) over( order by measure.ts) endts, measure.type, measure.value from clock_event_filter,trace_range
76                                                                                                                                                               left join measure
77             where clock_event_filter.name = '${args.clockName}' and clock_event_filter.type != 'clock_set_rate' and clock_event_filter.id = measure.filter_id
78             order by measure.ts))
79            select s.filter_id as filterId,s.ts-r.start_ts as startNs,s.type,s.value,s.dur from state s,trace_range r`;
80  } else if (args.sqlType === 'screenState') {
81    return `select m.type, m.ts-r.start_ts as startNs, value, filter_id  as filterId
82    from measure m,trace_range r
83    where filter_id in (select id from process_measure_filter where name = 'ScreenState')  order by startNs;`;
84  } else {
85    return '';
86  }
87};
88
89export function clockDataReceiver(data: unknown, proc: Function): void {
90  // @ts-ignore
91  if (data.params.trafic === TraficEnum.Memory) {
92    let res: unknown[];
93    let list: unknown[];
94    // @ts-ignore
95    if (!clockList.has(data.params.sqlType + data.params.clockName)) {
96      // @ts-ignore
97      list = proc(chartClockDataSqlMem(data.params));
98      for (let j = 0; j < list.length; j++) {
99        if (j === list.length - 1) {
100          // @ts-ignore
101          list[j].dur = (data.params.totalNS || 0) - (list[j].startNs || 0);
102        } else {
103          // @ts-ignore
104          list[j].dur = (list[j + 1].startNs || 0) - (list[j].startNs || 0);
105        }
106      }
107      // @ts-ignore
108      clockList.set(data.params.sqlType + data.params.clockName, list);
109    } else {
110      // @ts-ignore
111      list = clockList.get(data.params.sqlType + data.params.clockName) || [];
112    }
113    // @ts-ignore
114    if (data.params.queryAll) {
115      //框选时候取数据,只需要根据时间过滤数据
116      res = (list || []).filter(
117        // @ts-ignore
118        (it) => it.startNs + it.dur >= data.params.selectStartNS && it.startNs <= data.params.selectEndNS
119      );
120    } else {
121      res = filterDataByGroup(
122        list || [],
123        'startNs',
124        'dur',
125        // @ts-ignore
126        data.params.startNS,
127        // @ts-ignore
128        data.params.endNS,
129        // @ts-ignore
130        data.params.width,
131        'value'
132      );
133    }
134    arrayBufferHandler(data, res, true);
135  } else {
136    // @ts-ignore
137    let sql = chartClockDataSql(data.params);
138    let res = proc(sql);
139    // @ts-ignore
140    arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
141  }
142}
143
144function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
145  // @ts-ignore
146  let dur = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.dur);
147  // @ts-ignore
148  let startNS = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startNS);
149  // @ts-ignore
150  let value = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.value);
151  // @ts-ignore
152  let filterId = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.filterId);
153  res.forEach((it, i) => {
154    // @ts-ignore
155    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.clockData);
156    // @ts-ignore
157    dur[i] = it.dur;
158    // @ts-ignore
159    startNS[i] = it.startNs;
160    // @ts-ignore
161    filterId[i] = it.filterId;
162    // @ts-ignore
163    value[i] = it.value;
164  });
165  (self as unknown as Worker).postMessage(
166    {
167      // @ts-ignore
168      id: data.id,
169      // @ts-ignore
170      action: data.action,
171      results: transfer
172        ? {
173            dur: dur.buffer,
174            startNS: startNS.buffer,
175            value: value.buffer,
176            filterId: filterId.buffer,
177          }
178        : {},
179      len: res.length,
180      transfer: transfer,
181    },
182    transfer ? [dur.buffer, startNS.buffer, value.buffer, filterId.buffer] : []
183  );
184}
185