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