• 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 {cpuFreqLimitList} from "../utils/AllMemoryCache";
17
18export const chartCpuFreqLimitDataSql = (args: any): string => {
19  return `
20      SELECT
21             max AS max,
22             min AS min,
23             value,
24             max(dura)     AS dur,
25             startNs AS startNs,
26             ${args.cpu} AS cpu,
27          (startNs / (${Math.floor((args.endNS - args.startNS) / args.width)})) AS px
28      FROM (
29          SELECT  ts - ${args.recordStartNS} AS startNs,
30          case when dur is null then (${args.endNS + args.recordStartNS} - ts) else dur end AS dura,
31          value,
32          MAX (value) AS max,
33          MIN (value) AS min
34          FROM measure
35          WHERE filter_id IN (${args.maxId}, ${args.minId})
36            AND startNs + dura >= ${Math.floor(args.startNS)}
37            AND startNs <= ${Math.floor(args.endNS)}
38          GROUP BY ts
39          ) AS subquery
40      GROUP BY px;
41  `;
42};
43
44export const chartCpuFreqLimitDataSqlMem = (args: any): string => {
45  return `
46      select ts - ${args.recordStartNS} as startNs,
47           dur,
48           max(value) as max,
49           min(value) as min,
50            $cpu as cpu
51    from measure where filter_id in (${args.maxId}, ${args.minId})
52                 group by ts;
53  `;
54};
55
56
57
58export function cpuFreqLimitReceiver(data: any, proc: Function): void {
59  if (data.params.trafic === TraficEnum.Memory) {
60    let res: any[], list: any[];
61    if (!cpuFreqLimitList.has(data.params.cpu)) {
62      let sql = chartCpuFreqLimitDataSqlMem(data.params);
63      list = proc(sql);
64      for (let i = 0; i < list.length; i++) {
65        if(i<list.length-1){
66          list[i].dur = list[i+1].startNs - list[i].startNs;
67        }else{
68          list[i].dur = data.params.endNS - list[i].startNs;
69        }
70      }
71      cpuFreqLimitList.set(data.params.cpu, list);
72    } else {
73      list = cpuFreqLimitList.get(data.params.cpu) || [];
74    }
75    res = filterDataByGroup(list || [], 'startNs', 'dur', data.params.startNS, data.params.endNS, data.params.width,"value");
76    arrayBufferHandler(data, res,true);
77  } else {
78    let sql = chartCpuFreqLimitDataSql(data.params);
79    let res = proc(sql);
80    arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
81  }
82}
83
84function arrayBufferHandler(data: any, res: any[], transfer: boolean): void {
85  let startNs = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startNs);
86  let dur = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.dur);
87  let value = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.value);
88  let max = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.max);
89  let min = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.min);
90  res.forEach((it, i) => {
91    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.cpuFreqLimitData);
92    startNs[i] = it.startNs;
93    dur[i] = it.dur;
94    value[i] = it.value;
95    max[i] = it.max;
96    min[i] = it.min;
97  });
98  (self as unknown as Worker).postMessage(
99    {
100      id: data.id,
101      action: data.action,
102      results: transfer
103        ? {
104            startNs: startNs.buffer,
105            dur: dur.buffer,
106            value: value.buffer,
107            max: max.buffer,
108            min: min.buffer,
109          }
110        : {},
111      len: res.length,
112      transfer: transfer,
113    },
114    transfer ? [startNs.buffer, dur.buffer, value.buffer, max.buffer, min.buffer] : []
115  );
116}
117