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 { type JsCpuProfilerChartFrame } from '../bean/JsStruct'; 16import { type SnapshotStruct } from '../database/ui-worker/ProcedureWorkerSnapshot'; 17import { type RangeSelectStruct, TraceRow } from './trace/base/TraceRow'; 18import { KeyPathStruct } from '../bean/KeyPathStruct'; 19import { warn } from '../../log/Log'; 20 21export function setSelectState( 22 data: JsCpuProfilerChartFrame, 23 frameSelectDataIdArr: Array<number>, 24 parent?: JsCpuProfilerChartFrame 25): void { 26 if (TraceRow.rangeSelectObject?.startNS && TraceRow.rangeSelectObject?.endNS) { 27 let startTime = 0; 28 let endTime = 0; 29 if (data.startTime < TraceRow.rangeSelectObject?.startNS) { 30 startTime = TraceRow.rangeSelectObject?.startNS; 31 } else { 32 startTime = data.startTime; 33 } 34 if (data.endTime > TraceRow.rangeSelectObject.endNS) { 35 endTime = TraceRow.rangeSelectObject?.endNS; 36 } else { 37 endTime = data.endTime; 38 } 39 data.totalTime = endTime - startTime; 40 data.selfTime = data.totalTime; 41 if (parent) { 42 parent.selfTime -= data.totalTime; 43 } 44 } 45 46 data.isSelect = true; 47 if (data.children.length > 0) { 48 for (let child of data.children) { 49 if (child === null) { 50 continue; 51 } 52 if (frameSelectDataIdArr.includes(child.id)) { 53 setSelectState(child, frameSelectDataIdArr, data); 54 } 55 } 56 } 57} 58 59export function intersectData(row: TraceRow<any>): any[] { 60 let isIntersect = (snapshotStruct: SnapshotStruct, rangeSelectStruct: RangeSelectStruct): boolean => 61 Math.max(snapshotStruct.startNs! + snapshotStruct.dur!, rangeSelectStruct!.endNS || 0) - 62 Math.min(snapshotStruct.startNs!, rangeSelectStruct!.startNS || 0) < 63 snapshotStruct.dur! + (rangeSelectStruct!.endNS || 0) - (rangeSelectStruct!.startNS || 0); 64 let intersectData = row.dataListCache.filter((struct: SnapshotStruct) => { 65 return isIntersect(struct, TraceRow.rangeSelectObject!); 66 }); 67 return intersectData; 68} 69export function isExistPidInArray(arr: Array<{ pid: number; ipid: number }>, pid: number): boolean { 70 return arr.some((item) => item.pid === pid); 71} 72 73/** 74 * 校验导入的json, 导出数据结构, 不匹配的不导入 75 * @param content json 内容 76 * @returns Array<KeyPathStruct> 77 */ 78export function parseKeyPathJson(content: string): Array<KeyPathStruct> { 79 const threads = JSON.parse(content); 80 const parseResult = []; 81 for (let threadKey in threads) { 82 const tsArray = threads[threadKey]; 83 threadKey = threadKey.trim(); 84 const regex = /\[(\d+)\]/; 85 const matches = threadKey.match(regex); 86 const tid = matches ? parseInt(matches[1]) : -1; 87 const spaceIndex = threadKey.indexOf(' '); 88 const threadName = spaceIndex !== -1 ? threadKey.substring(0, spaceIndex) : ''; 89 if (tid && threadName && tsArray.length > 0) { 90 const keyPath = new KeyPathStruct(tid, threadName, tsArray); 91 parseResult.push(keyPath); 92 } else { 93 warn('parse key path fail ', threadKey); 94 } 95 } 96 return parseResult; 97} 98