• 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 { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from '../utils/QueryEnum';
15import { getThreadPool } from '../../SqlLite';
16import { TraceRow } from '../../../component/trace/base/TraceRow';
17import { SysCallMap } from '../../../component/trace/base/SysCallUtils';
18import { Utils } from '../../../component/trace/base/Utils';
19import { ThreadSysCallStruct } from '../../ui-worker/ProcedureWorkerThreadSysCall';
20
21export function threadSysCallDataSender(
22  itid: number,
23  tid: number,
24  pid: number,
25  row: TraceRow<ThreadSysCallStruct>,
26  traceId?: string
27): Promise<ThreadSysCallStruct[] | boolean> {
28  let trafic: number = TraficEnum.Memory;
29  let width = row.clientWidth || row.parentRowEl!.clientWidth - CHART_OFFSET_LEFT;
30  if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) {
31    row.sharedArrayBuffers = {
32      startTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
33      dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT),
34      id: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
35      itid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
36      ipid: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT),
37    };
38  }
39  return new Promise((resolve): void => {
40    getThreadPool(traceId).submitProto(
41      QueryEnum.ThreadDataSysCall,
42      {
43        itid: itid,
44        startNS: TraceRow.range?.startNS || 0,
45        endNS: TraceRow.range?.endNS || 0,
46        recordStartNS: Utils.getInstance().getRecordStartNS(traceId),
47        recordEndNS: Utils.getInstance().getRecordEndNS(traceId),
48        width: width,
49        trafic: trafic,
50        sharedArrayBuffers: row.sharedArrayBuffers,
51      },
52      (res: unknown, len: number, transfer: boolean, isEmpty: boolean): void => {
53        if (isEmpty) {
54          resolve(true);
55        } else {
56          resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len, tid, pid));
57        }
58      }
59    );
60  });
61}
62
63function arrayBufferHandler(buffers: unknown, len: number, tid: number, pid: number): ThreadSysCallStruct[] {
64  let outArr: ThreadSysCallStruct[] = []; //@ts-ignore
65  let startTs = new Float64Array(buffers.startTs); //@ts-ignore
66  let dur = new Float64Array(buffers.dur); //@ts-ignore
67  let id = new Int32Array(buffers.id); //@ts-ignore
68  let itid = new Int32Array(buffers.itid); //@ts-ignore
69  for (let i = 0; i < len; i++) {
70    //@ts-ignore
71    outArr.push({
72      startTs: startTs[i],
73      dur: dur[i],
74      id: id[i],
75      itid: itid[i],
76      name: SysCallMap.get(id[i]) || 'unknown event',
77      tid: tid,
78      pid: pid,
79    } as ThreadSysCallStruct);
80  }
81  return outArr;
82}
83