aboutsummaryrefslogtreecommitdiff
path: root/tools/export_power_profiles.py
blob: cea30f815c4da311dba287d00197f068b7e18f97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import os
import sys
import xml.etree.ElementTree as ET


def ExtractValues(xml_path, correction):
  root = ET.parse(xml_path).getroot()

  speeds = []
  power = []
  clusters = []
  for array in root.iter('array'):
    if array.get('name') == 'cpu.clusters.cores':
      clusters = [int(value.text) for value in array.iter('value')]
    if array.get('name').startswith('cpu.core_speeds.'):
      speeds.append([int(value.text) for value in array.iter('value')])
    if array.get('name').startswith('cpu.core_power.'):
      power.append([float(value.text) for value in array.iter('value')])

  values = []
  cpu = 0
  for cluster, n_cpus in enumerate(clusters):
    for _ in range(n_cpus):
      for freq, drain in zip(speeds[cluster], power[cluster]):
        if correction:
          drain /= n_cpus
        values.append((cpu, cluster, freq, drain))
      cpu += 1

  return values


def ExportProfiles(device_xmls, sql_path):
  sql_values = []
  for device, xml_path, correction in device_xmls:
    sql_values += [
        '("%s", %s, %s, %s, %s)' % ((device,) + v)
        for v in ExtractValues(xml_path, correction == 'yes')
    ]

  with open(sql_path, 'w') as sql_file:
    sql_file.write('INSERT OR REPLACE INTO power_profile VALUES\n')
    sql_file.write(',\n'.join(sql_values))
    sql_file.write(';\n')


def main(args):
  parser = argparse.ArgumentParser(
      description='Export XML power profile as a SQL INSERT query.',
      epilog='Example usage:\n'
      'python export_power_profiles.py '
      '--device-xml sailfish sailfish/power_profile.xml no '
      '--device-xml sargo sargo/power_profile.xml yes '
      '--output power_profile_data.sql')
  parser.add_argument(
      '--device-xml',
      nargs=3,
      metavar=('DEVICE', 'XML_FILE', 'CORRECTION'),
      action='append',
      help='First argument: device name; second argument: path to the XML '
      'file with the device power profile; third argument(yes|no): '
      'whether correction is necessary. Can be used multiple times.')
  parser.add_argument(
      '--output', metavar='SQL_FILE', help='Path to the output file.')

  args = parser.parse_args(args)

  sql_path = 'result.sql'
  ExportProfiles(args.device_xml, args.output)


if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))