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 { CpuStruct } from '../ui-worker/cpu/ProcedureWorkerCPU'; 15import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum'; 16import { threadPool } from '../SqlLite'; 17import { TraceRow } from '../../component/trace/base/TraceRow'; 18 19export function cpuDataSender(cpu: number, row: TraceRow<CpuStruct>): Promise<CpuStruct[]> { 20 let trafic: number = TraficEnum.Memory; 21 let width = row.clientWidth - CHART_OFFSET_LEFT; 22 if ((trafic === TraficEnum.SharedArrayBuffer) && !row.sharedArrayBuffers) { 23 row.sharedArrayBuffers = { 24 processId: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT), 25 id: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT), 26 tid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT), 27 cpu: new SharedArrayBuffer(Uint8Array.BYTES_PER_ELEMENT * MAX_COUNT), 28 dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 29 startTime: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 30 argSetId: new SharedArrayBuffer(Int8Array.BYTES_PER_ELEMENT * MAX_COUNT), 31 nofinish: new SharedArrayBuffer(Uint8Array.BYTES_PER_ELEMENT * MAX_COUNT), 32 }; 33 } 34 return new Promise((resolve): void => { 35 threadPool.submitProto(QueryEnum.CpuData, { 36 cpu: cpu, 37 startNS: TraceRow.range?.startNS || 0, 38 endNS: TraceRow.range?.endNS || 0, 39 recordStartNS: window.recordStartNS, 40 recordEndNS: window.recordEndNS, 41 width: width, 42 t: new Date().getTime(), 43 trafic: trafic, 44 sharedArrayBuffers: row.sharedArrayBuffers, 45 }, (res: any, len: number, transfer: boolean): void => { 46 resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 47 }); 48 }); 49} 50 51export function searchCpuDataSender(pidArr: Array<number>, tidArr: Array<number>): Promise<any[]> { 52 return new Promise((resolve): void => { 53 threadPool.submitProto(QueryEnum.SearchCpuData, { 54 tidArr: tidArr, 55 pidArr: pidArr, 56 trafic: TraficEnum.SharedArrayBuffer, 57 }, (res: any, len: number, transfer: boolean): void => { 58 resolve(searchArrayBufferHandler(res, len)); 59 }); 60 }); 61} 62 63function arrayBufferHandler(res: any, len: number): CpuStruct[] { 64 let outArr: CpuStruct[] = []; 65 let startTime = new Float64Array(res.startTime); 66 let dur = new Float64Array(res.dur); 67 let id = new Uint16Array(res.id); 68 let processId = new Uint16Array(res.processId); 69 let tid = new Uint16Array(res.tid); 70 let cpu = new Uint8Array(res.cpu); 71 let argSetID = new Int8Array(res.argSetID); 72 let nofinish = new Uint8Array(res.nofinish); 73 for (let i = 0; i < len; i++) { 74 outArr.push({ 75 processId: processId[i], 76 cpu: cpu[i], 77 tid: tid[i], 78 id: id[i], 79 dur: dur[i], 80 startTime: startTime[i], 81 argSetID: argSetID[i], 82 nofinish: nofinish[i] == 1 ? true : false 83 } as CpuStruct); 84 } 85 return outArr; 86} 87 88function searchArrayBufferHandler(res: any, len: number): CpuStruct[] { 89 let outArr: CpuStruct[] = []; 90 let startTime = new Float64Array(res.startTime); 91 let dur = new Float64Array(res.dur); 92 let id = new Uint16Array(res.id); 93 let processId = new Uint16Array(res.processId); 94 let tid = new Uint16Array(res.tid); 95 let cpu = new Uint8Array(res.cpu); 96 let argSetID = new Int8Array(res.argSetID); 97 for (let i = 0; i < len; i++) { 98 outArr.push({ 99 processId: processId[i], 100 cpu: cpu[i], 101 tid: tid[i], 102 id: id[i], 103 dur: dur[i], 104 startTime: startTime[i], 105 type: 'cpu', 106 argSetID: -1, 107 } as CpuStruct); 108 } 109 return outArr; 110} 111