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 |
| 21 | import subprocess |
| 22 | import sys |
| 23 | |
| 24 | CONFIG_PROTOS = ( |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 25 | 'protos/perfetto/common/sys_stats_counters.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 26 | 'protos/perfetto/config/chrome/chrome_config.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 27 | 'protos/perfetto/config/data_source_config.proto', |
| 28 | 'protos/perfetto/config/ftrace/ftrace_config.proto', |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 29 | 'protos/perfetto/config/inode_file/inode_file_config.proto', |
| 30 | 'protos/perfetto/config/process_stats/process_stats_config.proto', |
| 31 | 'protos/perfetto/config/sys_stats/sys_stats_config.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 32 | 'protos/perfetto/config/test_config.proto', |
| 33 | 'protos/perfetto/config/trace_config.proto', |
| 34 | ) |
| 35 | |
| 36 | MERGED_CONFIG_PROTO = 'protos/perfetto/config/perfetto_config.proto' |
| 37 | |
| 38 | TRACE_PROTOS = ( |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 39 | 'protos/perfetto/common/sys_stats_counters.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 40 | 'protos/perfetto/trace/trace.proto', |
| 41 | 'protos/perfetto/trace/trace_packet.proto', |
| 42 | 'protos/perfetto/trace/ftrace/ftrace_event_bundle.proto', |
| 43 | 'protos/perfetto/trace/ftrace/ftrace_event.proto', |
Florian Mayer | beba726 | 2018-04-17 19:05:43 +0100 | [diff] [blame] | 44 | 'protos/perfetto/trace/filesystem/inode_file_map.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 45 | 'protos/perfetto/trace/ps/process_tree.proto', |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 46 | 'protos/perfetto/trace/clock_snapshot.proto', |
| 47 | 'protos/perfetto/trace/sys_stats/sys_stats.proto', |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 48 | |
Hector Dearman | d046598 | 2018-09-11 19:44:02 +0100 | [diff] [blame] | 49 | # Print proto is special: it doesn't have a enable file so is |
| 50 | # not present in genfs_contexts. |
| 51 | 'protos/perfetto/trace/ftrace/print.proto', |
| 52 | |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 53 | # FTrace events |
| 54 | # Generated by tools/protos_from_genfs_contexts.sh. |
| 55 | # MISSING cpufreq_interactive_load_change |
| 56 | 'protos/perfetto/trace/ftrace/binder_locked.proto', |
| 57 | 'protos/perfetto/trace/ftrace/binder_lock.proto', |
| 58 | 'protos/perfetto/trace/ftrace/binder_transaction.proto', |
| 59 | 'protos/perfetto/trace/ftrace/binder_transaction_received.proto', |
| 60 | 'protos/perfetto/trace/ftrace/binder_unlock.proto', |
| 61 | 'protos/perfetto/trace/ftrace/block_rq_complete.proto', |
| 62 | 'protos/perfetto/trace/ftrace/block_rq_issue.proto', |
| 63 | 'protos/perfetto/trace/ftrace/cgroup_attach_task.proto', |
| 64 | 'protos/perfetto/trace/ftrace/cgroup_destroy_root.proto', |
| 65 | 'protos/perfetto/trace/ftrace/cgroup_mkdir.proto', |
| 66 | 'protos/perfetto/trace/ftrace/cgroup_release.proto', |
| 67 | 'protos/perfetto/trace/ftrace/cgroup_remount.proto', |
| 68 | 'protos/perfetto/trace/ftrace/cgroup_rename.proto', |
| 69 | 'protos/perfetto/trace/ftrace/cgroup_rmdir.proto', |
| 70 | 'protos/perfetto/trace/ftrace/cgroup_setup_root.proto', |
| 71 | 'protos/perfetto/trace/ftrace/cgroup_transfer_tasks.proto', |
| 72 | 'protos/perfetto/trace/ftrace/clock_set_rate.proto', |
| 73 | 'protos/perfetto/trace/ftrace/cpufreq_interactive_already.proto', |
| 74 | 'protos/perfetto/trace/ftrace/cpufreq_interactive_boost.proto', |
| 75 | 'protos/perfetto/trace/ftrace/cpufreq_interactive_notyet.proto', |
| 76 | 'protos/perfetto/trace/ftrace/cpufreq_interactive_setspeed.proto', |
| 77 | 'protos/perfetto/trace/ftrace/cpufreq_interactive_target.proto', |
| 78 | 'protos/perfetto/trace/ftrace/cpufreq_interactive_unboost.proto', |
| 79 | 'protos/perfetto/trace/ftrace/cpu_frequency_limits.proto', |
| 80 | 'protos/perfetto/trace/ftrace/cpu_frequency.proto', |
| 81 | 'protos/perfetto/trace/ftrace/cpu_idle.proto', |
| 82 | 'protos/perfetto/trace/ftrace/ext4_da_write_begin.proto', |
| 83 | 'protos/perfetto/trace/ftrace/ext4_da_write_end.proto', |
| 84 | 'protos/perfetto/trace/ftrace/ext4_es_lookup_extent_enter.proto', |
| 85 | 'protos/perfetto/trace/ftrace/ext4_es_lookup_extent_exit.proto', |
| 86 | 'protos/perfetto/trace/ftrace/ext4_load_inode.proto', |
| 87 | 'protos/perfetto/trace/ftrace/ext4_sync_file_enter.proto', |
| 88 | 'protos/perfetto/trace/ftrace/ext4_sync_file_exit.proto', |
| 89 | 'protos/perfetto/trace/ftrace/f2fs_get_data_block.proto', |
| 90 | 'protos/perfetto/trace/ftrace/f2fs_iget.proto', |
| 91 | 'protos/perfetto/trace/ftrace/f2fs_sync_file_enter.proto', |
| 92 | 'protos/perfetto/trace/ftrace/f2fs_sync_file_exit.proto', |
| 93 | 'protos/perfetto/trace/ftrace/f2fs_write_begin.proto', |
| 94 | 'protos/perfetto/trace/ftrace/f2fs_write_end.proto', |
| 95 | 'protos/perfetto/trace/ftrace/lowmemory_kill.proto', |
| 96 | 'protos/perfetto/trace/ftrace/mm_vmscan_direct_reclaim_begin.proto', |
| 97 | 'protos/perfetto/trace/ftrace/mm_vmscan_direct_reclaim_end.proto', |
| 98 | 'protos/perfetto/trace/ftrace/mm_vmscan_kswapd_sleep.proto', |
| 99 | 'protos/perfetto/trace/ftrace/mm_vmscan_kswapd_wake.proto', |
| 100 | 'protos/perfetto/trace/ftrace/sched_blocked_reason.proto', |
| 101 | 'protos/perfetto/trace/ftrace/sched_cpu_hotplug.proto', |
| 102 | 'protos/perfetto/trace/ftrace/sched_switch.proto', |
| 103 | 'protos/perfetto/trace/ftrace/sched_wakeup.proto', |
| 104 | ) |
| 105 | |
| 106 | MERGED_TRACE_PROTO = 'protos/perfetto/trace/perfetto_trace.proto' |
| 107 | |
| 108 | REPLACEMENT_HEADER = ''' |
| 109 | // AUTOGENERATED - DO NOT EDIT |
| 110 | // --------------------------- |
| 111 | // This file has been generated by |
| 112 | // AOSP://external/perfetto/%s |
| 113 | // merging the perfetto config protos. |
| 114 | // This fused proto is intended to be copied in: |
| 115 | // - Android tree, for statsd. |
| 116 | // - Google internal repos. |
| 117 | |
| 118 | syntax = "proto2"; |
| 119 | |
| 120 | package perfetto.protos; |
| 121 | ''' |
| 122 | |
| 123 | def merge_protos(proto_paths, output_path): |
| 124 | root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 125 | merged_content = '' |
| 126 | for proto in proto_paths: |
| 127 | path = os.path.join(root_dir, proto) |
| 128 | with open(path) as f: |
| 129 | content = f.read() |
| 130 | |
| 131 | # Remove header |
| 132 | header = re.match(r'\/(\*|\/)(?:.|\s)*?package.*;\n', content) |
| 133 | header = header.group(0) |
| 134 | content = content[len(header):] |
| 135 | if merged_content == '': |
| 136 | merged_content += REPLACEMENT_HEADER.lstrip() % __file__ |
| 137 | content = re.sub(r'^import.*?\n', '', content, flags=re.MULTILINE) |
| 138 | content = re.sub(r'TODO\([^)]*\):', 'TODO:', content) |
| 139 | merged_content += '\n// Begin of %s\n' % proto |
| 140 | merged_content += content |
| 141 | merged_content += '\n// End of %s\n' % proto |
| 142 | |
| 143 | definitions_re = r'^ *(?:message|enum) ([A-Z][A-Za-z0-9].*) {' |
| 144 | definitions = re.finditer(definitions_re, merged_content, re.MULTILINE) |
| 145 | types = set((match.group(1) for match in definitions)) |
| 146 | |
| 147 | uses_re = r'^( +)(?:repeated)?\s?([A-Z]\w+.*)\s+[a-z]\w+\s*=\s*(\d+);' |
| 148 | uses = re.finditer(uses_re, merged_content, re.MULTILINE) |
| 149 | substitutions = [] |
| 150 | for use in uses: |
| 151 | everything = use.group(0) |
| 152 | indentation = use.group(1) |
| 153 | used_type = use.group(2) |
| 154 | field_number = use.group(3) |
| 155 | if used_type not in types: |
| 156 | # replacement = '{}reserved {};'.format(indentation, field_number) |
| 157 | replacement = '{}// removed field with id {}'.format( |
| 158 | indentation, field_number) |
| 159 | substitutions.append((everything, replacement)) |
| 160 | |
| 161 | for before, after in substitutions: |
| 162 | merged_content = merged_content.replace(before, after) |
| 163 | |
| 164 | out_path = os.path.join(root_dir, output_path) |
| 165 | |
| 166 | prev_content = None |
| 167 | if os.path.exists(out_path): |
| 168 | with open(out_path, 'rb') as fprev: |
| 169 | prev_content = fprev.read() |
| 170 | |
| 171 | if prev_content == merged_content: |
| 172 | return True |
| 173 | |
| 174 | if '--check-only' in sys.argv: |
| 175 | return False |
| 176 | |
| 177 | print('Updating {}'.format(output_path)) |
| 178 | with open(out_path, 'wb') as fout: |
| 179 | fout.write(merged_content) |
| 180 | return True |
| 181 | |
| 182 | def main(): |
| 183 | config_result = merge_protos(CONFIG_PROTOS, MERGED_CONFIG_PROTO) |
| 184 | trace_result = merge_protos(TRACE_PROTOS, MERGED_TRACE_PROTO) |
| 185 | return 0 if config_result and trace_result else 1 |
| 186 | |
| 187 | if __name__ == '__main__': |
| 188 | sys.exit(main()) |