blob: a8f9aa637370e3de175b9f64c1775a92f1815bb7 [file] [log] [blame]
#!/usr/bin/env python
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import re
import sys
from codecs import open
COMMON_PROTOS = (
'protos/perfetto/common/android_log_constants.proto',
'protos/perfetto/common/data_source_descriptor.proto',
'protos/perfetto/common/gpu_counter_descriptor.proto',
'protos/perfetto/common/sys_stats_counters.proto',
'protos/perfetto/common/trace_stats.proto',
'protos/perfetto/common/tracing_service_state.proto',
'protos/perfetto/common/track_event_descriptor.proto',
)
CONFIG_PROTOS = (
'protos/perfetto/config/android/android_log_config.proto',
'protos/perfetto/config/chrome/chrome_config.proto',
'protos/perfetto/config/data_source_config.proto',
'protos/perfetto/config/ftrace/ftrace_config.proto',
'protos/perfetto/config/inode_file/inode_file_config.proto',
'protos/perfetto/config/power/android_power_config.proto',
'protos/perfetto/config/process_stats/process_stats_config.proto',
'protos/perfetto/config/sys_stats/sys_stats_config.proto',
'protos/perfetto/config/test_config.proto',
'protos/perfetto/config/trace_config.proto',
'protos/perfetto/config/profiling/heapprofd_config.proto',
'protos/perfetto/config/profiling/java_hprof_config.proto',
'protos/perfetto/config/profiling/perf_event_config.proto',
'protos/perfetto/config/gpu/gpu_counter_config.proto',
'protos/perfetto/config/gpu/vulkan_memory_config.proto',
'protos/perfetto/config/android/packages_list_config.proto',
)
MERGED_CONFIG_PROTO = 'protos/perfetto/config/perfetto_config.proto'
TRACE_PROTOS = (
'protos/perfetto/trace/android/android_log.proto',
'protos/perfetto/trace/android/graphics_frame_event.proto',
'protos/perfetto/trace/android/packages_list.proto',
'protos/perfetto/trace/chrome/chrome_benchmark_metadata.proto',
"protos/perfetto/trace/chrome/chrome_metadata.proto",
'protos/perfetto/trace/clock_snapshot.proto',
'protos/perfetto/trace/filesystem/inode_file_map.proto',
'protos/perfetto/trace/ftrace/binder.proto',
'protos/perfetto/trace/ftrace/block.proto',
'protos/perfetto/trace/ftrace/clk.proto',
'protos/perfetto/trace/ftrace/ext4.proto',
'protos/perfetto/trace/ftrace/f2fs.proto',
'protos/perfetto/trace/ftrace/filemap.proto',
'protos/perfetto/trace/ftrace/ftrace.proto',
'protos/perfetto/trace/ftrace/ftrace_event.proto',
'protos/perfetto/trace/ftrace/ftrace_event_bundle.proto',
'protos/perfetto/trace/ftrace/ftrace_stats.proto',
'protos/perfetto/trace/ftrace/generic.proto',
'protos/perfetto/trace/ftrace/kmem.proto',
'protos/perfetto/trace/ftrace/lowmemorykiller.proto',
'protos/perfetto/trace/ftrace/mm_event.proto',
'protos/perfetto/trace/ftrace/power.proto',
'protos/perfetto/trace/ftrace/raw_syscalls.proto',
'protos/perfetto/trace/ftrace/sched.proto',
'protos/perfetto/trace/ftrace/signal.proto',
'protos/perfetto/trace/ftrace/systrace.proto',
'protos/perfetto/trace/ftrace/task.proto',
'protos/perfetto/trace/ftrace/vmscan.proto',
'protos/perfetto/trace/interned_data/interned_data.proto',
'protos/perfetto/trace/perfetto/perfetto_metatrace.proto',
'protos/perfetto/trace/power/battery_counters.proto',
'protos/perfetto/trace/power/power_rails.proto',
'protos/perfetto/trace/profiling/heap_graph.proto',
'protos/perfetto/trace/profiling/profile_common.proto',
'protos/perfetto/trace/profiling/profile_packet.proto',
'protos/perfetto/trace/ps/process_stats.proto',
'protos/perfetto/trace/ps/process_tree.proto',
'protos/perfetto/trace/sys_stats/sys_stats.proto',
'protos/perfetto/trace/system_info.proto',
'protos/perfetto/trace/trace.proto',
'protos/perfetto/trace/trace_packet.proto',
'protos/perfetto/trace/trace_packet_defaults.proto',
'protos/perfetto/trace/track_event/chrome_compositor_scheduler_state.proto',
'protos/perfetto/trace/track_event/chrome_histogram_sample.proto',
'protos/perfetto/trace/track_event/chrome_keyed_service.proto',
'protos/perfetto/trace/track_event/chrome_legacy_ipc.proto',
'protos/perfetto/trace/track_event/chrome_process_descriptor.proto',
'protos/perfetto/trace/track_event/chrome_thread_descriptor.proto',
'protos/perfetto/trace/track_event/chrome_user_event.proto',
'protos/perfetto/trace/track_event/debug_annotation.proto',
'protos/perfetto/trace/track_event/log_message.proto',
'protos/perfetto/trace/track_event/process_descriptor.proto',
'protos/perfetto/trace/track_event/source_location.proto',
'protos/perfetto/trace/track_event/task_execution.proto',
'protos/perfetto/trace/track_event/thread_descriptor.proto',
'protos/perfetto/trace/track_event/track_descriptor.proto',
'protos/perfetto/trace/track_event/track_event.proto',
'protos/perfetto/trace/trigger.proto',
'protos/perfetto/trace/gpu/gpu_counter_event.proto',
'protos/perfetto/trace/gpu/gpu_log.proto',
'protos/perfetto/trace/gpu/gpu_render_stage_event.proto',
'protos/perfetto/trace/gpu/vulkan_api_event.proto',
)
MERGED_TRACE_PROTO = 'protos/perfetto/trace/perfetto_trace.proto'
METRICS_PROTOS = (
'protos/perfetto/metrics/metrics.proto',
)
MERGED_METRICS_PROTO = 'protos/perfetto/metrics/perfetto_merged_metrics.proto'
REPLACEMENT_HEADER = '''
// AUTOGENERATED - DO NOT EDIT
// ---------------------------
// This file has been generated by
// AOSP://external/perfetto/%s
// merging the perfetto config protos.
// This fused proto is intended to be copied in:
// - Android tree, for statsd.
// - Google internal repos.
syntax = "proto2";
package perfetto.protos;
'''
def merge_protos_content(proto_paths, follow_imports, added_files, add_header):
root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
merged_content = ''
for proto in proto_paths:
if proto in added_files:
continue
added_files.add(proto)
path = os.path.join(root_dir, proto)
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
# Remove header
header = re.match(r'\/(\*|\/)(?:.|\s)*?package .*;\n', content)
header = header.group(0)
content = content[len(header):]
if merged_content == '' and add_header:
merged_content += REPLACEMENT_HEADER.lstrip() % __file__
if follow_imports:
matches = re.findall(r'^import "(.*)";\n\n?', content, flags=re.MULTILINE)
merged_content += merge_protos_content(
matches, follow_imports, added_files, add_header=False)
content = re.sub(r'^import.*?\n\n?', '', content, flags=re.MULTILINE)
merged_content += '\n// Begin of %s\n' % proto
merged_content += content
merged_content += '\n// End of %s\n' % proto
definitions_re = r'^ *(?:message|enum) ([A-Z][A-Za-z0-9].*) {'
definitions = re.finditer(definitions_re, merged_content, re.MULTILINE)
types = set((match.group(1) for match in definitions))
uses_re = r'^( +)(?:repeated)?(?:optional)?\s'\
r'?([A-Z]\w+.*)\s+[a-z]\w+\s*=\s*(\d+);'
uses = re.finditer(uses_re, merged_content, re.MULTILINE)
substitutions = []
for use in uses:
everything = use.group(0)
indentation = use.group(1)
used_type = use.group(2)
field_number = use.group(3)
if used_type not in types:
replacement = '{}// removed field with id {}'.format(
indentation, field_number)
substitutions.append((everything, replacement))
for before, after in substitutions:
merged_content = merged_content.replace(before, after)
return merged_content
def merge_protos(proto_paths, output_path, follow_imports):
root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
merged_content = merge_protos_content(
proto_paths, follow_imports, added_files=set(), add_header=True)
out_path = os.path.join(root_dir, output_path)
prev_content = None
if os.path.exists(out_path):
with open(out_path, 'r', encoding='utf-8') as fprev:
prev_content = fprev.read()
if prev_content == merged_content:
return True
if '--check-only' in sys.argv:
return False
print('Updating {}'.format(output_path))
with open(out_path, 'w', encoding='utf-8') as fout:
fout.write(merged_content)
return True
def main():
result = merge_protos(COMMON_PROTOS + CONFIG_PROTOS, MERGED_CONFIG_PROTO,
False)
result &= merge_protos(COMMON_PROTOS + TRACE_PROTOS + CONFIG_PROTOS,
MERGED_TRACE_PROTO, False)
result &= merge_protos(METRICS_PROTOS, MERGED_METRICS_PROTO, True)
return 0 if result else 1
if __name__ == '__main__':
sys.exit(main())