Debian Codegraph @

perfprofd/scripts/perf_config_proto.py

raw
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 #
23 # Note: it is necessary to do a '*' import to not have to jump through hoops
24 # with reflective instantiation.
25 from perfprofd_config_pb2 import *
26
27 # Necessary for introspection.
28 from google.protobuf.descriptor import FieldDescriptor
29
30 import sys
31
32 PROTO_FIELD_TYPE_NAMES = {
33 FieldDescriptor.TYPE_DOUBLE: "double",
34 FieldDescriptor.TYPE_FLOAT: "float",
35 FieldDescriptor.TYPE_INT64: "int64",
36 FieldDescriptor.TYPE_UINT64: "uint64",
37 FieldDescriptor.TYPE_INT32: "int32",
38 FieldDescriptor.TYPE_FIXED64: "fixed64",
39 FieldDescriptor.TYPE_FIXED32: "fixed32",
40 FieldDescriptor.TYPE_BOOL: "bool",
41 FieldDescriptor.TYPE_STRING: "string",
42 FieldDescriptor.TYPE_GROUP: "group",
43 FieldDescriptor.TYPE_MESSAGE: "message",
44 FieldDescriptor.TYPE_BYTES: "bytes",
45 FieldDescriptor.TYPE_UINT32: "uint32",
46 FieldDescriptor.TYPE_ENUM: "enum",
47 FieldDescriptor.TYPE_SFIXED32: "sfixed32",
48 FieldDescriptor.TYPE_SFIXED64: "sfixed64",
49 FieldDescriptor.TYPE_SINT32: "sint32",
50 FieldDescriptor.TYPE_SINT64: "sint64",
51 }
52 def get_type_string(proto_field_type):
53 if proto_field_type in PROTO_FIELD_TYPE_NAMES:
54 return PROTO_FIELD_TYPE_NAMES[proto_field_type]
55 return "unknown type"
56
57
58 def read_message(msg_descriptor, indent):
59 istr = ' ' * indent
60 print('%s%s' % (istr, msg_descriptor.name))
61 # Create an instance
62 instance = globals()[msg_descriptor.name]()
63
64 # Fill fields.
65
66 primitive_fields = [None]
67 message_fields = [None]
68 for field in msg_descriptor.fields:
69 if field.type == FieldDescriptor.TYPE_MESSAGE:
70 message_fields.append(field)
71 else:
72 primitive_fields.append(field)
73
74 def table_loop(fields, field_fn):
75 while True:
76 # Print selection table
77 maxlen = len(str(len(fields) - 1))
78 def pad_index(key):
79 while len(key) < maxlen:
80 key = ' ' + key
81 return key
82
83 for i in xrange(1, len(fields)):
84 print('%s%s: %s' % (istr, pad_index(str(i)), fields[i].name))
85 print('%s%s: done' % (istr, pad_index('0')))
86 print('%s%s: end' % (istr, pad_index('!')))
87
88 sel = raw_input('%s ? ' % (istr))
89 if sel == '!':
90 # Special-case input, end argument collection.
91 return False
92
93 try:
94 sel_int = int(sel)
95 if sel_int == 0:
96 return True
97
98 if sel_int > 0 and sel_int < len(fields):
99 field = fields[sel_int]
100 if not field_fn(field):
101 return False
102 else:
103 print('Not a valid input (%d)!' % (sel_int))
104 continue
105 except:
106 print('Not a valid input! (%s, %s)' % (sel, str(sys.exc_info()[0])))
107 continue
108
109 # # 1) Non-message-type fields.
110 if len(primitive_fields) > 1:
111 print('%s(Primitives)' % (istr))
112
113 def primitive_fn(field):
114 input = raw_input('%s -> %s (%s): ' % (istr, field.name, get_type_string(field.type)))
115 if input == '':
116 # Skip this field
117 return True
118 if input == '!':
119 # Special-case input, end argument collection.
120 return False
121
122 # Simplification: assume ints or bools or strings, but not floats
123 if field.type == FieldDescriptor.TYPE_BOOL:
124 input = input.lower()
125 set_val = True if input == 'y' or input == 'true' or input == '1' else False
126 elif field.type == FieldDescriptor.TYPE_STRING:
127 set_val = input
128 else:
129 try:
130 set_val = int(input)
131 except:
132 print('Could not parse input as integer!')
133 return True
134 if field.label == FieldDescriptor.LABEL_REPEATED:
135 getattr(instance, field.name).extend([set_val])
136 else:
137 setattr(instance, field.name, set_val)
138 return True
139
140 if not table_loop(primitive_fields, primitive_fn):
141 return (instance, False)
142
143 # 2) Message-type fields.
144 if len(message_fields) > 1:
145 print('%s(Nested messages)' % (istr))
146
147 def message_fn(field):
148 sub_msg, cont = read_message(field.message_type, indent + 4)
149 if sub_msg is not None:
150 if field.label == FieldDescriptor.LABEL_REPEATED:
151 # Repeated field, use extend.
152 getattr(instance, field.name).extend([sub_msg])
153 else:
154 # Singular field, copy into.
155 getattr(instance, field.name).CopyFrom(sub_msg)
156 return cont
157
158 if not table_loop(message_fields, message_fn):
159 return (instance, False)
160
161 return (instance, True)
162
163
164 def collect_and_write(filename):
165 config, _ = read_message(ProfilingConfig.DESCRIPTOR, 0)
166
167 if config is not None:
168 with open(filename, "wb") as f:
169 f.write(config.SerializeToString())
170
171
172 def read_and_print(filename):
173 config = ProfilingConfig()
174
175 with open(filename, "rb") as f:
176 config.ParseFromString(f.read())
177
178 print config
179
180
181 def print_usage():
182 print('Usage: python perf_config_proto.py (read|write) filename')
183
184
185 if __name__ == "__main__":
186 if len(sys.argv) < 3:
187 print_usage()
188 elif sys.argv[1] == 'read':
189 read_and_print(sys.argv[2])
190 elif sys.argv[1] == 'write':
191 collect_and_write(sys.argv[2])
192 else:
193 print_usage()