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 chartProcessSoInitDataSql = (args: Args): string => {
18  return `
19  select
20    A.depth,
21    P.pid,
22    T.tid,
23    A.call_id as itid,
24    (A.start_time - B.start_ts) as startTime,
25    (A.end_time - A.start_time) as dur,
26    A.id,
27    A.so_name as soName
28from static_initalize A,trace_range B
29left join process P on A.ipid = P.ipid
30left join thread T on A.call_id = T.itid
31where P.pid = ${args.pid};`;
32};
33
34export function processSoInitDataReceiver(data: unknown, proc: Function): void {
35  //@ts-ignore
36  let sql = chartProcessSoInitDataSql(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.startTs); //@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 depth = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.depth); //@ts-ignore
49  let id = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.id);
50  res.forEach((it, i) => {
51    //@ts-ignore
52    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processSoInitData); //@ts-ignore
53    dur[i] = it.dur || 0; //@ts-ignore
54    startTs[i] = it.startTime || 0; //@ts-ignore
55    pid[i] = it.pid || 0; //@ts-ignore
56    tid[i] = it.tid || 0; //@ts-ignore
57    itid[i] = it.itid || 0; //@ts-ignore
58    depth[i] = it.depth || 0; //@ts-ignore
59    id[i] = it.id || 0;
60  });
61  (self as unknown as Worker).postMessage(
62    {
63      transfer: transfer, //@ts-ignore
64      id: data.id, //@ts-ignore
65      action: data.action,
66      results: transfer
67        ? {
68            dur: dur.buffer,
69            startTs: startTs.buffer,
70            pid: pid.buffer,
71            tid: tid.buffer,
72            itid: itid.buffer,
73            depth: depth.buffer,
74            id: id.buffer,
75          }
76        : {},
77      len: res.length,
78    },
79    transfer ? [dur.buffer, startTs.buffer, pid.buffer, tid.buffer, itid.buffer, depth.buffer, id.buffer] : []
80  );
81}
82