1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 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//  VM Tracker Gpu Resourcet泳道图
16fb726d48Sopenharmony_ciimport type { SnapshotStruct } from '../ui-worker/ProcedureWorkerSnapshot';
17fb726d48Sopenharmony_ciimport { query } from '../SqlLite';
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_ciexport const queryGpuResourceData = (categoryNameId: number): Promise<Array<SnapshotStruct>> =>
20fb726d48Sopenharmony_ci  query(
21fb726d48Sopenharmony_ci    'queryGpuResourceData',
22fb726d48Sopenharmony_ci    `SELECT
23fb726d48Sopenharmony_ci    subquery1.startNs,
24fb726d48Sopenharmony_ci    IFNULL(subquery1.totalSize, 0) as aSize, 
25fb726d48Sopenharmony_ci    IFNULL(subquery2.size, 0) as bSize,
26fb726d48Sopenharmony_ci    (IFNULL(subquery1.totalSize, 0) - IFNULL(subquery2.size, 0)) AS value
27fb726d48Sopenharmony_ci  FROM
28fb726d48Sopenharmony_ci    (SELECT (ts - start_ts) AS startNs,SUM(total_size) AS totalSize
29fb726d48Sopenharmony_ci     FROM memory_profile, trace_range
30fb726d48Sopenharmony_ci     WHERE ts between start_ts and end_ts
31fb726d48Sopenharmony_ci     GROUP BY ts) AS subquery1
32fb726d48Sopenharmony_ci   LEFT JOIN
33fb726d48Sopenharmony_ci    (SELECT (ts - start_ts) AS startNs, SUM(size) AS size
34fb726d48Sopenharmony_ci     FROM memory_window_gpu, trace_range
35fb726d48Sopenharmony_ci     WHERE ts between start_ts and end_ts
36fb726d48Sopenharmony_ci    AND category_name_id = ${categoryNameId}
37fb726d48Sopenharmony_ci     GROUP BY ts) AS subquery2
38fb726d48Sopenharmony_ci  ON subquery1.startNs = subquery2.startNs`
39fb726d48Sopenharmony_ci  );
40fb726d48Sopenharmony_ciexport const queryisExistsGpuResourceData = (categoryNameId: number): Promise<Array<SnapshotStruct>> =>
41fb726d48Sopenharmony_ci  query(
42fb726d48Sopenharmony_ci    'queryisExistsGpuResourceData',
43fb726d48Sopenharmony_ci    `SELECT EXISTS (
44fb726d48Sopenharmony_ci      SELECT 1
45fb726d48Sopenharmony_ci    FROM
46fb726d48Sopenharmony_ci      (SELECT (ts - start_ts) AS startNs
47fb726d48Sopenharmony_ci      FROM memory_profile, trace_range
48fb726d48Sopenharmony_ci      WHERE ts between start_ts and end_ts
49fb726d48Sopenharmony_ci      GROUP BY ts) AS subquery1
50fb726d48Sopenharmony_ci    LEFT JOIN
51fb726d48Sopenharmony_ci      (SELECT (ts - start_ts) AS startNs
52fb726d48Sopenharmony_ci      FROM memory_window_gpu, trace_range
53fb726d48Sopenharmony_ci      WHERE ts between start_ts and end_ts
54fb726d48Sopenharmony_ci      AND category_name_id = ${categoryNameId}
55fb726d48Sopenharmony_ci      GROUP BY ts) AS subquery2
56fb726d48Sopenharmony_ci    ON subquery1.startNs = subquery2.startNs
57fb726d48Sopenharmony_ci    ) AS data_exists
58fb726d48Sopenharmony_ci    `
59fb726d48Sopenharmony_ci  );
60fb726d48Sopenharmony_ci
61fb726d48Sopenharmony_ci//  VM Tracker Gpu Resource Tab页
62fb726d48Sopenharmony_ciexport const queryGpuResourceTabData = (
63fb726d48Sopenharmony_ci  startNs: number
64fb726d48Sopenharmony_ci): Promise<Array<{ startNs: number; channelId: number; totalSize: number }>> =>
65fb726d48Sopenharmony_ci  query(
66fb726d48Sopenharmony_ci    'queryGpuResourceTabData',
67fb726d48Sopenharmony_ci    `SELECT (ts - start_ts) as startNs, channel_id as channelId, sum(total_size) as totalSize 
68fb726d48Sopenharmony_ci    FROM memory_profile, trace_range
69fb726d48Sopenharmony_ci    WHERE (ts - start_ts) = ${startNs}
70fb726d48Sopenharmony_ci    GROUP by ts, channelId`
71fb726d48Sopenharmony_ci  );
72fb726d48Sopenharmony_ci
73fb726d48Sopenharmony_ciexport const queryGpuTotalType = (): Promise<Array<{ id: number; data: string }>> =>
74fb726d48Sopenharmony_ci  query(
75fb726d48Sopenharmony_ci    'queryGpuTotalType',
76fb726d48Sopenharmony_ci    `
77fb726d48Sopenharmony_ci    select distinct module_name_id id,data
78fb726d48Sopenharmony_ci    from memory_window_gpu A, trace_range TR left join data_dict B on A.module_name_id = B.id
79fb726d48Sopenharmony_ci    where window_name_id = 0
80fb726d48Sopenharmony_ci    and A.ts < TR.end_ts
81fb726d48Sopenharmony_ci  `
82fb726d48Sopenharmony_ci  );
83fb726d48Sopenharmony_ci
84fb726d48Sopenharmony_ciexport const queryGpuDataByTs = (
85fb726d48Sopenharmony_ci  ts: number,
86fb726d48Sopenharmony_ci  window: number,
87fb726d48Sopenharmony_ci  module: number | null
88fb726d48Sopenharmony_ci): Promise<
89fb726d48Sopenharmony_ci  Array<{
90fb726d48Sopenharmony_ci    windowNameId: number;
91fb726d48Sopenharmony_ci    windowId: number;
92fb726d48Sopenharmony_ci    moduleId: number;
93fb726d48Sopenharmony_ci    categoryId: number;
94fb726d48Sopenharmony_ci    size: number;
95fb726d48Sopenharmony_ci  }>
96fb726d48Sopenharmony_ci> => {
97fb726d48Sopenharmony_ci  let condition =
98fb726d48Sopenharmony_ci    module === null
99fb726d48Sopenharmony_ci      ? `and window_name_id = ${window}`
100fb726d48Sopenharmony_ci      : `and window_name_id = ${window} and module_name_id = ${module}`;
101fb726d48Sopenharmony_ci  let sql = `select window_name_id as windowNameId,
102fb726d48Sopenharmony_ci       window_id as windowId,
103fb726d48Sopenharmony_ci       module_name_id as moduleId,
104fb726d48Sopenharmony_ci       category_name_id as categoryId,
105fb726d48Sopenharmony_ci       size
106fb726d48Sopenharmony_ci       from memory_window_gpu, trace_range
107fb726d48Sopenharmony_ci       where ts - start_ts = ${ts} ${condition}
108fb726d48Sopenharmony_ci        `;
109fb726d48Sopenharmony_ci  return query('queryGpuDataByTs', sql);
110fb726d48Sopenharmony_ci};
111fb726d48Sopenharmony_ci
112fb726d48Sopenharmony_ciexport const queryGpuTotalData = (moduleId: number | null): Promise<Array<{ startNs: number; value: number }>> => {
113fb726d48Sopenharmony_ci  let moduleCondition = moduleId === null ? '' : `and module_name_id = ${moduleId}`;
114fb726d48Sopenharmony_ci  let sql = `
115fb726d48Sopenharmony_ci  select (ts - start_ts) startNs, sum(size) value
116fb726d48Sopenharmony_ci    from memory_window_gpu,trace_range
117fb726d48Sopenharmony_ci    where window_name_id = 0 ${moduleCondition}
118fb726d48Sopenharmony_ci    and ts< end_ts
119fb726d48Sopenharmony_ci    group by ts;
120fb726d48Sopenharmony_ci  `;
121fb726d48Sopenharmony_ci  return query('queryGpuTotalData', sql);
122fb726d48Sopenharmony_ci};
123fb726d48Sopenharmony_ci
124fb726d48Sopenharmony_ci// GL 或 Graph 泳道图
125fb726d48Sopenharmony_ciexport const queryGpuData = (ipid: number, name: string): Promise<Array<{ startNs: number; value: number }>> => {
126fb726d48Sopenharmony_ci  let sql = `
127fb726d48Sopenharmony_ci    select (ts - start_ts) startNs,sum(value) value
128fb726d48Sopenharmony_ci  from process_measure, trace_range
129fb726d48Sopenharmony_ci  where filter_id = (
130fb726d48Sopenharmony_ci      select id
131fb726d48Sopenharmony_ci      from process_measure_filter
132fb726d48Sopenharmony_ci      where name = ${name} and ipid = ${ipid}
133fb726d48Sopenharmony_ci      )
134fb726d48Sopenharmony_ci  and ts between start_ts and end_ts
135fb726d48Sopenharmony_ci  group by ts;
136fb726d48Sopenharmony_ci    `;
137fb726d48Sopenharmony_ci  return query('queryGpuData', sql);
138fb726d48Sopenharmony_ci};
139fb726d48Sopenharmony_ci// 判断VM Tracker Gl或 Graph泳道图是否有数据
140fb726d48Sopenharmony_ciexport const queryisExistsGpuData = (
141fb726d48Sopenharmony_ci  ipid: number,
142fb726d48Sopenharmony_ci  name: string
143fb726d48Sopenharmony_ci): Promise<Array<{ startNs: number; value: number }>> => {
144fb726d48Sopenharmony_ci  let sql = `
145fb726d48Sopenharmony_ci   SELECT EXISTS (
146fb726d48Sopenharmony_ci      SELECT 1
147fb726d48Sopenharmony_ci      FROM process_measure, trace_range
148fb726d48Sopenharmony_ci      WHERE filter_id = (
149fb726d48Sopenharmony_ci         SELECT id
150fb726d48Sopenharmony_ci         FROM process_measure_filter
151fb726d48Sopenharmony_ci         WHERE name = ${name} AND ipid = ${ipid}
152fb726d48Sopenharmony_ci         )
153fb726d48Sopenharmony_ci         AND ts BETWEEN start_ts AND end_ts
154fb726d48Sopenharmony_ci   ) AS data_exists;
155fb726d48Sopenharmony_ci     `;
156fb726d48Sopenharmony_ci  return query('queryGpuData', sql);
157fb726d48Sopenharmony_ci};
158fb726d48Sopenharmony_ci
159fb726d48Sopenharmony_ci// GL 或 Graph 框选Tab页
160fb726d48Sopenharmony_ciexport const queryGpuDataTab = (
161fb726d48Sopenharmony_ci  ipid: number,
162fb726d48Sopenharmony_ci  leftNs: number,
163fb726d48Sopenharmony_ci  rightNs: number,
164fb726d48Sopenharmony_ci  interval: number,
165fb726d48Sopenharmony_ci  name: string
166fb726d48Sopenharmony_ci): Promise<Array<{ startTs: number; size: number }>> => {
167fb726d48Sopenharmony_ci  let sql = `
168fb726d48Sopenharmony_ci    select (ts - start_ts) startTs,sum(value) * 1024 size
169fb726d48Sopenharmony_ci  from process_measure, trace_range
170fb726d48Sopenharmony_ci  where filter_id = (
171fb726d48Sopenharmony_ci      select id
172fb726d48Sopenharmony_ci      from process_measure_filter
173fb726d48Sopenharmony_ci      where name = ${name} and ipid = ${ipid}
174fb726d48Sopenharmony_ci      )
175fb726d48Sopenharmony_ci  and not ((startTs + ${interval} < ${leftNs}) or (startTs > ${rightNs}))
176fb726d48Sopenharmony_ci  group by ts;
177fb726d48Sopenharmony_ci    `;
178fb726d48Sopenharmony_ci  return query('queryGpuGLDataByRange', sql);
179fb726d48Sopenharmony_ci};
180fb726d48Sopenharmony_ci
181fb726d48Sopenharmony_ciexport const queryGpuDataByRange = (
182fb726d48Sopenharmony_ci  leftNs: number,
183fb726d48Sopenharmony_ci  rightNs: number,
184fb726d48Sopenharmony_ci  interval: number
185fb726d48Sopenharmony_ci): Promise<
186fb726d48Sopenharmony_ci  Array<{
187fb726d48Sopenharmony_ci    startTs: number;
188fb726d48Sopenharmony_ci    windowId: number;
189fb726d48Sopenharmony_ci    moduleId: number;
190fb726d48Sopenharmony_ci    categoryId: number;
191fb726d48Sopenharmony_ci    avgSize: number;
192fb726d48Sopenharmony_ci    maxSize: number;
193fb726d48Sopenharmony_ci    minSize: number;
194fb726d48Sopenharmony_ci  }>
195fb726d48Sopenharmony_ci> => {
196fb726d48Sopenharmony_ci  let sql = `select (ts - start_ts) startTs,
197fb726d48Sopenharmony_ci    window_name_id windowId,
198fb726d48Sopenharmony_ci    module_name_id moduleId,
199fb726d48Sopenharmony_ci    category_name_id categoryId,
200fb726d48Sopenharmony_ci    avg(size) avgSize,
201fb726d48Sopenharmony_ci    max(size) maxSize,
202fb726d48Sopenharmony_ci    min(size) minSize
203fb726d48Sopenharmony_ci  from memory_window_gpu,trace_range
204fb726d48Sopenharmony_ci  where not ((startTs + ${interval} < ${leftNs}) or (startTs > ${rightNs}))
205fb726d48Sopenharmony_ci  group by window_name_id,module_name_id,category_name_id
206fb726d48Sopenharmony_ci  order by avgSize DESC;
207fb726d48Sopenharmony_ci  `;
208fb726d48Sopenharmony_ci  return query('queryGpuWindowData', sql);
209fb726d48Sopenharmony_ci};
210fb726d48Sopenharmony_ci
211fb726d48Sopenharmony_ciexport const queryGpuWindowData = (
212fb726d48Sopenharmony_ci  windowId: number,
213fb726d48Sopenharmony_ci  moduleId: number | null
214fb726d48Sopenharmony_ci): Promise<Array<{ startNs: number; value: number }>> => {
215fb726d48Sopenharmony_ci  let moduleCondition = moduleId === null ? '' : `and module_name_id = ${moduleId}`;
216fb726d48Sopenharmony_ci  let sql = `
217fb726d48Sopenharmony_ci  select (ts - start_ts) startNs, sum(size) value
218fb726d48Sopenharmony_ci    from memory_window_gpu,trace_range
219fb726d48Sopenharmony_ci    where window_name_id = ${windowId} ${moduleCondition}
220fb726d48Sopenharmony_ci    and ts < end_ts
221fb726d48Sopenharmony_ci    group by ts;
222fb726d48Sopenharmony_ci  `;
223fb726d48Sopenharmony_ci  return query('queryGpuWindowData', sql);
224fb726d48Sopenharmony_ci};
225fb726d48Sopenharmony_ci
226fb726d48Sopenharmony_ciexport const queryGpuWindowType = (): Promise<Array<{ id: number; data: string; pid: number }>> =>
227fb726d48Sopenharmony_ci  query(
228fb726d48Sopenharmony_ci    'queryGpuWindowType',
229fb726d48Sopenharmony_ci    `
230fb726d48Sopenharmony_ci  select distinct A.window_name_id as id,B.data, null as pid
231fb726d48Sopenharmony_cifrom memory_window_gpu A, trace_range tr left join data_dict B on A.window_name_id = B.id
232fb726d48Sopenharmony_ciwhere window_name_id != 0
233fb726d48Sopenharmony_ciand A.ts < tr.end_ts
234fb726d48Sopenharmony_ciunion all
235fb726d48Sopenharmony_ciselect distinct A.module_name_id id, B.data, A.window_name_id pid
236fb726d48Sopenharmony_cifrom memory_window_gpu A, trace_range TR left join data_dict B on A.module_name_id = B.id
237fb726d48Sopenharmony_ciwhere window_name_id != 0
238fb726d48Sopenharmony_ciand A.ts < TR.end_ts
239fb726d48Sopenharmony_ci  `
240fb726d48Sopenharmony_ci  );
241fb726d48Sopenharmony_ciexport const queryGpuDur = (
242fb726d48Sopenharmony_ci  id: number
243fb726d48Sopenharmony_ci): //@ts-ignore
244fb726d48Sopenharmony_ciPromise<unknown[]> =>
245fb726d48Sopenharmony_ci  query(
246fb726d48Sopenharmony_ci    'queryGpuDur',
247fb726d48Sopenharmony_ci    `
248fb726d48Sopenharmony_ci        SELECT dur AS gpu_dur
249fb726d48Sopenharmony_ci        FROM gpu_slice
250fb726d48Sopenharmony_ci        WHERE frame_row = $id;`,
251fb726d48Sopenharmony_ci    { $id: id }
252fb726d48Sopenharmony_ci  );
253