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 { getThreadPool } from '../../SqlLite'; 17import { TraceRow } from '../../../component/trace/base/TraceRow'; 18import { XpowerThreadCountStruct } from '../../ui-worker/ProcedureWorkerXpowerThreadCount'; 19import { XpowerThreadInfoStruct } from '../../ui-worker/ProcedureWorkerXpowerThreadInfo'; 20import { Utils } from '../../../component/trace/base/Utils'; 21 22export function xpowerThreadCountDataSender( 23 row: TraceRow<XpowerThreadCountStruct>, 24 args?: unknown 25): Promise<XpowerThreadCountStruct[]> { 26 let trafic: number = TraficEnum.Memory; 27 let width = row.clientWidth - CHART_OFFSET_LEFT; 28 if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) { 29 row.sharedArrayBuffers = { 30 value: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 31 startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 32 dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 33 }; 34 } 35 return new Promise((resolve, reject): void => { 36 getThreadPool(row.traceId).submitProto( 37 QueryEnum.XpowerThreadCountData, 38 { 39 startNS: TraceRow.range?.startNS || 0, 40 endNS: TraceRow.range?.endNS || 0, 41 totalNS: TraceRow.range?.totalNS || 0, 42 recordStartNS: Utils.getInstance().getRecordStartNS(row.traceId), 43 recordEndNS: Utils.getInstance().getRecordEndNS(row.traceId), 44 // @ts-ignore 45 queryAll: args && args.queryAll, 46 // @ts-ignore 47 selectStartNS: args ? args.startNS : 0, 48 // @ts-ignore 49 selectEndNS: args ? args.endNS : 0, 50 // @ts-ignore 51 selectTotalNS: args ? args.endNS - args.startNS : 0, 52 t: Date.now(), 53 width: width, 54 trafic: trafic, 55 sharedArrayBuffers: row.sharedArrayBuffers, 56 }, 57 (res: unknown, len: number, transfer: boolean): void => { 58 resolve(threadCountArrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 59 } 60 ); 61 }); 62} 63 64function threadCountArrayBufferHandler(buffers: unknown, len: number): XpowerThreadCountStruct[] { 65 let outArr: XpowerThreadCountStruct[] = []; 66 // @ts-ignore 67 let value = new Float64Array(buffers.value); 68 // @ts-ignore 69 let startNS = new Float64Array(buffers.startNS); 70 // @ts-ignore 71 let dur = new Float64Array(buffers.dur); 72 for (let i = 0; i < len; i++) { 73 outArr.push({ 74 value: value[i], 75 startNS: startNS[i], 76 dur: dur[i], 77 } as unknown as XpowerThreadCountStruct); 78 } 79 return outArr; 80} 81 82export function xpowerThreadInfoDataSender( 83 valueType: string, 84 row: TraceRow<XpowerThreadInfoStruct>, 85 args?: unknown 86): Promise<XpowerThreadInfoStruct[]> { 87 let trafic: number = TraficEnum.Memory; 88 let width = row.clientWidth - CHART_OFFSET_LEFT; 89 if (trafic === TraficEnum.SharedArrayBuffer && !row.sharedArrayBuffers) { 90 row.sharedArrayBuffers = { 91 value: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 92 startNS: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 93 dur: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 94 threadTime: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 95 threadNameId: new SharedArrayBuffer(Float64Array.BYTES_PER_ELEMENT * MAX_COUNT), 96 }; 97 } 98 return new Promise((resolve, reject): void => { 99 getThreadPool(row.traceId).submitProto( 100 QueryEnum.XpowerThreadInfoData, 101 { 102 valueType: valueType, 103 startNS: TraceRow.range?.startNS || 0, 104 endNS: TraceRow.range?.endNS || 0, 105 totalNS: TraceRow.range?.totalNS || 0, 106 recordStartNS: Utils.getInstance().getRecordStartNS(row.traceId), 107 recordEndNS: Utils.getInstance().getRecordEndNS(row.traceId), 108 // @ts-ignore 109 queryAll: args && args.queryAll, 110 // @ts-ignore 111 selectStartNS: args ? args.startNS : 0, 112 // @ts-ignore 113 selectEndNS: args ? args.endNS : 0, 114 // @ts-ignore 115 selectTotalNS: args ? args.endNS - args.startNS : 0, 116 t: Date.now(), 117 width: width, 118 trafic: trafic, 119 sharedArrayBuffers: row.sharedArrayBuffers, 120 }, 121 (res: unknown, len: number, transfer: boolean): void => { 122 resolve(threadInfoArrayBufferHandler(transfer ? res : row.sharedArrayBuffers, len)); 123 } 124 ); 125 }); 126} 127 128function threadInfoArrayBufferHandler(buffers: unknown, len: number): XpowerThreadInfoStruct[] { 129 let outArr: XpowerThreadInfoStruct[] = []; 130 // @ts-ignore 131 let value = new Float64Array(buffers.value); 132 // @ts-ignore 133 let startNS = new Float64Array(buffers.startNS); 134 // @ts-ignore 135 let dur = new Float64Array(buffers.dur); 136 // @ts-ignore 137 let threadTime = new Float64Array(buffers.threadTime); 138 // @ts-ignore 139 let threadNameId = new Float64Array(buffers.threadNameId); 140 for (let i = 0; i < len; i++) { 141 outArr.push({ 142 value: value[i], 143 startNS: startNS[i], 144 dur: dur[i], 145 threadTime: threadTime[i], 146 threadNameId: threadNameId[i], 147 } as unknown as XpowerThreadInfoStruct); 148 } 149 return outArr; 150} 151