OSDN Git Service

Perfprofd: Use ConfigReader for shellCommand startProfiling
[android-x86/system-extras.git] / perfprofd / tests / perfprofd_test.cc
index 54b36ea..1f5ae82 100644 (file)
  * limitations under the License.
  */
 
-#include <gtest/gtest.h>
 #include <algorithm>
 #include <cctype>
-#include <string>
+#include <functional>
+#include <iterator>
+#include <memory>
+#include <mutex>
 #include <regex>
+#include <string>
+
+#include <fcntl.h>
 #include <stdio.h>
+#include <libgen.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <fcntl.h>
 
-#include <android-base/properties.h>
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/macros.h>
 #include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <android-base/test_utils.h>
+#include <android-base/thread_annotations.h>
+#include <gtest/gtest.h>
+#include <zlib.h>
 
 #include "config.h"
 #include "configreader.h"
+#include "map_utils.h"
 #include "perfprofdcore.h"
-#include "perfprofdutils.h"
-#include "perfprofdmockutils.h"
+#include "perfprofd_cmdline.h"
+#include "quipper_helper.h"
+#include "symbolizer.h"
+
+#include "perfprofd_record.pb.h"
 
-#include "perf_profile.pb.h"
-#include "google/protobuf/text_format.h"
+using namespace android::perfprofd::quipper;
+
+static_assert(android::base::kEnableDChecks, "Expected DCHECKs to be enabled");
 
 //
 // Set to argv[0] on startup
 //
-static const char *executable_path;
+static std::string gExecutableRealpath;
 
