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