1/* 2 * Copyright (C) 2023 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 */ 15 16function getBusyTime( 17 initFreqResult: Array<any>, 18 initStateResult: Array<any>, 19 sampleMap: Map<any, any>, 20 leftStartNs: number, 21 rightEndNs: number 22) { 23 if (initFreqResult.length == 0) { return }; 24 if (initStateResult.length == 0) { return }; 25 //处理被框选的freq的第一个数据 26 let includeData = initFreqResult.findIndex((a) => a.ts >= leftStartNs); 27 if (includeData !== 0) { 28 initFreqResult = initFreqResult.slice( 29 includeData === -1 ? initFreqResult.length - 1 : includeData - 1, 30 initFreqResult.length 31 ); 32 } 33 let startNS = includeData === 0 ? initFreqResult[0].ts : leftStartNs; 34 //处理对应的state泳道被框选的第一个数据 35 let includeStateData = initStateResult.findIndex((a) => a.ts >= startNS); 36 if (includeStateData !== 0) { 37 initStateResult = initStateResult.slice( 38 includeStateData === -1 ? initStateResult.length - 1 : includeStateData - 1, 39 initStateResult.length 40 ); 41 } 42 if (initStateResult[0].ts < startNS && includeStateData !== 0 && includeStateData !== -1) 43 initStateResult[0].ts = startNS; 44 //处理被框选的freq最后一个数据 45 if (initFreqResult[initFreqResult.length - 1].ts !== rightEndNs) { 46 initFreqResult.push({ 47 ts: rightEndNs, 48 value: initFreqResult[initFreqResult.length - 1].value, 49 filterId: initFreqResult[initFreqResult.length - 1].filterId, 50 }); 51 } 52 //处理被框选的freq最后一个数据 53 if (initStateResult[initStateResult.length - 1].ts !== rightEndNs) { 54 initStateResult.push({ 55 ts: rightEndNs, 56 value: initStateResult[initStateResult.length - 1].value, 57 }); 58 } 59 handleBusyTimeLogic(initFreqResult, initStateResult, sampleMap, startNS); 60} 61 62function handleBusyTimeLogic(initFreqResult: Array<any>, initStateResult: Array<any>, sampleMap: Map<any, any>, startNS: number) { 63 let freqIndex = 1; 64 let stateIndex = 1; 65 let beginNs = startNS; 66 //value和Id的起始值是第0项 67 let freqId = initFreqResult[0].filterId; 68 let freqVal = initFreqResult[0].value; 69 let stateVal = initStateResult[0].value; 70 //从index = 1开始循环 71 while (freqIndex < initFreqResult.length && stateIndex < initStateResult.length) { 72 let newBeginNs = beginNs; 73 let newfreqId = freqId; 74 let newfreqVal = freqVal; 75 let newStateVal = stateVal; 76 let busyTime = 0; 77 //比较ts值,每次比较取ts相对小的那一项 78 if (initFreqResult[freqIndex].ts < initStateResult[stateIndex].ts) { 79 newfreqVal = initFreqResult[freqIndex].value; 80 newBeginNs = initFreqResult[freqIndex].ts; 81 newfreqId = initFreqResult[freqIndex].filterId; 82 freqIndex++; 83 } else if (initFreqResult[freqIndex].ts > initStateResult[stateIndex].ts) { 84 newStateVal = initStateResult[stateIndex].value; 85 newBeginNs = initStateResult[stateIndex].ts; 86 stateIndex++; 87 } else { 88 newStateVal = initStateResult[stateIndex].value; 89 newfreqVal = initFreqResult[freqIndex].value; 90 newfreqId = initFreqResult[freqIndex].filterId; 91 newBeginNs = initStateResult[stateIndex].ts; 92 freqIndex++; 93 stateIndex++; 94 } 95 //取state = 0的情况并根据频率去加等赋值 96 if (stateVal == 0) { 97 busyTime = newBeginNs - beginNs; 98 if (sampleMap.has(freqId + '-' + freqVal)) { 99 let obj = sampleMap.get(freqId + '-' + freqVal); 100 obj.busyTime += busyTime; 101 } 102 } 103 beginNs = newBeginNs; 104 freqId = newfreqId; 105 freqVal = newfreqVal; 106 stateVal = newStateVal; 107 } 108} 109 110self.onmessage = (e: MessageEvent) => { 111 let leftStartNs = e.data.frqSampleParam.leftNs + e.data.frqSampleParam.recordStartNs; 112 let rightEndNs = e.data.frqSampleParam.rightNs + e.data.frqSampleParam.recordStartNs; 113 e.data.cpuFiliterOrder.forEach((a: number) => { 114 getBusyTime( 115 e.data.result.filter((f: any) => f.cpu == a), 116 e.data.res.filter((f: any) => f.cpu == a), 117 e.data.sampleMap, 118 leftStartNs, 119 rightEndNs 120 ); 121 }); 122 e.data.sampleMap.forEach((a: any) => { 123 a.busyTime = parseFloat((a.busyTime / 1000000.0).toFixed(6)); 124 }); 125 126 self.postMessage(e.data.sampleMap); 127}; 128