-//
-// test_dir is the directory containing the test executable and
-// any files associated with the test (will be created by the harness).
-//
-// dest_dir is a subdirectory of test_dir that we'll create on the fly
-// at the start of each testpoint (into which new files can be written),
-// then delete at end of testpoint.
-//
-static std::string test_dir;
-static std::string dest_dir;
+namespace {
 
-// Path to perf executable on device
-#define PERFPATH "/system/bin/perf"
+using android::base::LogId;
+using android::base::LogSeverity;
 
-// Temporary config file that we will emit for the daemon to read
-#define CONFIGFILE "perfprofd.conf"
-
-static std::string encoded_file_path(int seq)
-{
-  return android::base::StringPrintf("%s/perf.data.encoded.%d",
-                                     dest_dir.c_str(), seq);
-}
-
-class PerfProfdTest : public testing::Test {
- protected:
-  virtual void SetUp() {
-    mock_perfprofdutils_init();
-    create_dest_dir();
-    yesclean();
-  }
-
-  virtual void TearDown() {
-    mock_perfprofdutils_finish();
+class TestLogHelper {
+ public:
+  void Install() {
+    using namespace std::placeholders;
+    android::base::SetLogger(
+        std::bind(&TestLogHelper::TestLogFunction, this, _1, _2, _3, _4, _5, _6));
   }
 
-  void noclean() {
-    clean_ = false;
+  std::string JoinTestLog(const char* delimiter) {
+    std::unique_lock<std::mutex> ul(lock_);
+    return android::base::Join(test_log_messages_, delimiter);
   }
-  void yesclean() {
-    clean_ = true;
+  template <typename Predicate>
+  std::string JoinTestLog(const char* delimiter, Predicate pred) {
+    std::unique_lock<std::mutex> ul(lock_);
+    std::vector<std::string> tmp;
+    std::copy_if(test_log_messages_.begin(),
+                 test_log_messages_.end(),
+                 std::back_inserter(tmp),
+                 pred);
+    return android::base::Join(tmp, delimiter);
   }
 
  private:
-  bool clean_;
-
-  void create_dest_dir() {
-    setup_dirs();
-    ASSERT_FALSE(dest_dir == "");
-    if (clean_) {
-      std::string cmd("rm -rf ");
-      cmd += dest_dir;
-      system(cmd.c_str());
+  void TestLogFunction(LogId log_id,
+                       LogSeverity severity,
+                       const char* tag,
+                       const char* file,
+                       unsigned int line,
+                       const char* message) {
+    std::unique_lock<std::mutex> ul(lock_);
+    constexpr char log_characters[] = "VDIWEFF";
+    char severity_char = log_characters[severity];
+    test_log_messages_.push_back(android::base::StringPrintf("%c: %s", severity_char, message));
+
+    if (severity >= LogSeverity::FATAL_WITHOUT_ABORT) {
+      android::base::StderrLogger(log_id, severity, tag, file, line, message);
     }
-    std::string cmd("mkdir -p ");
-    cmd += dest_dir;
-    system(cmd.c_str());
   }
 
-  void setup_dirs()
-  {
-    if (test_dir == "") {
-      ASSERT_TRUE(executable_path != nullptr);
-      std::string s(executable_path);
-      auto found = s.find_last_of('/');
-      test_dir = s.substr(0,found);
-      dest_dir = test_dir;
-      dest_dir += "/tmp";
-    }
-  }
+ private:
+  std::mutex lock_;
 
+  std::vector<std::string> test_log_messages_;
 };
 
+}  // namespace
+
+// Path to perf executable on device
+#define PERFPATH "/system/bin/perf"
+
+// Temporary config file that we will emit for the daemon to read
+#define CONFIGFILE "perfprofd.conf"
+
 static bool bothWhiteSpace(char lhs, char rhs)
 {
   return (std::isspace(lhs) && std::isspace(rhs));
@@ -161,6 +165,7 @@ static std::string replaceAll(const std::string &str,
 //
 // Replace occurrences of special variables in the string.
 //
+#ifdef __ANDROID__
 static std::string expandVars(const std::string &str) {
 #ifdef __LP64__
   return replaceAll(str, "$NATIVE_TESTS", "/data/nativetest64");
@@ -168,6 +173,88 @@ static std::string expandVars(const std::string &str) {
   return replaceAll(str, "$NATIVE_TESTS", "/data/nativetest");
 #endif
 }
+#endif
+
+class PerfProfdTest : public testing::Test {
+ protected:
+  virtual void SetUp() {
+    test_logger.Install();
+    create_dirs();
+  }
+
+  virtual void TearDown() {
+    android::base::SetLogger(android::base::StderrLogger);
+
+    // TODO: proper management of test files. For now, use old system() code.
+    for (const auto dir : { &dest_dir, &conf_dir }) {
+      std::string cmd("rm -rf ");
+      cmd += *dir;
+      int ret = system(cmd.c_str());
+      CHECK_EQ(0, ret);
+    }
+  }
+
+ protected:
+  //
+  // Check to see if the log messages emitted by the daemon
+  // match the expected result. By default we use a partial
+  // match, e.g. if we see the expected excerpt anywhere in the
+  // result, it's a match (for exact match, set exact to true)
+  //
+  void CompareLogMessages(const std::string& expected,
+                          const char* testpoint,
+                          bool exactMatch = false) {
+     std::string sqexp = squeezeWhite(expected, "expected");
+
+     // Strip out JIT errors.
+     std::regex jit_regex("E: Failed to open ELF file: [^ ]*ashmem/dalvik-jit-code-cache.*");
+     auto strip_jit = [&](const std::string& str) {
+       std::smatch jit_match;
+       return !std::regex_match(str, jit_match, jit_regex);
+     };
+     std::string sqact = squeezeWhite(test_logger.JoinTestLog(" ", strip_jit), "actual");
+
+     if (exactMatch) {
+       EXPECT_STREQ(sqexp.c_str(), sqact.c_str());
+     } else {
+       std::size_t foundpos = sqact.find(sqexp);
+       bool wasFound = true;
+       if (foundpos == std::string::npos) {
+         std::cerr << testpoint << ": expected result not found\n";
+         std::cerr << " Actual: \"" << sqact << "\"\n";
+         std::cerr << " Expected: \"" << sqexp << "\"\n";
+         wasFound = false;
+       }
+       EXPECT_TRUE(wasFound);
+     }
+  }
+
+  // test_dir is the directory containing the test executable and
+  // any files associated with the test (will be created by the harness).
+  std::string test_dir;
+
+  // dest_dir is a temporary directory that we're using as the destination directory.
+  // It is backed by temp_dir1.
+  std::string dest_dir;
+
+  // conf_dir is a temporary directory that we're using as the configuration directory.
+  // It is backed by temp_dir2.
+  std::string conf_dir;
+
+  TestLogHelper test_logger;
+
+ private:
+  void create_dirs() {
+    temp_dir1.reset(new TemporaryDir());
+    temp_dir2.reset(new TemporaryDir());
+    dest_dir = temp_dir1->path;
+    conf_dir = temp_dir2->path;
+    test_dir = android::base::Dirname(gExecutableRealpath);
+  }
+
+  std::unique_ptr<TemporaryDir> temp_dir1;
+  std::unique_ptr<TemporaryDir> temp_dir2;
+};
 
 ///
 /// Helper class to kick off a run of the perfprofd daemon with a specific
@@ -175,10 +262,10 @@ static std::string expandVars(const std::string &str) {
 ///
 class PerfProfdRunner {
  public:
-  PerfProfdRunner()
-      : config_path_(test_dir)
+  explicit PerfProfdRunner(const std::string& config_dir)
+      : config_dir_(config_dir)
   {
-    config_path_ += "/" CONFIGFILE;
+    config_path_ = config_dir + "/" CONFIGFILE;
   }
 
   ~PerfProfdRunner()
@@ -194,21 +281,21 @@ class PerfProfdRunner {
 
   void remove_semaphore_file()
   {
-    std::string semaphore(test_dir);
+    std::string semaphore(config_dir_);
     semaphore += "/" SEMAPHORE_FILENAME;
     unlink(semaphore.c_str());
   }
 
   void create_semaphore_file()
   {
-    std::string semaphore(test_dir);
+    std::string semaphore(config_dir_);
     semaphore += "/" SEMAPHORE_FILENAME;
     close(open(semaphore.c_str(), O_WRONLY|O_CREAT, 0600));
   }
 
   void write_processed_file(int start_seq, int end_seq)
   {
-    std::string processed = test_dir + "/" PROCESSED_FILENAME;
+    std::string processed = config_dir_ + "/" PROCESSED_FILENAME;
     FILE *fp = fopen(processed.c_str(), "w");
     for (int i = start_seq; i < end_seq; i++) {
       fprintf(fp, "%d\n", i);
@@ -218,14 +305,14 @@ class PerfProfdRunner {
 
   void remove_processed_file()
   {
-    std::string processed = test_dir + "/" PROCESSED_FILENAME;
+    std::string processed = config_dir_ + "/" PROCESSED_FILENAME;
     unlink(processed.c_str());
   }
 
   struct LoggingConfig : public Config {
     void Sleep(size_t seconds) override {
       // Log sleep calls but don't sleep.
-      perfprofd_log_info("sleep %d seconds", seconds);
+      LOG(INFO) << "sleep " << seconds << " seconds";
     }
 
     bool IsProfilingEnabled() const override {
@@ -233,8 +320,7 @@ class PerfProfdRunner {
       // Check for existence of semaphore file in config directory
       //
       if (access(config_directory.c_str(), F_OK) == -1) {
-        W_ALOGW("unable to open config directory %s: (%s)",
-                config_directory.c_str(), strerror(errno));
+        PLOG(WARNING) << "unable to open config directory " << config_directory;
         return false;
       }
 
@@ -262,6 +348,7 @@ class PerfProfdRunner {
   }
 
  private:
+  std::string config_dir_;
   std::string config_path_;
   std::string config_text_;
 
@@ -277,85 +364,83 @@ class PerfProfdRunner {
 
 //......................................................................
 
-static void readEncodedProfile(const char *testpoint,
-                               wireless_android_play_playlog::AndroidPerfProfile &encodedProfile)
+static std::string encoded_file_path(const std::string& dest_dir,
+                                     int seq) {
+  return android::base::StringPrintf("%s/perf.data.encoded.%d",
+                                     dest_dir.c_str(), seq);
+}
+
+static void readEncodedProfile(const std::string& dest_dir,
+                               bool compressed,
+                               android::perfprofd::PerfprofdRecord& encodedProfile)
 {
   struct stat statb;
-  int perf_data_stat_result = stat(encoded_file_path(0).c_str(), &statb);
+  int perf_data_stat_result = stat(encoded_file_path(dest_dir, 0).c_str(), &statb);
   ASSERT_NE(-1, perf_data_stat_result);
 
   // read
   std::string encoded;
   encoded.resize(statb.st_size);
-  FILE *ifp = fopen(encoded_file_path(0).c_str(), "r");
+  FILE *ifp = fopen(encoded_file_path(dest_dir, 0).c_str(), "r");
   ASSERT_NE(nullptr, ifp);
   size_t items_read = fread((void*) encoded.data(), statb.st_size, 1, ifp);
   ASSERT_EQ(1, items_read);
   fclose(ifp);
 
-  // decode
-  encodedProfile.ParseFromString(encoded);
-}
-
-static std::string encodedLoadModuleToString(const wireless_android_play_playlog::LoadModule &lm)
-{
-  std::stringstream ss;
-  ss << "name: \"" << lm.name() << "\"\n";
-  if (lm.build_id() != "") {
-    ss << "build_id: \"" << lm.build_id() << "\"\n";
-  }
-  return ss.str();
-}
+  // uncompress
+  if (compressed && !encoded.empty()) {
+    z_stream stream;
+    stream.zalloc = Z_NULL;
+    stream.zfree = Z_NULL;
+    stream.opaque = Z_NULL;
+
+    {
+      constexpr int kWindowBits = 15;
+      constexpr int kGzipEncoding = 16;
+      int init_result = inflateInit2(&stream, kWindowBits | kGzipEncoding);
+      if (init_result != Z_OK) {
+        LOG(ERROR) << "Could not initialize libz stream " << init_result;
+        return;
+      }
+    }
 
-static std::string encodedModuleSamplesToString(const wireless_android_play_playlog::LoadModuleSamples &mod)
-{
-  std::stringstream ss;
-
-  ss << "load_module_id: " << mod.load_module_id() << "\n";
-  for (size_t k = 0; k < mod.address_samples_size(); k++) {
-    const auto &sample = mod.address_samples(k);
-    ss << "  address_samples {\n";
-    for (size_t l = 0; l < mod.address_samples(k).address_size();
-         l++) {
-      auto address = mod.address_samples(k).address(l);
-      ss << "    address: " << address << "\n";
+    std::string buf;
+    buf.reserve(2 * encoded.size());
+    stream.avail_in = encoded.size();
+    stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(encoded.data()));
+
+    int result;
+    do {
+      uint8_t chunk[1024];
+      stream.next_out = static_cast<Bytef*>(chunk);
+      stream.avail_out = arraysize(chunk);
+
+      result = inflate(&stream, 0);
+      const size_t amount = arraysize(chunk) - stream.avail_out;
+      if (amount > 0) {
+        if (buf.capacity() - buf.size() < amount) {
+          buf.reserve(buf.capacity() + 64u * 1024u);
+          CHECK_LE(amount, buf.capacity() - buf.size());
+        }
+        size_t index = buf.size();
+        buf.resize(buf.size() + amount);
+        memcpy(reinterpret_cast<uint8_t*>(const_cast<char*>(buf.data())) + index, chunk, amount);
+      }
+    } while (result == Z_OK);
+    inflateEnd(&stream);
+    if (result != Z_STREAM_END) {
+      LOG(ERROR) << "Finished with not-Z_STREAM_END " << result;
+      return;
     }
-    ss << "    count: " << sample.count() << "\n";
-    ss << "  }\n";
+    encoded = buf;
   }
-  return ss.str();
+
+  // decode
+  encodedProfile.ParseFromString(encoded);
 }
 
 #define RAW_RESULT(x) #x
 
-//
-// Check to see if the log messages emitted by the daemon
-// match the expected result. By default we use a partial
-// match, e.g. if we see the expected excerpt anywhere in the
-// result, it's a match (for exact match, set exact to true)
-//
-static void compareLogMessages(const std::string &actual,
-                               const std::string &expected,
-                               const char *testpoint,
-                               bool exactMatch=false)
-{
-   std::string sqexp = squeezeWhite(expected, "expected");
-   std::string sqact = squeezeWhite(actual, "actual");
-   if (exactMatch) {
-     EXPECT_STREQ(sqexp.c_str(), sqact.c_str());
-   } else {
-     std::size_t foundpos = sqact.find(sqexp);
-     bool wasFound = true;
-     if (foundpos == std::string::npos) {
-       std::cerr << testpoint << ": expected result not found\n";
-       std::cerr << " Actual: \"" << sqact << "\"\n";
-       std::cerr << " Expected: \"" << sqexp << "\"\n";
-       wasFound = false;
-     }
-     EXPECT_TRUE(wasFound);
-   }
-}
-
 TEST_F(PerfProfdTest, TestUtil)
 {
   EXPECT_EQ("", replaceAll("", "", ""));
@@ -374,7 +459,7 @@ TEST_F(PerfProfdTest, MissingGMS)
   // may not be there. This test insures that the daemon does the
   // right thing in this case.
   //
-  PerfProfdRunner runner;
+  PerfProfdRunner runner(conf_dir);
   runner.addToConfig("only_debug_build=0");
   runner.addToConfig("trace_config_read=0");
   runner.addToConfig("config_directory=/does/not/exist");
@@ -391,13 +476,12 @@ TEST_F(PerfProfdTest, MissingGMS)
   // Verify log contents
   const std::string expected = RAW_RESULT(
       I: sleep 90 seconds
-      W: unable to open config directory /does/not/exist: (No such file or directory)
+      W: unable to open config directory /does/not/exist: No such file or directory
       I: profile collection skipped (missing config directory)
                                           );
 
   // check to make sure entire log matches
-  compareLogMessages(mock_perfprofdutils_getlogged(),
-                     expected, "MissingGMS");
+  CompareLogMessages(expected, "MissingGMS");
 }
 
 
@@ -410,9 +494,9 @@ TEST_F(PerfProfdTest, MissingOptInSemaphoreFile)
   // passes, then it creates a semaphore file for the daemon to pick
   // up on.
   //
-  PerfProfdRunner runner;
+  PerfProfdRunner runner(conf_dir);
   runner.addToConfig("only_debug_build=0");
-  std::string cfparam("config_directory="); cfparam += test_dir;
+  std::string cfparam("config_directory="); cfparam += conf_dir;
   runner.addToConfig(cfparam);
   std::string ddparam("destination_directory="); ddparam += dest_dir;
   runner.addToConfig(ddparam);
@@ -430,11 +514,10 @@ TEST_F(PerfProfdTest, MissingOptInSemaphoreFile)
 
   // Verify log contents
   const std::string expected = RAW_RESULT(
-      I: profile collection skipped (missing semaphore file)
+      I: profile collection skipped (missing config directory)
                                           );
   // check to make sure log excerpt matches
-  compareLogMessages(mock_perfprofdutils_getlogged(),
-                     expected, "MissingOptInSemaphoreFile");
+  CompareLogMessages(expected, "MissingOptInSemaphoreFile");
 }
 
 TEST_F(PerfProfdTest, MissingPerfExecutable)
@@ -445,10 +528,10 @@ TEST_F(PerfProfdTest, MissingPerfExecutable)
   // checks to make sure that if 'simpleperf' is not present we bail out
   // from collecting profiles.
   //
-  PerfProfdRunner runner;
+  PerfProfdRunner runner(conf_dir);
   runner.addToConfig("only_debug_build=0");
   runner.addToConfig("trace_config_read=1");
-  std::string cfparam("config_directory="); cfparam += test_dir;
+  std::string cfparam("config_directory="); cfparam += conf_dir;
   runner.addToConfig(cfparam);
   std::string ddparam("destination_directory="); ddparam += dest_dir;
   runner.addToConfig(ddparam);
@@ -471,8 +554,7 @@ TEST_F(PerfProfdTest, MissingPerfExecutable)
       I: profile collection skipped (missing 'perf' executable)
                                           );
   // check to make sure log excerpt matches
-  compareLogMessages(mock_perfprofdutils_getlogged(),
-                     expected, "MissingPerfExecutable");
+  CompareLogMessages(expected, "MissingPerfExecutable");
 }
 
 TEST_F(PerfProfdTest, BadPerfRun)
@@ -483,16 +565,20 @@ TEST_F(PerfProfdTest, BadPerfRun)
   // crash. This test makes sure that we detect such a case and log
   // the error.
   //
-  PerfProfdRunner runner;
+  PerfProfdRunner runner(conf_dir);
   runner.addToConfig("only_debug_build=0");
-  std::string cfparam("config_directory="); cfparam += test_dir;
+  std::string cfparam("config_directory="); cfparam += conf_dir;
   runner.addToConfig(cfparam);
   std::string ddparam("destination_directory="); ddparam += dest_dir;
   runner.addToConfig(ddparam);
   runner.addToConfig("main_loop_iterations=1");
   runner.addToConfig("use_fixed_seed=1");
   runner.addToConfig("collection_interval=100");
+#ifdef __ANDROID__
   runner.addToConfig("perf_path=/system/bin/false");
+#else
+  runner.addToConfig("perf_path=/bin/false");
+#endif
 
   // Create semaphore file
   runner.create_semaphore_file();
@@ -505,12 +591,12 @@ TEST_F(PerfProfdTest, BadPerfRun)
 
   // Verify log contents
   const std::string expected = RAW_RESULT(
-      I: profile collection failed (perf record returned bad exit status)
+      W: perf bad exit status 1
+      W: profile collection failed
                                           );
 
   // check to make sure log excerpt matches
-  compareLogMessages(mock_perfprofdutils_getlogged(),
-                     expected, "BadPerfRun");
+  CompareLogMessages(expected, "BadPerfRun");
 }
 
 TEST_F(PerfProfdTest, ConfigFileParsing)
@@ -518,7 +604,7 @@ TEST_F(PerfProfdTest, ConfigFileParsing)
   //
   // Gracefully handly malformed items in the config file
   //
-  PerfProfdRunner runner;
+  PerfProfdRunner runner(conf_dir);
   runner.addToConfig("only_debug_build=0");
   runner.addToConfig("main_loop_iterations=1");
   runner.addToConfig("collection_interval=100");
@@ -528,7 +614,6 @@ TEST_F(PerfProfdTest, ConfigFileParsing)
   // assorted bad syntax
   runner.addToConfig("collection_interval=0");
   runner.addToConfig("collection_interval=-1");
-  runner.addToConfig("collection_interval=2");
   runner.addToConfig("nonexistent_key=something");
   runner.addToConfig("no_equals_stmt");
 
@@ -540,16 +625,14 @@ TEST_F(PerfProfdTest, ConfigFileParsing)
 
   // Verify log contents
   const std::string expected = RAW_RESULT(
-      W: line 6: specified value 0 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
+      W: line 6: specified value 0 for 'collection_interval' outside permitted range [1 4294967295] (ignored)
       W: line 7: malformed unsigned value (ignored)
-      W: line 8: specified value 2 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
-      W: line 9: unknown option 'nonexistent_key' ignored
-      W: line 10: line malformed (no '=' found)
+      W: line 8: unknown option 'nonexistent_key' ignored
+      W: line 9: line malformed (no '=' found)
                                           );
 
   // check to make sure log excerpt matches
-  compareLogMessages(mock_perfprofdutils_getlogged(),
-                     expected, "ConfigFileParsing");
+  CompareLogMessages(expected, "ConfigFileParsing");
 }
 
 TEST_F(PerfProfdTest, ProfileCollectionAnnotations)
@@ -562,11 +645,193 @@ TEST_F(PerfProfdTest, ProfileCollectionAnnotations)
   // completed booted, will be on charger, and will not have the camera
   // active.
   EXPECT_FALSE(get_booting());
+#ifdef __ANDROID__
   EXPECT_TRUE(get_charging());
+#endif
   EXPECT_FALSE(get_camera_active());
 }
 
-TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
+namespace {
+
+template <typename Iterator>
+size_t CountEvents(const quipper::PerfDataProto& proto) {
+  size_t count = 0;
+  for (Iterator it(proto); it != it.end(); ++it) {
+    count++;
+  }
+  return count;
+}
+
+size_t CountCommEvents(const quipper::PerfDataProto& proto) {
+  return CountEvents<CommEventIterator>(proto);
+}
+size_t CountMmapEvents(const quipper::PerfDataProto& proto) {
+  return CountEvents<MmapEventIterator>(proto);
+}
+size_t CountSampleEvents(const quipper::PerfDataProto& proto) {
+  return CountEvents<SampleEventIterator>(proto);
+}
+size_t CountForkEvents(const quipper::PerfDataProto& proto) {
+  return CountEvents<ForkEventIterator>(proto);
+}
+size_t CountExitEvents(const quipper::PerfDataProto& proto) {
+  return CountEvents<ExitEventIterator>(proto);
+}
+
+std::string CreateStats(const quipper::PerfDataProto& proto) {
+  std::ostringstream oss;
+  oss << "Mmap events: "   << CountMmapEvents(proto) << std::endl;
+  oss << "Sample events: " << CountSampleEvents(proto) << std::endl;
+  oss << "Comm events: "   << CountCommEvents(proto) << std::endl;
+  oss << "Fork events: "   << CountForkEvents(proto) << std::endl;
+  oss << "Exit events: "   << CountExitEvents(proto) << std::endl;
+  return oss.str();
+}
+
+std::string FormatSampleEvent(const quipper::PerfDataProto_SampleEvent& sample) {
+  std::ostringstream oss;
+  if (sample.has_pid()) {
+    oss << "pid=" << sample.pid();
+  }
+  if (sample.has_tid()) {
+    oss << " tid=" << sample.tid();
+  }
+  if (sample.has_ip()) {
+      oss << " ip=" << sample.ip();
+    }
+  if (sample.has_addr()) {
+    oss << " addr=" << sample.addr();
+  }
+  if (sample.callchain_size() > 0) {
+    oss << " callchain=";
+    for (uint64_t cc : sample.callchain()) {
+      oss << "->" << cc;
+    }
+  }
+  return oss.str();
+}
+
+}
+
+struct BasicRunWithCannedPerf : PerfProfdTest {
+  void VerifyBasicCannedProfile(const android::perfprofd::PerfprofdRecord& encodedProfile) {
+    ASSERT_TRUE(encodedProfile.has_perf_data()) << test_logger.JoinTestLog(" ");
+    const quipper::PerfDataProto& perf_data = encodedProfile.perf_data();
+
+    // Expect 21108 events.
+    EXPECT_EQ(21108, perf_data.events_size()) << CreateStats(perf_data);
+
+    EXPECT_EQ(48,    CountMmapEvents(perf_data)) << CreateStats(perf_data);
+    EXPECT_EQ(19986, CountSampleEvents(perf_data)) << CreateStats(perf_data);
+    EXPECT_EQ(1033,  CountCommEvents(perf_data)) << CreateStats(perf_data);
+    EXPECT_EQ(15,    CountForkEvents(perf_data)) << CreateStats(perf_data);
+    EXPECT_EQ(26,    CountExitEvents(perf_data)) << CreateStats(perf_data);
+
+    if (HasNonfatalFailure()) {
+      FAIL();
+    }
+
+    {
+      MmapEventIterator mmap(perf_data);
+      constexpr std::pair<const char*, uint64_t> kMmapEvents[] = {
+          std::make_pair("[kernel.kallsyms]_text", 0),
+          std::make_pair("/system/lib/libc.so", 3067412480u),
+          std::make_pair("/system/vendor/lib/libdsutils.so", 3069911040u),
+          std::make_pair("/system/lib/libc.so", 3067191296u),
+          std::make_pair("/system/lib/libc++.so", 3069210624u),
+          std::make_pair("/data/dalvik-cache/arm/system@framework@boot.oat", 1900048384u),
+          std::make_pair("/system/lib/libjavacore.so", 2957135872u),
+          std::make_pair("/system/vendor/lib/libqmi_encdec.so", 3006644224u),
+          std::make_pair("/data/dalvik-cache/arm/system@framework@wifi-service.jar@classes.dex",
+                         3010351104u),
+                         std::make_pair("/system/lib/libart.so", 3024150528u),
+                         std::make_pair("/system/lib/libz.so", 3056410624u),
+                         std::make_pair("/system/lib/libicui18n.so", 3057610752u),
+      };
+      for (auto& pair : kMmapEvents) {
+        EXPECT_STREQ(pair.first, mmap->mmap_event().filename().c_str());
+        EXPECT_EQ(pair.second, mmap->mmap_event().start()) << pair.first;
+        ++mmap;
+      }
+    }
+
+    {
+      CommEventIterator comm(perf_data);
+      constexpr const char* kCommEvents[] = {
+          "init", "kthreadd", "ksoftirqd/0", "kworker/u:0H", "migration/0", "khelper",
+          "netns", "modem_notifier", "smd_channel_clo", "smsm_cb_wq", "rpm-smd", "kworker/u:1H",
+      };
+      for (auto str : kCommEvents) {
+        EXPECT_STREQ(str, comm->comm_event().comm().c_str());
+        ++comm;
+      }
+    }
+
+    {
+      SampleEventIterator samples(perf_data);
+      constexpr const char* kSampleEvents[] = {
+          "pid=0 tid=0 ip=3222720196",
+          "pid=0 tid=0 ip=3222910876",
+          "pid=0 tid=0 ip=3222910876",
+          "pid=0 tid=0 ip=3222910876",
+          "pid=0 tid=0 ip=3222910876",
+          "pid=0 tid=0 ip=3222910876",
+          "pid=0 tid=0 ip=3222910876",
+          "pid=3 tid=3 ip=3231975108",
+          "pid=5926 tid=5926 ip=3231964952",
+          "pid=5926 tid=5926 ip=3225342428",
+          "pid=5926 tid=5926 ip=3223841448",
+          "pid=5926 tid=5926 ip=3069807920",
+      };
+      for (auto str : kSampleEvents) {
+        EXPECT_STREQ(str, FormatSampleEvent(samples->sample_event()).c_str());
+        ++samples;
+      }
+
+      // Skip some samples.
+      for (size_t i = 0; i != 5000; ++i) {
+        ++samples;
+      }
+      constexpr const char* kSampleEvents2[] = {
+          "pid=5938 tid=5938 ip=3069630992",
+          "pid=5938 tid=5938 ip=3069626616",
+          "pid=5938 tid=5938 ip=3069626636",
+          "pid=5938 tid=5938 ip=3069637212",
+          "pid=5938 tid=5938 ip=3069637208",
+          "pid=5938 tid=5938 ip=3069637252",
+          "pid=5938 tid=5938 ip=3069346040",
+          "pid=5938 tid=5938 ip=3069637128",
+          "pid=5938 tid=5938 ip=3069626616",
+      };
+      for (auto str : kSampleEvents2) {
+        EXPECT_STREQ(str, FormatSampleEvent(samples->sample_event()).c_str());
+        ++samples;
+      }
+
+      // Skip some samples.
+      for (size_t i = 0; i != 5000; ++i) {
+        ++samples;
+      }
+      constexpr const char* kSampleEvents3[] = {
+          "pid=5938 tid=5938 ip=3069912036",
+          "pid=5938 tid=5938 ip=3069637260",
+          "pid=5938 tid=5938 ip=3069631024",
+          "pid=5938 tid=5938 ip=3069346064",
+          "pid=5938 tid=5938 ip=3069637356",
+          "pid=5938 tid=5938 ip=3069637144",
+          "pid=5938 tid=5938 ip=3069912036",
+          "pid=5938 tid=5938 ip=3069912036",
+          "pid=5938 tid=5938 ip=3069631244",
+      };
+      for (auto str : kSampleEvents3) {
+        EXPECT_STREQ(str, FormatSampleEvent(samples->sample_event()).c_str());
+        ++samples;
+      }
+    }
+  }
+};
+
+TEST_F(BasicRunWithCannedPerf, Basic)
 {
   //
   // Verify the portion of the daemon that reads and encodes
@@ -582,65 +847,134 @@ TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
   config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
   config_reader.overrideUnsignedEntry("collect_charging_state", 0);
   config_reader.overrideUnsignedEntry("collect_camera_active", 0);
+
+  // Disable compression.
+  config_reader.overrideUnsignedEntry("compress", 0);
+
   PerfProfdRunner::LoggingConfig config;
   config_reader.FillConfig(&config);
 
   // Kick off encoder and check return code
   PROFILE_RESULT result =
-      encode_to_proto(input_perf_data, encoded_file_path(0).c_str(), config, 0);
-  EXPECT_EQ(OK_PROFILE_COLLECTION, result);
+      encode_to_proto(input_perf_data, encoded_file_path(dest_dir, 0).c_str(), config, 0, nullptr);
+  ASSERT_EQ(OK_PROFILE_COLLECTION, result) << test_logger.JoinTestLog(" ");
 
   // Read and decode the resulting perf.data.encoded file
-  wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
-  readEncodedProfile("BasicRunWithCannedPerf",
-                     encodedProfile);
-
-  // Expect 45 programs
-  EXPECT_EQ(45, encodedProfile.programs_size());
-
-  // Check a couple of load modules
-  { const auto &lm0 = encodedProfile.load_modules(0);
-    std::string act_lm0 = encodedLoadModuleToString(lm0);
-    std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
-    const std::string expected_lm0 = RAW_RESULT(
-        name: "/data/app/com.google.android.apps.plus-1/lib/arm/libcronet.so"
-                                                );
-    std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
-    EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
-  }
-  { const auto &lm9 = encodedProfile.load_modules(9);
-    std::string act_lm9 = encodedLoadModuleToString(lm9);
-    std::string sqact9 = squeezeWhite(act_lm9, "actual for lm 9");
-    const std::string expected_lm9 = RAW_RESULT(
-        name: "/system/lib/libandroid_runtime.so" build_id: "8164ed7b3a8b8f5a220d027788922510"
-                                                );
-    std::string sqexp9 = squeezeWhite(expected_lm9, "expected_lm9");
-    EXPECT_STREQ(sqexp9.c_str(), sqact9.c_str());
-  }
+  android::perfprofd::PerfprofdRecord encodedProfile;
+  readEncodedProfile(dest_dir, false, encodedProfile);
 
-  // Examine some of the samples now
-  { const auto &p1 = encodedProfile.programs(0);
-    const auto &lm1 = p1.modules(0);
-    std::string act_lm1 = encodedModuleSamplesToString(lm1);
-    std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
-    const std::string expected_lm1 = RAW_RESULT(
-        load_module_id: 9 address_samples { address: 296100 count: 1 }
-                                                );
-    std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
-    EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
-  }
-  { const auto &p1 = encodedProfile.programs(2);
-    const auto &lm2 = p1.modules(0);
-    std::string act_lm2 = encodedModuleSamplesToString(lm2);
-    std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
-    const std::string expected_lm2 = RAW_RESULT(
-        load_module_id: 2
-        address_samples { address: 28030244 count: 1 }
-        address_samples { address: 29657840 count: 1 }
-                                                );
-    std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
-    EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
-  }
+  VerifyBasicCannedProfile(encodedProfile);
+}
+
+TEST_F(BasicRunWithCannedPerf, Compressed)
+{
+  //
+  // Verify the portion of the daemon that reads and encodes
+  // perf.data files. Here we run the encoder on a canned perf.data
+  // file and verify that the resulting protobuf contains what
+  // we think it should contain.
+  //
+  std::string input_perf_data(test_dir);
+  input_perf_data += "/canned.perf.data";
+
+  // Set up config to avoid these annotations (they are tested elsewhere)
+  ConfigReader config_reader;
+  config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
+  config_reader.overrideUnsignedEntry("collect_charging_state", 0);
+  config_reader.overrideUnsignedEntry("collect_camera_active", 0);
+
+  // Enable compression.
+  config_reader.overrideUnsignedEntry("compress", 1);
+
+  PerfProfdRunner::LoggingConfig config;
+  config_reader.FillConfig(&config);
+
+  // Kick off encoder and check return code
+  PROFILE_RESULT result =
+      encode_to_proto(input_perf_data, encoded_file_path(dest_dir, 0).c_str(), config, 0, nullptr);
+  ASSERT_EQ(OK_PROFILE_COLLECTION, result) << test_logger.JoinTestLog(" ");
+
+  // Read and decode the resulting perf.data.encoded file
+  android::perfprofd::PerfprofdRecord encodedProfile;
+  readEncodedProfile(dest_dir, true, encodedProfile);
+
+  VerifyBasicCannedProfile(encodedProfile);
+}
+
+TEST_F(BasicRunWithCannedPerf, WithSymbolizer)
+{
+  //
+  // Verify the portion of the daemon that reads and encodes
+  // perf.data files. Here we run the encoder on a canned perf.data
+  // file and verify that the resulting protobuf contains what
+  // we think it should contain.
+  //
+  std::string input_perf_data(test_dir);
+  input_perf_data += "/canned.perf.data";
+
+  // Set up config to avoid these annotations (they are tested elsewhere)
+  ConfigReader config_reader;
+  config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
+  config_reader.overrideUnsignedEntry("collect_charging_state", 0);
+  config_reader.overrideUnsignedEntry("collect_camera_active", 0);
+
+  // Disable compression.
+  config_reader.overrideUnsignedEntry("compress", 0);
+
+  PerfProfdRunner::LoggingConfig config;
+  config_reader.FillConfig(&config);
+
+  // Kick off encoder and check return code
+  struct TestSymbolizer : public perfprofd::Symbolizer {
+    std::string Decode(const std::string& dso, uint64_t address) override {
+      return dso + "@" + std::to_string(address);
+    }
+    bool GetMinExecutableVAddr(const std::string& dso, uint64_t* addr) override {
+      *addr = 4096;
+      return true;
+    }
+  };
+  TestSymbolizer test_symbolizer;
+  PROFILE_RESULT result =
+      encode_to_proto(input_perf_data,
+                      encoded_file_path(dest_dir, 0).c_str(),
+                      config,
+                      0,
+                      &test_symbolizer);
+  ASSERT_EQ(OK_PROFILE_COLLECTION, result);
+
+  // Read and decode the resulting perf.data.encoded file
+  android::perfprofd::PerfprofdRecord encodedProfile;
+  readEncodedProfile(dest_dir, false, encodedProfile);
+
+  VerifyBasicCannedProfile(encodedProfile);
+
+  auto find_symbol = [&](const std::string& filename)
+      -> const android::perfprofd::PerfprofdRecord_SymbolInfo* {
+    for (auto& symbol_info : encodedProfile.symbol_info()) {
+      if (symbol_info.filename() == filename) {
+        return &symbol_info;
+      }
+    }
+    return nullptr;
+  };
+  auto all_filenames = [&]() {
+    std::ostringstream oss;
+    for (auto& symbol_info : encodedProfile.symbol_info()) {
+      oss << " " << symbol_info.filename();
+    }
+    return oss.str();
+  };
+
+  EXPECT_TRUE(find_symbol("/data/app/com.google.android.apps.plus-1/lib/arm/libcronet.so")
+                  != nullptr) << all_filenames() << test_logger.JoinTestLog("\n");
+  EXPECT_TRUE(find_symbol("/data/dalvik-cache/arm/system@framework@wifi-service.jar@classes.dex")
+                  != nullptr) << all_filenames();
+  EXPECT_TRUE(find_symbol("/data/dalvik-cache/arm/data@app@com.google.android.gms-2@base.apk@"
+                          "classes.dex")
+                  != nullptr) << all_filenames();
+  EXPECT_TRUE(find_symbol("/data/dalvik-cache/arm/system@framework@boot.oat") != nullptr)
+      << all_filenames();
 }
 
 TEST_F(PerfProfdTest, CallchainRunWithCannedPerf)
@@ -656,87 +990,95 @@ TEST_F(PerfProfdTest, CallchainRunWithCannedPerf)
   config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
   config_reader.overrideUnsignedEntry("collect_charging_state", 0);
   config_reader.overrideUnsignedEntry("collect_camera_active", 0);
+
+  // Disable compression.
+  config_reader.overrideUnsignedEntry("compress", 0);
+
   PerfProfdRunner::LoggingConfig config;
   config_reader.FillConfig(&config);
 
   // Kick off encoder and check return code
   PROFILE_RESULT result =
-      encode_to_proto(input_perf_data, encoded_file_path(0).c_str(), config, 0);
-  EXPECT_EQ(OK_PROFILE_COLLECTION, result);
+      encode_to_proto(input_perf_data, encoded_file_path(dest_dir, 0).c_str(), config, 0, nullptr);
+  ASSERT_EQ(OK_PROFILE_COLLECTION, result);
 
   // Read and decode the resulting perf.data.encoded file
-  wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
-  readEncodedProfile("BasicRunWithCannedPerf",
-                     encodedProfile);
-
-
-  // Expect 3 programs 8 load modules
-  EXPECT_EQ(3, encodedProfile.programs_size());
-  EXPECT_EQ(8, encodedProfile.load_modules_size());
-
-  // Check a couple of load modules
-  { const auto &lm0 = encodedProfile.load_modules(0);
-    std::string act_lm0 = encodedLoadModuleToString(lm0);
-    std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
-    const std::string expected_lm0 = RAW_RESULT(
-        name: "/system/bin/dex2oat"
-        build_id: "ee12bd1a1de39422d848f249add0afc4"
-                                                );
-    std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
-    EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
-  }
-  { const auto &lm1 = encodedProfile.load_modules(1);
-    std::string act_lm1 = encodedLoadModuleToString(lm1);
-    std::string sqact1 = squeezeWhite(act_lm1, "actual for lm 1");
-    const std::string expected_lm1 = RAW_RESULT(
-        name: "/system/bin/linker"
-        build_id: "a36715f673a4a0aa76ef290124c516cc"
-                                                );
-    std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
-    EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
-  }
+  android::perfprofd::PerfprofdRecord encodedProfile;
+  readEncodedProfile(dest_dir, false, encodedProfile);
 
-  // Examine some of the samples now
-  { const auto &p0 = encodedProfile.programs(0);
-    const auto &lm1 = p0.modules(0);
-    std::string act_lm1 = encodedModuleSamplesToString(lm1);
-    std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
-    const std::string expected_lm1 = RAW_RESULT(
-        load_module_id: 0
-        address_samples { address: 108552 count: 2 }
-                                                );
-    std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
-    EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
-  }
-  { const auto &p4 = encodedProfile.programs(2);
-    const auto &lm2 = p4.modules(1);
-    std::string act_lm2 = encodedModuleSamplesToString(lm2);
-    std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
-    const std::string expected_lm2 = RAW_RESULT(
-        load_module_id: 2 address_samples { address: 403913 count: 1 } address_samples { address: 840761 count: 1 } address_samples { address: 846481 count: 1 } address_samples { address: 999053 count: 1 } address_samples { address: 1012959 count: 1 } address_samples { address: 1524309 count: 1 } address_samples { address: 1580779 count: 1 } address_samples { address: 4287986288 count: 1 }
-                                                );
-    std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
-    EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
+
+  ASSERT_TRUE(encodedProfile.has_perf_data());
+  const quipper::PerfDataProto& perf_data = encodedProfile.perf_data();
+
+  // Expect 21108 events.
+  EXPECT_EQ(2224, perf_data.events_size()) << CreateStats(perf_data);
+
+  {
+      SampleEventIterator samples(perf_data);
+      constexpr const char* kSampleEvents[] = {
+          "0: pid=6225 tid=6225 ip=18446743798834668032 callchain=->18446744073709551488->"
+              "18446743798834668032->18446743798834782596->18446743798834784624->"
+              "18446743798835055136->18446743798834788016->18446743798834789192->"
+              "18446743798834789512->18446743798834790216->18446743798833756776",
+          "1: pid=6225 tid=6225 ip=18446743798835685700 callchain=->18446744073709551488->"
+              "18446743798835685700->18446743798835688704->18446743798835650964->"
+              "18446743798834612104->18446743798834612276->18446743798835055528->"
+              "18446743798834788016->18446743798834789192->18446743798834789512->"
+              "18446743798834790216->18446743798833756776",
+          "2: pid=6225 tid=6225 ip=18446743798835055804 callchain=->18446744073709551488->"
+              "18446743798835055804->18446743798834788016->18446743798834789192->"
+              "18446743798834789512->18446743798834790216->18446743798833756776",
+          "3: pid=6225 tid=6225 ip=18446743798835991212 callchain=->18446744073709551488->"
+              "18446743798835991212->18446743798834491060->18446743798834675572->"
+              "18446743798834676516->18446743798834612172->18446743798834612276->"
+              "18446743798835056664->18446743798834788016->18446743798834789192->"
+              "18446743798834789512->18446743798834790216->18446743798833756776",
+          "4: pid=6225 tid=6225 ip=18446743798844881108 callchain=->18446744073709551488->"
+              "18446743798844881108->18446743798834836140->18446743798834846384->"
+              "18446743798834491100->18446743798834675572->18446743798834676516->"
+              "18446743798834612172->18446743798834612276->18446743798835056784->"
+              "18446743798834788016->18446743798834789192->18446743798834789512->"
+              "18446743798834790216->18446743798833756776",
+      };
+      size_t cmp_index = 0;
+      for (size_t index = 0; samples != samples.end(); ++samples, ++index) {
+        if (samples->sample_event().callchain_size() > 0) {
+          std::ostringstream oss;
+          oss << index << ": " << FormatSampleEvent(samples->sample_event());
+          EXPECT_STREQ(kSampleEvents[cmp_index], oss.str().c_str());
+          cmp_index++;
+          if (cmp_index == arraysize(kSampleEvents)) {
+            break;
+          }
+        }
+      }
   }
 }
 
+#ifdef __ANDROID__
+
 TEST_F(PerfProfdTest, BasicRunWithLivePerf)
 {
   //
   // Basic test to exercise the main loop of the daemon. It includes
   // a live 'perf' run
   //
-  PerfProfdRunner runner;
+  PerfProfdRunner runner(conf_dir);
   runner.addToConfig("only_debug_build=0");
   std::string ddparam("destination_directory="); ddparam += dest_dir;
   runner.addToConfig(ddparam);
-  std::string cfparam("config_directory="); cfparam += test_dir;
+  std::string cfparam("config_directory="); cfparam += conf_dir;
   runner.addToConfig(cfparam);
   runner.addToConfig("main_loop_iterations=1");
   runner.addToConfig("use_fixed_seed=12345678");
   runner.addToConfig("max_unprocessed_profiles=100");
   runner.addToConfig("collection_interval=9999");
   runner.addToConfig("sample_duration=2");
+  // Avoid the symbolizer for spurious messages.
+  runner.addToConfig("use_elf_symbolizer=0");
+
+  // Disable compression.
+  runner.addToConfig("compress=0");
 
   // Create semaphore file
   runner.create_semaphore_file();
@@ -745,30 +1087,31 @@ TEST_F(PerfProfdTest, BasicRunWithLivePerf)
   int daemon_main_return_code = runner.invoke();
 
   // Check return code from daemon
-  EXPECT_EQ(0, daemon_main_return_code);
+  ASSERT_EQ(0, daemon_main_return_code);
 
   // Read and decode the resulting perf.data.encoded file
-  wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
-  readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
+  android::perfprofd::PerfprofdRecord encodedProfile;
+  readEncodedProfile(dest_dir, false, encodedProfile);
 
   // Examine what we get back. Since it's a live profile, we can't
   // really do much in terms of verifying the contents.
-  EXPECT_LT(0, encodedProfile.programs_size());
+  EXPECT_LT(0, encodedProfile.perf_data().events_size());
 
   // Verify log contents
-  const std::string expected = RAW_RESULT(
-      I: starting Android Wide Profiling daemon
-      I: config file path set to $NATIVE_TESTS/perfprofd_test/perfprofd.conf
+  const std::string expected = std::string(
+      "I: starting Android Wide Profiling daemon ") +
+      "I: config file path set to " + conf_dir + "/perfprofd.conf " +
+      RAW_RESULT(
       I: random seed set to 12345678
       I: sleep 674 seconds
       I: initiating profile collection
+      I: sleep 2 seconds
       I: profile collection complete
       I: sleep 9325 seconds
       I: finishing Android Wide Profiling daemon
                                           );
   // check to make sure log excerpt matches
-  compareLogMessages(mock_perfprofdutils_getlogged(),
-                     expandVars(expected), "BasicRunWithLivePerf", true);
+  CompareLogMessages(expandVars(expected), "BasicRunWithLivePerf", true);
 }
 
 TEST_F(PerfProfdTest, MultipleRunWithLivePerf)
@@ -777,16 +1120,22 @@ TEST_F(PerfProfdTest, MultipleRunWithLivePerf)
   // Basic test to exercise the main loop of the daemon. It includes
   // a live 'perf' run
   //
-  PerfProfdRunner runner;
+  PerfProfdRunner runner(conf_dir);
   runner.addToConfig("only_debug_build=0");
   std::string ddparam("destination_directory="); ddparam += dest_dir;
   runner.addToConfig(ddparam);
-  std::string cfparam("config_directory="); cfparam += test_dir;
+  std::string cfparam("config_directory="); cfparam += conf_dir;
   runner.addToConfig(cfparam);
   runner.addToConfig("main_loop_iterations=3");
   runner.addToConfig("use_fixed_seed=12345678");
   runner.addToConfig("collection_interval=9999");
   runner.addToConfig("sample_duration=2");
+  // Avoid the symbolizer for spurious messages.
+  runner.addToConfig("use_elf_symbolizer=0");
+
+  // Disable compression.
+  runner.addToConfig("compress=0");
+
   runner.write_processed_file(1, 2);
 
   // Create semaphore file
@@ -796,43 +1145,46 @@ TEST_F(PerfProfdTest, MultipleRunWithLivePerf)
   int daemon_main_return_code = runner.invoke();
 
   // Check return code from daemon
-  EXPECT_EQ(0, daemon_main_return_code);
+  ASSERT_EQ(0, daemon_main_return_code);
 
   // Read and decode the resulting perf.data.encoded file
-  wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
-  readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
+  android::perfprofd::PerfprofdRecord encodedProfile;
+  readEncodedProfile(dest_dir, false, encodedProfile);
 
   // Examine what we get back. Since it's a live profile, we can't
   // really do much in terms of verifying the contents.
-  EXPECT_LT(0, encodedProfile.programs_size());
+  EXPECT_LT(0, encodedProfile.perf_data().events_size());
 
   // Examine that encoded.1 file is removed while encoded.{0|2} exists.
-  EXPECT_EQ(0, access(encoded_file_path(0).c_str(), F_OK));
-  EXPECT_NE(0, access(encoded_file_path(1).c_str(), F_OK));
-  EXPECT_EQ(0, access(encoded_file_path(2).c_str(), F_OK));
+  EXPECT_EQ(0, access(encoded_file_path(dest_dir, 0).c_str(), F_OK));
+  EXPECT_NE(0, access(encoded_file_path(dest_dir, 1).c_str(), F_OK));
+  EXPECT_EQ(0, access(encoded_file_path(dest_dir, 2).c_str(), F_OK));
 
   // Verify log contents
-  const std::string expected = RAW_RESULT(
-      I: starting Android Wide Profiling daemon
-      I: config file path set to $NATIVE_TESTS/perfprofd_test/perfprofd.conf
+  const std::string expected = std::string(
+      "I: starting Android Wide Profiling daemon ") +
+      "I: config file path set to " + conf_dir + "/perfprofd.conf " +
+      RAW_RESULT(
       I: random seed set to 12345678
       I: sleep 674 seconds
       I: initiating profile collection
+      I: sleep 2 seconds
       I: profile collection complete
       I: sleep 9325 seconds
       I: sleep 4974 seconds
       I: initiating profile collection
+      I: sleep 2 seconds
       I: profile collection complete
       I: sleep 5025 seconds
       I: sleep 501 seconds
       I: initiating profile collection
+      I: sleep 2 seconds
       I: profile collection complete
       I: sleep 9498 seconds
       I: finishing Android Wide Profiling daemon
                                           );
   // check to make sure log excerpt matches
-  compareLogMessages(mock_perfprofdutils_getlogged(),
-                     expandVars(expected), "BasicRunWithLivePerf", true);
+  CompareLogMessages(expandVars(expected), "BasicRunWithLivePerf", true);
 }
 
 TEST_F(PerfProfdTest, CallChainRunWithLivePerf)
@@ -841,10 +1193,10 @@ TEST_F(PerfProfdTest, CallChainRunWithLivePerf)
   // Collect a callchain profile, so as to exercise the code in
   // perf_data post-processing that digests callchains.
   //
-  PerfProfdRunner runner;
+  PerfProfdRunner runner(conf_dir);
   std::string ddparam("destination_directory="); ddparam += dest_dir;
   runner.addToConfig(ddparam);
-  std::string cfparam("config_directory="); cfparam += test_dir;
+  std::string cfparam("config_directory="); cfparam += conf_dir;
   runner.addToConfig(cfparam);
   runner.addToConfig("main_loop_iterations=1");
   runner.addToConfig("use_fixed_seed=12345678");
@@ -852,6 +1204,11 @@ TEST_F(PerfProfdTest, CallChainRunWithLivePerf)
   runner.addToConfig("collection_interval=9999");
   runner.addToConfig("stack_profile=1");
   runner.addToConfig("sample_duration=2");
+  // Avoid the symbolizer for spurious messages.
+  runner.addToConfig("use_elf_symbolizer=0");
+
+  // Disable compression.
+  runner.addToConfig("compress=0");
 
   // Create semaphore file
   runner.create_semaphore_file();
@@ -860,34 +1217,91 @@ TEST_F(PerfProfdTest, CallChainRunWithLivePerf)
   int daemon_main_return_code = runner.invoke();
 
   // Check return code from daemon
-  EXPECT_EQ(0, daemon_main_return_code);
+  ASSERT_EQ(0, daemon_main_return_code);
 
   // Read and decode the resulting perf.data.encoded file
-  wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
-  readEncodedProfile("CallChainRunWithLivePerf", encodedProfile);
+  android::perfprofd::PerfprofdRecord encodedProfile;
+  readEncodedProfile(dest_dir, false, encodedProfile);
 
   // Examine what we get back. Since it's a live profile, we can't
   // really do much in terms of verifying the contents.
-  EXPECT_LT(0, encodedProfile.programs_size());
+  EXPECT_LT(0, encodedProfile.perf_data().events_size());
 
   // Verify log contents
-  const std::string expected = RAW_RESULT(
-      I: starting Android Wide Profiling daemon
-      I: config file path set to $NATIVE_TESTS/perfprofd_test/perfprofd.conf
+  const std::string expected = std::string(
+      "I: starting Android Wide Profiling daemon ") +
+      "I: config file path set to " + conf_dir + "/perfprofd.conf " +
+      RAW_RESULT(
       I: random seed set to 12345678
       I: sleep 674 seconds
       I: initiating profile collection
+      I: sleep 2 seconds
       I: profile collection complete
       I: sleep 9325 seconds
       I: finishing Android Wide Profiling daemon
                                           );
   // check to make sure log excerpt matches
-  compareLogMessages(mock_perfprofdutils_getlogged(),
-                     expandVars(expected), "CallChainRunWithLivePerf", true);
+  CompareLogMessages(expandVars(expected), "CallChainRunWithLivePerf", true);
+
+  // Check that we have at least one SampleEvent with a callchain.
+  SampleEventIterator samples(encodedProfile.perf_data());
+  bool found_callchain = false;
+  while (!found_callchain && samples != samples.end()) {
+    found_callchain = samples->sample_event().callchain_size() > 0;
+  }
+  EXPECT_TRUE(found_callchain) << CreateStats(encodedProfile.perf_data());
+}
+
+#endif
+
+class RangeMapTest : public testing::Test {
+};
+
+TEST_F(RangeMapTest, TestRangeMap) {
+  using namespace android::perfprofd;
+
+  RangeMap<std::string, uint64_t> map;
+  auto print = [&]() {
+    std::ostringstream oss;
+    for (auto& aggr_sym : map) {
+      oss << aggr_sym.first << "#" << aggr_sym.second.symbol;
+      oss << "[";
+      for (auto& x : aggr_sym.second.offsets) {
+        oss << x << ",";
+      }
+      oss << "]";
+    }
+    return oss.str();
+  };
+
+  EXPECT_STREQ("", print().c_str());
+
+  map.Insert("a", 10);
+  EXPECT_STREQ("10#a[10,]", print().c_str());
+  map.Insert("a", 100);
+  EXPECT_STREQ("10#a[10,100,]", print().c_str());
+  map.Insert("a", 1);
+  EXPECT_STREQ("1#a[1,10,100,]", print().c_str());
+  map.Insert("a", 1);
+  EXPECT_STREQ("1#a[1,10,100,]", print().c_str());
+  map.Insert("a", 2);
+  EXPECT_STREQ("1#a[1,2,10,100,]", print().c_str());
+
+  map.Insert("b", 200);
+  EXPECT_STREQ("1#a[1,2,10,100,]200#b[200,]", print().c_str());
+  map.Insert("b", 199);
+  EXPECT_STREQ("1#a[1,2,10,100,]199#b[199,200,]", print().c_str());
+
+  map.Insert("c", 50);
+  EXPECT_STREQ("1#a[1,2,10,]50#c[50,]100#a[100,]199#b[199,200,]", print().c_str());
 }
 
 int main(int argc, char **argv) {
-  executable_path = argv[0];
+  // Always log to cerr, so that device failures are visible.
+  android::base::SetLogger(android::base::StderrLogger);
+
+  CHECK(android::base::Realpath(argv[0], &gExecutableRealpath));
+
   // switch to / before starting testing (perfprofd
   // should be location-independent)
   chdir("/");