import pytest
import subprocess
import re
import time
import threading
import sqlite3
from string import Template
import os
from tools.utils import run_and_get_output
def get_file_size(file_path):
size = os.path.getsize(file_path)
return size
def task():
run_and_get_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_memory_plugin.txt -o /data/local/tmp/test_memory_kernel.htrace -t 60 -s -k"')
def task_vminfo():
run_and_get_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_memory_vmeminfo.txt -o /data/local/tmp/test_memory_vmeminfo.htrace -t 60 -s -k"')
def task_vmtracker():
run_and_get_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_memory_vmtracker.txt -o /data/local/tmp/test_memory_vmtracker.htrace -t 60 -s -k"')
def write_str_file(file_path, large_string):
lines = large_string.split('\n')
with open(file_path, 'w') as file:
for line in lines:
file.write(line + '\n')
class TestHiprofilerMemoryPlugin:
@pytest.mark.L0
def test_memory_plugin_kernel(self):
run_and_get_output(f"hdc file send ..\inputfiles\memory_plugin\config_memory_plugin.txt /data/local/tmp/", shell=False,
text=True, encoding="utf-8")
task_thread = threading.Thread(target=task, args=())
task_thread.start()
task_thread.join()
subprocess.run(f'hdc file recv /data/local/tmp/test_memory_kernel.htrace ../outputfiles/', shell=False,
text=True, encoding="utf-8")
file_size = get_file_size(f"../outputfiles/test_memory_kernel.htrace")
assert (file_size > 1024)
run_and_get_output(
r"../inputfiles/trace_streamer_db.exe ../outputfiles/test_memory_kernel.htrace -e ../outputfiles/test_memory_kernel.db")
conn = sqlite3.connect(r'../outputfiles/test_memory_kernel.db')
cursor = conn.cursor()
cursor.execute("select * from sys_mem_measure,sys_event_filter where filter_id = id and name ='sys.mem.mapped'")
result = cursor.fetchall()
row_count = len(result)
assert(row_count > 0)
for row in result:
assert(row[3] >= 0)
cursor = conn.cursor()
cursor.execute("select * from sys_mem_measure,sys_event_filter where filter_id = id and name ='sys.mem.total'")
result = cursor.fetchall()
row_count = len(result)
assert(row_count > 0)
for row in result:
assert(row[3] >= 0)
cursor.close()
conn.close()
@pytest.mark.L0
def test_memory_plugin_vmeminfo(self):
run_and_get_output(f"hdc file send ..\inputfiles\memory_plugin\config_memory_vmeminfo.txt /data/local/tmp/", shell=False,
text=True, encoding="utf-8")
task_thread = threading.Thread(target=task_vminfo, args=())
task_thread.start()
task_thread.join()
subprocess.run(f'hdc file recv /data/local/tmp/test_memory_vmeminfo.htrace ../outputfiles/', shell=False,
text=True, encoding="utf-8")
file_size = get_file_size(f"../outputfiles/test_memory_vmeminfo.htrace")
assert (file_size > 1024)
run_and_get_output(
r"../inputfiles/trace_streamer_db.exe ../outputfiles/test_memory_vmeminfo.htrace -e ../outputfiles/test_memory_vmeminfo.db")
conn = sqlite3.connect(r'../outputfiles/test_memory_vmeminfo.db')
cursor = conn.cursor()
cursor.execute("select * from sys_mem_measure,sys_event_filter where filter_id = id and name ='sys.virtual.mem.nr.free.pages'")
result = cursor.fetchall()
row_count = len(result)
assert(row_count > 0)
for row in result:
assert(row[3] >= 0)
cursor = conn.cursor()
cursor.execute("select * from sys_mem_measure,sys_event_filter where filter_id = id and name ='sys.virtual.mem.nr.active_file'")
result = cursor.fetchall()
row_count = len(result)
assert(row_count > 0)
for row in result:
assert(row[3] >= 0)
cursor.close()
conn.close()
@pytest.mark.L0
def test_memory_plugin_vmtracker(self):
sys_bit = subprocess.run(f"hdc shell getconf LONG_BIT", stdout=subprocess.PIPE, text=True, check=True)
sysinfo = sys_bit.stdout
if sysinfo.strip() == "32":
run_and_get_output(f"hdc shell aa start -a com.ohos.settings.MainAbility -b com.ohos.settings", shell=False, text=True, encoding="utf-8")
time.sleep(2)
pid_text = subprocess.run(f"hdc shell pidof 'com.ohos.settings'", stdout=subprocess.PIPE, text=True, check=True)
else:
pid_text = subprocess.run(f"hdc shell pidof 'com.ohos.launcher'", stdout=subprocess.PIPE, text=True, check=True)
pidinfo = pid_text.stdout
file_content = Template('request_id: 1 \n'
' session_config { \n'
' buffers { \n'
' pages: 16384 \n'
' } \n'
' } \n'
' plugin_configs { \n'
' plugin_name: "memory-plugin" \n'
' sample_interval: 5000 \n'
' config_data { \n'
' report_process_tree: false \n'
' report_sysmem_mem_info: false \n'
' report_sysmem_vmem_info: false \n'
' report_process_mem_info: true \n'
' report_app_mem_info: false \n'
' report_app_mem_by_memory_service: false\n'
' pid: ${s1} \n'
' report_purgeable_ashmem_info: true \n'
' report_dma_mem_info: true \n'
' report_gpu_mem_info: true \n'
' report_smaps_mem_info: true \n'
' report_gpu_dump_info: true \n'
' } \n'
' } \n')
vmfile = file_content.safe_substitute(s1=pidinfo.strip())
write_str_file("../inputfiles/memory_plugin/config_memory_vmtracker.txt", vmfile)
run_and_get_output(f"hdc file send ..\inputfiles\memory_plugin\config_memory_vmtracker.txt /data/local/tmp/", shell=False,
text=True, encoding="utf-8")
task_thread = threading.Thread(target=task_vmtracker, args=())
task_thread.start()
task_thread.join()
subprocess.run(f'hdc file recv /data/local/tmp/test_memory_vmtracker.htrace ../outputfiles/', shell=False,
text=True, encoding="utf-8")
file_size = get_file_size(f"../outputfiles/test_memory_vmtracker.htrace")
assert (file_size > 1024)
run_and_get_output(
r"../inputfiles/trace_streamer_db.exe ../outputfiles/test_memory_vmtracker.htrace -e ../outputfiles/test_memory_vmtracker.db")
conn = sqlite3.connect(r'../outputfiles/test_memory_vmtracker.db')
output_text = subprocess.run(f'hdc shell "ps -ef | grep hidumper"', stdout=subprocess.PIPE, text=True, check=True)
process_info = output_text.stdout
lines = process_info.strip().split('\n')
check_index = False
for line in lines:
if line.find("hidumper -s") != -1:
check_index = True
assert (check_index == False)
cursor = conn.cursor()
cursor.execute("select * from memory_dma limit 0,10")
result = cursor.fetchall()
row_count = len(result)
assert(row_count > 0)
for row in result:
assert(row[3] > 0 and row[4] > 0 and row[5] > 0 and row[6] > 0 and row[7] > 0 and row[8] > 0 and row[9] > 0)
cursor.execute("select * from smaps limit 0,10")
result = cursor.fetchall()
row_count = len(result)
assert(row_count > 0)
for row in result:
assert(row[2] != '' and row[3] != '' and row[7] >= 0 and row[8] >= 0)
cursor.execute("select * from memory_process_gpu limit 0,10")
result = cursor.fetchall()
row_count = len(result)
assert(row_count > 0)
for row in result:
assert(row[3] > 0 and row[7] > 0)
cursor.close()
conn.close()