Rename Chunk to Slice to avoid ambiguity

Non functional refactor. The term "Chunk" ended up being overloaded:
1) Chunk as fraction of a page in the SharedMemoryABI
2) Chunk as a virtually contiguous fraction of a packet

Renaming 2 to Slice

Change-Id: I2d283748e566839c4adcdb987282a811c06ed226
diff --git a/Android.bp b/Android.bp
index 0f910c6..84c80ea 100644
--- a/Android.bp
+++ b/Android.bp
@@ -54,7 +54,6 @@
     "src/traced/probes/probes.cc",
     "src/traced/probes/probes_producer.cc",
     "src/traced/service/service.cc",
-    "src/tracing/core/chunked_protobuf_input_stream.cc",
     "src/tracing/core/data_source_config.cc",
     "src/tracing/core/data_source_descriptor.cc",
     "src/tracing/core/id_allocator.cc",
@@ -62,6 +61,7 @@
     "src/tracing/core/service_impl.cc",
     "src/tracing/core/shared_memory_abi.cc",
     "src/tracing/core/shared_memory_arbiter_impl.cc",
+    "src/tracing/core/sliced_protobuf_input_stream.cc",
     "src/tracing/core/trace_config.cc",
     "src/tracing/core/trace_packet.cc",
     "src/tracing/core/trace_writer_impl.cc",
@@ -124,7 +124,6 @@
     "src/protozero/scattered_stream_writer.cc",
     "src/traced/perfetto_cmd/main.cc",
     "src/traced/perfetto_cmd/perfetto_cmd.cc",
-    "src/tracing/core/chunked_protobuf_input_stream.cc",
     "src/tracing/core/data_source_config.cc",
     "src/tracing/core/data_source_descriptor.cc",
     "src/tracing/core/id_allocator.cc",
@@ -132,6 +131,7 @@
     "src/tracing/core/service_impl.cc",
     "src/tracing/core/shared_memory_abi.cc",
     "src/tracing/core/shared_memory_arbiter_impl.cc",
+    "src/tracing/core/sliced_protobuf_input_stream.cc",
     "src/tracing/core/trace_config.cc",
     "src/tracing/core/trace_packet.cc",
     "src/tracing/core/trace_writer_impl.cc",
@@ -236,7 +236,6 @@
     "src/protozero/proto_utils.cc",
     "src/protozero/scattered_stream_writer.cc",
     "src/traced/probes/probes_producer.cc",
-    "src/tracing/core/chunked_protobuf_input_stream.cc",
     "src/tracing/core/data_source_config.cc",
     "src/tracing/core/data_source_descriptor.cc",
     "src/tracing/core/id_allocator.cc",
@@ -244,6 +243,7 @@
     "src/tracing/core/service_impl.cc",
     "src/tracing/core/shared_memory_abi.cc",
     "src/tracing/core/shared_memory_arbiter_impl.cc",
+    "src/tracing/core/sliced_protobuf_input_stream.cc",
     "src/tracing/core/trace_config.cc",
     "src/tracing/core/trace_packet.cc",
     "src/tracing/core/trace_writer_impl.cc",
@@ -1819,7 +1819,6 @@
     "src/protozero/message_handle.cc",
     "src/protozero/proto_utils.cc",
     "src/protozero/scattered_stream_writer.cc",
-    "src/tracing/core/chunked_protobuf_input_stream.cc",
     "src/tracing/core/data_source_config.cc",
     "src/tracing/core/data_source_descriptor.cc",
     "src/tracing/core/id_allocator.cc",
@@ -1827,6 +1826,7 @@
     "src/tracing/core/service_impl.cc",
     "src/tracing/core/shared_memory_abi.cc",
     "src/tracing/core/shared_memory_arbiter_impl.cc",
+    "src/tracing/core/sliced_protobuf_input_stream.cc",
     "src/tracing/core/trace_config.cc",
     "src/tracing/core/trace_packet.cc",
     "src/tracing/core/trace_writer_impl.cc",
@@ -1949,8 +1949,6 @@
     "src/protozero/scattered_stream_writer_unittest.cc",
     "src/protozero/test/fake_scattered_buffer.cc",
     "src/protozero/test/protozero_conformance_unittest.cc",
-    "src/tracing/core/chunked_protobuf_input_stream.cc",
-    "src/tracing/core/chunked_protobuf_input_stream_unittest.cc",
     "src/tracing/core/data_source_config.cc",
     "src/tracing/core/data_source_descriptor.cc",
     "src/tracing/core/id_allocator.cc",
@@ -1963,6 +1961,8 @@
     "src/tracing/core/shared_memory_abi_unittest.cc",
     "src/tracing/core/shared_memory_arbiter_impl.cc",
     "src/tracing/core/shared_memory_arbiter_impl_unittest.cc",
+    "src/tracing/core/sliced_protobuf_input_stream.cc",
+    "src/tracing/core/sliced_protobuf_input_stream_unittest.cc",
     "src/tracing/core/trace_config.cc",
     "src/tracing/core/trace_packet.cc",
     "src/tracing/core/trace_packet_unittest.cc",
