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 { TraceRow } from '../../../component/trace/base/TraceRow'; 15import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from '../utils/QueryEnum'; 16import { threadPool } from '../../SqlLite'; 17import { HiPerfCpuStruct } from '../../ui-worker/hiperf/ProcedureWorkerHiPerfCPU2'; 18import { HiPerfProcessStruct } from '../../ui-worker/hiperf/ProcedureWorkerHiPerfProcess2'; 19 20export function hiperfProcessDataSender( 21 pid: number, 22 drawType: number, 23 intervalPerf: number, 24 scale: number, 25 row: TraceRow<any> 26): Promise<any[]> { 27 let trafic: number = TraficEnum.ProtoBuffer; 28 let width = row.clientWidth - CHART_OFFSET_LEFT; 29 if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) { 30 row.sharedArrayBuffers = { 31 startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 32 eventCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 33 sampleCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 34 eventTypeId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 35 callChainId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 36 height: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 37 }; 38 } 39 return new Promise((resolve): void => { 40 threadPool.submitProto( 41 QueryEnum.HiperfProcessData, 42 { 43 intervalPerf: intervalPerf, 44 startNS: TraceRow.range?.startNS || 0, 45 endNS: TraceRow.range?.endNS || 0, 46 recordStartNS: window.recordStartNS, 47 recordEndNS: window.recordEndNS, 48 width: width, 49 trafic: trafic, 50 sharedArrayBuffers: row.sharedArrayBuffers, 51 pid: pid, 52 maxCpuCount: -1, 53 scale: scale, 54 drawType: drawType, 55 }, 56 (res: any, len: number, transfer: boolean): void => { 57 resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 58 } 59 ); 60 }); 61} 62 63function arrayBufferHandler(buffers: any, len: number): HiPerfProcessStruct[] { 64 let outArr: HiPerfProcessStruct[] = []; 65 let startNS = new Float64Array(buffers.startNS); 66 let eventCount = new Int32Array(buffers.eventCount); 67 let sampleCount = new Int32Array(buffers.sampleCount); 68 let eventTypeId = new Int32Array(buffers.eventTypeId); 69 let callChainId = new Int32Array(buffers.callChainId); 70 let height = new Int32Array(buffers.height); 71 for (let i = 0; i < len; i++) { 72 outArr.push({ 73 startNS: startNS[i], 74 event_count: eventCount[i], 75 sampleCount: sampleCount[i], 76 event_type_id: eventTypeId[i], 77 callchain_id: callChainId[i], 78 height: height[i], 79 dur: 10_000_000, 80 } as unknown as HiPerfProcessStruct); 81 } 82 return outArr; 83} 84