1fb726d48Sopenharmony_ci// Copyright (c) 2021 Huawei Device Co., Ltd.
2fb726d48Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
3fb726d48Sopenharmony_ci// you may not use this file except in compliance with the License.
4fb726d48Sopenharmony_ci// You may obtain a copy of the License at
5fb726d48Sopenharmony_ci//
6fb726d48Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
7fb726d48Sopenharmony_ci//
8fb726d48Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
9fb726d48Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
10fb726d48Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11fb726d48Sopenharmony_ci// See the License for the specific language governing permissions and
12fb726d48Sopenharmony_ci// limitations under the License.
13fb726d48Sopenharmony_ci
14fb726d48Sopenharmony_ciimport { Args } from '../CommonArgs';
15fb726d48Sopenharmony_ciimport { TraficEnum } from '../utils/QueryEnum';
16fb726d48Sopenharmony_ci
17fb726d48Sopenharmony_ciexport const chartProcessSoInitDataSql = (args: Args): string => {
18fb726d48Sopenharmony_ci  return `
19fb726d48Sopenharmony_ci  select
20fb726d48Sopenharmony_ci    A.depth,
21fb726d48Sopenharmony_ci    P.pid,
22fb726d48Sopenharmony_ci    T.tid,
23fb726d48Sopenharmony_ci    A.call_id as itid,
24fb726d48Sopenharmony_ci    (A.start_time - B.start_ts) as startTime,
25fb726d48Sopenharmony_ci    (A.end_time - A.start_time) as dur,
26fb726d48Sopenharmony_ci    A.id,
27fb726d48Sopenharmony_ci    A.so_name as soName
28fb726d48Sopenharmony_cifrom static_initalize A,trace_range B
29fb726d48Sopenharmony_cileft join process P on A.ipid = P.ipid
30fb726d48Sopenharmony_cileft join thread T on A.call_id = T.itid
31fb726d48Sopenharmony_ciwhere P.pid = ${args.pid};`;
32fb726d48Sopenharmony_ci};
33fb726d48Sopenharmony_ci
34fb726d48Sopenharmony_ciexport function processSoInitDataReceiver(data: unknown, proc: Function): void {
35fb726d48Sopenharmony_ci  //@ts-ignore
36fb726d48Sopenharmony_ci  let sql = chartProcessSoInitDataSql(data.params);
37fb726d48Sopenharmony_ci  let res = proc(sql); //@ts-ignore
38fb726d48Sopenharmony_ci  arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
39fb726d48Sopenharmony_ci}
40fb726d48Sopenharmony_ci
41fb726d48Sopenharmony_cifunction arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
42fb726d48Sopenharmony_ci  //@ts-ignore
43fb726d48Sopenharmony_ci  let startTs = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startTs); //@ts-ignore
44fb726d48Sopenharmony_ci  let dur = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.dur); //@ts-ignore
45fb726d48Sopenharmony_ci  let pid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.pid); //@ts-ignore
46fb726d48Sopenharmony_ci  let tid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.tid); //@ts-ignore
47fb726d48Sopenharmony_ci  let itid = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.itid); //@ts-ignore
48fb726d48Sopenharmony_ci  let depth = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.depth); //@ts-ignore
49fb726d48Sopenharmony_ci  let id = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.id);
50fb726d48Sopenharmony_ci  res.forEach((it, i) => {
51fb726d48Sopenharmony_ci    //@ts-ignore
52fb726d48Sopenharmony_ci    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.processSoInitData); //@ts-ignore
53fb726d48Sopenharmony_ci    dur[i] = it.dur || 0; //@ts-ignore
54fb726d48Sopenharmony_ci    startTs[i] = it.startTime || 0; //@ts-ignore
55fb726d48Sopenharmony_ci    pid[i] = it.pid || 0; //@ts-ignore
56fb726d48Sopenharmony_ci    tid[i] = it.tid || 0; //@ts-ignore
57fb726d48Sopenharmony_ci    itid[i] = it.itid || 0; //@ts-ignore
58fb726d48Sopenharmony_ci    depth[i] = it.depth || 0; //@ts-ignore
59fb726d48Sopenharmony_ci    id[i] = it.id || 0;
60fb726d48Sopenharmony_ci  });
61fb726d48Sopenharmony_ci  (self as unknown as Worker).postMessage(
62fb726d48Sopenharmony_ci    {
63fb726d48Sopenharmony_ci      transfer: transfer, //@ts-ignore
64fb726d48Sopenharmony_ci      id: data.id, //@ts-ignore
65fb726d48Sopenharmony_ci      action: data.action,
66fb726d48Sopenharmony_ci      results: transfer
67fb726d48Sopenharmony_ci        ? {
68fb726d48Sopenharmony_ci            dur: dur.buffer,
69fb726d48Sopenharmony_ci            startTs: startTs.buffer,
70fb726d48Sopenharmony_ci            pid: pid.buffer,
71fb726d48Sopenharmony_ci            tid: tid.buffer,
72fb726d48Sopenharmony_ci            itid: itid.buffer,
73fb726d48Sopenharmony_ci            depth: depth.buffer,
74fb726d48Sopenharmony_ci            id: id.buffer,
75fb726d48Sopenharmony_ci          }
76fb726d48Sopenharmony_ci        : {},
77fb726d48Sopenharmony_ci      len: res.length,
78fb726d48Sopenharmony_ci    },
79fb726d48Sopenharmony_ci    transfer ? [dur.buffer, startTs.buffer, pid.buffer, tid.buffer, itid.buffer, depth.buffer, id.buffer] : []
80fb726d48Sopenharmony_ci  );
81fb726d48Sopenharmony_ci}
82