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