diff --git a/include/perfetto/tracing/core/BUILD.gn b/include/perfetto/tracing/core/BUILD.gn
index 134fa0a..7e39163 100644
--- a/include/perfetto/tracing/core/BUILD.gn
+++ b/include/perfetto/tracing/core/BUILD.gn
@@ -18,7 +18,6 @@
   ]
   sources = [
     "basic_types.h",
-    "chunk.h",
     "consumer.h",
     "data_source_config.h",
     "data_source_descriptor.h",
@@ -27,6 +26,7 @@
     "shared_memory.h",
     "shared_memory_abi.h",
     "shared_memory_arbiter.h",
+    "slice.h",
     "trace_config.h",
     "trace_packet.h",
     "trace_writer.h",
diff --git a/include/perfetto/tracing/core/chunk.h b/include/perfetto/tracing/core/slice.h
similarity index 64%
rename from include/perfetto/tracing/core/chunk.h
rename to include/perfetto/tracing/core/slice.h
index 77e6d2a..416d2ad 100644
--- a/include/perfetto/tracing/core/chunk.h
+++ b/include/perfetto/tracing/core/slice.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef INCLUDE_PERFETTO_TRACING_CORE_CHUNK_H_
-#define INCLUDE_PERFETTO_TRACING_CORE_CHUNK_H_
+#ifndef INCLUDE_PERFETTO_TRACING_CORE_SLICE_H_
+#define INCLUDE_PERFETTO_TRACING_CORE_SLICE_H_
 
 #include <stddef.h>
 #include <string.h>
@@ -27,14 +27,14 @@
 
 // A simple wrapper around a virtually contiguous memory range that contains a
 // TracePacket, or just a portion of it.
-struct Chunk {
-  Chunk() : start(nullptr), size(0) {}
-  Chunk(const void* st, size_t sz) : start(st), size(sz) {}
-  Chunk(Chunk&& other) noexcept = default;
+struct Slice {
+  Slice() : start(nullptr), size(0) {}
+  Slice(const void* st, size_t sz) : start(st), size(sz) {}
+  Slice(Slice&& other) noexcept = default;
 
-  // Create a Chunk which contains (and owns) a copy of the given memory.
-  static Chunk Copy(const void* start, size_t size) {
-    Chunk c;
+  // Create a Slice which contains (and owns) a copy of the given memory.
+  static Slice Copy(const void* start, size_t size) {
+    Slice c;
     c.own_data_.reset(new uint8_t[size]);
     c.size = size;
     c.start = &c.own_data_[0];
@@ -46,17 +46,17 @@
   size_t size;
 
  private:
-  Chunk(const Chunk&) = delete;
-  void operator=(const Chunk&) = delete;
+  Slice(const Slice&) = delete;
+  void operator=(const Slice&) = delete;
 
   std::unique_ptr<uint8_t[]> own_data_;
 };
 
-// TODO(primiano): most TracePacket(s) fit in a chunk or two. We need something
-// a bit more clever here that has inline capacity for 2 chunks and then uses a
+// TODO(primiano): most TracePacket(s) fit in a slice or two. We need something
+// a bit more clever here that has inline capacity for 2 slices and then uses a
 // std::forward_list or a std::vector for the less likely cases.
-using ChunkSequence = std::vector<Chunk>;
+using Slices = std::vector<Slice>;
 
 }  // namespace perfetto
 
-#endif  // INCLUDE_PERFETTO_TRACING_CORE_CHUNK_H_
+#endif  // INCLUDE_PERFETTO_TRACING_CORE_SLICE_H_
diff --git a/include/perfetto/tracing/core/trace_packet.h b/include/perfetto/tracing/core/trace_packet.h
index f1c703b..1491807 100644
--- a/include/perfetto/tracing/core/trace_packet.h
+++ b/include/perfetto/tracing/core/trace_packet.h
@@ -22,7 +22,7 @@
 #include <memory>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/tracing/core/chunk.h"
+#include "perfetto/tracing/core/slice.h"
 
 class TracePacket;
 
@@ -42,7 +42,7 @@
 // fact, might have an older version .proto which is newer on the producer.
 class TracePacket {
  public:
-  using const_iterator = ChunkSequence::const_iterator;
+  using const_iterator = Slices::const_iterator;
 
   using DecodedTracePacket = protos::TracePacket;
   TracePacket();
@@ -50,9 +50,9 @@
   TracePacket(TracePacket&&) noexcept;
   TracePacket& operator=(TracePacket&&);
 
-  // Accesses all the raw chunks in the packet, for saving them to file/network.
-  const_iterator begin() const { return chunks_.begin(); }
-  const_iterator end() const { return chunks_.end(); }
+  // Accesses all the raw slices in the packet, for saving them to file/network.
+  const_iterator begin() const { return slices_.begin(); }
+  const_iterator end() const { return slices_.end(); }
 
   // Decodes the packet for inline use.
   bool Decode();
@@ -65,19 +65,19 @@
   const DecodedTracePacket& operator*() { return *(operator->()); }
 
   // Mutator, used only by the service and tests.
-  void AddChunk(Chunk);
+  void AddSlice(Slice);
 
-  // Total size of all chunks.
+  // Total size of all slices.
   size_t size() const { return size_; }
 
  private:
   TracePacket(const TracePacket&) = delete;
   TracePacket& operator=(const TracePacket&) = delete;
 
-  // TODO(primiano): who owns the memory of the chunks? Figure out later.
+  // TODO(primiano): who owns the memory of the slices? Figure out later.
 
-  ChunkSequence chunks_;  // Not owned.
-  size_t size_ = 0;       // SUM(chunk.size for chunk in chunks_).
+  Slices slices_;    // Not owned.
+  size_t size_ = 0;  // SUM(slice.size for slice in slices_).
   std::unique_ptr<DecodedTracePacket> decoded_packet_;
 };
 
diff --git a/src/traced/perfetto_cmd/perfetto_cmd.cc b/src/traced/perfetto_cmd/perfetto_cmd.cc
index 8c40546..82c0f0b 100644
--- a/src/traced/perfetto_cmd/perfetto_cmd.cc
+++ b/src/traced/perfetto_cmd/perfetto_cmd.cc
@@ -52,7 +52,7 @@
 #endif  // defined(PERFETTO_BUILD_WITH_ANDROID)
 
 // TODO(primiano): add the ability to pass the file descriptor directly to the
-// traced service instead of receiving a copy of the chunks and writing them
+// traced service instead of receiving a copy of the slices and writing them
 // from this process.
 namespace perfetto {
 namespace {
@@ -266,15 +266,15 @@
 void PerfettoCmd::OnTraceData(std::vector<TracePacket> packets, bool has_more) {
   PERFETTO_DLOG("Received trace packet, has_more=%d", has_more);
   for (TracePacket& packet : packets) {
-    for (const Chunk& chunk : packet) {
+    for (const Slice& slice : packet) {
       uint8_t preamble[16];
       uint8_t* pos = preamble;
       pos = WriteVarInt(
           MakeTagLengthDelimited(protos::Trace::kPacketFieldNumber), pos);
-      pos = WriteVarInt(static_cast<uint32_t>(chunk.size), pos);
+      pos = WriteVarInt(static_cast<uint32_t>(slice.size), pos);
       fwrite(reinterpret_cast<const char*>(preamble), pos - preamble, 1,
              trace_out_stream_.get());
-      fwrite(reinterpret_cast<const char*>(chunk.start), chunk.size, 1,
+      fwrite(reinterpret_cast<const char*>(slice.start), slice.size, 1,
              trace_out_stream_.get());
     }
   }
diff --git a/src/tracing/BUILD.gn b/src/tracing/BUILD.gn
index 3811987..e56c7ea 100644
--- a/src/tracing/BUILD.gn
+++ b/src/tracing/BUILD.gn
@@ -29,8 +29,6 @@
     "../base",
   ]
   sources = [
-    "core/chunked_protobuf_input_stream.cc",
-    "core/chunked_protobuf_input_stream.h",
     "core/data_source_config.cc",
     "core/data_source_descriptor.cc",
     "core/id_allocator.cc",
@@ -42,6 +40,8 @@
     "core/shared_memory_abi.cc",
     "core/shared_memory_arbiter_impl.cc",
     "core/shared_memory_arbiter_impl.h",
+    "core/sliced_protobuf_input_stream.cc",
+    "core/sliced_protobuf_input_stream.h",
     "core/trace_config.cc",
     "core/trace_packet.cc",
     "core/trace_writer_impl.cc",
@@ -113,12 +113,12 @@
     "../base:test_support",
   ]
   sources = [
-    "core/chunked_protobuf_input_stream_unittest.cc",
     "core/id_allocator_unittest.cc",
     "core/packet_stream_validator_unittest.cc",
     "core/service_impl_unittest.cc",
     "core/shared_memory_abi_unittest.cc",
     "core/shared_memory_arbiter_impl_unittest.cc",
+    "core/sliced_protobuf_input_stream_unittest.cc",
     "core/trace_packet_unittest.cc",
     "core/trace_writer_impl_unittest.cc",
     "ipc/posix_shared_memory_unittest.cc",
diff --git a/src/tracing/core/chunked_protobuf_input_stream.cc b/src/tracing/core/chunked_protobuf_input_stream.cc
deleted file mode 100644
index 6415e17..0000000
--- a/src/tracing/core/chunked_protobuf_input_stream.cc
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright (C) 2017 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.
- */
-
-#include "src/tracing/core/chunked_protobuf_input_stream.h"
-
-#include <algorithm>
-
-#include "perfetto/base/logging.h"
-
-namespace perfetto {
-
-ChunkedProtobufInputStream::ChunkedProtobufInputStream(
-    const ChunkSequence* chunks)
-    : chunks_(chunks), cur_chunk_(chunks_->begin()) {}
-
-ChunkedProtobufInputStream::~ChunkedProtobufInputStream() = default;
-
-bool ChunkedProtobufInputStream::Next(const void** data, int* size) {
-  if (cur_chunk_ == chunks_->end())
-    return false;
-
-  PERFETTO_DCHECK(Validate());
-  *data = reinterpret_cast<const void*>(
-      reinterpret_cast<uintptr_t>(cur_chunk_->start) + pos_in_cur_chunk_);
-  *size = static_cast<int>(cur_chunk_->size - pos_in_cur_chunk_);
-  cur_chunk_++;
-  pos_in_cur_chunk_ = 0;
-  PERFETTO_DCHECK(Validate());
-
-  return true;
-}
-
-void ChunkedProtobufInputStream::BackUp(int count) {
-  size_t n = static_cast<size_t>(count);
-  PERFETTO_DCHECK(Validate());
-  while (n) {
-    if (cur_chunk_ == chunks_->end() || pos_in_cur_chunk_ == 0) {
-      if (cur_chunk_ == chunks_->begin()) {
-        // The protobuf library is violating its contract and backing up more
-        // bytes than available.
-        PERFETTO_DCHECK(false);
-        return;
-      }
-      cur_chunk_--;
-      pos_in_cur_chunk_ = cur_chunk_->size;
-      continue;
-    }
-
-    const size_t decrement = std::min(n, pos_in_cur_chunk_);
-    pos_in_cur_chunk_ -= decrement;
-    n -= decrement;
-  }
-  PERFETTO_DCHECK(Validate());
-}
-
-bool ChunkedProtobufInputStream::Skip(int count) {
-  PERFETTO_DCHECK(Validate());
-  size_t n = static_cast<size_t>(count);
-  while (n) {
-    PERFETTO_DCHECK(Validate());
-    if (cur_chunk_ == chunks_->end())
-      return false;
-
-    const size_t increment = std::min(n, cur_chunk_->size - pos_in_cur_chunk_);
-    pos_in_cur_chunk_ += increment;
-    n -= increment;
-
-    if (pos_in_cur_chunk_ >= cur_chunk_->size) {
-      cur_chunk_++;
-      pos_in_cur_chunk_ = 0;
-    }
-  }
-  PERFETTO_DCHECK(Validate());
-  return true;
-}
-
-google::protobuf::int64 ChunkedProtobufInputStream::ByteCount() const {
-  PERFETTO_DCHECK(Validate());
-  google::protobuf::int64 count = 0;
-  for (auto it = chunks_->begin(); it != chunks_->end(); it++) {
-    if (it == cur_chunk_) {
-      count += static_cast<google::protobuf::int64>(pos_in_cur_chunk_);
-      break;
-    }
-    count += static_cast<google::protobuf::int64>(it->size);
-  }
-  return count;
-}
-
-bool ChunkedProtobufInputStream::Validate() const {
-  return ((cur_chunk_ == chunks_->end() && pos_in_cur_chunk_ == 0) ||
-          pos_in_cur_chunk_ < cur_chunk_->size ||
-          (pos_in_cur_chunk_ == 0 && cur_chunk_->size == 0));
-}
-
-}  // namespace perfetto
diff --git a/src/tracing/core/packet_stream_validator.cc b/src/tracing/core/packet_stream_validator.cc
index b0728fe..0f7c982 100644
--- a/src/tracing/core/packet_stream_validator.cc
+++ b/src/tracing/core/packet_stream_validator.cc
@@ -27,14 +27,14 @@
 namespace perfetto {
 
 // static
-bool PacketStreamValidator::Validate(const ChunkSequence& sequence) {
+bool PacketStreamValidator::Validate(const Slices& slices) {
   static_assert(protos::TracePacket::kTrustedUidFieldNumber ==
                     protos::TrustedPacket::kTrustedUidFieldNumber,
                 "trusted uid field id mismatch");
-  ChunkedProtobufInputStream stream(&sequence);
+  SlicedProtobufInputStream stream(&slices);
   size_t size = 0;
-  for (const Chunk& chunk : sequence)
-    size += chunk.size;
+  for (const Slice& slice : slices)
+    size += slice.size;
 
   protos::TrustedPacket packet;
   if (!packet.ParseFromBoundedZeroCopyStream(&stream, size))
diff --git a/src/tracing/core/packet_stream_validator.h b/src/tracing/core/packet_stream_validator.h
index a334daa..8494f4e 100644
--- a/src/tracing/core/packet_stream_validator.h
+++ b/src/tracing/core/packet_stream_validator.h
@@ -17,7 +17,7 @@
 #ifndef SRC_TRACING_CORE_PACKET_STREAM_VALIDATOR_H_
 #define SRC_TRACING_CORE_PACKET_STREAM_VALIDATOR_H_
 
-#include "src/tracing/core/chunked_protobuf_input_stream.h"
+#include "src/tracing/core/sliced_protobuf_input_stream.h"
 
 namespace perfetto {
 
@@ -34,7 +34,7 @@
  public:
   PacketStreamValidator() = delete;
 
-  static bool Validate(const ChunkSequence&);
+  static bool Validate(const Slices&);
 };
 
 }  // namespace perfetto
diff --git a/src/tracing/core/packet_stream_validator_unittest.cc b/src/tracing/core/packet_stream_validator_unittest.cc
index 1f6f6b8..03f15f4 100644
--- a/src/tracing/core/packet_stream_validator_unittest.cc
+++ b/src/tracing/core/packet_stream_validator_unittest.cc
@@ -27,7 +27,7 @@
 
 TEST(PacketStreamValidatorTest, NullPacket) {
   std::string ser_buf;
-  ChunkSequence seq;
+  Slices seq;
   EXPECT_TRUE(PacketStreamValidator::Validate(seq));
 }
 
@@ -36,7 +36,7 @@
   proto.mutable_for_testing()->set_str("string field");
   std::string ser_buf = proto.SerializeAsString();
 
-  ChunkSequence seq;
+  Slices seq;
   seq.emplace_back(&ser_buf[0], ser_buf.size());
   EXPECT_TRUE(PacketStreamValidator::Validate(seq));
 }
@@ -53,7 +53,7 @@
   ft->mutable_sched_switch()->set_next_pid(456);
   std::string ser_buf = proto.SerializeAsString();
 
-  ChunkSequence seq;
+  Slices seq;
   seq.emplace_back(&ser_buf[0], ser_buf.size());
   EXPECT_TRUE(PacketStreamValidator::Validate(seq));
 }
@@ -63,7 +63,7 @@
   proto.set_trusted_uid(123);
   std::string ser_buf = proto.SerializeAsString();
 
-  ChunkSequence seq;
+  Slices seq;
   seq.emplace_back(&ser_buf[0], ser_buf.size());
   EXPECT_FALSE(PacketStreamValidator::Validate(seq));
 }
@@ -73,7 +73,7 @@
   proto.set_trusted_uid(0);
   std::string ser_buf = proto.SerializeAsString();
 
-  ChunkSequence seq;
+  Slices seq;
   seq.emplace_back(&ser_buf[0], ser_buf.size());
   EXPECT_FALSE(PacketStreamValidator::Validate(seq));
 }
@@ -83,7 +83,7 @@
   proto.set_trusted_uid(-1);
   std::string ser_buf = proto.SerializeAsString();
 
-  ChunkSequence seq;
+  Slices seq;
   seq.emplace_back(&ser_buf[0], ser_buf.size());
   EXPECT_FALSE(PacketStreamValidator::Validate(seq));
 }
@@ -101,7 +101,7 @@
   proto.set_trusted_uid(123);
   std::string ser_buf = proto.SerializeAsString();
 
-  ChunkSequence seq;
+  Slices seq;
   seq.emplace_back(&ser_buf[0], ser_buf.size());
   EXPECT_FALSE(PacketStreamValidator::Validate(seq));
 }
@@ -119,7 +119,7 @@
   std::string ser_buf = proto.SerializeAsString();
 
   for (size_t i = 0; i < ser_buf.size(); i++) {
-    ChunkSequence seq;
+    Slices seq;
     seq.emplace_back(&ser_buf[0], i);
     seq.emplace_back(&ser_buf[i], ser_buf.size() - i);
     EXPECT_TRUE(PacketStreamValidator::Validate(seq));
@@ -141,7 +141,7 @@
   std::string ser_buf = proto.SerializeAsString();
 
   for (size_t i = 0; i < ser_buf.size(); i++) {
-    ChunkSequence seq;
+    Slices seq;
     seq.emplace_back(&ser_buf[0], i);
     seq.emplace_back(&ser_buf[i], ser_buf.size() - i);
     EXPECT_FALSE(PacketStreamValidator::Validate(seq));
@@ -154,7 +154,7 @@
   std::string ser_buf = proto.SerializeAsString();
 
   for (size_t i = 1; i < ser_buf.size(); i++) {
-    ChunkSequence seq;
+    Slices seq;
     seq.emplace_back(&ser_buf[0], i);
     EXPECT_FALSE(PacketStreamValidator::Validate(seq));
   }
@@ -166,7 +166,7 @@
   std::string ser_buf = proto.SerializeAsString();
   ser_buf += "bike is short for bichael";
 
-  ChunkSequence seq;
+  Slices seq;
   seq.emplace_back(&ser_buf[0], ser_buf.size());
   EXPECT_FALSE(PacketStreamValidator::Validate(seq));
 }
diff --git a/src/tracing/core/service_impl.cc b/src/tracing/core/service_impl.cc
index 764ce98..cae70f2 100644
--- a/src/tracing/core/service_impl.cc
+++ b/src/tracing/core/service_impl.cc
@@ -341,17 +341,17 @@
             PERFETTO_DLOG("out of bounds!");
             break;
           }
-          ChunkSequence chunk_seq;
-          chunk_seq.emplace_back(ptr, pack_size);
-          if (!skip && !PacketStreamValidator::Validate(chunk_seq)) {
+          Slices slices;
+          slices.emplace_back(ptr, pack_size);
+          if (!skip && !PacketStreamValidator::Validate(slices)) {
             PERFETTO_DLOG("Dropping invalid packet");
             skip = true;
           }
 
           if (!skip) {
             packets->emplace_back();
-            for (Chunk& validated_chunk : chunk_seq)
-              packets->back().AddChunk(std::move(validated_chunk));
+            for (Slice& validated_slice : slices)
+              packets->back().AddSlice(std::move(validated_slice));
 
             // Append a chunk with the trusted UID of the producer. This can't
             // be spoofed because above we validated that the existing chunks
@@ -366,8 +366,8 @@
             uint8_t trusted_buf[16];
             PERFETTO_CHECK(trusted_packet.SerializeToArray(
                 &trusted_buf, sizeof(trusted_buf)));
-            packets->back().AddChunk(
-                Chunk::Copy(trusted_buf, trusted_packet.ByteSize()));
+            packets->back().AddSlice(
+                Slice::Copy(trusted_buf, trusted_packet.ByteSize()));
           }
           ptr += pack_size;
         }  // for(packet)
diff --git a/src/tracing/core/sliced_protobuf_input_stream.cc b/src/tracing/core/sliced_protobuf_input_stream.cc
new file mode 100644
index 0000000..a500a95
--- /dev/null
+++ b/src/tracing/core/sliced_protobuf_input_stream.cc
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#include "src/tracing/core/sliced_protobuf_input_stream.h"
+
+#include <algorithm>
+
+#include "perfetto/base/logging.h"
+
+namespace perfetto {
+
+SlicedProtobufInputStream::SlicedProtobufInputStream(const Slices* slices)
+    : slices_(slices), cur_slice_(slices_->begin()) {}
+
+SlicedProtobufInputStream::~SlicedProtobufInputStream() = default;
+
+bool SlicedProtobufInputStream::Next(const void** data, int* size) {
+  if (cur_slice_ == slices_->end())
+    return false;
+
+  PERFETTO_DCHECK(Validate());
+  *data = reinterpret_cast<const void*>(
+      reinterpret_cast<uintptr_t>(cur_slice_->start) + pos_in_cur_slice_);
+  *size = static_cast<int>(cur_slice_->size - pos_in_cur_slice_);
+  cur_slice_++;
+  pos_in_cur_slice_ = 0;
+  PERFETTO_DCHECK(Validate());
+
+  return true;
+}
+
+void SlicedProtobufInputStream::BackUp(int count) {
+  size_t n = static_cast<size_t>(count);
+  PERFETTO_DCHECK(Validate());
+  while (n) {
+    if (cur_slice_ == slices_->end() || pos_in_cur_slice_ == 0) {
+      if (cur_slice_ == slices_->begin()) {
+        // The protobuf library is violating its contract and backing up more
+        // bytes than available.
+        PERFETTO_DCHECK(false);
+        return;
+      }
+      cur_slice_--;
+      pos_in_cur_slice_ = cur_slice_->size;
+      continue;
+    }
+
+    const size_t decrement = std::min(n, pos_in_cur_slice_);
+    pos_in_cur_slice_ -= decrement;
+    n -= decrement;
+  }
+  PERFETTO_DCHECK(Validate());
+}
+
+bool SlicedProtobufInputStream::Skip(int count) {
+  PERFETTO_DCHECK(Validate());
+  size_t n = static_cast<size_t>(count);
+  while (n) {
+    PERFETTO_DCHECK(Validate());
+    if (cur_slice_ == slices_->end())
+      return false;
+
+    const size_t increment = std::min(n, cur_slice_->size - pos_in_cur_slice_);
+    pos_in_cur_slice_ += increment;
+    n -= increment;
+
+    if (pos_in_cur_slice_ >= cur_slice_->size) {
+      cur_slice_++;
+      pos_in_cur_slice_ = 0;
+    }
+  }
+  PERFETTO_DCHECK(Validate());
+  return true;
+}
+
+google::protobuf::int64 SlicedProtobufInputStream::ByteCount() const {
+  PERFETTO_DCHECK(Validate());
+  google::protobuf::int64 count = 0;
+  for (auto it = slices_->begin(); it != slices_->end(); it++) {
+    if (it == cur_slice_) {
+      count += static_cast<google::protobuf::int64>(pos_in_cur_slice_);
+      break;
+    }
+    count += static_cast<google::protobuf::int64>(it->size);
+  }
+  return count;
+}
+
+bool SlicedProtobufInputStream::Validate() const {
+  return ((cur_slice_ == slices_->end() && pos_in_cur_slice_ == 0) ||
+          pos_in_cur_slice_ < cur_slice_->size ||
+          (pos_in_cur_slice_ == 0 && cur_slice_->size == 0));
+}
+
+}  // namespace perfetto
diff --git a/src/tracing/core/chunked_protobuf_input_stream.h b/src/tracing/core/sliced_protobuf_input_stream.h
similarity index 65%
rename from src/tracing/core/chunked_protobuf_input_stream.h
rename to src/tracing/core/sliced_protobuf_input_stream.h
index c7c6c77..71c7e60 100644
--- a/src/tracing/core/chunked_protobuf_input_stream.h
+++ b/src/tracing/core/sliced_protobuf_input_stream.h
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACING_CORE_CHUNKED_PROTOBUF_INPUT_STREAM_H_
-#define SRC_TRACING_CORE_CHUNKED_PROTOBUF_INPUT_STREAM_H_
+#ifndef SRC_TRACING_CORE_SLICED_PROTOBUF_INPUT_STREAM_H_
+#define SRC_TRACING_CORE_SLICED_PROTOBUF_INPUT_STREAM_H_
 
-#include "perfetto/tracing/core/chunk.h"
+#include "perfetto/tracing/core/slice.h"
 
 #include <stdint.h>
 
@@ -25,13 +25,13 @@
 
 namespace perfetto {
 
-// Wraps a ChunkSequence in a protobuf ZeroCopyInputStream that can be passed
-// to protobuf::Message::ParseFromZeroCopyStream().
-class ChunkedProtobufInputStream
+// Wraps a sequence of Slice(s) in a protobuf ZeroCopyInputStream that can be
+// passed to protobuf::Message::ParseFromZeroCopyStream().
+class SlicedProtobufInputStream
     : public google::protobuf::io::ZeroCopyInputStream {
  public:
-  explicit ChunkedProtobufInputStream(const ChunkSequence*);
-  ~ChunkedProtobufInputStream() override;
+  explicit SlicedProtobufInputStream(const Slices*);
+  ~SlicedProtobufInputStream() override;
 
   // ZeroCopyInputStream implementation. See zero_copy_stream.h for the API
   // contract of the methods below.
@@ -43,11 +43,11 @@
  private:
   bool Validate() const;
 
-  const ChunkSequence* const chunks_;
-  ChunkSequence::const_iterator cur_chunk_;
-  size_t pos_in_cur_chunk_ = 0;
+  const Slices* const slices_;
+  Slices::const_iterator cur_slice_;
+  size_t pos_in_cur_slice_ = 0;
 };
 
 }  // namespace perfetto
 
-#endif  // SRC_TRACING_CORE_CHUNKED_PROTOBUF_INPUT_STREAM_H_
+#endif  // SRC_TRACING_CORE_SLICED_PROTOBUF_INPUT_STREAM_H_
diff --git a/src/tracing/core/chunked_protobuf_input_stream_unittest.cc b/src/tracing/core/sliced_protobuf_input_stream_unittest.cc
similarity index 87%
rename from src/tracing/core/chunked_protobuf_input_stream_unittest.cc
rename to src/tracing/core/sliced_protobuf_input_stream_unittest.cc
index 800cf1e..076afc1 100644
--- a/src/tracing/core/chunked_protobuf_input_stream_unittest.cc
+++ b/src/tracing/core/sliced_protobuf_input_stream_unittest.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/tracing/core/chunked_protobuf_input_stream.h"
+#include "src/tracing/core/sliced_protobuf_input_stream.h"
 
 #include "gtest/gtest.h"
 #include "perfetto/base/utils.h"
@@ -22,18 +22,18 @@
 namespace perfetto {
 namespace {
 
-// The tests below work on chunks, that are a (start pointer, size) tuple but
+// The tests below work on slices, that are a (start pointer, size) tuple but
 // never dereference the memory in the pointer. Hence, we just use an array of
 // integers that is used both to derive N distinct pointers and to keep track
-// of N distinct sizes. In other words, the tests below will see Chunks of the
+// of N distinct sizes. In other words, the tests below will see Slices of the
 // form {start: &kBuf[0], end: &kBuf[0] + kBuf[0]}, and so on. As long as we
 // don't dereference those pointers, this int array should be enough.
 const int kBufs[]{100, 200, 1024, 0, 10, 0, 1, 1, 7};
 
-TEST(ChunkedProtobufInputStreamTest, SingleChunk) {
-  ChunkSequence seq;
+TEST(SlicedProtobufInputStreamTest, SingleSlice) {
+  Slices seq;
   seq.emplace_back(&kBufs[0], kBufs[0]);
-  ChunkedProtobufInputStream istr(&seq);
+  SlicedProtobufInputStream istr(&seq);
 
   const void* ptr = nullptr;
   int size = 0;
@@ -67,11 +67,11 @@
   ASSERT_FALSE(istr.Next(&ptr, &size));
 }
 
-TEST(ChunkedProtobufInputStreamTest, SimpleSequence) {
-  ChunkSequence seq;
+TEST(SlicedProtobufInputStreamTest, SimpleSequence) {
+  Slices seq;
   for (size_t i = 0; i < base::ArraySize(kBufs); i++)
     seq.emplace_back(&kBufs[i], kBufs[i]);
-  ChunkedProtobufInputStream istr(&seq);
+  SlicedProtobufInputStream istr(&seq);
   int num_bytes = 0;
   const void* ptr = nullptr;
   int size = 0;
@@ -86,11 +86,11 @@
   ASSERT_FALSE(istr.Next(&ptr, &size));
 }
 
-TEST(ChunkedProtobufInputStreamTest, SequenceWithSkipsAndBackups) {
-  ChunkSequence seq;
+TEST(SlicedProtobufInputStreamTest, SequenceWithSkipsAndBackups) {
+  Slices seq;
   for (size_t i = 0; i < base::ArraySize(kBufs); i++)
     seq.emplace_back(&kBufs[i], kBufs[i]);
-  ChunkedProtobufInputStream istr(&seq);
+  SlicedProtobufInputStream istr(&seq);
   ASSERT_TRUE(istr.Skip(99));
   ASSERT_EQ(99, istr.ByteCount());
 
diff --git a/src/tracing/core/trace_packet.cc b/src/tracing/core/trace_packet.cc
index 3f711c7..ee762bf 100644
--- a/src/tracing/core/trace_packet.cc
+++ b/src/tracing/core/trace_packet.cc
@@ -16,7 +16,7 @@
 
 #include "perfetto/tracing/core/trace_packet.h"
 
-#include "src/tracing/core/chunked_protobuf_input_stream.h"
+#include "src/tracing/core/sliced_protobuf_input_stream.h"
 
 #include "perfetto/trace/trace_packet.pb.h"
 
@@ -32,7 +32,7 @@
   if (decoded_packet_)
     return true;
   decoded_packet_.reset(new DecodedTracePacket());
-  ChunkedProtobufInputStream istr(&chunks_);
+  SlicedProtobufInputStream istr(&slices_);
   if (!decoded_packet_->ParseFromZeroCopyStream(&istr)) {
     decoded_packet_.reset();
     return false;
@@ -40,9 +40,9 @@
   return true;
 }
 
-void TracePacket::AddChunk(Chunk chunk) {
-  size_ += chunk.size;
-  chunks_.push_back(std::move(chunk));
+void TracePacket::AddSlice(Slice slice) {
+  size_ += slice.size;
+  slices_.push_back(std::move(slice));
 }
 
 }  // namespace perfetto
diff --git a/src/tracing/core/trace_packet_unittest.cc b/src/tracing/core/trace_packet_unittest.cc
index 914249d..64f5187 100644
--- a/src/tracing/core/trace_packet_unittest.cc
+++ b/src/tracing/core/trace_packet_unittest.cc
@@ -30,12 +30,12 @@
   proto.mutable_for_testing()->set_str("string field");
   std::string ser_buf = proto.SerializeAsString();
   TracePacket tp;
-  tp.AddChunk({ser_buf.data(), ser_buf.size()});
-  auto chunk = tp.begin();
-  ASSERT_NE(tp.end(), chunk);
-  ASSERT_EQ(ser_buf.data(), chunk->start);
-  ASSERT_EQ(ser_buf.size(), chunk->size);
-  ASSERT_EQ(tp.end(), ++chunk);
+  tp.AddSlice({ser_buf.data(), ser_buf.size()});
+  auto slice = tp.begin();
+  ASSERT_NE(tp.end(), slice);
+  ASSERT_EQ(ser_buf.data(), slice->start);
+  ASSERT_EQ(ser_buf.size(), slice->size);
+  ASSERT_EQ(tp.end(), ++slice);
 
   ASSERT_TRUE(tp.Decode());
   ASSERT_TRUE(tp.Decode());  // Decode() should be idempotent.
@@ -54,31 +54,31 @@
   ASSERT_EQ(proto.for_testing().str(), moved_tp_2->for_testing().str());
 }
 
-TEST(TracePacketTest, Chunked) {
+TEST(TracePacketTest, Sliced) {
   protos::TracePacket proto;
   proto.mutable_for_testing()->set_str(
       "this is an arbitrarily long string ........................");
   std::string ser_buf = proto.SerializeAsString();
   TracePacket tp;
-  tp.AddChunk({ser_buf.data(), 3});
-  tp.AddChunk({ser_buf.data() + 3, 5});
-  tp.AddChunk({ser_buf.data() + 3 + 5, ser_buf.size() - 3 - 5});
+  tp.AddSlice({ser_buf.data(), 3});
+  tp.AddSlice({ser_buf.data() + 3, 5});
+  tp.AddSlice({ser_buf.data() + 3 + 5, ser_buf.size() - 3 - 5});
   ASSERT_EQ(ser_buf.size(), tp.size());
 
-  auto chunk = tp.begin();
-  ASSERT_NE(tp.end(), chunk);
-  ASSERT_EQ(ser_buf.data(), chunk->start);
-  ASSERT_EQ(3u, chunk->size);
+  auto slice = tp.begin();
+  ASSERT_NE(tp.end(), slice);
+  ASSERT_EQ(ser_buf.data(), slice->start);
+  ASSERT_EQ(3u, slice->size);
 
-  ASSERT_NE(tp.end(), ++chunk);
-  ASSERT_EQ(ser_buf.data() + 3, chunk->start);
-  ASSERT_EQ(5u, chunk->size);
+  ASSERT_NE(tp.end(), ++slice);
+  ASSERT_EQ(ser_buf.data() + 3, slice->start);
+  ASSERT_EQ(5u, slice->size);
 
-  ASSERT_NE(tp.end(), ++chunk);
-  ASSERT_EQ(ser_buf.data() + 3 + 5, chunk->start);
-  ASSERT_EQ(ser_buf.size() - 3 - 5, chunk->size);
+  ASSERT_NE(tp.end(), ++slice);
+  ASSERT_EQ(ser_buf.data() + 3 + 5, slice->start);
+  ASSERT_EQ(ser_buf.size() - 3 - 5, slice->size);
 
-  ASSERT_EQ(tp.end(), ++chunk);
+  ASSERT_EQ(tp.end(), ++slice);
 
   ASSERT_TRUE(tp.Decode());
   ASSERT_NE(nullptr, tp.operator->());
@@ -90,7 +90,7 @@
   proto.mutable_for_testing()->set_str("string field");
   std::string ser_buf = proto.SerializeAsString();
   TracePacket tp;
-  tp.AddChunk({ser_buf.data(), ser_buf.size() - 2});  // corrupted.
+  tp.AddSlice({ser_buf.data(), ser_buf.size() - 2});  // corrupted.
   ASSERT_FALSE(tp.Decode());
 }
 
diff --git a/src/tracing/ipc/consumer/consumer_ipc_client_impl.cc b/src/tracing/ipc/consumer/consumer_ipc_client_impl.cc
index e95017f..24efca6 100644
--- a/src/tracing/ipc/consumer/consumer_ipc_client_impl.cc
+++ b/src/tracing/ipc/consumer/consumer_ipc_client_impl.cc
@@ -127,8 +127,8 @@
   trace_packets.reserve(response->trace_packets().size());
   for (const std::string& bytes : response->trace_packets()) {
     trace_packets.emplace_back();
-    trace_packets.back().AddChunk(
-        Chunk(reinterpret_cast<const void*>(bytes.data()), bytes.size()));
+    trace_packets.back().AddSlice(
+        Slice(reinterpret_cast<const void*>(bytes.data()), bytes.size()));
   }
   consumer_->OnTraceData(std::move(trace_packets), response.has_more());
 }
diff --git a/src/tracing/ipc/service/consumer_ipc_service.cc b/src/tracing/ipc/service/consumer_ipc_service.cc
index b0fce4a..1dc9a5a 100644
--- a/src/tracing/ipc/service/consumer_ipc_service.cc
+++ b/src/tracing/ipc/service/consumer_ipc_service.cc
@@ -21,8 +21,8 @@
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
 #include "perfetto/ipc/host.h"
-#include "perfetto/tracing/core/chunk.h"
 #include "perfetto/tracing/core/service.h"
+#include "perfetto/tracing/core/slice.h"
 #include "perfetto/tracing/core/trace_config.h"
 #include "perfetto/tracing/core/trace_packet.h"
 
@@ -109,13 +109,13 @@
 
   auto result = ipc::AsyncResult<ReadBuffersResponse>::Create();
   result.set_has_more(has_more);
-  // TODO(primiano): Expose the chunks to the Consumer rather than stitching
+  // TODO(primiano): Expose the slices to the Consumer rather than stitching
   // them and wasting cpu time to hide this detail.
   for (const TracePacket& trace_packet : trace_packets) {
     std::string* dst = result->add_trace_packets();
     dst->reserve(trace_packet.size());
-    for (const Chunk& chunk : trace_packet)
-      dst->append(reinterpret_cast<const char*>(chunk.start), chunk.size);
+    for (const Slice& slice : trace_packet)
+      dst->append(reinterpret_cast<const char*>(slice.start), slice.size);
   }
   read_buffers_response.Resolve(std::move(result));
 }