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