1/*
2 * Copyright (C) 2022 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
16import { convertJSON, LogicHandler } from './ProcedureLogicWorkerCommon';
17
18export class ProcedureLogicWorkerCpuState extends LogicHandler {
19  currentEventId: string = '';
20
21  handle(data: unknown): void {
22    //@ts-ignore
23    this.currentEventId = data.id;
24    //@ts-ignore
25    if (data && data.type) {
26      //@ts-ignore
27      switch (data.type) {
28        case 'CpuState-getCpuState':
29          //@ts-ignore
30          if (data.params.list) {
31            //@ts-ignore
32            let arr = convertJSON(data.params.list) || [];
33            self.postMessage({
34              //@ts-ignore
35              id: data.id,
36              //@ts-ignore
37              action: data.action,
38              //@ts-ignore
39              results: this.supplementCpuState(arr),
40            });
41            arr = [];
42          } else {
43            //@ts-ignore
44            this.getCpuState(data.params.cpu);
45          }
46          break;
47      }
48    }
49  }
50
51  clearAll(): void {}
52
53  getCpuState(cpu: number): void {
54    this.queryData(
55      this.currentEventId,
56      'CpuState-getCpuState',
57      `
58            select (A.ts - B.start_ts) as startTs,
59                A.dur,
60                (A.ts - B.start_ts + A.dur) as endTs,
61                (case
62                    when state = 'Running' then 0
63                    when state = 'S' then 1
64                    when state = 'R' or state = 'R+' then 2
65                    else 3 end)    as value
66                from thread_state A,trace_range B
67            where cpu = $cpu and startTs >= 0;
68        `,
69      { $cpu: cpu }
70    );
71  }
72
73  supplementCpuState(arr: Array<CpuState>): Array<CpuState> {
74    let source: Array<CpuState> = [];
75    if (arr.length > 0) {
76      let first = arr[0];
77      if (first.startTs > 0) {
78        let cs: CpuState = new CpuState();
79        cs.startTs = 0;
80        cs.value = 3;
81        cs.dur = first.startTs;
82        cs.endTs = first.startTs;
83        source.push(cs);
84      }
85      source.push(first);
86      for (let i = 1, len = arr.length; i < len; i++) {
87        let last = arr[i - 1];
88        let current = arr[i];
89        if (current.startTs > last.endTs) {
90          let cs: CpuState = new CpuState();
91          cs.startTs = last.endTs;
92          cs.value = 3;
93          cs.dur = current.startTs - last.endTs;
94          cs.endTs = current.startTs;
95          source.push(cs);
96        }
97        source.push(current);
98      }
99    }
100    return source;
101  }
102}
103
104export class CpuState {
105  startTs: number = 0;
106  endTs: number = 0;
107  dur: number = 0;
108  value: number = 0;
109}
110