1#!/usr/bin/env vpython 2# Copyright 2020 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import argparse 7import os 8import sys 9import xml.etree.ElementTree as ET 10 11 12def ExtractValues(xml_path, correction): 13 root = ET.parse(xml_path).getroot() 14 15 speeds = [] 16 power = [] 17 clusters = [] 18 for array in root.iter('array'): 19 if array.get('name') == 'cpu.clusters.cores': 20 clusters = [int(value.text) for value in array.iter('value')] 21 if array.get('name').startswith('cpu.core_speeds.'): 22 speeds.append([int(value.text) for value in array.iter('value')]) 23 if array.get('name').startswith('cpu.core_power.'): 24 power.append([float(value.text) for value in array.iter('value')]) 25 26 values = [] 27 cpu = 0 28 for cluster, n_cpus in enumerate(clusters): 29 for _ in range(n_cpus): 30 for freq, drain in zip(speeds[cluster], power[cluster]): 31 if correction: 32 drain /= n_cpus 33 values.append((cpu, cluster, freq, drain)) 34 cpu += 1 35 36 return values 37 38 39def ExportProfiles(device_xmls, sql_path): 40 sql_values = [] 41 for device, xml_path, correction in device_xmls: 42 sql_values += [ 43 '("%s", %s, %s, %s, %s)' % ((device,) + v) 44 for v in ExtractValues(xml_path, correction == 'yes') 45 ] 46 47 with open(sql_path, 'w') as sql_file: 48 sql_file.write('INSERT OR REPLACE INTO power_profile VALUES\n') 49 sql_file.write(',\n'.join(sql_values)) 50 sql_file.write(';\n') 51 52 53def main(args): 54 parser = argparse.ArgumentParser( 55 description='Export XML power profile as a SQL INSERT query.', 56 epilog='Example usage:\n' 57 'python export_power_profiles.py ' 58 '--device-xml sailfish sailfish/power_profile.xml no ' 59 '--device-xml sargo sargo/power_profile.xml yes ' 60 '--output power_profile_data.sql') 61 parser.add_argument( 62 '--device-xml', 63 nargs=3, 64 metavar=('DEVICE', 'XML_FILE', 'CORRECTION'), 65 action='append', 66 help='First argument: device name; second argument: path to the XML ' 67 'file with the device power profile; third argument(yes|no): ' 68 'whether correction is necessary. Can be used multiple times.') 69 parser.add_argument( 70 '--output', metavar='SQL_FILE', help='Path to the output file.') 71 72 args = parser.parse_args(args) 73 74 sql_path = 'result.sql' 75 ExportProfiles(args.device_xml, args.output) 76 77 78if __name__ == '__main__': 79 sys.exit(main(sys.argv[1:])) 80