1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import { CHART_OFFSET_LEFT, MAX_COUNT, QueryEnum, TraficEnum } from './utils/QueryEnum'; 16import { threadPool } from '../SqlLite'; 17import { TraceRow } from '../../component/trace/base/TraceRow'; 18import { LogStruct } from '../ui-worker/ProcedureWorkerLog'; 19 20export function LogDataSender(row: TraceRow<LogStruct>): Promise<LogStruct[]> { 21 let trafic: number = TraficEnum.ProtoBuffer; 22 let width = row.clientWidth - CHART_OFFSET_LEFT; 23 if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) { 24 row.sharedArrayBuffers = { 25 id: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT), 26 startTs: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 27 pid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT), 28 tid: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT), 29 dur: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT), 30 depth: new SharedArrayBuffer(Uint16Array.BYTES_PER_ELEMENT * MAX_COUNT), 31 }; 32 } 33 return new Promise((resolve) => { 34 threadPool.submitProto( 35 QueryEnum.HilogData, 36 { 37 startNS: TraceRow.range?.startNS || 0, 38 endNS: TraceRow.range?.endNS || 0, 39 recordStartNS: window.recordStartNS, 40 recordEndNS: window.recordEndNS, 41 width: width, 42 trafic: trafic, 43 sharedArrayBuffers: row.sharedArrayBuffers, 44 oneDayTime: window.recordEndNS - ONE_DAY_NS, 45 }, 46 (res: any, len: number, transfer: boolean) => { 47 resolve(arrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 48 } 49 ); 50 }); 51} 52 53function arrayBufferHandler(res: any, len: number) { 54 let outArr: LogStruct[] = []; 55 let id = new Uint16Array(res.id); 56 let startTs = new Float64Array(res.startTs); 57 let pid = new Uint16Array(res.pid); 58 let tid = new Uint16Array(res.tid); 59 let dur = new Uint16Array(res.dur); 60 let depth = new Uint16Array(res.depth); 61 for (let index = 0; index < len; index++) { 62 outArr.push({ 63 id: id[index], 64 startTs: startTs[index], 65 pid: pid[index], 66 tid: tid[index], 67 dur: dur[index], 68 depth: depth[index], 69 } as unknown as LogStruct); 70 } 71 return outArr; 72} 73 74const ONE_DAY_NS = 86_400_000_000_000; 75