Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (C) 2018 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | from __future__ import absolute_import |
| 17 | from __future__ import division |
| 18 | from __future__ import print_function |
| 19 | import os |
| 20 | import re |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 21 | import sys |
| 22 | |
Isabelle Taylor | 80ec5f7 | 2019-02-27 11:27:41 +0000 | [diff] [blame] | 23 | COMMON_PROTOS = ( |
Primiano Tucci | 64df2ca | 2019-01-03 22:26:47 +0000 | [diff] [blame] | 24 | 'protos/perfetto/common/android_log_constants.proto', |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 25 | 'protos/perfetto/common/sys_stats_counters.proto', |
Isabelle Taylor | 80ec5f7 | 2019-02-27 11:27:41 +0000 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | CONFIG_PROTOS = ( |
Primiano Tucci | 64df2ca | 2019-01-03 22:26:47 +0000 | [diff] [blame] | 29 | 'protos/perfetto/config/android/android_log_config.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 30 | 'protos/perfetto/config/chrome/chrome_config.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 31 | 'protos/perfetto/config/data_source_config.proto', |
| 32 | 'protos/perfetto/config/ftrace/ftrace_config.proto', |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 33 | 'protos/perfetto/config/inode_file/inode_file_config.proto', |
Primiano Tucci | b86e9ca | 2018-12-03 20:20:11 +0100 | [diff] [blame] | 34 | 'protos/perfetto/config/power/android_power_config.proto', |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 35 | 'protos/perfetto/config/process_stats/process_stats_config.proto', |
| 36 | 'protos/perfetto/config/sys_stats/sys_stats_config.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 37 | 'protos/perfetto/config/test_config.proto', |
| 38 | 'protos/perfetto/config/trace_config.proto', |
Florian Mayer | 8e7eac4 | 2018-11-05 16:04:11 +0000 | [diff] [blame] | 39 | 'protos/perfetto/config/profiling/heapprofd_config.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | MERGED_CONFIG_PROTO = 'protos/perfetto/config/perfetto_config.proto' |
| 43 | |
| 44 | TRACE_PROTOS = ( |
Ryan Savitski | 41ce459 | 2019-05-02 14:43:59 +0100 | [diff] [blame] | 45 | 'protos/perfetto/common/trace_stats.proto', # only referenced by trace protos |
Primiano Tucci | 64df2ca | 2019-01-03 22:26:47 +0000 | [diff] [blame] | 46 | 'protos/perfetto/trace/android/android_log.proto', |
Florian Mayer | 12f0f8e | 2019-04-02 12:31:13 +0100 | [diff] [blame] | 47 | 'protos/perfetto/trace/android/packages_list.proto', |
Mikhail Khokhlov | 5b60af5 | 2019-05-15 10:09:34 +0100 | [diff] [blame] | 48 | 'protos/perfetto/trace/chrome/chrome_benchmark_metadata.proto', |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 49 | 'protos/perfetto/trace/clock_snapshot.proto', |
Hector Dearman | e92c674 | 2018-11-22 21:42:39 +0000 | [diff] [blame] | 50 | 'protos/perfetto/trace/filesystem/inode_file_map.proto', |
Hector Dearman | e0e5780 | 2018-11-21 16:09:56 +0000 | [diff] [blame] | 51 | 'protos/perfetto/trace/ftrace/binder.proto', |
| 52 | 'protos/perfetto/trace/ftrace/block.proto', |
| 53 | 'protos/perfetto/trace/ftrace/clk.proto', |
| 54 | 'protos/perfetto/trace/ftrace/ext4.proto', |
| 55 | 'protos/perfetto/trace/ftrace/f2fs.proto', |
| 56 | 'protos/perfetto/trace/ftrace/filemap.proto', |
| 57 | 'protos/perfetto/trace/ftrace/ftrace.proto', |
| 58 | 'protos/perfetto/trace/ftrace/ftrace_event.proto', |
| 59 | 'protos/perfetto/trace/ftrace/ftrace_event_bundle.proto', |
| 60 | 'protos/perfetto/trace/ftrace/ftrace_stats.proto', |
Hector Dearman | e92c674 | 2018-11-22 21:42:39 +0000 | [diff] [blame] | 61 | 'protos/perfetto/trace/ftrace/generic.proto', |
Hector Dearman | e0e5780 | 2018-11-21 16:09:56 +0000 | [diff] [blame] | 62 | 'protos/perfetto/trace/ftrace/kmem.proto', |
| 63 | 'protos/perfetto/trace/ftrace/lowmemorykiller.proto', |
Lalit Maganti | 3bd67ef | 2019-01-18 17:44:09 +0000 | [diff] [blame] | 64 | 'protos/perfetto/trace/ftrace/mm_event.proto', |
Hector Dearman | e0e5780 | 2018-11-21 16:09:56 +0000 | [diff] [blame] | 65 | 'protos/perfetto/trace/ftrace/power.proto', |
Lalit Maganti | a73a725 | 2019-01-25 10:32:40 +0000 | [diff] [blame] | 66 | 'protos/perfetto/trace/ftrace/raw_syscalls.proto', |
Hector Dearman | e0e5780 | 2018-11-21 16:09:56 +0000 | [diff] [blame] | 67 | 'protos/perfetto/trace/ftrace/sched.proto', |
| 68 | 'protos/perfetto/trace/ftrace/signal.proto', |
| 69 | 'protos/perfetto/trace/ftrace/task.proto', |
| 70 | 'protos/perfetto/trace/ftrace/vmscan.proto', |
Eric Seckler | c73ef06 | 2019-02-25 12:30:35 +0000 | [diff] [blame] | 71 | 'protos/perfetto/trace/interned_data/interned_data.proto', |
Primiano Tucci | b86e9ca | 2018-12-03 20:20:11 +0100 | [diff] [blame] | 72 | 'protos/perfetto/trace/power/battery_counters.proto', |
Esteban Talavera | 7714cec | 2019-01-31 15:53:34 +0000 | [diff] [blame] | 73 | 'protos/perfetto/trace/power/power_rails.proto', |
Hector Dearman | 685f752 | 2019-03-12 14:28:56 +0000 | [diff] [blame] | 74 | 'protos/perfetto/trace/profiling/profile_packet.proto', |
Hector Dearman | e92c674 | 2018-11-22 21:42:39 +0000 | [diff] [blame] | 75 | 'protos/perfetto/trace/ps/process_stats.proto', |
| 76 | 'protos/perfetto/trace/ps/process_tree.proto', |
| 77 | 'protos/perfetto/trace/sys_stats/sys_stats.proto', |
Hector Dearman | 685f752 | 2019-03-12 14:28:56 +0000 | [diff] [blame] | 78 | 'protos/perfetto/trace/system_info.proto', |
Hector Dearman | e92c674 | 2018-11-22 21:42:39 +0000 | [diff] [blame] | 79 | 'protos/perfetto/trace/trace.proto', |
| 80 | 'protos/perfetto/trace/trace_packet.proto', |
Eric Seckler | c73ef06 | 2019-02-25 12:30:35 +0000 | [diff] [blame] | 81 | 'protos/perfetto/trace/track_event/debug_annotation.proto', |
| 82 | 'protos/perfetto/trace/track_event/process_descriptor.proto', |
| 83 | 'protos/perfetto/trace/track_event/task_execution.proto', |
| 84 | 'protos/perfetto/trace/track_event/thread_descriptor.proto', |
| 85 | 'protos/perfetto/trace/track_event/track_event.proto', |
Stephen Nusko | 70ea330 | 2019-04-01 19:44:40 +0100 | [diff] [blame] | 86 | 'protos/perfetto/trace/trigger.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 87 | ) |
| 88 | |
| 89 | MERGED_TRACE_PROTO = 'protos/perfetto/trace/perfetto_trace.proto' |
| 90 | |
| 91 | REPLACEMENT_HEADER = ''' |
| 92 | // AUTOGENERATED - DO NOT EDIT |
| 93 | // --------------------------- |
| 94 | // This file has been generated by |
| 95 | // AOSP://external/perfetto/%s |
| 96 | // merging the perfetto config protos. |
| 97 | // This fused proto is intended to be copied in: |
| 98 | // - Android tree, for statsd. |
| 99 | // - Google internal repos. |
| 100 | |
| 101 | syntax = "proto2"; |
| 102 | |
| 103 | package perfetto.protos; |
| 104 | ''' |
| 105 | |
| 106 | def merge_protos(proto_paths, output_path): |
| 107 | root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 108 | merged_content = '' |
| 109 | for proto in proto_paths: |
| 110 | path = os.path.join(root_dir, proto) |
| 111 | with open(path) as f: |
| 112 | content = f.read() |
| 113 | |
| 114 | # Remove header |
Florian Mayer | 12f0f8e | 2019-04-02 12:31:13 +0100 | [diff] [blame] | 115 | header = re.match(r'\/(\*|\/)(?:.|\s)*?package .*;\n', content) |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 116 | header = header.group(0) |
| 117 | content = content[len(header):] |
| 118 | if merged_content == '': |
| 119 | merged_content += REPLACEMENT_HEADER.lstrip() % __file__ |
Isabelle Taylor | 80ec5f7 | 2019-02-27 11:27:41 +0000 | [diff] [blame] | 120 | content = re.sub(r'^import.*?\n\n?', '', content, flags=re.MULTILINE) |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 121 | merged_content += '\n// Begin of %s\n' % proto |
| 122 | merged_content += content |
| 123 | merged_content += '\n// End of %s\n' % proto |
| 124 | |
| 125 | definitions_re = r'^ *(?:message|enum) ([A-Z][A-Za-z0-9].*) {' |
| 126 | definitions = re.finditer(definitions_re, merged_content, re.MULTILINE) |
| 127 | types = set((match.group(1) for match in definitions)) |
| 128 | |
Eric Seckler | c73ef06 | 2019-02-25 12:30:35 +0000 | [diff] [blame] | 129 | uses_re = r'^( +)(?:repeated)?(?:optional)?\s'\ |
| 130 | r'?([A-Z]\w+.*)\s+[a-z]\w+\s*=\s*(\d+);' |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 131 | uses = re.finditer(uses_re, merged_content, re.MULTILINE) |
| 132 | substitutions = [] |
| 133 | for use in uses: |
| 134 | everything = use.group(0) |
| 135 | indentation = use.group(1) |
| 136 | used_type = use.group(2) |
| 137 | field_number = use.group(3) |
| 138 | if used_type not in types: |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 139 | replacement = '{}// removed field with id {}'.format( |
| 140 | indentation, field_number) |
| 141 | substitutions.append((everything, replacement)) |
| 142 | |
| 143 | for before, after in substitutions: |
| 144 | merged_content = merged_content.replace(before, after) |
| 145 | |
| 146 | out_path = os.path.join(root_dir, output_path) |
| 147 | |
| 148 | prev_content = None |
| 149 | if os.path.exists(out_path): |
| 150 | with open(out_path, 'rb') as fprev: |
| 151 | prev_content = fprev.read() |
| 152 | |
| 153 | if prev_content == merged_content: |
| 154 | return True |
| 155 | |
| 156 | if '--check-only' in sys.argv: |
| 157 | return False |
| 158 | |
| 159 | print('Updating {}'.format(output_path)) |
| 160 | with open(out_path, 'wb') as fout: |
| 161 | fout.write(merged_content) |
| 162 | return True |
| 163 | |
| 164 | def main(): |
Isabelle Taylor | 80ec5f7 | 2019-02-27 11:27:41 +0000 | [diff] [blame] | 165 | config_result = merge_protos(COMMON_PROTOS + CONFIG_PROTOS, |
| 166 | MERGED_CONFIG_PROTO) |
| 167 | trace_result = merge_protos(COMMON_PROTOS + TRACE_PROTOS + CONFIG_PROTOS, |
| 168 | MERGED_TRACE_PROTO) |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 169 | return 0 if config_result and trace_result else 1 |
| 170 | |
| 171 | if __name__ == '__main__': |
| 172 | sys.exit(main()) |