Add protoc_helper

Add a mechanism for turning human readable TraceConfig configs
into protobuf encoded versions.

Change-Id: I66419f074c1cd8f10583d20d12e75ea02f094c7e
diff --git a/BUILD.gn b/BUILD.gn
index e62e1ed..782f9a5 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -28,6 +28,7 @@
     ":perfetto_tests",
     "src/ipc/protoc_plugin:ipc_plugin($host_toolchain)",
     "src/protozero/protoc_plugin($host_toolchain)",
+    "tools:protoc_helper",
     "tools/ftrace_proto_gen:ftrace_proto_gen",
     "tools/proto_to_cpp",
     "tools/trace_to_text",
@@ -39,6 +40,7 @@
       ":traced",
       ":traced_probes",
       "src/ftrace_reader:ftrace_reader_integrationtests",
+      "test/configs",
     ]
   }
 }
diff --git a/test/configs/BUILD.gn b/test/configs/BUILD.gn
new file mode 100644
index 0000000..cef304c
--- /dev/null
+++ b/test/configs/BUILD.gn
@@ -0,0 +1,42 @@
+# 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.
+
+import("../../gn/perfetto.gni")
+
+if (!build_with_chromium) {
+  action_foreach("configs") {
+    testonly = true
+    script = "$root_out_dir/protoc_helper"
+
+    deps = [
+      "../../tools:protoc_helper",
+    ]
+
+    sources = [
+      "atrace.cfg",
+      "ftrace.cfg",
+    ]
+
+    outputs = [
+      "$root_out_dir/{{source_file_part}}.protobuf",
+    ]
+
+    args = [
+      "encode",
+      "--root=" + rebase_path(perfetto_root_path, ""),
+      "--input={{source}}",
+      "--output={{source_file_part}}.protobuf",
+    ]
+  }
+}
diff --git a/test/configs/README.md b/test/configs/README.md
new file mode 100644
index 0000000..b278377
--- /dev/null
+++ b/test/configs/README.md
@@ -0,0 +1,15 @@
+# Configs
+
+This directory contains a number of perfetto TraceConfigs in human readable
+form for easier testing. Each file is serialized into the protobuf format at
+build time.  For example the file `ftrace.cfg` is serialized to
+`out/some_gn_config/ftrace.cfg.protobuf`.
+
+## Example
+
+```bash
+$ adb push out/some_gn_config/ftrace.cfg.protobuf /data/local/tmp
+$ adb push out/some_gn_config/perfetto /data/local/tmp
+$ adb shell 'cd /data/local/tmp && ./perfetto -c ftrace_cfg.protobuf -o out.protobuf'
+```
+
diff --git a/test/configs/atrace.cfg b/test/configs/atrace.cfg
new file mode 100644
index 0000000..b0275e7
--- /dev/null
+++ b/test/configs/atrace.cfg
@@ -0,0 +1,18 @@
+buffers {
+  size_kb: 100024
+  optimize_for: ONE_SHOT_READ
+  fill_policy: RING_BUFFER
+}
+
+# TODO(hjd): Add some atrace config here.
+data_sources {
+  config {
+    name: "com.google.perfetto.ftrace"
+    target_buffer: 0
+    ftrace_config {
+      event_names: "sched_switch"
+    }
+  }
+}
+
+duration_ms: 10000
diff --git a/test/configs/ftrace.cfg b/test/configs/ftrace.cfg
new file mode 100644
index 0000000..6416643
--- /dev/null
+++ b/test/configs/ftrace.cfg
@@ -0,0 +1,17 @@
+buffers {
+  size_kb: 100024
+  optimize_for: ONE_SHOT_READ
+  fill_policy: RING_BUFFER
+}
+
+data_sources {
+  config {
+    name: "com.google.perfetto.ftrace"
+    target_buffer: 0
+    ftrace_config {
+      event_names: "sched_switch"
+    }
+  }
+}
+
+duration_ms: 10000
diff --git a/tools/BUILD.gn b/tools/BUILD.gn
new file mode 100644
index 0000000..d5b6a98
--- /dev/null
+++ b/tools/BUILD.gn
@@ -0,0 +1,33 @@
+# 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.
+
+copy("copy_protoc_helper") {
+  testonly = true
+  sources = [
+    "protoc_helper.py",
+  ]
+  outputs = [
+    "${root_out_dir}/protoc_helper",
+  ]
+}
+
+group("protoc_helper") {
+  testonly = true
+  public_deps = [
+    ":copy_protoc_helper",
+  ]
+  deps = [
+    "../buildtools:protoc($host_toolchain)",
+  ]
+}
diff --git a/tools/protoc_helper.py b/tools/protoc_helper.py
new file mode 100755
index 0000000..efeb274
--- /dev/null
+++ b/tools/protoc_helper.py
@@ -0,0 +1,67 @@
+#!/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.
+
+import argparse
+import os
+import subprocess
+import sys
+
+
+def run(encode_or_decode, protoc_path, proto, root, input, output):
+  cmd = [
+      protoc_path,
+      '--{}=perfetto.protos.{}'.format(encode_or_decode, proto),
+      os.path.join(root, 'protos/tracing_service/trace_config.proto'),
+      os.path.join(root, 'protos/trace.proto'),
+      '--proto_path={}'.format(root),
+  ]
+  subprocess.check_call(cmd, stdin=input, stdout=output, stderr=sys.stderr)
+
+
+def main():
+  parser = argparse.ArgumentParser()
+  parser.add_argument(
+      'encode_or_decode',
+      choices=['encode', 'decode'],
+      help='encode into binary format or decode to text.'
+  )
+  parser.add_argument('--proto_name', default='TraceConfig',
+      help='name of proto to encode/decode (default: TraceConfig).')
+  parser.add_argument('--protoc_path', default=None,
+      help='path to protoc')
+  parser.add_argument('--root', default='.',
+      help='root directory (default: ".")')
+  parser.add_argument('--input', default='-',
+      help='input file, or "-" for stdin (default: "-")')
+  parser.add_argument('--output', default='-',
+      help='output file, or "-" for stdout (default: "-")')
+  args = parser.parse_args()
+
+  encode_or_decode = args.encode_or_decode
+  proto_name = args.proto_name
+  protoc_path =  args.protoc_path
+  root =  args.root
+  input = sys.stdin if args.input == '-' else open(args.input, 'rb')
+  output = sys.stdout if args.output == '-' else open(args.output, 'wb')
+  if not protoc_path:
+    directory = os.path.dirname(__file__)
+    protoc_path = os.path.join(directory, 'gcc_like_host', 'protoc')
+  run(encode_or_decode, protoc_path, proto_name, root, input, output)
+  return 0
+
+
+if __name__ == '__main__':
+  sys.exit(main())
+