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 { Args } from '../CommonArgs';
15import { TraficEnum } from '../utils/QueryEnum';
16
17export const chartProcessStartupDataSql = (args: Args): string => {
18  return `
19      select P.pid,
20             A.tid,
21             A.call_id                                                                         as itid,
22             (case when A.start_time < ${args.recordStartNS} then 0 else (A.start_time - ${args.recordStartNS}) end) as startTime,
23             (case
24                  when A.start_time < ${args.recordStartNS} then (A.end_time - ${args.recordStartNS})
25                  when A.end_time = -1 then 0
26                  else (A.end_time - A.start_time) end)                                        as dur,
27             A.start_name                                                                      as startName
28      from app_startup A
29               left join process P on A.ipid = P.ipid
30      where P.pid = ${args.pid}
31      order by start_name;`;
32};
33
34export function processStartupDataReceiver(data: unknown, proc: Function): void {
35  //@ts-ignore
36  let sql = chartProcessStartupDataSql(data.params);
37  let res = proc(sql); //@ts-ignore
38  arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
39}
40
41function arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
42  //@ts-ignore
43  let startTs = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startTime); //@ts-ignore
44  let dur = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.dur); //@ts-ignore
45  let pid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.pid); //@ts-ignore
46  let tid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.tid); //@ts-ignore
47  let itid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.itid); //@ts-ignore
48  let startName = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.startName);
49  res.forEach((it, i) => {
50    //@ts-ignore
51    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processStartupData); //@ts-ignore
52    dur[i] = it.dur || 0; //@ts-ignore
53    startTs[i] = it.startTime || 0; //@ts-ignore
54    pid[i] = it.pid || 0; //@ts-ignore
55    tid[i] = it.tid || 0; //@ts-ignore
56    itid[i] = it.itid || 0; //@ts-ignore
57    startName[i] = it.startName || 0;
58  });
59  (self as unknown as Worker).postMessage(
60    {
61      //@ts-ignore
62      id: data.id, //@ts-ignore
63      action: data.action,
64      results: transfer
65        ? {
66            dur: dur.buffer,
67            startTs: startTs.buffer,
68            pid: pid.buffer,
69            tid: tid.buffer,
70            itid: itid.buffer,
71            startName: startName.buffer,
72          }
73        : {},
74      len: res.length,
75      transfer: transfer,
76    },
77    transfer ? [dur.buffer, startTs.buffer, pid.buffer, tid.buffer, itid.buffer] : []
78  );
79}
80