• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (C) 2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import pytest
17import subprocess
18import re
19import time
20import os
21import threading
22import sqlite3
23
24
25def get_file_size(file_path):
26    size = os.path.getsize(file_path)
27    return size
28
29
30def task():
31    subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/inputfiles/ftrace/config_ftrace.txt -o /data/local/tmp/test_ftrace.htrace -t 10 -s -k"')
32
33
34def task_event():
35    subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/inputfiles/ftrace/config_ftrace_event.txt -o /data/local/tmp/test_ftrace_event.htrace -t 10 -s -k"')
36
37
38def task_freq():
39    subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/inputfiles/ftrace/config_ftrace_freq.txt -o /data/local/tmp/test_ftrace_freq.htrace -t 30 -s -k"')
40
41
42class TestHiprofilerFtracePlugin:
43    @pytest.mark.L0
44    def test_ftraceplugin(self):
45        subprocess.check_output(f"hdc file send ./inputfiles/ftrace/config_ftrace.txt /data/local/tmp/", shell=False,
46                                text=True, encoding="utf-8")
47        task_thread = threading.Thread(target=task, args=())
48        task_thread.start()
49        task_thread.join()
50        subprocess.check_output(f"hdc file recv /data/local/tmp/test_ftrace.htrace ./outputfiles/ ", shell=False,
51                                text=True, encoding="utf-8")
52        # 检查文件大小
53        file_size = get_file_size(f"./outputfiles/test_ftrace.htrace")
54        assert (file_size > 1024)
55        subprocess.check_output(
56            r"./inputfiles/trace_streamer_db.exe ./outputfiles/test_ftrace.htrace -e ./outputfiles/test_ftrace.db")
57        # 连接数据库文件
58        conn = sqlite3.connect(r'./outputfiles/test_ftrace.db')
59        # 创建游标对象
60        cursor = conn.cursor()
61        # 执行SQL查询
62        # 检查binder
63        cursor.execute("select * from callstack where cat ='binder' limit 0,10")
64        result = cursor.fetchall()
65        for row in result:
66            assert (row[5] == 'binder transaction' or row[5] == 'binder reply' or row[5] == 'binder transaction async' or row[5] == 'binder async rcv')
67        cursor.close()
68        conn.close()
69
70    @pytest.mark.L0
71    def test_ftrace_events(self):
72        subprocess.check_output(f"hdc file send ./inputfiles/ftrace/config_ftrace_event.txt /data/local/tmp/", shell=False,
73                                text=True, encoding="utf-8")
74        task_thread = threading.Thread(target=task_event, args=())
75        task_thread.start()
76        task_thread.join()
77        subprocess.check_output(f"hdc file recv /data/local/tmp/test_ftrace_event.htrace ./outputfiles/ ", shell=False,
78                                text=True, encoding="utf-8")
79        # 检查文件大小
80        file_size = get_file_size(f"./outputfiles/test_ftrace_event.htrace")
81        assert (file_size > 1024)
82        assert (file_size < 1024 * 1024 * 1024)
83        subprocess.check_output(
84            r"./inputfiles/trace_streamer_db.exe ./outputfiles/test_ftrace_event.htrace -e ./outputfiles/test_ftrace_event.db")
85        # 连接数据库文件
86        conn = sqlite3.connect(r'./outputfiles/test_ftrace_event.db')
87        # 创建游标对象
88        cursor = conn.cursor()
89        # 执行SQL查询
90        check_wake = False
91        check_newtask = False
92        check_exit = False
93        table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')]
94        for table in table_list:
95            cursor.execute('SELECT * FROM ' + table[0])
96            result = cursor.fetchall()
97            for row in result:
98                if 'sched_wakeup' in row:
99                    check_wake = True
100                if 'task_newtask' in row:
101                    check_newtask = True
102                if 'sched_process_exit' in row:
103                    check_exit = True
104        # 检查 wakeup 和waking 事件
105        cursor.execute("select * from instant where name = 'sched_wakeup' limit 10")
106        result = cursor.fetchall()
107        for row in result:
108            assert (row[2] > 0)
109            assert (row[3] > 0)
110        cursor.execute("select * from instant where name = 'sched_waking' limit 10")
111        result = cursor.fetchall()
112        for row in result:
113            assert (row[2] > 0)
114            assert (row[3] > 0)
115        cursor.close()
116        conn.close()
117        assert check_wake
118        assert check_newtask
119        assert check_exit
120
121    @pytest.mark.L0
122    def test_ftrace_freq(self):
123        subprocess.check_output(f"hdc file send ./inputfiles/ftrace/config_ftrace_freq.txt /data/local/tmp/", shell=False,
124                                text=True, encoding="utf-8")
125        task_thread = threading.Thread(target=task_freq, args=())
126        task_thread.start()
127        task_thread.join()
128        subprocess.check_output(f"hdc file recv /data/local/tmp/test_ftrace_freq.htrace ./outputfiles/ ", shell=False,
129                                text=True, encoding="utf-8")
130        # 检查文件大小
131        file_size = get_file_size(f"./outputfiles/test_ftrace_freq.htrace")
132        assert (file_size > 1024)
133        subprocess.check_output(
134            r"./inputfiles/trace_streamer_db.exe ./outputfiles/test_ftrace_freq.htrace -e ./outputfiles/test_ftrace_freq.db")
135        # 连接数据库文件
136        conn = sqlite3.connect(r'./outputfiles/test_ftrace_freq.db')
137        # 创建游标对象
138        cursor = conn.cursor()
139         # 执行SQL查询
140        cursor.execute("select end_ts - start_ts as time from trace_range")
141        result = cursor.fetchall()
142        for row in result:
143            assert (row[0] > 10 * 1000 * 1000 * 1000)
144        #检查cpu 频率
145        cursor.execute("select count(0) from cpu_measure_filter where name = 'cpu_frequency'")
146        result = cursor.fetchall()
147        for row in result:
148            assert (row[0] == 12)
149
150        cursor.execute("select * from measure,cpu_measure_filter where filter_id = id and name ='cpu_frequency' limit 10")
151        result = cursor.fetchall()
152        for row in result:
153            assert (row[3] > 0)
154        cursor.close()
155        conn.close()
156
157