fixit: move proto_ring_buffer to protozero

This allows it to be used outside trace processor RPC usecase (e.g. in
trace_to_text).

Change-Id: I86030b817be18ab0379822d5e9ad13fa89603625
Bug: 148130323
diff --git a/Android.bp b/Android.bp
index 9b00686..d17a5a8 100644
--- a/Android.bp
+++ b/Android.bp
@@ -7494,6 +7494,14 @@
   ],
 }
 
+// GN: //src/protozero:proto_ring_buffer
+filegroup {
+  name: "perfetto_src_protozero_proto_ring_buffer",
+  srcs: [
+    "src/protozero/proto_ring_buffer.cc",
+  ],
+}
+
 // GN: //src/protozero/protoc_plugin:cppgen_plugin
 cc_binary_host {
   name: "perfetto_src_protozero_protoc_plugin_cppgen_plugin",
@@ -7717,6 +7725,7 @@
     "src/protozero/message_handle_unittest.cc",
     "src/protozero/message_unittest.cc",
     "src/protozero/proto_decoder_unittest.cc",
+    "src/protozero/proto_ring_buffer_unittest.cc",
     "src/protozero/proto_utils_unittest.cc",
     "src/protozero/scattered_stream_writer_unittest.cc",
     "src/protozero/test/cppgen_conformance_unittest.cc",
@@ -8056,7 +8065,6 @@
 filegroup {
   name: "perfetto_src_trace_processor_rpc_rpc",
   srcs: [
-    "src/trace_processor/rpc/proto_ring_buffer.cc",
     "src/trace_processor/rpc/query_result_serializer.cc",
     "src/trace_processor/rpc/rpc.cc",
   ],
@@ -8066,7 +8074,6 @@
 filegroup {
   name: "perfetto_src_trace_processor_rpc_unittests",
   srcs: [
-    "src/trace_processor/rpc/proto_ring_buffer_unittest.cc",
     "src/trace_processor/rpc/query_result_serializer_unittest.cc",
   ],
 }
@@ -9257,6 +9264,7 @@
     ":perfetto_src_protozero_filtering_filter_util",
     ":perfetto_src_protozero_filtering_message_filter",
     ":perfetto_src_protozero_filtering_unittests",
+    ":perfetto_src_protozero_proto_ring_buffer",
     ":perfetto_src_protozero_protozero",
     ":perfetto_src_protozero_testing_messages_cpp_gen",
     ":perfetto_src_protozero_testing_messages_lite_gen",
diff --git a/BUILD b/BUILD
index 69ab922..b8f2ba2 100644
--- a/BUILD
+++ b/BUILD
@@ -803,6 +803,15 @@
     ],
 )
 
