1/*
2 * Copyright (C) 2024 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 { query } from "../SqlLite";
17import { CpuAndIrqBean} from "../../component/trace/sheet/cpu/CpuAndIrqBean";
18export const getCpuData = (cpus: Array<number>, leftNS: number, rightNS: number): Promise<Array<CpuAndIrqBean>> =>
19    query(
20      'getCpuData',
21      `
22      SELECT
23        TS.pid AS pid,
24        TS.tid AS tid,
25        TS.cpu,
26        'cpu' AS cat,
27        1 As occurrences,
28        1 As priority,
29        true As isFirstObject,
30        MAX(${leftNS},TS.ts - TR.start_ts) AS startTime,
31        MIN(${rightNS},(TS.ts - TR.start_ts + iif(TS.dur = -1 OR TS.dur IS NULL, 0, TS.dur))) AS endTime,
32        MIN(${rightNS},(TS.ts - TR.start_ts + iif(TS.dur = -1 OR TS.dur IS NULL, 0, TS.dur))) - MAX(${leftNS},TS.ts - TR.start_ts) AS dur
33      FROM
34        thread_state AS TS
35      LEFT JOIN
36        trace_range AS TR
37      WHERE
38        TS.cpu IN (${cpus.join(',')})
39      AND
40        NOT ((TS.ts - TR.start_ts + iif(TS.dur = -1 OR TS.dur IS NULL, 0, TS.dur) < ${leftNS}) OR (TS.ts - TR.start_ts > ${rightNS}))`
41    );
42export const getIrqAndSoftIrqData = (cpus: Array<number>, leftNS: number, rightNS: number): Promise<Array<CpuAndIrqBean>> =>{
43  return query(
44    'getIrqAndSoftIrqData',
45    `
46    SELECT
47      CASE
48        WHEN i.cat = 'softirq'
49        THEN 'softirq'
50        ELSE 'irq'
51        END AS cat,
52      CASE
53        WHEN i.cat = 'softirq'
54        THEN 2
55        ELSE 3
56        END AS priority,
57      i.callid as cpu,
58      1 As occurrences,
59      true As isFirstObject,
60      MAX(${leftNS},i.ts - TR.start_ts) AS startTime,
61      MIN(${rightNS},(i.ts - TR.start_ts + iif(i.dur = -1 OR i.dur IS NULL, 0, i.dur))) AS endTime,
62      MIN(${rightNS},(i.ts - TR.start_ts + iif(i.dur = -1 OR i.dur IS NULL, 0, i.dur))) - MAX(${leftNS},i.ts - TR.start_ts) AS dur
63    FROM
64      irq i 
65    LEFT JOIN
66      trace_range AS TR
67    WHERE
68      i.callid IN (${cpus.join(',')})
69    AND
70      NOT ((i.ts - TR.start_ts + iif(i.dur = -1 OR i.dur IS NULL, 0, i.dur) < ${leftNS}) OR (i.ts - TR.start_ts > ${rightNS}))
71    AND ( ( i.cat = 'irq' AND i.flag = '1' ) OR i.cat = 'ipi' OR  i.cat = 'softirq' )`
72  );
73}