Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/python
      2 #
      3 # Copyright (C) 2017 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 # Test converter of a Config proto.
     18 
     19 # Generate with:
     20 #  aprotoc -I=system/extras/perfprofd --python_out=system/extras/perfprofd/scripts \
     21 #      system/extras/perfprofd/binder_interface/perfprofd_config.proto
     22 import perfprofd_config_pb2
     23 
     24 import sys
     25 
     26 config_options = [
     27     ('collection_interval_in_s', 'u'),
     28     ('use_fixed_seed', 'u'),
     29     ('main_loop_iterations', 'u'),
     30     ('destination_directory', 's'),
     31     ('config_directory', 's'),
     32     ('perf_path', 's'),
     33     ('sampling_period', 'u'),
     34     ('sample_duration_in_s', 'u'),
     35     ('only_debug_build', 'b'),
     36     ('hardwire_cpus', 'b'),
     37     ('hardwire_cpus_max_duration_in_s', 'u'),
     38     ('max_unprocessed_profiles', 'u'),
     39     ('stack_profile', 'b'),
     40     ('collect_cpu_utilization', 'b'),
     41     ('collect_charging_state', 'b'),
     42     ('collect_booting', 'b'),
     43     ('collect_camera_active', 'b'),
     44     ('process', 'i'),
     45     ('use_elf_symbolizer', 'b'),
     46     ('send_to_dropbox', 'b'),
     47 ]
     48 
     49 def collect_and_write(filename):
     50     config = perfprofd_config_pb2.ProfilingConfig()
     51 
     52     for (option, option_type) in config_options:
     53         input = raw_input('%s(%s): ' % (option, option_type))
     54         if input == '':
     55             # Skip this argument.
     56             continue
     57         elif input == '!':
     58             # Special-case input, end argument collection.
     59             break
     60         # Now do some actual parsing work.
     61         if option_type == 'u' or option_type == 'i':
     62             option_val = int(input)
     63         elif option_type == 'b':
     64             if input == '1' or input == 't' or input == 'true':
     65                 option_val = True
     66             elif input == '0' or input == 'f' or input == 'false':
     67                 option_val = False
     68             else:
     69                 assert False, 'Unknown boolean %s' % input
     70         else:
     71             assert False, 'Unknown type %s' % type
     72         setattr(config, option, option_val)
     73 
     74     f = open(filename, "wb")
     75     f.write(config.SerializeToString())
     76     f.close()
     77 
     78 def read_and_print(filename):
     79     config = perfprofd_config_pb2.ProfilingConfig()
     80 
     81     f = open(filename, "rb")
     82     config.ParseFromString(f.read())
     83     f.close()
     84 
     85     print config
     86 
     87 if sys.argv[1] == 'read':
     88     read_and_print(sys.argv[2])
     89 elif sys.argv[1] == 'write':
     90     collect_and_write(sys.argv[2])
     91 else:
     92     print 'Usage: python perf_config_proto.py (read|write) filename'
     93