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'; 18 19export function hiperfCpuDataSender( 20 cpu: number, 21 drawType: number, 22 maxCpuCount: number, 23 intervalPerf: number, 24 scale: number, 25 // @ts-ignore 26 row: TraceRow<unknown> 27): Promise<unknown[]> { 28 let trafic: number = TraficEnum.ProtoBuffer; 29 let width = row.clientWidth - CHART_OFFSET_LEFT; 30 if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) { 31 row.sharedArrayBuffers = { 32 height: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 33 startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 34 eventCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 35 sampleCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 36 eventTypeId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 37 callChainId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 38 }; 39 } 40 return new Promise((resolve, reject) => { 41 threadPool.submitProto( 42 QueryEnum.HiperfCpuData, 43 { 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 cpu: cpu, 52 scale: scale, 53 maxCpuCount: maxCpuCount, 54 drawType: drawType, 55 intervalPerf: intervalPerf, 56 }, 57 (res: unknown, len: number, transfer: boolean) => { 58 resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 59 } 60 ); 61 }); 62} 63 64function arrayBufferHandler(buffers: unknown, len: number): HiPerfCpuStruct[] { 65 let outArr: HiPerfCpuStruct[] = []; 66 // @ts-ignore 67 let startNS = new Float64Array(buffers.startNS); 68 // @ts-ignore 69 let eventCount = new Int32Array(buffers.eventCount); 70 // @ts-ignore 71 let sampleCount = new Int32Array(buffers.sampleCount); 72 // @ts-ignore 73 let eventTypeId = new Int32Array(buffers.eventTypeId); 74 // @ts-ignore 75 let callChainId = new Int32Array(buffers.callChainId); 76 // @ts-ignore 77 let height = new Int32Array(buffers.height); 78 for (let i = 0; i < len; i++) { 79 outArr.push({ 80 startNS: startNS[i], 81 eventCount: eventCount[i], 82 sampleCount: sampleCount[i], 83 event_type_id: eventTypeId[i], 84 callchain_id: callChainId[i], 85 height: height[i], 86 dur: 10_000_000, 87 } as HiPerfCpuStruct); 88 } 89 return outArr; 90} 91