• 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 */
15import {query} from "../SqlLite";
16import {NativeHookMalloc, NativeHookProcess, NativeHookSampleQueryInfo} from "../../bean/NativeHook";
17
18export const queryNativeHookResponseTypes = (
19  leftNs: number,
20  rightNs: number,
21  types: Array<string | number>,
22  isStatistic: boolean
23): Promise<Array<any>> => {
24  const table = isStatistic ? 'native_hook_statistic' : 'native_hook';
25  const tsKey = isStatistic ? 'ts' : 'start_ts';
26  const type = isStatistic ? 'type' : 'event_type';
27  return query(
28    'queryNativeHookResponseTypes',
29    `
30          select
31            distinct last_lib_id as lastLibId,
32            data_dict.data as value
33          from
34            ${table} A ,trace_range B
35            left join data_dict on A.last_lib_id = data_dict.id
36          where
37          A.${tsKey} - B.start_ts
38          between ${leftNs} and ${rightNs} and A.${type} in (${types.join(',')});
39      `,
40    { $leftNs: leftNs, $rightNs: rightNs, $types: types }
41  );
42};
43export const queryNativeHookStatistics = (
44  leftNs: number,
45  rightNs: number,
46  ipid: number
47): Promise<Array<NativeHookMalloc>> =>
48  query(
49    'queryNativeHookStatistics',
50    `
51    select
52      event_type as eventType,
53      sub_type_id as subTypeId,
54      max(heap_size) as max,
55      sum(case when ((A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}) then heap_size else 0 end) as allocByte,
56      sum(case when ((A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}) then 1 else 0 end) as allocCount,
57      sum(case when ((A.end_ts - B.start_ts) between ${leftNs} and ${rightNs} ) then heap_size else 0 end) as freeByte,
58      sum(case when ((A.end_ts - B.start_ts) between ${leftNs} and ${rightNs} ) then 1 else 0 end) as freeCount
59    from
60      native_hook A,
61      trace_range B
62    where
63      (A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}
64     and (event_type = 'AllocEvent' or event_type = 'MmapEvent')
65     and ipid = ${ipid}
66    group by event_type;`,
67    { $leftNs: leftNs, $rightNs: rightNs }
68  );
69
70export const queryNativeHookStatisticsMalloc = (
71  leftNs: number,
72  rightNs: number,
73  ipid: number
74): Promise<Array<NativeHookMalloc>> =>
75  query(
76    'queryNativeHookStatisticsMalloc',
77    `
78    select
79      event_type as eventType,
80      heap_size as heapSize,
81      sum(case when ((A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}) then heap_size else 0 end) as allocByte,
82      sum(case when ((A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}) then 1 else 0 end) as allocCount,
83      sum(case when ((A.end_ts - B.start_ts) between ${leftNs} and ${rightNs} ) then heap_size else 0 end) as freeByte,
84      sum(case when ((A.end_ts - B.start_ts) between ${leftNs} and ${rightNs} ) then 1 else 0 end) as freeCount
85    from
86      native_hook A,
87      trace_range B
88    where
89      (A.start_ts - B.start_ts) between ${leftNs} and ${rightNs}
90    and
91      (event_type = 'AllocEvent' or event_type = 'MmapEvent')
92    and
93      sub_type_id is null
94      and ipid = ${ipid}
95    group by
96      event_type,
97      heap_size
98    order by heap_size desc
99    `,
100    { $leftNs: leftNs, $rightNs: rightNs }
101  );
102
103export const queryNativeHookStatisticsSubType = (
104  leftNs: number,
105  rightNs: number,
106  ipid: number
107): Promise<Array<NativeHookMalloc>> =>
108  query(
109    'queryNativeHookStatisticsSubType',
110    `
111    select
112      event_type as eventType,
113      sub_type_id as subTypeId,
114      max(heap_size) as max,
115      sum(case when ((NH.start_ts - TR.start_ts) between ${leftNs} and ${rightNs}) then heap_size else 0 end) as allocByte,
116      sum(case when ((NH.start_ts - TR.start_ts) between ${leftNs} and ${rightNs}) then 1 else 0 end) as allocCount,
117      sum(case when ((NH.end_ts - TR.start_ts) between ${leftNs} and ${rightNs} ) then heap_size else 0 end) as freeByte,
118      sum(case when ((NH.end_ts - TR.start_ts) between ${leftNs} and ${rightNs} ) then 1 else 0 end) as freeCount
119    from
120      native_hook NH,
121      trace_range TR
122    where
123      (NH.start_ts - TR.start_ts) between ${leftNs} and ${rightNs}
124    and
125      (event_type = 'MmapEvent')
126    and ipid = ${ipid}
127    group by
128      event_type,sub_type_id;
129        `,
130    { $leftNs: leftNs, $rightNs: rightNs }
131  );
132
133export const queryNativeHookSubType = (leftNs: number, rightNs: number, ipid: number): Promise<Array<any>> =>
134  query(
135    'queryNativeHookSubType',
136    `select distinct(
137  case when sub_type_id is null  then -1 else sub_type_id end
138) as subTypeId,
139(case when sub_type_id is null then 'Other MmapEvent' else DD.data end) as subType
140      from
141        native_hook NH,
142        trace_range TR
143      left join data_dict DD on NH.sub_type_id = DD.id
144where event_type = 'MmapEvent' and
145        (NH.start_ts - TR.start_ts) between ${leftNs} and ${rightNs}
146        and ipid = ${ipid}
147        `,
148    { $leftNs: leftNs, $rightNs: rightNs }
149  );
150
151export const queryNativeHookStatisticSubType = (leftNs: number, rightNs: number, ipid: number): Promise<Array<any>> =>
152  query(
153    'queryNativeHookStatisticSubType',
154    `SELECT DISTINCT
155      CASE
156        WHEN type = 3 AND sub_type_id NOT NULL THEN sub_type_id
157        ELSE type
158      END AS subTypeId,
159      CASE
160        WHEN type = 2 THEN 'FILE_PAGE_MSG'
161        WHEN type = 3 AND sub_type_id NOT NULL THEN D.data
162        WHEN type = 3 THEN 'MEMORY_USING_MSG'
163        ELSE 'Other MmapEvent'
164      END AS subType
165      FROM
166        native_hook_statistic NHS
167        LEFT JOIN data_dict D ON NHS.sub_type_id = D.id,
168        trace_range TR
169      WHERE
170        NHS.type >= 1 AND
171        (NHS.ts - TR.start_ts) between ${leftNs} and ${rightNs}
172        AND ipid = ${ipid}
173      `,
174    { $leftNs: leftNs, $rightNs: rightNs }
175  );
176
177export const queryNativeHookStatisticsCount = (): Promise<Array<{ num: number }>> =>
178  query('queryNativeHookStatisticsCount', `select count(1) num from native_hook_statistic`, {});
179
180export const queryNativeHookProcess = (table: string): Promise<Array<NativeHookProcess>> => {
181  let sql = `
182    select
183      distinct ${table}.ipid,
184      pid,
185      name
186    from
187      ${table}
188    left join
189      process p
190    on
191      ${table}.ipid = p.id
192    `;
193  return query('queryNativeHookProcess', sql, {});
194};
195
196export const queryNativeHookSnapshotTypes = (ipid: number): Promise<Array<NativeHookSampleQueryInfo>> =>
197  query(
198    'queryNativeHookSnapshotTypes',
199    `
200select
201      event_type as eventType,
202      data as subType
203    from
204      native_hook left join data_dict on native_hook.sub_type_id = data_dict.id
205    where
206      (event_type = 'AllocEvent' or event_type = 'MmapEvent')
207      and ipid = ${ipid}
208    group by
209      event_type,data;`,
210    {}
211  );
212
213export const queryAllHookData = (rightNs: number, ipid: number): Promise<Array<NativeHookSampleQueryInfo>> =>
214  query(
215    'queryAllHookData',
216    `
217    select
218      callchain_id as eventId,
219      event_type as eventType,
220      data as subType,
221      addr,
222      heap_size as growth,
223      (n.start_ts - t.start_ts) as startTs,
224      (n.end_ts - t.start_ts) as endTs
225    from
226      native_hook n left join data_dict on n.sub_type_id = data_dict.id,
227      trace_range t
228    where
229      (event_type = 'AllocEvent' or event_type = 'MmapEvent')
230      and ipid = ${ipid}
231    and
232      n.start_ts between t.start_ts and ${rightNs} + t.start_ts`,
233    { $rightNs: rightNs }
234  );