1// Copyright (c) 2021 Huawei Device Co., Ltd.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14import { QueryEnum, TraficEnum } from './utils/QueryEnum';
15import { getThreadPool } from '../SqlLite';
16import { TraceRow } from '../../component/trace/base/TraceRow';
17import { Utils } from '../../component/trace/base/Utils';
18
19export function sliceSender(traceId?: string): Promise<unknown> {
20  let trafic: number = TraficEnum.Memory;
21  return new Promise((resolve): void => {
22    getThreadPool(traceId).submitProto(
23      QueryEnum.SliceData,
24      {
25        trafic: trafic,
26        startNS: TraceRow.range?.startNS || 0,
27        endNS: TraceRow.range?.endNS || 0,
28        recordStartNS: Utils.getInstance().getRecordStartNS(traceId),
29        recordEndNS: Utils.getInstance().getRecordEndNS(traceId),
30      },
31      (res: unknown): void => {
32        resolve(res);
33      }
34    );
35  });
36}
37
38export function sliceSPTSender(leftNs: number, rightNs: number, cpus: Array<number>,
39  func: string, traceId?: string): Promise<unknown[]> {
40  return new Promise((resolve): void => {
41    getThreadPool(traceId).submitProto(
42      QueryEnum.SliceSPTData,
43      {
44        leftNs: leftNs,
45        rightNs: rightNs,
46        cpus: cpus,
47        func: func,
48        trafic: TraficEnum.Memory,
49      },
50      (res: unknown, len: number, transfer: boolean): void => {
51        //@ts-ignore
52        resolve(res);
53      }
54    );
55  });
56}
57
58export function sliceChildBoxSender(func: string, leftNs: number, rightNs: number, threadId?: number | number[], processId?: number | number[],
59  cpus?: Array<number>, state?: string, traceId?: string): Promise<unknown[]> {
60  return new Promise((resolve): void => {
61    getThreadPool(traceId).submitProto(
62      QueryEnum.SliceChildBoxData,
63      {
64        leftNs: leftNs,
65        rightNs: rightNs,
66        cpus: cpus ? cpus : [],
67        processId: processId ? processId : [],
68        threadId: threadId ? threadId : [],
69        state: state ? state : '',
70        func: func ? func : '',
71        trafic: TraficEnum.Memory,
72      },
73      (res: unknown, len: number, transfer: boolean): void => {
74        //@ts-ignore
75        resolve(res);
76      }
77    );
78  });
79}
80
81export function threadNearData(func: string, pid: number, tid: number, startTime: number, traceId?: string): Promise<unknown[]> {
82  return new Promise((resolve): void => {
83    getThreadPool(traceId).submitProto(
84      QueryEnum.ThreadNearData,
85      {
86        pid: pid,
87        tid: tid,
88        startTime: startTime,
89        func: func,
90        trafic: TraficEnum.Memory,
91      },
92      (res: unknown, len: number, transfer: boolean): void => {
93        //@ts-ignore
94        resolve(res);
95      }
96    );
97  });
98}
99