* Copyright (C) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function getBusyTime(
initFreqResult: Array<unknown>,
initStateResult: Array<unknown>,
sampleMap: Map<string, unknown>,
leftStartNs: number,
rightEndNs: number
): void {
if (initFreqResult.length === 0 || initStateResult.length === 0) {
return;
}
let handle = (result: Array<unknown>, startNS: number): Array<unknown> => {
let firstDataIndex = result.findIndex((a) => a.ts > startNS);
if (firstDataIndex !== 0) {
result = result.slice(
firstDataIndex === -1 ? result.length - 1 : firstDataIndex - 1,
result.length
);
result[0].ts = startNS;
}
return result;
};
let startNS = Math.max(initFreqResult[0].ts, initStateResult[0].ts, leftStartNs);
initFreqResult = handle(initFreqResult, startNS);
initStateResult = handle(initStateResult, startNS);
if (initFreqResult[initFreqResult.length - 1].ts !== rightEndNs) {
initFreqResult.push({
ts: rightEndNs,
value: initFreqResult[initFreqResult.length - 1].value,
filterId: initFreqResult[initFreqResult.length - 1].filterId,
});
}
if (initStateResult[initStateResult.length - 1].ts !== rightEndNs) {
initStateResult.push({
ts: rightEndNs,
value: initStateResult[initStateResult.length - 1].value,
});
}
handleBusyTimeLogic(initFreqResult, initStateResult, sampleMap, startNS);
}
function handleBusyTimeLogic(
initFreqResult: Array<unknown>,
initStateResult: Array<unknown>,
sampleMap: Map<string, unknown>,
startNS: number
): void {
let freqIndex = 1;
let stateIndex = 1;
let beginNs = startNS;
let freqId = initFreqResult[0].filterId;
let freqVal = initFreqResult[0].value;
let stateVal = initStateResult[0].value;
while (freqIndex < initFreqResult.length && stateIndex < initStateResult.length) {
let newBeginNs = beginNs;
let newfreqId = freqId;
let newfreqVal = freqVal;
let newStateVal = stateVal;
let busyTime = 0;
if (initFreqResult[freqIndex].ts < initStateResult[stateIndex].ts) {
newfreqVal = initFreqResult[freqIndex].value;
newBeginNs = initFreqResult[freqIndex].ts;
newfreqId = initFreqResult[freqIndex].filterId;
freqIndex++;
} else if (initFreqResult[freqIndex].ts > initStateResult[stateIndex].ts) {
newStateVal = initStateResult[stateIndex].value;
newBeginNs = initStateResult[stateIndex].ts;
stateIndex++;
} else {
newStateVal = initStateResult[stateIndex].value;
newfreqVal = initFreqResult[freqIndex].value;
newfreqId = initFreqResult[freqIndex].filterId;
newBeginNs = initStateResult[stateIndex].ts;
freqIndex++;
stateIndex++;
}
if (stateVal === 0) {
busyTime = newBeginNs - beginNs;
if (sampleMap.has(freqId + '-' + freqVal)) {
let obj = sampleMap.get(freqId + '-' + freqVal);
obj.busyTime += busyTime;
}
}
beginNs = newBeginNs;
freqId = newfreqId;
freqVal = newfreqVal;
stateVal = newStateVal;
}
}
self.onmessage = (e: MessageEvent): void => {
let leftStartNs = (e.data.timeParam.leftNs + e.data.timeParam.recordStartNs) as number;
let rightEndNs = (e.data.timeParam.rightNs + e.data.timeParam.recordStartNs) as number;
e.data.cpuFiliterOrder.forEach((a: number) => {
getBusyTime(
e.data.result.filter((f: unknown) => f.cpu === a),
e.data.res.filter((f: unknown) => f.cpu === a),
e.data.sampleMap,
leftStartNs,
rightEndNs
);
});
e.data.sampleMap.forEach((a: unknown) => {
a.busyTime = parseFloat((a.busyTime / 1000000.0).toFixed(6));
});
self.postMessage(e.data.sampleMap);
};