1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_cifunction getBusyTime(
17fb726d48Sopenharmony_ci  initFreqResult: Array<unknown>,
18fb726d48Sopenharmony_ci  initStateResult: Array<unknown>,
19fb726d48Sopenharmony_ci  sampleMap: Map<string, unknown>,
20fb726d48Sopenharmony_ci  leftStartNs: number,
21fb726d48Sopenharmony_ci  rightEndNs: number
22fb726d48Sopenharmony_ci): void {
23fb726d48Sopenharmony_ci  if (initFreqResult.length === 0) {
24fb726d48Sopenharmony_ci    return;
25fb726d48Sopenharmony_ci  }
26fb726d48Sopenharmony_ci  if (initStateResult.length === 0) {
27fb726d48Sopenharmony_ci    return;
28fb726d48Sopenharmony_ci  }
29fb726d48Sopenharmony_ci  //处理被框选的freq的第一个数据
30fb726d48Sopenharmony_ci  //@ts-ignore
31fb726d48Sopenharmony_ci  let includeData = initFreqResult.findIndex((a) => a.ts >= leftStartNs);
32fb726d48Sopenharmony_ci  if (includeData !== 0) {
33fb726d48Sopenharmony_ci    initFreqResult = initFreqResult.slice(
34fb726d48Sopenharmony_ci      includeData === -1 ? initFreqResult.length - 1 : includeData - 1,
35fb726d48Sopenharmony_ci      initFreqResult.length
36fb726d48Sopenharmony_ci    );
37fb726d48Sopenharmony_ci  }
38fb726d48Sopenharmony_ci  //@ts-ignore
39fb726d48Sopenharmony_ci  let startNS = includeData === 0 ? initFreqResult[0].ts : leftStartNs;
40fb726d48Sopenharmony_ci  //处理对应的state泳道被框选的第一个数据
41fb726d48Sopenharmony_ci  //@ts-ignore
42fb726d48Sopenharmony_ci  let includeStateData = initStateResult.findIndex((a) => a.ts >= startNS);
43fb726d48Sopenharmony_ci  if (includeStateData !== 0) {
44fb726d48Sopenharmony_ci    initStateResult = initStateResult.slice(
45fb726d48Sopenharmony_ci      includeStateData === -1 ? initStateResult.length - 1 : includeStateData - 1,
46fb726d48Sopenharmony_ci      initStateResult.length
47fb726d48Sopenharmony_ci    );
48fb726d48Sopenharmony_ci  }
49fb726d48Sopenharmony_ci  //@ts-ignore
50fb726d48Sopenharmony_ci  if (initStateResult[0].ts < startNS && includeStateData !== 0 && includeStateData !== -1) {
51fb726d48Sopenharmony_ci    //@ts-ignore
52fb726d48Sopenharmony_ci    initStateResult[0].ts = startNS;
53fb726d48Sopenharmony_ci  }
54fb726d48Sopenharmony_ci  //处理被框选的freq最后一个数据
55fb726d48Sopenharmony_ci  //@ts-ignore
56fb726d48Sopenharmony_ci  if (initFreqResult[initFreqResult.length - 1].ts !== rightEndNs) {
57fb726d48Sopenharmony_ci    initFreqResult.push({
58fb726d48Sopenharmony_ci      ts: rightEndNs,
59fb726d48Sopenharmony_ci      //@ts-ignore
60fb726d48Sopenharmony_ci      value: initFreqResult[initFreqResult.length - 1].value,
61fb726d48Sopenharmony_ci      //@ts-ignore
62fb726d48Sopenharmony_ci      filterId: initFreqResult[initFreqResult.length - 1].filterId,
63fb726d48Sopenharmony_ci    });
64fb726d48Sopenharmony_ci  }
65fb726d48Sopenharmony_ci  //处理被框选的freq最后一个数据
66fb726d48Sopenharmony_ci  //@ts-ignore
67fb726d48Sopenharmony_ci  if (initStateResult[initStateResult.length - 1].ts !== rightEndNs) {
68fb726d48Sopenharmony_ci    initStateResult.push({
69fb726d48Sopenharmony_ci      ts: rightEndNs,
70fb726d48Sopenharmony_ci      //@ts-ignore
71fb726d48Sopenharmony_ci      value: initStateResult[initStateResult.length - 1].value,
72fb726d48Sopenharmony_ci    });
73fb726d48Sopenharmony_ci  }
74fb726d48Sopenharmony_ci  handleBusyTimeLogic(initFreqResult, initStateResult, sampleMap, startNS);
75fb726d48Sopenharmony_ci}
76fb726d48Sopenharmony_ci
77fb726d48Sopenharmony_cifunction handleBusyTimeLogic(
78fb726d48Sopenharmony_ci  initFreqResult: Array<unknown>,
79fb726d48Sopenharmony_ci  initStateResult: Array<unknown>,
80fb726d48Sopenharmony_ci  sampleMap: Map<string, unknown>,
81fb726d48Sopenharmony_ci  startNS: number
82fb726d48Sopenharmony_ci): void {
83fb726d48Sopenharmony_ci  let freqIndex = 1;
84fb726d48Sopenharmony_ci  let stateIndex = 1;
85fb726d48Sopenharmony_ci  let beginNs = startNS;
86fb726d48Sopenharmony_ci  //value和Id的起始值是第0项
87fb726d48Sopenharmony_ci  //@ts-ignore
88fb726d48Sopenharmony_ci  let freqId = initFreqResult[0].filterId;
89fb726d48Sopenharmony_ci  //@ts-ignore
90fb726d48Sopenharmony_ci  let freqVal = initFreqResult[0].value;
91fb726d48Sopenharmony_ci  //@ts-ignore
92fb726d48Sopenharmony_ci  let stateVal = initStateResult[0].value;
93fb726d48Sopenharmony_ci  //从index = 1开始循环
94fb726d48Sopenharmony_ci  while (freqIndex < initFreqResult.length && stateIndex < initStateResult.length) {
95fb726d48Sopenharmony_ci    let newBeginNs = beginNs;
96fb726d48Sopenharmony_ci    let newfreqId = freqId;
97fb726d48Sopenharmony_ci    let newfreqVal = freqVal;
98fb726d48Sopenharmony_ci    let newStateVal = stateVal;
99fb726d48Sopenharmony_ci    let busyTime = 0;
100fb726d48Sopenharmony_ci    //比较ts值,每次比较取ts相对小的那一项
101fb726d48Sopenharmony_ci    //@ts-ignore
102fb726d48Sopenharmony_ci    if (initFreqResult[freqIndex].ts < initStateResult[stateIndex].ts) {
103fb726d48Sopenharmony_ci      //@ts-ignore
104fb726d48Sopenharmony_ci      newfreqVal = initFreqResult[freqIndex].value;
105fb726d48Sopenharmony_ci      //@ts-ignore
106fb726d48Sopenharmony_ci      newBeginNs = initFreqResult[freqIndex].ts;
107fb726d48Sopenharmony_ci      //@ts-ignore
108fb726d48Sopenharmony_ci      newfreqId = initFreqResult[freqIndex].filterId;
109fb726d48Sopenharmony_ci      freqIndex++;
110fb726d48Sopenharmony_ci      //@ts-ignore
111fb726d48Sopenharmony_ci    } else if (initFreqResult[freqIndex].ts > initStateResult[stateIndex].ts) {
112fb726d48Sopenharmony_ci      //@ts-ignore
113fb726d48Sopenharmony_ci      newStateVal = initStateResult[stateIndex].value;
114fb726d48Sopenharmony_ci      //@ts-ignore
115fb726d48Sopenharmony_ci      newBeginNs = initStateResult[stateIndex].ts;
116fb726d48Sopenharmony_ci      stateIndex++;
117fb726d48Sopenharmony_ci    } else {
118fb726d48Sopenharmony_ci      //@ts-ignore
119fb726d48Sopenharmony_ci      newStateVal = initStateResult[stateIndex].value;
120fb726d48Sopenharmony_ci      //@ts-ignore
121fb726d48Sopenharmony_ci      newfreqVal = initFreqResult[freqIndex].value;
122fb726d48Sopenharmony_ci      //@ts-ignore
123fb726d48Sopenharmony_ci      newfreqId = initFreqResult[freqIndex].filterId;
124fb726d48Sopenharmony_ci      //@ts-ignore
125fb726d48Sopenharmony_ci      newBeginNs = initStateResult[stateIndex].ts;
126fb726d48Sopenharmony_ci      freqIndex++;
127fb726d48Sopenharmony_ci      stateIndex++;
128fb726d48Sopenharmony_ci    }
129fb726d48Sopenharmony_ci    //取state = 0的情况并根据频率去加等赋值
130fb726d48Sopenharmony_ci    if (stateVal === 0) {
131fb726d48Sopenharmony_ci      busyTime = newBeginNs - beginNs;
132fb726d48Sopenharmony_ci      if (sampleMap.has(freqId + '-' + freqVal)) {
133fb726d48Sopenharmony_ci        let obj = sampleMap.get(freqId + '-' + freqVal);
134fb726d48Sopenharmony_ci        //@ts-ignore
135fb726d48Sopenharmony_ci        obj.busyTime += busyTime;
136fb726d48Sopenharmony_ci      }
137fb726d48Sopenharmony_ci    }
138fb726d48Sopenharmony_ci    beginNs = newBeginNs;
139fb726d48Sopenharmony_ci    freqId = newfreqId;
140fb726d48Sopenharmony_ci    freqVal = newfreqVal;
141fb726d48Sopenharmony_ci    stateVal = newStateVal;
142fb726d48Sopenharmony_ci  }
143fb726d48Sopenharmony_ci}
144fb726d48Sopenharmony_ci
145fb726d48Sopenharmony_ciself.onmessage = (e: MessageEvent): void => {
146fb726d48Sopenharmony_ci  let leftStartNs = (e.data.timeParam.leftNs + e.data.timeParam.recordStartNs) as number;
147fb726d48Sopenharmony_ci  let rightEndNs = (e.data.timeParam.rightNs + e.data.timeParam.recordStartNs) as number;
148fb726d48Sopenharmony_ci  e.data.cpuFiliterOrder.forEach((a: number) => {
149fb726d48Sopenharmony_ci    getBusyTime(
150fb726d48Sopenharmony_ci      //@ts-ignore
151fb726d48Sopenharmony_ci      e.data.result.filter((f: unknown) => f.cpu === a),
152fb726d48Sopenharmony_ci      //@ts-ignore
153fb726d48Sopenharmony_ci      e.data.res.filter((f: unknown) => f.cpu === a),
154fb726d48Sopenharmony_ci      e.data.sampleMap,
155fb726d48Sopenharmony_ci      leftStartNs,
156fb726d48Sopenharmony_ci      rightEndNs
157fb726d48Sopenharmony_ci    );
158fb726d48Sopenharmony_ci  });
159fb726d48Sopenharmony_ci  e.data.sampleMap.forEach((a: unknown) => {
160fb726d48Sopenharmony_ci  //@ts-ignore
161fb726d48Sopenharmony_ci    a.busyTime = parseFloat((a.busyTime / 1000000.0).toFixed(6));
162fb726d48Sopenharmony_ci  });
163fb726d48Sopenharmony_ci
164fb726d48Sopenharmony_ci  self.postMessage(e.data.sampleMap);
165fb726d48Sopenharmony_ci};
166