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 { threadPool } from '../../SqlLite'; 16import { TraceRow } from '../../../component/trace/base/TraceRow'; 17import { CpuFreqLimitsStruct } from '../../ui-worker/cpu/ProcedureWorkerCpuFreqLimits'; 18 19export function cpuFreqLimitSender( 20 maxId: number, 21 minId: number, 22 cpu: number, 23 row: TraceRow<CpuFreqLimitsStruct> 24): Promise<CpuFreqLimitsStruct[]> { 25 let trafic: number = TraficEnum.Memory; 26 let width = row.clientWidth - CHART_OFFSET_LEFT; 27 if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) { 28 row.sharedArrayBuffers = { 29 value: new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT * MAX_COUNT), 30 max: new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT * MAX_COUNT), 31 min: new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT * MAX_COUNT), 32 dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 33 startNs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 34 }; 35 } 36 return new Promise((resolve, reject): void => { 37 threadPool.submitProto( 38 QueryEnum.CpuFreqLimitData, 39 { 40 startNS: TraceRow.range?.startNS || 0, 41 endNS: TraceRow.range?.endNS || 0, 42 recordStartNS: window.recordStartNS, 43 recordEndNS: window.recordEndNS, 44 width: width, 45 trafic: trafic, 46 sharedArrayBuffers: row.sharedArrayBuffers, 47 maxId: maxId, 48 minId: minId, 49 cpu: cpu, 50 }, 51 (res: any, len: number, transfer: boolean): void => { 52 resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 53 } 54 ); 55 }); 56} 57 58function arrayBufferHandler(res: any, len: number): CpuFreqLimitsStruct[] { 59 let outArr: CpuFreqLimitsStruct[] = []; 60 let startNs = new Float64Array(res.startNs); 61 let dur = new Float64Array(res.dur); 62 let value = new Uint32Array(res.value); 63 let max = new Uint32Array(res.max); 64 let min = new Uint32Array(res.min); 65 for (let i = 0; i < len; i++) { 66 outArr.push({ 67 value: value[i], 68 max: max[i], 69 min: min[i], 70 dur: dur[i], 71 startNs: startNs[i], 72 } as unknown as CpuFreqLimitsStruct); 73 } 74 return outArr; 75} 76