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_ciimport { query } from '../SqlLite';
16fb726d48Sopenharmony_ciimport { DmaFenceDataBean } from '../../../trace/component/trace/sheet/dma-fence/DmaFenceBean';
17fb726d48Sopenharmony_ci
18fb726d48Sopenharmony_ciexport const queryDmaFenceName = (): Promise<Array<{timeline:string}>> =>
19fb726d48Sopenharmony_ci  query(
20fb726d48Sopenharmony_ci    'queryDmaFenceName',
21fb726d48Sopenharmony_ci    `SELECT DISTINCT timeline  
22fb726d48Sopenharmony_ci     FROM dma_fence;`
23fb726d48Sopenharmony_ci  );
24fb726d48Sopenharmony_ciexport const queryDmaFenceIdAndCat = (): Promise<Array<{ id: number; cat: string; seqno: number;driver:string;context:string}>> =>
25fb726d48Sopenharmony_ci  query(
26fb726d48Sopenharmony_ci    'queryDmaFenceIdAndCat',
27fb726d48Sopenharmony_ci    `SELECT
28fb726d48Sopenharmony_ci       id,
29fb726d48Sopenharmony_ci       cat,
30fb726d48Sopenharmony_ci       seqno,
31fb726d48Sopenharmony_ci       driver,
32fb726d48Sopenharmony_ci       context 
33fb726d48Sopenharmony_ci     FROM
34fb726d48Sopenharmony_ci       dma_fence;`
35fb726d48Sopenharmony_ci  );
36fb726d48Sopenharmony_ci
37fb726d48Sopenharmony_ciexport const queryDmaFenceData = (leftNS: number, rightNS: number, nameList: String[]): Promise<Array<DmaFenceDataBean>> =>{
38fb726d48Sopenharmony_ci  const inClause = nameList.map(name => `'${name}'`).join(', ');
39fb726d48Sopenharmony_ci  const sql = `WITH state AS (
40fb726d48Sopenharmony_ci        SELECT
41fb726d48Sopenharmony_ci          id,
42fb726d48Sopenharmony_ci          ts,
43fb726d48Sopenharmony_ci          cat,
44fb726d48Sopenharmony_ci          driver,
45fb726d48Sopenharmony_ci          timeline,
46fb726d48Sopenharmony_ci          context,
47fb726d48Sopenharmony_ci          seqno,
48fb726d48Sopenharmony_ci        CASE
49fb726d48Sopenharmony_ci            WHEN LAG( cat ) OVER ( PARTITION BY timeline ORDER BY ts ) = 'dma_fence_init' AND  LAG(driver) OVER (PARTITION BY timeline ORDER BY ts) = ''
50fb726d48Sopenharmony_ci            THEN LAG( ts, 2 ) OVER ( PARTITION BY timeline ORDER BY ts ) 
51fb726d48Sopenharmony_ci            ELSE LAG( ts ) OVER ( PARTITION BY timeline ORDER BY ts ) 
52fb726d48Sopenharmony_ci            END AS new_ts,
53fb726d48Sopenharmony_ci        CASE
54fb726d48Sopenharmony_ci            WHEN LAG( cat ) OVER ( PARTITION BY timeline ORDER BY ts ) = 'dma_fence_init' AND  LAG(driver) OVER (PARTITION BY timeline ORDER BY ts) = ''
55fb726d48Sopenharmony_ci            THEN Dur + LAG( dur ) OVER ( PARTITION BY timeline ORDER BY ts ) 
56fb726d48Sopenharmony_ci            ELSE Dur 
57fb726d48Sopenharmony_ci            END AS newDur,
58fb726d48Sopenharmony_ci        ROW_NUMBER( ) OVER ( PARTITION BY timeline ORDER BY ts ) AS rn
59fb726d48Sopenharmony_ci        FROM
60fb726d48Sopenharmony_ci            dma_fence 
61fb726d48Sopenharmony_ci        WHERE
62fb726d48Sopenharmony_ci            timeline IN ( ${inClause} ) 
63fb726d48Sopenharmony_ci          )
64fb726d48Sopenharmony_ci        SELECT
65fb726d48Sopenharmony_ci          s.id,
66fb726d48Sopenharmony_ci          s.new_ts - COALESCE( r.start_ts, 0 ) AS startTime,
67fb726d48Sopenharmony_ci          s.newDur AS dur,
68fb726d48Sopenharmony_ci          s.new_ts - COALESCE(r.start_ts, 0) + s.newDur AS endTime,    
69fb726d48Sopenharmony_ci          s.cat,
70fb726d48Sopenharmony_ci          s.driver,
71fb726d48Sopenharmony_ci          s.timeline,
72fb726d48Sopenharmony_ci          s.context,
73fb726d48Sopenharmony_ci          s.seqno 
74fb726d48Sopenharmony_ci        FROM
75fb726d48Sopenharmony_ci          state s
76fb726d48Sopenharmony_ci          LEFT JOIN trace_range r ON s.new_ts BETWEEN r.start_ts AND r.end_ts 
77fb726d48Sopenharmony_ci        WHERE
78fb726d48Sopenharmony_ci          s.driver <> '' 
79fb726d48Sopenharmony_ci          AND s.cat <> 'dma_fence_init' 
80fb726d48Sopenharmony_ci          AND s.rn > 1 
81fb726d48Sopenharmony_ci          AND (  
82fb726d48Sopenharmony_ci            s.new_ts BETWEEN ${leftNS} + r.start_ts AND ${rightNS} + r.start_ts   
83fb726d48Sopenharmony_ci            OR   
84fb726d48Sopenharmony_ci            (s.new_ts + s.newDur) BETWEEN ${leftNS} + r.start_ts AND ${rightNS} + r.start_ts 
85fb726d48Sopenharmony_ci            OR 
86fb726d48Sopenharmony_ci            (s.new_ts < ${leftNS} + r.start_ts AND (s.new_ts + s.newDur) > ${rightNS} + r.start_ts)
87fb726d48Sopenharmony_ci          ) 
88fb726d48Sopenharmony_ci        ORDER BY
89fb726d48Sopenharmony_ci          s.new_ts;`;
90fb726d48Sopenharmony_ci  return query('queryDmaFenceData', sql); 
91fb726d48Sopenharmony_ci};