def mem_monitor_procrank_main(): init_path_vars() if g_flag_build_report: MonitorUtils.g_create_report_dir(g_report_dir_path) f_report = MonitorUtils.g_create_and_open_report_with_append(g_report_file_path) try: create_report_header(f_report) init_default_content() if g_flag_only_process: loop_for_subprocess(subprocess_run_cmd_and_write_report_for_process, f_report) elif g_flag_only_total: loop_for_subprocess(subprocess_run_cmd_and_write_report_for_total, f_report) elif g_flag_process_total: loop_for_subprocess(subprocess_run_cmd_and_write_report_for_process_and_total, f_report) elif g_flag_all: loop_for_subprocess(subprocess_run_cmd_and_write_report_for_all, f_report) else: print 'Error: no monitor flag set and exit.' exit(1) finally: create_report_trailer(f_report) f_report.close() if g_flag_parse_report: # create_report_sorted_by_category() create_separated_report_for_process_service_total()
def cpu_monitor_top_setup(): if not AdbUtils.verify_adb_devices_connect(): print 'Error, no adb devices connected!' exit(1) init_path_vars(g_report_root_path) MUtils.g_create_report_dir(g_report_root_path)
def mem_monitor_procrank_setup(): if not AdbUtils.verify_adb_devices_connect(): print 'Error, no adb devices connected!' exit(1) init_path_vars(g_report_root_path) MUtils.g_create_report_dir(g_report_root_path)
def cpu_monitor_top_main(): init_path_vars() MonitorUtils.g_create_report_dir(g_report_dir_path) if g_flag_top: run_top_cmd_main() if g_flag_top_for_pkg: run_top_cmd_for_pkg_main() if g_flag_parse_report_for_pkg: parse_top_for_pkg_report_main()
def create_report_sorted_by_category(): if g_flag_print_log: print 'log: create report sorted by category: APP process, service, and total.' lines = read_lines_from_file(g_report_file_path) if len(lines) == 0: print 'Error, the size of file is zero --> %s' %(g_report_file_path) return lines_process = [] lines_sevice = [] lines_total = [] lines_header = [] lines_trailer = [] keyword_end = 'END' for line in lines: if g_category_process in line: lines_process.append(line) elif g_category_sevice in line: lines_sevice.append(line) elif g_category_total in line: lines_total.append(line) elif keyword_end in line: lines_trailer.append(line) else: lines_header.append(line) # lines.sort(key=lambda x:x.split(',')[1]) f_category_report = MonitorUtils.g_create_and_open_report_with_write(g_category_report_file_path) try: write_lines_into_file(f_category_report,lines_header,lines_process,lines_sevice,lines_total,lines_trailer) finally: files_flush_and_close(f_category_report)
def parse_report_line(lines): java_vm_heap_size = DEFAULT_NULL_CONTENT java_vm_heap_alloc = DEFAULT_NULL_CONTENT total_mem = DEFAULT_NULL_CONTENT app_views = DEFAULT_NULL_CONTENT app_activities = DEFAULT_NULL_CONTENT for line in lines: if 'Dalvik Heap' in line: java_vm_heap_size, java_vm_heap_alloc = parse_java_vm_heap_size( line) elif 'TOTAL' in line: total_mem = parse_total_mem(line) elif ' Views:' in line: app_views = parse_app_views(line) elif 'Activities:' in line: app_activities = parse_app_activities(line) java_vm_heap_size = format_mem_size(java_vm_heap_size) java_vm_heap_alloc = format_mem_size(java_vm_heap_alloc) total_mem = format_mem_size(total_mem) cur_time = MUtils.g_get_current_time() tmp_fields = (cur_time, total_mem, java_vm_heap_size, java_vm_heap_alloc, app_activities, app_views) return MUtils.g_report_limiter.join(tmp_fields)
def read_lines_from_file(g_report_file_path): lines = [] f_report = MonitorUtils.g_open_report_with_read(g_report_file_path) try: lines = f_report.readlines() finally: f_report.close() return lines
def mem_monitor_procrank_main_for_all(): f_report = MUtils.g_create_and_open_report_with_append( g_report_file_for_all_path) try: create_and_write_report_header(f_report) loop_for_subprocess(run_cmd_and_write_report_for_all, f_report) create_and_write_report_trailer(f_report) finally: file_flush_and_close(f_report)
def parse_top_for_pkg_report_main(): f_report = MonitorUtils.g_open_report_with_read( g_report_file_path_top_for_pkg) lines_for_total, lines_for_pkg = parse_top_for_pkg_report( read_lines_from_report(f_report)) lines_for_total = parse_lines_for_total(lines_for_total) f_total_category = MonitorUtils.g_create_and_open_report_with_write( g_report_file_path_total_category) f_pkg_category = MonitorUtils.g_create_and_open_report_with_write( g_report_file_path_pkg_category) try: write_multiple_lines_report(f_total_category, lines_for_total) write_multiple_lines_report(f_pkg_category, lines_for_pkg) finally: file_close_and_flush(f_total_category) file_close_and_flush(f_pkg_category)
def run_top_cmd_main(): f_report = MonitorUtils.g_create_and_open_report_with_append( g_report_file_path_top) try: write_single_line_report(f_report, build_report_header_for_top_cmd()) loop_process(run_top_cmd_and_write_output, f_report) finally: write_single_line_report(f_report, build_report_trailer_for_top_cmd()) file_close_and_flush(f_report)
def create_separated_report_for_process_service_total(): if g_flag_print_log: print 'log: create separate report for APP process, service, and total.' lines = read_lines_from_file(g_report_file_path) if len(lines) == 0: print 'Error, the file size is zero --> %s' %(g_report_file_path) return f_total = MonitorUtils.g_create_and_open_report_with_write(g_path_total) f_process = MonitorUtils.g_create_and_open_report_with_write(g_path_app_process) f_sevice = MonitorUtils.g_create_and_open_report_with_write(g_path_app_sevice) try: for line in lines: if g_category_process in line: f_process.write(line) elif g_category_total in line: f_total.write(line) elif g_category_sevice in line: f_sevice.write(line) finally: files_flush_and_close(f_process,f_sevice,f_total)
def run_top_cmd_for_total_pkg_main(): if g_report_file_path_top_for_pkg_and_total == '': print 'Error, g_report_file_path_top_for_pkg_and_total is null!' exit(1) f_report = MUtils.g_create_and_open_report_with_append(g_report_file_path_top_for_pkg_and_total) try: title_line = build_report_header_title_line_for_top() info_line = build_report_header_info_line_for_top(g_pkg_name, g_monitor_interval) write_multiple_lines_report(f_report, (title_line, info_line)) loop_process(run_top_cmd_for_total_pkg_and_write_output, f_report) write_single_line_report(f_report, build_report_trailer_line_for_top()) finally: file_flush_and_close(f_report)
def run_top_cmd_for_total_pkg_main(): if g_report_file_path_top_for_pkg_and_total == '': print 'Error, g_report_file_path_top_for_pkg_and_total is null!' exit(1) f_report = MUtils.g_create_and_open_report_with_append( g_report_file_path_top_for_pkg_and_total) try: title_line = build_report_header_title_line_for_top() info_line = build_report_header_info_line_for_top( g_pkg_name, g_monitor_interval) write_multiple_lines_report(f_report, (title_line, info_line)) loop_process(run_top_cmd_for_total_pkg_and_write_output, f_report) write_single_line_report(f_report, build_report_trailer_line_for_top()) finally: file_flush_and_close(f_report)
def parse_report_line(lines): java_vm_heap_size = DEFAULT_NULL_CONTENT java_vm_heap_alloc = DEFAULT_NULL_CONTENT total_mem = DEFAULT_NULL_CONTENT app_views = DEFAULT_NULL_CONTENT app_activities = DEFAULT_NULL_CONTENT for line in lines: if 'Dalvik Heap' in line: java_vm_heap_size, java_vm_heap_alloc = parse_java_vm_heap_size(line) elif 'TOTAL' in line: total_mem = parse_total_mem(line) elif ' Views:' in line: app_views = parse_app_views(line) elif 'Activities:' in line: app_activities = parse_app_activities(line) java_vm_heap_size = format_mem_size(java_vm_heap_size) java_vm_heap_alloc = format_mem_size(java_vm_heap_alloc) total_mem = format_mem_size(total_mem) cur_time = MUtils.g_get_current_time() tmp_fields = (cur_time, total_mem, java_vm_heap_size, java_vm_heap_alloc, app_activities, app_views) return MUtils.g_report_limiter.join(tmp_fields)
def prepare_report_file(): MUtils.g_create_report_dir(g_report_root_path) f_report = MUtils.g_create_and_open_report_with_append(g_report_file_path) return f_report
def build_prefix_line_for_top_cmd_output(): cur_datetime = MUtils.g_get_current_datetime() return cur_datetime + ' ' + '-' * 40
def build_prefix_line_for_procrank_cmd_output(): cur_datetime = MUtils.g_get_current_datetime() return '%s %s\n' % (cur_datetime, '-' * 40)
if not AdbUtils.verify_adb_devices_connect(): print 'No adb devices connected!' exit(1) init_path_vars(g_report_root_path) def mem_monitor_dumpsys_main(): mem_monitor_dumpsys_setup() f_report = prepare_report_file() try: create_report_header(f_report) loop_process(parse_dumpsys_meminfo_and_write_report, f_report) create_report_trailer(f_report) finally: force_write_lines_in_report(f_report) f_report.close() if __name__ == '__main__': g_pkg_name = MUtils.g_pkg_name_launcher g_run_time = 5 * MUtils.g_min g_monitor_interval = 3 g_run_num = '01' g_report_root_path = r'%s\%s_%s' % (MUtils.g_get_report_root_path(), MUtils.g_get_current_date(), g_run_num) mem_monitor_dumpsys_main() print 'Memory monitor by dumpsys meminfo, DONE!'
def build_prefix_for_top_cmd_output_line(): cur_time = MonitorUtils.g_get_current_time() return '%s -----------------------------------' % (cur_time)
def write_report_line_with_time_in_file(f_report, write_line): cur_time = MUtils.g_get_current_time() tmp_line = cur_time + ' ' + write_line.replace('\r\n', '\n') print tmp_line f_report.write(tmp_line) f_report.flush()
print 'Error, no adb devices connected!' exit(1) init_path_vars(g_report_root_path) MUtils.g_create_report_dir(g_report_root_path) def mem_monitor_procrank_main(): mem_monitor_procrank_setup() if g_is_process: mem_monitor_procrank_main_for_process() else: mem_monitor_procrank_main_for_all() if __name__ == '__main__': g_is_process = True g_pkg_name = MUtils.g_pkg_name_launcher g_run_time = 5 * MUtils.g_min g_monitor_interval = MUtils.g_short_interval g_run_num = '01' g_report_root_path = r'%s\%s_%s' % (MUtils.g_get_report_root_path(), MUtils.g_get_current_date(), g_run_num) mem_monitor_procrank_main() print 'Memory monitor by procrank, DONE!'
def run_top_cmd_for_pkg_and_write_output(f_report): write_line = '%s %s' % (MUtils.g_get_current_time(), run_top_command_for_pkg()) write_single_line_report(f_report, write_line) f_report.flush()