1#
2# Copyright (c) 2024 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#
15
16import os
17import sys
18import subprocess
19import sqlite3
20import threading
21import time
22import re
23import shutil
24import uuid
25import random
26import json
27import sqlite3
28import datetime
29import pandas as pd
30import inspect
31
32# The following programs are packaged as exe commands
33# pyinstaller --onefile get_trace_loop.py
34
35trace_duration = 30
36
37now_time = "null"
38now_version = "null"
39need_reboot = False
40target_p_name = "sensor_host"
41trace_file_name = ""
42p_name_id = ""
43
44
45# Run a cmd command
46def run_cmd(cmd):
47    __func__ = inspect.currentframe().f_code.co_name
48    print(f"{__func__}: {cmd}")
49    output = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True,
50                            check=True).stdout
51    print(f"{__func__}: result:{str(output)}")
52    return output
53
54
55# Wait for the HDC to connect to the device
56def wait_for_device():
57    __func__ = inspect.currentframe().f_code.co_name
58    print(f"{__func__}: in")
59    run_cmd("hdc wait-for-device")
60
61
62# Restart your device
63def reboot():
64    __func__ = inspect.currentframe().f_code.co_name
65    print(f"{__func__}: in")
66    run_cmd("hdc shell reboot")
67
68
69# Gets the current time, which is used as the file name part of the scraped data
70def update_now_time():
71    __func__ = inspect.currentframe().f_code.co_name
72    print(f"{__func__}: in")
73    global now_time
74    now_time = str(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
75
76
77# Obtain the current device version number, which is used as the file name part of the scraped data
78def update_now_version():
79    __func__ = inspect.currentframe().f_code.co_name
80    print(f"{__func__}: in")
81    global now_version
82    now_version = str(run_cmd("hdc shell param get const.product.software.version")).replace("\n", "").replace(" ", "")
83
84
85# Obtain the strings of process name and process ID
86def get_p_name_id():
87    __func__ = inspect.currentframe().f_code.co_name
88    print(f"{__func__}: in")
89    global p_name_id
90    cmd = "hdc shell \"pidof " + target_p_name + "\""
91    pid = str(run_cmd(cmd)).replace("\n", "")
92    p_name_id = target_p_name + "-" + pid
93
94
95# Obtain the main method of trace
96def get_trace():
97    __func__ = inspect.currentframe().f_code.co_name
98    print(f"{__func__}: in")
99    global trace_file_name
100    trace_file_name = "trace-" + now_version + "-" + p_name_id + "-" + now_time + ".trace"
101    cmd = "hdc shell \"hitrace -b 40960 -t " + str(trace_duration) + " --overwrite hdf -o /data/log/this.trace\""
102    result = run_cmd(cmd)
103    if "OpenRecording failed" in result:
104        reboot()
105        return
106    cmd = "hdc file recv /data/log/this.trace " + trace_file_name
107    result = run_cmd(cmd)
108
109
110# Perform a one-time crawl of memory data for all processes configured
111def get_data_once():
112    update_now_time()
113    update_now_version()
114    get_p_name_id()
115    get_trace()
116
117
118# Perform num fetch of the memory data of all processes configured at daily intervals
119def get_data_more(num, daily):
120    for i in range(num):
121        get_data_once()
122        time.sleep(daily)
123
124
125if __name__ == "__main__":
126    get_data_more(10000, 0)
127    pass
128