+# GN target: //src/protozero:proto_ring_buffer
+filegroup(
+    name = "src_protozero_proto_ring_buffer",
+    srcs = [
+        "src/protozero/proto_ring_buffer.cc",
+        "src/protozero/proto_ring_buffer.h",
+    ],
+)
+
 # GN target: //src/trace_processor/analysis:analysis
 filegroup(
     name = "src_trace_processor_analysis_analysis",
@@ -1048,8 +1057,6 @@
 filegroup(
     name = "src_trace_processor_rpc_rpc",
     srcs = [
-        "src/trace_processor/rpc/proto_ring_buffer.cc",
-        "src/trace_processor/rpc/proto_ring_buffer.h",
         "src/trace_processor/rpc/query_result_serializer.cc",
         "src/trace_processor/rpc/query_result_serializer.h",
         "src/trace_processor/rpc/rpc.cc",
@@ -3644,6 +3651,7 @@
         ":src_profiling_deobfuscator",
         ":src_profiling_symbolizer_symbolize_database",
         ":src_profiling_symbolizer_symbolizer",
+        ":src_protozero_proto_ring_buffer",
         ":src_trace_processor_analysis_analysis",
         ":src_trace_processor_db_db",
         ":src_trace_processor_export_json",
diff --git a/src/protozero/BUILD.gn b/src/protozero/BUILD.gn
index 69d911a..b11a569 100644
--- a/src/protozero/BUILD.gn
+++ b/src/protozero/BUILD.gn
@@ -44,9 +44,22 @@
   ]
 }
 
+source_set("proto_ring_buffer") {
+  deps = [
+    ":protozero",
+    "../../gn:default_deps",
+    "../base",
+  ]
+  sources = [
+    "proto_ring_buffer.cc",
+    "proto_ring_buffer.h",
+  ]
+}
+
 perfetto_unittest_source_set("unittests") {
   testonly = true
   deps = [
+    ":proto_ring_buffer",
     ":protozero",
     ":testing_messages_cpp",
     ":testing_messages_lite",
@@ -62,6 +75,7 @@
     "message_handle_unittest.cc",
     "message_unittest.cc",
     "proto_decoder_unittest.cc",
+    "proto_ring_buffer_unittest.cc",
     "proto_utils_unittest.cc",
     "scattered_stream_writer_unittest.cc",
     "test/cppgen_conformance_unittest.cc",
diff --git a/src/trace_processor/rpc/proto_ring_buffer.cc b/src/protozero/proto_ring_buffer.cc
similarity index 95%
rename from src/trace_processor/rpc/proto_ring_buffer.cc
rename to src/protozero/proto_ring_buffer.cc
index 3354efe..92fe153 100644
--- a/src/trace_processor/rpc/proto_ring_buffer.cc
+++ b/src/protozero/proto_ring_buffer.cc
@@ -14,14 +14,13 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/rpc/proto_ring_buffer.h"
+#include "src/protozero/proto_ring_buffer.h"
 
 #include "perfetto/base/logging.h"
 #include "perfetto/ext/base/paged_memory.h"
 #include "perfetto/protozero/proto_utils.h"
 
-namespace perfetto {
-namespace trace_processor {
+namespace protozero {
 
 namespace {
 constexpr size_t kGrowBytes = 128 * 1024;
@@ -74,7 +73,7 @@
 }  // namespace
 
 ProtoRingBuffer::ProtoRingBuffer()
-    : buf_(base::PagedMemory::Allocate(kGrowBytes)) {}
+    : buf_(perfetto::base::PagedMemory::Allocate(kGrowBytes)) {}
 ProtoRingBuffer::~ProtoRingBuffer() = default;
 
 void ProtoRingBuffer::Append(const void* data_void, size_t data_len) {
@@ -137,7 +136,7 @@
         failed_ = true;
         return;
       }
-      auto new_buf = base::PagedMemory::Allocate(new_size);
+      auto new_buf = perfetto::base::PagedMemory::Allocate(new_size);
       memcpy(new_buf.Get(), buf_.Get(), buf_.size());
       buf_ = std::move(new_buf);
       avail = new_size - wr_;
@@ -184,5 +183,4 @@
   return msg;
 }
 
-}  // namespace trace_processor
-}  // namespace perfetto
+}  // namespace protozero
diff --git a/src/trace_processor/rpc/proto_ring_buffer.h b/src/protozero/proto_ring_buffer.h
similarity index 85%
rename from src/trace_processor/rpc/proto_ring_buffer.h
rename to src/protozero/proto_ring_buffer.h
index 62934ca..06ef539 100644
--- a/src/trace_processor/rpc/proto_ring_buffer.h
+++ b/src/protozero/proto_ring_buffer.h
@@ -14,28 +14,28 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_RPC_PROTO_RING_BUFFER_H_
-#define SRC_TRACE_PROCESSOR_RPC_PROTO_RING_BUFFER_H_
+#ifndef SRC_PROTOZERO_PROTO_RING_BUFFER_H_
+#define SRC_PROTOZERO_PROTO_RING_BUFFER_H_
 
 #include <stdint.h>
 
 #include "perfetto/ext/base/paged_memory.h"
 
-namespace perfetto {
-namespace trace_processor {
+namespace protozero {
 
-// This class buffers and tokenizes proto messages used for the TraceProcessor
-// RPC interface (See comments in trace_processor.proto).
-// From a logical level, the RPC is a sequence of protos like this.
+// This class buffers and tokenizes proto messages.
+//
+// From a logical level, it works with a sequence of protos like this.
 // [ header 1 ] [ payload 1   ]
 // [ header 2 ] [ payload 2  ]
 // [ header 3 ] [ payload 3     ]
 // Where [ header ] is a variable-length sequence of:
 // [ Field ID = 1, type = length-delimited] [ length (varint) ].
-// The RPC pipe is byte-oriented, not message-oriented (like a TCP stream).
-// The pipe is not required to respect the boundaries of each message, it only
-// guarantees that data is not lost or duplicated. The following sequence of
-// inbound events is possible:
+//
+// The input to this class is byte-oriented, not message-oriented (like a TCP
+// stream or a pipe). The caller is not required to respect the boundaries of
+// each message; only guarantee that data is not lost or duplicated. The
+// following sequence of inbound events is possible:
 // 1. [ hdr 1 (incomplete) ... ]
 // 2. [ ... hdr 1 ] [ payload 1 ] [ hdr 2 ] [ payoad 2 ] [ hdr 3 ] [ pay... ]
 // 3. [ ...load 3 ]
@@ -126,14 +126,13 @@
   size_t avail() const { return buf_.size() - (wr_ - rd_); }
 
  private:
-  base::PagedMemory buf_;
+  perfetto::base::PagedMemory buf_;
   Message fastpath_{};
   bool failed_ = false;  // Set in case of an unrecoverable framing faiulre.
   size_t rd_ = 0;        // Offset of the read cursor in |buf_|.
   size_t wr_ = 0;        // Offset of the write cursor in |buf_|.
 };
 
-}  // namespace trace_processor
-}  // namespace perfetto
+}  // namespace protozero
 
-#endif  // SRC_TRACE_PROCESSOR_RPC_PROTO_RING_BUFFER_H_
+#endif  // SRC_PROTOZERO_PROTO_RING_BUFFER_H_
diff --git a/src/trace_processor/rpc/proto_ring_buffer_unittest.cc b/src/protozero/proto_ring_buffer_unittest.cc
similarity index 95%
rename from src/trace_processor/rpc/proto_ring_buffer_unittest.cc
rename to src/protozero/proto_ring_buffer_unittest.cc
index 0e3944c..263baf3 100644
--- a/src/trace_processor/rpc/proto_ring_buffer_unittest.cc
+++ b/src/protozero/proto_ring_buffer_unittest.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/rpc/proto_ring_buffer.h"
+#include "src/protozero/proto_ring_buffer.h"
 
 #include <stdint.h>
 #include <sys/types.h>
@@ -30,8 +30,7 @@
 
 using testing::ElementsAre;
 
-namespace perfetto {
-namespace trace_processor {
+namespace protozero {
 
 // For ASSERT_EQ()
 inline bool operator==(const ProtoRingBuffer::Message& a,
@@ -58,6 +57,8 @@
 
 namespace {
 
+using ::perfetto::base::ArraySize;
+
 constexpr uint32_t kMaxMsgSize = ProtoRingBuffer::kMaxMsgSize;
 
 class ProtoRingBufferTest : public ::testing::Test {
@@ -137,14 +138,14 @@
 
   uint32_t frag_lens[] = {120, 20, 471, 1};
   uint32_t frag_sum = 0;
-  for (uint32_t i = 0; i < base::ArraySize(frag_lens); i++)
+  for (uint32_t i = 0; i < ArraySize(frag_lens); i++)
     frag_sum += frag_lens[i];
   ASSERT_EQ(frag_sum, last_msg_.size());
 
   // Append the messages in such a way that each appen either passes a portion
   // of a message (the 20 ones) or more than a message.
   uint32_t written = 0;
-  for (uint32_t i = 0; i < base::ArraySize(frag_lens); i++) {
+  for (uint32_t i = 0; i < ArraySize(frag_lens); i++) {
     buf.Append(&last_msg_[written], frag_lens[i]);
     written += frag_lens[i];
     for (;;) {
@@ -228,5 +229,4 @@
 }
 
 }  // namespace
-}  // namespace trace_processor
-}  // namespace perfetto
+}  // namespace protozero
diff --git a/src/trace_processor/rpc/BUILD.gn b/src/trace_processor/rpc/BUILD.gn
index 63cc752..09b9061 100644
--- a/src/trace_processor/rpc/BUILD.gn
+++ b/src/trace_processor/rpc/BUILD.gn
@@ -23,8 +23,6 @@
 # interface) and by the :httpd module for the HTTP interface.
 source_set("rpc") {
   sources = [
-    "proto_ring_buffer.cc",
-    "proto_ring_buffer.h",
     "query_result_serializer.cc",
     "query_result_serializer.h",
     "rpc.cc",
@@ -38,15 +36,13 @@
     "../../../protos/perfetto/trace_processor:zero",
     "../../base",
     "../../protozero",
+    "../../protozero:proto_ring_buffer",
   ]
 }
 
 perfetto_unittest_source_set("unittests") {
   testonly = true
-  sources = [
-    "proto_ring_buffer_unittest.cc",
-    "query_result_serializer_unittest.cc",
-  ]
+  sources = [ "query_result_serializer_unittest.cc" ]
   deps = [
     ":rpc",
     "..:lib",