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 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 height: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 32 startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 33 eventCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 34 sampleCount: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 35 eventTypeId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 36 callChainId: new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * MAX_COUNT), 37 }; 38 } 39 return new Promise((resolve, reject) => { 40 threadPool.submitProto( 41 QueryEnum.HiperfCpuData, 42 { 43 startNS: TraceRow.range?.startNS || 0, 44 endNS: TraceRow.range?.endNS || 0, 45 recordStartNS: window.recordStartNS, 46 recordEndNS: window.recordEndNS, 47 width: width, 48 trafic: trafic, 49 sharedArrayBuffers: row.sharedArrayBuffers, 50 cpu: cpu, 51 scale: scale, 52 maxCpuCount: maxCpuCount, 53 drawType: drawType, 54 intervalPerf: intervalPerf, 55 }, 56 (res: any, len: number, transfer: boolean) => { 57 resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 58 } 59 ); 60 }); 61} 62 63function arrayBufferHandler(buffers: any, len: number): HiPerfCpuStruct[] { 64 let outArr: HiPerfCpuStruct[] = []; 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 eventCount: 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 HiPerfCpuStruct); 81 } 82 return outArr; 83} 84