Merge "Simpleperf: add --log option to ease debugging."
diff --git a/simpleperf/cmd_report.cpp b/simpleperf/cmd_report.cpp
index 59acc2c..87479bd 100644
--- a/simpleperf/cmd_report.cpp
+++ b/simpleperf/cmd_report.cpp
@@ -34,192 +34,195 @@
 #include "record_file.h"
 #include "sample_tree.h"
 
-typedef int (*compare_sample_entry_t)(const SampleEntry& sample1, const SampleEntry& sample2);
-typedef std::string (*print_sample_entry_header_t)();
-typedef std::string (*print_sample_entry_t)(const SampleEntry& sample);
-
-struct ReportItem {
-  size_t width;
-  compare_sample_entry_t compare_function;
-  print_sample_entry_header_t print_header_function;
-  print_sample_entry_t print_function;
-};
-
-static int ComparePid(const SampleEntry& sample1, const SampleEntry& sample2) {
-  return sample1.thread->pid - sample2.thread->pid;
-}
-
-static std::string PrintHeaderPid() {
-  return "Pid";
-}
-
-static std::string PrintPid(const SampleEntry& sample) {
-  return android::base::StringPrintf("%d", sample.thread->pid);
-}
-
-static ReportItem report_pid = {
-    .compare_function = ComparePid,
-    .print_header_function = PrintHeaderPid,
-    .print_function = PrintPid,
-};
-
-static int CompareTid(const SampleEntry& sample1, const SampleEntry& sample2) {
-  return sample1.thread->tid - sample2.thread->tid;
-}
-
-static std::string PrintHeaderTid() {
-  return "Tid";
-}
-
-static std::string PrintTid(const SampleEntry& sample) {
-  return android::base::StringPrintf("%d", sample.thread->tid);
-}
-
-static ReportItem report_tid = {
-    .compare_function = CompareTid,
-    .print_header_function = PrintHeaderTid,
-    .print_function = PrintTid,
-};
-
-static int CompareComm(const SampleEntry& sample1, const SampleEntry& sample2) {
-  return strcmp(sample1.thread_comm, sample2.thread_comm);
-}
-
-static std::string PrintHeaderComm() {
-  return "Command";
-}
-
-static std::string PrintComm(const SampleEntry& sample) {
-  return sample.thread_comm;
-}
-
-static ReportItem report_comm = {
-    .compare_function = CompareComm,
-    .print_header_function = PrintHeaderComm,
-    .print_function = PrintComm,
-};
-
-static int CompareDso(const SampleEntry& sample1, const SampleEntry& sample2) {
-  return strcmp(sample1.map->dso->path.c_str(), sample2.map->dso->path.c_str());
-}
-
-static std::string PrintHeaderDso() {
-  return "Shared Object";
-}
-
-static std::string PrintDso(const SampleEntry& sample) {
-  std::string filename = sample.map->dso->path;
-  if (filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) {
-    filename = "[unknown]";
+class Displayable {
+ public:
+  Displayable(const std::string& name) : name_(name), width_(name.size()) {
   }
-  return filename;
-}
 
-static ReportItem report_dso = {
-    .compare_function = CompareDso,
-    .print_header_function = PrintHeaderDso,
-    .print_function = PrintDso,
+  virtual ~Displayable() {
+  }
+
+  const std::string& Name() const {
+    return name_;
+  }
+  size_t Width() const {
+    return width_;
+  }
+
+  virtual std::string Show(const SampleEntry& sample) const = 0;
+  void AdjustWidth(const SampleEntry& sample) {
+    size_t size = Show(sample).size();
+    width_ = std::max(width_, size);
+  }
+
+ private:
+  const std::string name_;
+  size_t width_;
 };
 
-static int CompareSymbol(const SampleEntry& sample1, const SampleEntry& sample2) {
-  return strcmp(sample1.symbol->name.c_str(), sample2.symbol->name.c_str());
-}
+class AccumulatedOverheadItem : public Displayable {
+ public:
+  AccumulatedOverheadItem(const SampleTree& sample_tree)
+      : Displayable("Children"), sample_tree_(sample_tree) {
+  }
 
-static std::string PrintHeaderSymbol() {
-  return "Symbol";
-}
+  std::string Show(const SampleEntry& sample) const override {
+    uint64_t period = sample.period + sample.accumulated_period;
+    uint64_t total_period = sample_tree_.TotalPeriod();
+    double percentage = (total_period != 0) ? 100.0 * period / total_period : 0.0;
+    return android::base::StringPrintf("%.2lf%%", percentage);
+  }
 
-static std::string PrintSymbol(const SampleEntry& sample) {
-  return sample.symbol->name;
-}
-
-static ReportItem report_symbol = {
-    .compare_function = CompareSymbol,
-    .print_header_function = PrintHeaderSymbol,
-    .print_function = PrintSymbol,
+ private:
+  const SampleTree& sample_tree_;
 };
 
-static int CompareDsoFrom(const SampleEntry& sample1, const SampleEntry& sample2) {
-  return strcmp(sample1.branch_from.map->dso->path.c_str(),
-                sample2.branch_from.map->dso->path.c_str());
-}
+class SelfOverheadItem : public Displayable {
+ public:
+  SelfOverheadItem(const SampleTree& sample_tree, const std::string& name = "Self")
+      : Displayable(name), sample_tree_(sample_tree) {
+  }
 
-static std::string PrintHeaderDsoFrom() {
-  return "Source Shared Object";
-}
+  std::string Show(const SampleEntry& sample) const override {
+    uint64_t period = sample.period;
+    uint64_t total_period = sample_tree_.TotalPeriod();
+    double percentage = (total_period != 0) ? 100.0 * period / total_period : 0.0;
+    return android::base::StringPrintf("%.2lf%%", percentage);
+  }
 
-static std::string PrintDsoFrom(const SampleEntry& sample) {
-  return sample.branch_from.map->dso->path;
-}
-
-static ReportItem report_dso_from = {
-    .compare_function = CompareDsoFrom,
-    .print_header_function = PrintHeaderDsoFrom,
-    .print_function = PrintDsoFrom,
+ private:
+  const SampleTree& sample_tree_;
 };
 
-static std::string PrintHeaderDsoTo() {
-  return "Target Shared Object";
-}
+class SampleCountItem : public Displayable {
+ public:
+  SampleCountItem() : Displayable("Sample") {
+  }
 
-static ReportItem report_dso_to = {
-    .compare_function = CompareDso,
-    .print_header_function = PrintHeaderDsoTo,
-    .print_function = PrintDso,
+  std::string Show(const SampleEntry& sample) const override {
+    return android::base::StringPrintf("%" PRId64, sample.sample_count);
+  }
 };
 
-static int CompareSymbolFrom(const SampleEntry& sample1, const SampleEntry& sample2) {
-  return strcmp(sample1.branch_from.symbol->name.c_str(), sample2.branch_from.symbol->name.c_str());
-}
+class Comparable {
+ public:
+  virtual ~Comparable() {
+  }
 
-static std::string PrintHeaderSymbolFrom() {
-  return "Source Symbol";
-}
-
-static std::string PrintSymbolFrom(const SampleEntry& sample) {
-  return sample.branch_from.symbol->name;
-}
-
-static ReportItem report_symbol_from = {
-    .compare_function = CompareSymbolFrom,
-    .print_header_function = PrintHeaderSymbolFrom,
-    .print_function = PrintSymbolFrom,
+  virtual int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const = 0;
 };
 
-static std::string PrintHeaderSymbolTo() {
-  return "Target Symbol";
-}
+class PidItem : public Displayable, public Comparable {
+ public:
+  PidItem() : Displayable("Pid") {
+  }
 
-static ReportItem report_symbol_to = {
-    .compare_function = CompareSymbol,
-    .print_header_function = PrintHeaderSymbolTo,
-    .print_function = PrintSymbol,
+  int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
+    return sample1.thread->pid - sample2.thread->pid;
+  }
+
+  std::string Show(const SampleEntry& sample) const override {
+    return android::base::StringPrintf("%d", sample.thread->pid);
+  }
 };
 
-static std::string PrintHeaderSampleCount() {
-  return "Sample";
-}
+class TidItem : public Displayable, public Comparable {
+ public:
+  TidItem() : Displayable("Tid") {
+  }
 
-static std::string PrintSampleCount(const SampleEntry& sample) {
-  return android::base::StringPrintf("%" PRId64, sample.sample_count);
-}
+  int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
+    return sample1.thread->tid - sample2.thread->tid;
+  }
 
-static ReportItem report_sample_count = {
-    .compare_function = nullptr,
-    .print_header_function = PrintHeaderSampleCount,
-    .print_function = PrintSampleCount,
+  std::string Show(const SampleEntry& sample) const override {
+    return android::base::StringPrintf("%d", sample.thread->tid);
+  }
 };
 
-static std::unordered_map<std::string, ReportItem*> report_item_map = {
-    {"comm", &report_comm},
-    {"pid", &report_pid},
-    {"tid", &report_tid},
-    {"dso", &report_dso},
-    {"symbol", &report_symbol},
-    {"dso_from", &report_dso_from},
-    {"dso_to", &report_dso_to},
-    {"symbol_from", &report_symbol_from},
-    {"symbol_to", &report_symbol_to}};
+class CommItem : public Displayable, public Comparable {
+ public:
+  CommItem() : Displayable("Command") {
+  }
+
+  int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
+    return strcmp(sample1.thread_comm, sample2.thread_comm);
+  }
+
+  std::string Show(const SampleEntry& sample) const override {
+    return sample.thread_comm;
+  }
+};
+
+class DsoItem : public Displayable, public Comparable {
+ public:
+  DsoItem(const std::string& name = "Shared Object") : Displayable(name) {
+  }
+
+  int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
+    return strcmp(sample1.map->dso->path.c_str(), sample2.map->dso->path.c_str());
+  }
+
+  std::string Show(const SampleEntry& sample) const override {
+    return sample.map->dso->path;
+  }
+};
+
+class SymbolItem : public Displayable, public Comparable {
+ public:
+  SymbolItem(const std::string& name = "Symbol") : Displayable(name) {
+  }
+
+  int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
+    return strcmp(sample1.symbol->name.c_str(), sample2.symbol->name.c_str());
+  }
+
+  std::string Show(const SampleEntry& sample) const override {
+    return sample.symbol->name;
+  }
+};
+
+class DsoFromItem : public Displayable, public Comparable {
+ public:
+  DsoFromItem() : Displayable("Source Shared Object") {
+  }
+
+  int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
+    return strcmp(sample1.branch_from.map->dso->path.c_str(),
+                  sample2.branch_from.map->dso->path.c_str());
+  }
+
+  std::string Show(const SampleEntry& sample) const override {
+    return sample.branch_from.map->dso->path;
+  }
+};
+
+class DsoToItem : public DsoItem {
+ public:
+  DsoToItem() : DsoItem("Target Shared Object") {
+  }
+};
+
+class SymbolFromItem : public Displayable, public Comparable {
+ public:
+  SymbolFromItem() : Displayable("Source Symbol") {
+  }
+
+  int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
+    return strcmp(sample1.branch_from.symbol->name.c_str(),
+                  sample2.branch_from.symbol->name.c_str());
+  }
+
+  std::string Show(const SampleEntry& sample) const override {
+    return sample.branch_from.symbol->name;
+  }
+};
+
+class SymbolToItem : public SymbolItem {
+ public:
+  SymbolToItem() : SymbolItem("Target Symbol") {
+  }
+};
 
 static std::set<std::string> branch_sort_keys = {
     "dso_from", "dso_to", "symbol_from", "symbol_to",
@@ -233,7 +236,8 @@
             "Usage: simpleperf report [options]\n"
             "    -b            Use the branch-to addresses in sampled take branches instead of\n"
             "                  the instruction addresses. Only valid for perf.data recorded with\n"
-            "                  -b/-j option."
+            "                  -b/-j option.\n"
+            "    --children    Print the overhead accumulated by appearing in the callchain.\n"
             "    -i <file>     Specify path of record file, default is perf.data.\n"
             "    -n            Print the sample count for each item.\n"
             "    --no-demangle        Don't demangle symbol names.\n"
@@ -244,7 +248,11 @@
             "                  used with -b option. Default keys are \"comm,pid,tid,dso,symbol\"\n"
             "    --symfs <dir>  Look for files with symbols relative to this directory.\n"),
         record_filename_("perf.data"),
-        use_branch_address_(false) {
+        use_branch_address_(false),
+        accumulate_callchain_(false) {
+    compare_sample_func_t compare_sample_callback = std::bind(
+        &ReportCommand::CompareSampleEntry, this, std::placeholders::_1, std::placeholders::_2);
+    sample_tree_ = std::unique_ptr<SampleTree>(new SampleTree(compare_sample_callback));
   }
 
   bool Run(const std::vector<std::string>& args);
@@ -253,6 +261,7 @@
   bool ParseOptions(const std::vector<std::string>& args);
   bool ReadEventAttrFromRecordFile();
   void ReadSampleTreeFromRecordFile();
+  void ProcessSampleRecord(const SampleRecord& r);
   void ReadFeaturesFromRecordFile();
   int CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2);
   void PrintReport();
@@ -265,10 +274,12 @@
   std::string record_filename_;
   std::unique_ptr<RecordFileReader> record_file_reader_;
   perf_event_attr event_attr_;
-  std::vector<ReportItem*> report_items_;
+  std::vector<std::unique_ptr<Displayable>> displayable_items_;
+  std::vector<Comparable*> comparable_items_;
   std::unique_ptr<SampleTree> sample_tree_;
   bool use_branch_address_;
   std::string record_cmdline_;
+  bool accumulate_callchain_;
 };
 
 bool ReportCommand::Run(const std::vector<std::string>& args) {
@@ -286,6 +297,7 @@
     return false;
   }
   ReadSampleTreeFromRecordFile();
+  ReadFeaturesFromRecordFile();
 
   // 3. Show collected information.
   PrintReport();
@@ -299,6 +311,8 @@
   for (size_t i = 0; i < args.size(); ++i) {
     if (args[i] == "-b") {
       use_branch_address_ = true;
+    } else if (args[i] == "--children") {
+      accumulate_callchain_ = true;
     } else if (args[i] == "-i") {
       if (!NextArgumentOrError(args, &i)) {
         return false;
@@ -329,17 +343,58 @@
     }
   }
 
+  if (!accumulate_callchain_) {
+    displayable_items_.push_back(
+        std::unique_ptr<Displayable>(new SelfOverheadItem(*sample_tree_, "Overhead")));
+  } else {
+    displayable_items_.push_back(
+        std::unique_ptr<Displayable>(new AccumulatedOverheadItem(*sample_tree_)));
+    displayable_items_.push_back(std::unique_ptr<Displayable>(new SelfOverheadItem(*sample_tree_)));
+  }
   if (print_sample_count) {
-    report_items_.push_back(&report_sample_count);
+    displayable_items_.push_back(std::unique_ptr<Displayable>(new SampleCountItem));
   }
   for (auto& key : sort_keys) {
     if (!use_branch_address_ && branch_sort_keys.find(key) != branch_sort_keys.end()) {
       LOG(ERROR) << "sort key '" << key << "' can only be used with -b option.";
       return false;
     }
-    auto it = report_item_map.find(key);
-    if (it != report_item_map.end()) {
-      report_items_.push_back(it->second);
+    if (key == "pid") {
+      PidItem* item = new PidItem;
+      displayable_items_.push_back(std::unique_ptr<Displayable>(item));
+      comparable_items_.push_back(item);
+    } else if (key == "tid") {
+      TidItem* item = new TidItem;
+      displayable_items_.push_back(std::unique_ptr<Displayable>(item));
+      comparable_items_.push_back(item);
+    } else if (key == "comm") {
+      CommItem* item = new CommItem;
+      displayable_items_.push_back(std::unique_ptr<Displayable>(item));
+      comparable_items_.push_back(item);
+    } else if (key == "dso") {
+      DsoItem* item = new DsoItem;
+      displayable_items_.push_back(std::unique_ptr<Displayable>(item));
+      comparable_items_.push_back(item);
+    } else if (key == "symbol") {
+      SymbolItem* item = new SymbolItem;
+      displayable_items_.push_back(std::unique_ptr<Displayable>(item));
+      comparable_items_.push_back(item);
+    } else if (key == "dso_from") {
+      DsoFromItem* item = new DsoFromItem;
+      displayable_items_.push_back(std::unique_ptr<Displayable>(item));
+      comparable_items_.push_back(item);
+    } else if (key == "dso_to") {
+      DsoToItem* item = new DsoToItem;
+      displayable_items_.push_back(std::unique_ptr<Displayable>(item));
+      comparable_items_.push_back(item);
+    } else if (key == "symbol_from") {
+      SymbolFromItem* item = new SymbolFromItem;
+      displayable_items_.push_back(std::unique_ptr<Displayable>(item));
+      comparable_items_.push_back(item);
+    } else if (key == "symbol_to") {
+      SymbolToItem* item = new SymbolToItem;
+      displayable_items_.push_back(std::unique_ptr<Displayable>(item));
+      comparable_items_.push_back(item);
     } else {
       LOG(ERROR) << "Unknown sort key: " << key;
       return false;
@@ -363,9 +418,6 @@
 }
 
 void ReportCommand::ReadSampleTreeFromRecordFile() {
-  compare_sample_func_t compare_sample_callback = std::bind(
-      &ReportCommand::CompareSampleEntry, this, std::placeholders::_1, std::placeholders::_2);
-  sample_tree_ = std::unique_ptr<SampleTree>(new SampleTree(compare_sample_callback));
   sample_tree_->AddThread(0, 0, "swapper");
 
   std::vector<std::unique_ptr<const Record>> records = record_file_reader_->DataSection();
@@ -385,23 +437,13 @@
         sample_tree_->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff,
                                    r.sample_id.time_data.time, r.filename);
       } else {
+        std::string filename =
+            (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename;
         sample_tree_->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff,
-                                   r.sample_id.time_data.time, r.filename);
+                                   r.sample_id.time_data.time, filename);
       }
     } else if (record->header.type == PERF_RECORD_SAMPLE) {
-      const SampleRecord& r = *static_cast<const SampleRecord*>(record.get());
-      if (use_branch_address_ == false) {
-        bool in_kernel = (r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL;
-        sample_tree_->AddSample(r.tid_data.pid, r.tid_data.tid, r.ip_data.ip, r.time_data.time,
-                                r.period_data.period, in_kernel);
-      } else {
-        for (auto& item : r.branch_stack_data.stack) {
-          if (item.from != 0 && item.to != 0) {
-            sample_tree_->AddBranchSample(r.tid_data.pid, r.tid_data.tid, item.from, item.to,
-                                          item.flags, r.time_data.time, r.period_data.period);
-          }
-        }
-      }
+      ProcessSampleRecord(*static_cast<const SampleRecord*>(record.get()));
     } else if (record->header.type == PERF_RECORD_COMM) {
       const CommRecord& r = *static_cast<const CommRecord*>(record.get());
       sample_tree_->AddThread(r.data.pid, r.data.tid, r.comm);
@@ -412,6 +454,46 @@
   }
 }
 
+void ReportCommand::ProcessSampleRecord(const SampleRecord& r) {
+  if (use_branch_address_ && (r.sample_type & PERF_SAMPLE_BRANCH_STACK)) {
+    for (auto& item : r.branch_stack_data.stack) {
+      if (item.from != 0 && item.to != 0) {
+        sample_tree_->AddBranchSample(r.tid_data.pid, r.tid_data.tid, item.from, item.to,
+                                      item.flags, r.time_data.time, r.period_data.period);
+      }
+    }
+  } else {
+    bool in_kernel = (r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL;
+    SampleEntry* sample = sample_tree_->AddSample(r.tid_data.pid, r.tid_data.tid, r.ip_data.ip,
+                                                  r.time_data.time, r.period_data.period, in_kernel);
+    CHECK(sample != nullptr);
+    if (accumulate_callchain_ && (r.sample_type & PERF_SAMPLE_CALLCHAIN) != 0) {
+      std::vector<SampleEntry*> callchain;
+      callchain.push_back(sample);
+      const std::vector<uint64_t>& ips = r.callchain_data.ips;
+      for (auto& ip : ips) {
+        if (ip >= PERF_CONTEXT_MAX) {
+          switch (ip) {
+            case PERF_CONTEXT_KERNEL:
+              in_kernel = true;
+              break;
+            case PERF_CONTEXT_USER:
+              in_kernel = false;
+              break;
+            default:
+              LOG(ERROR) << "Unexpected perf_context in callchain: " << ip;
+          }
+        } else {
+          sample =
+              sample_tree_->AddCallChainSample(r.tid_data.pid, r.tid_data.tid, ip, r.time_data.time,
+                                               r.period_data.period, in_kernel, callchain);
+          callchain.push_back(sample);
+        }
+      }
+    }
+  }
+}
+
 void ReportCommand::ReadFeaturesFromRecordFile() {
   std::vector<std::string> cmdline = record_file_reader_->ReadCmdlineFeature();
   if (!cmdline.empty()) {
@@ -420,12 +502,10 @@
 }
 
 int ReportCommand::CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2) {
-  for (auto& item : report_items_) {
-    if (item->compare_function != nullptr) {
-      int result = item->compare_function(sample1, sample2);
-      if (result != 0) {
-        return result;
-      }
+  for (auto& item : comparable_items_) {
+    int result = item->Compare(sample1, sample2);
+    if (result != 0) {
+      return result;
     }
   }
   return 0;
@@ -458,45 +538,36 @@
 }
 
 void ReportCommand::CollectReportWidth() {
-  for (auto& item : report_items_) {
-    std::string s = item->print_header_function();
-    item->width = s.size();
-  }
   sample_tree_->VisitAllSamples(
       std::bind(&ReportCommand::CollectReportEntryWidth, this, std::placeholders::_1));
 }
 
 void ReportCommand::CollectReportEntryWidth(const SampleEntry& sample) {
-  for (auto& item : report_items_) {
-    std::string s = item->print_function(sample);
-    item->width = std::max(item->width, s.size());
+  for (auto& item : displayable_items_) {
+    item->AdjustWidth(sample);
   }
 }
 
 void ReportCommand::PrintReportHeader() {
-  printf("%8s", "Overhead");
-  for (size_t i = 0; i < report_items_.size(); ++i) {
-    auto& item = report_items_[i];
-    printf("  ");
-    std::string s = item->print_header_function();
-    printf("%-*s", (i + 1 == report_items_.size()) ? 0 : static_cast<int>(item->width), s.c_str());
+  for (size_t i = 0; i < displayable_items_.size(); ++i) {
+    auto& item = displayable_items_[i];
+    if (i != displayable_items_.size() - 1) {
+      printf("%-*s  ", static_cast<int>(item->Width()), item->Name().c_str());
+    } else {
+      printf("%s\n", item->Name().c_str());
+    }
   }
-  printf("\n");
 }
 
 void ReportCommand::PrintReportEntry(const SampleEntry& sample) {
-  double percentage = 0.0;
-  if (sample_tree_->TotalPeriod() != 0) {
-    percentage = 100.0 * sample.period / sample_tree_->TotalPeriod();
+  for (size_t i = 0; i < displayable_items_.size(); ++i) {
+    auto& item = displayable_items_[i];
+    if (i != displayable_items_.size() - 1) {
+      printf("%-*s  ", static_cast<int>(item->Width()), item->Show(sample).c_str());
+    } else {
+      printf("%s\n", item->Show(sample).c_str());
+    }
   }
-  printf("%7.2lf%%", percentage);
-  for (size_t i = 0; i < report_items_.size(); ++i) {
-    auto& item = report_items_[i];
-    printf("  ");
-    std::string s = item->print_function(sample);
-    printf("%-*s", (i + 1 == report_items_.size()) ? 0 : static_cast<int>(item->width), s.c_str());
-  }
-  printf("\n");
 }
 
 __attribute__((constructor)) static void RegisterReportCommand() {
diff --git a/simpleperf/cmd_report_test.cpp b/simpleperf/cmd_report_test.cpp
index ceaf9c7..236673f 100644
--- a/simpleperf/cmd_report_test.cpp
+++ b/simpleperf/cmd_report_test.cpp
@@ -62,3 +62,8 @@
         << "This test does nothing as branch stack sampling is not supported on this device.";
   }
 }
+
+TEST(report_cmd, children_option) {
+  ASSERT_TRUE(RecordCmd()->Run({"-g", "sleep", "1"}));
+  ASSERT_TRUE(ReportCmd()->Run({"--children"}));
+}
diff --git a/simpleperf/sample_tree.cpp b/simpleperf/sample_tree.cpp
index 89d632c..d07a8d2 100644
--- a/simpleperf/sample_tree.cpp
+++ b/simpleperf/sample_tree.cpp
@@ -160,14 +160,15 @@
   return &unknown_map_;
 }
 
-void SampleTree::AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period,
-                           bool in_kernel) {
+SampleEntry* SampleTree::AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period,
+                                   bool in_kernel) {
   const ThreadEntry* thread = FindThreadOrNew(pid, tid);
   const MapEntry* map = FindMap(thread, ip, in_kernel);
   const SymbolEntry* symbol = FindSymbol(map, ip);
 
-  SampleEntry sample = {
+  SampleEntry value = {
       ip, time, period,
+      0,  // accumulated_period
       1,  // sample_count
       thread,
       thread->comm,  // thead_comm
@@ -179,7 +180,7 @@
           0,        // flags
       },
   };
-  InsertSample(sample);
+  return InsertSample(value);
 }
 
 void SampleTree::AddBranchSample(int pid, int tid, uint64_t from_ip, uint64_t to_ip,
@@ -196,34 +197,78 @@
   }
   const SymbolEntry* to_symbol = FindSymbol(to_map, to_ip);
 
-  SampleEntry sample = {to_ip,  // ip
-                        time, period,
-                        1,  // sample_count
-                        thread,
-                        thread->comm,  // thread_comm
-                        to_map,        // map
-                        to_symbol,     // symbol
-                        BranchFromEntry{
-                            from_ip,       // ip
-                            from_map,      // map
-                            from_symbol,   // symbol
-                            branch_flags,  // flags
-                        }};
-  InsertSample(sample);
+  SampleEntry value = {to_ip,  // ip
+                       time, period,
+                       0,  // accumulated_period
+                       1,  // sample_count
+                       thread,
+                       thread->comm,  // thread_comm
+                       to_map,        // map
+                       to_symbol,     // symbol
+                       BranchFromEntry{
+                           from_ip,       // ip
+                           from_map,      // map
+                           from_symbol,   // symbol
+                           branch_flags,  // flags
+                       }};
+  InsertSample(value);
 }
 
-void SampleTree::InsertSample(const SampleEntry& sample) {
-  auto it = sample_tree_.find(sample);
+SampleEntry* SampleTree::AddCallChainSample(int pid, int tid, uint64_t ip, uint64_t time,
+                                            uint64_t period, bool in_kernel,
+                                            const std::vector<SampleEntry*>& callchain) {
+  const ThreadEntry* thread = FindThreadOrNew(pid, tid);
+  const MapEntry* map = FindMap(thread, ip, in_kernel);
+  const SymbolEntry* symbol = FindSymbol(map, ip);
+
+  SampleEntry value = {
+      ip, time,
+      0,       // period
+      period,  // accumulated_period
+      0,       // sample_count
+      thread,
+      thread->comm,  // thread_comm
+      map, symbol,
+      BranchFromEntry{
+          0,        // ip
+          nullptr,  // map
+          nullptr,  // symbol
+          0,        // flags
+      },
+  };
+  auto it = sample_tree_.find(&value);
+  if (it != sample_tree_.end()) {
+    SampleEntry* sample = *it;
+    // Process only once for recursive function call.
+    if (std::find(callchain.begin(), callchain.end(), sample) != callchain.end()) {
+      return sample;
+    }
+  }
+  return InsertSample(value);
+}
+
+SampleEntry* SampleTree::InsertSample(SampleEntry& value) {
+  SampleEntry* result;
+  auto it = sample_tree_.find(&value);
   if (it == sample_tree_.end()) {
-    auto pair = sample_tree_.insert(sample);
+    result = AllocateSample(value);
+    auto pair = sample_tree_.insert(result);
     CHECK(pair.second);
   } else {
-    SampleEntry* find_sample = const_cast<SampleEntry*>(&*it);
-    find_sample->period += sample.period;
-    find_sample->sample_count++;
+    result = *it;
+    result->period += value.period;
+    result->accumulated_period += value.accumulated_period;
+    result->sample_count += value.sample_count;
   }
-  total_samples_++;
-  total_period_ += sample.period;
+  total_samples_ += value.sample_count;
+  total_period_ += value.period;
+  return result;
+}
+
+SampleEntry* SampleTree::AllocateSample(const SampleEntry& value) {
+  SampleEntry* sample = new SampleEntry(value);
+  sample_storage_.push_back(std::unique_ptr<SampleEntry>(sample));
+  return sample;
 }
 
 const SymbolEntry* SampleTree::FindSymbol(const MapEntry* map, uint64_t ip) {
@@ -248,6 +293,6 @@
     }
   }
   for (auto& sample : sorted_sample_tree_) {
-    callback(sample);
+    callback(*sample);
   }
 }
diff --git a/simpleperf/sample_tree.h b/simpleperf/sample_tree.h
index 8ed2e99..0be8286 100644
--- a/simpleperf/sample_tree.h
+++ b/simpleperf/sample_tree.h
@@ -56,6 +56,7 @@
   uint64_t ip;
   uint64_t time;
   uint64_t period;
+  uint64_t accumulated_period;  // Accumulated when appearing in other samples' callchain.
   uint64_t sample_count;
   const ThreadEntry* thread;
   const char* thread_comm;  // It refers to the thread comm when the sample happens.
@@ -96,9 +97,12 @@
                     const std::string& filename);
   void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
                     uint64_t time, const std::string& filename);
-  void AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period, bool in_kernel);
+  SampleEntry* AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period,
+                         bool in_kernel);
   void AddBranchSample(int pid, int tid, uint64_t from_ip, uint64_t to_ip, uint64_t branch_flags,
                        uint64_t time, uint64_t period);
+  SampleEntry* AddCallChainSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period,
+                                  bool in_kernel, const std::vector<SampleEntry*>& callchain);
   void VisitAllSamples(std::function<void(const SampleEntry&)> callback);
 
   uint64_t TotalSamples() const {
@@ -115,11 +119,12 @@
   DsoEntry* FindKernelDsoOrNew(const std::string& filename);
   DsoEntry* FindUserDsoOrNew(const std::string& filename);
   const SymbolEntry* FindSymbol(const MapEntry* map, uint64_t ip);
-  void InsertSample(const SampleEntry& sample);
+  SampleEntry* InsertSample(SampleEntry& value);
+  SampleEntry* AllocateSample(const SampleEntry& value);
 
   struct SampleComparator {
-    bool operator()(const SampleEntry& sample1, const SampleEntry& sample2) const {
-      return compare_function(sample1, sample2) < 0;
+    bool operator()(SampleEntry* sample1, SampleEntry* sample2) const {
+      return compare_function(*sample1, *sample2) < 0;
     }
     SampleComparator(compare_sample_func_t compare_function) : compare_function(compare_function) {
     }
@@ -128,11 +133,13 @@
   };
 
   struct SortedSampleComparator {
-    bool operator()(const SampleEntry& sample1, const SampleEntry& sample2) const {
-      if (sample1.period != sample2.period) {
-        return sample1.period > sample2.period;
+    bool operator()(SampleEntry* sample1, SampleEntry* sample2) const {
+      uint64_t period1 = sample1->period + sample1->accumulated_period;
+      uint64_t period2 = sample2->period + sample2->accumulated_period;
+      if (period1 != period2) {
+        return period1 > period2;
       }
-      return compare_function(sample1, sample2) < 0;
+      return compare_function(*sample1, *sample2) < 0;
     }
     SortedSampleComparator(compare_sample_func_t compare_function)
         : compare_function(compare_function) {
@@ -155,9 +162,10 @@
   SymbolEntry unknown_symbol_;
 
   SampleComparator sample_comparator_;
-  std::set<SampleEntry, SampleComparator> sample_tree_;
+  std::set<SampleEntry*, SampleComparator> sample_tree_;
   SortedSampleComparator sorted_sample_comparator_;
-  std::set<SampleEntry, SortedSampleComparator> sorted_sample_tree_;
+  std::set<SampleEntry*, SortedSampleComparator> sorted_sample_tree_;
+  std::vector<std::unique_ptr<SampleEntry>> sample_storage_;
 
   uint64_t total_samples_;
   uint64_t total_period_;
diff --git a/tests/net_test/iproute.py b/tests/net_test/iproute.py
index cf1f282..9cc8257 100644
--- a/tests/net_test/iproute.py
+++ b/tests/net_test/iproute.py
@@ -33,6 +33,7 @@
 # Request constants.
 NLM_F_REQUEST = 1
 NLM_F_ACK = 4
+NLM_F_REPLACE = 0x100
 NLM_F_EXCL = 0x200
 NLM_F_CREATE = 0x400
 NLM_F_DUMP = 0x300
@@ -407,13 +408,14 @@
   def _AddressFamily(self, version):
     return {4: socket.AF_INET, 6: socket.AF_INET6}[version]
 
-  def _SendNlRequest(self, command, data):
+  def _SendNlRequest(self, command, data, flags=0):
     """Sends a netlink request and expects an ack."""
-    flags = NLM_F_REQUEST
+    flags |= NLM_F_REQUEST
     if CommandVerb(command) != "GET":
       flags |= NLM_F_ACK
     if CommandVerb(command) == "NEW":
-      flags |= (NLM_F_EXCL | NLM_F_CREATE)
+      if not flags & NLM_F_REPLACE:
+        flags |= (NLM_F_EXCL | NLM_F_CREATE)
 
     length = len(NLMsgHdr) + len(data)
     nlmsg = NLMsgHdr((length, command, flags, self.seq, self.pid)).Pack()
@@ -497,7 +499,7 @@
 
     if nlmsghdr.type == NLMSG_ERROR or nlmsghdr.type == NLMSG_DONE:
       print "done"
-      return None, data
+      return (None, None), data
 
     nlmsg, data = cstruct.Read(data, msgtype)
     self._Debug("    %s" % nlmsg)
@@ -657,12 +659,12 @@
     routes = self._GetMsgList(RTMsg, data, False)
     return routes
 
-  def _Neighbour(self, version, is_add, addr, lladdr, dev, state):
+  def _Neighbour(self, version, is_add, addr, lladdr, dev, state, flags=0):
     """Adds or deletes a neighbour cache entry."""
     family = self._AddressFamily(version)
 
     # Convert the link-layer address to a raw byte string.
-    if is_add:
+    if is_add and lladdr:
       lladdr = lladdr.split(":")
       if len(lladdr) != 6:
         raise ValueError("Invalid lladdr %s" % ":".join(lladdr))
@@ -670,10 +672,10 @@
 
     ndmsg = NdMsg((family, dev, state, 0, RTN_UNICAST)).Pack()
     ndmsg += self._NlAttrIPAddress(NDA_DST, family, addr)
-    if is_add:
+    if is_add and lladdr:
       ndmsg += self._NlAttr(NDA_LLADDR, lladdr)
     command = RTM_NEWNEIGH if is_add else RTM_DELNEIGH
-    self._SendNlRequest(command, ndmsg)
+    self._SendNlRequest(command, ndmsg, flags)
 
   def AddNeighbour(self, version, addr, lladdr, dev):
     self._Neighbour(version, True, addr, lladdr, dev, NUD_PERMANENT)
@@ -681,10 +683,18 @@
   def DelNeighbour(self, version, addr, lladdr, dev):
     self._Neighbour(version, False, addr, lladdr, dev, 0)
 
+  def UpdateNeighbour(self, version, addr, lladdr, dev, state):
+    self._Neighbour(version, True, addr, lladdr, dev, state,
+                    flags=NLM_F_REPLACE)
+
   def DumpNeighbours(self, version):
     ndmsg = NdMsg((self._AddressFamily(version), 0, 0, 0, 0))
     return self._Dump(RTM_GETNEIGH, ndmsg, NdMsg)
 
+  def ParseNeighbourMessage(self, msg):
+    msg, _ = self._ParseNLMsg(msg, NdMsg)
+    return msg
+
 
 if __name__ == "__main__":
   iproute = IPRoute()
diff --git a/tests/net_test/multinetwork_base.py b/tests/net_test/multinetwork_base.py
index 5952d56..97e4d37 100644
--- a/tests/net_test/multinetwork_base.py
+++ b/tests/net_test/multinetwork_base.py
@@ -190,6 +190,10 @@
     return {4: cls._MyIPv4Address(netid),
             6: cls._MyIPv6Address(netid)}[version]
 
+  @classmethod
+  def MyLinkLocalAddress(cls, netid):
+    return net_test.GetLinkAddress(cls.GetInterfaceName(netid), True)
+
   @staticmethod
   def IPv6Prefix(netid):
     return "2001:db8:%02x::" % netid
@@ -221,7 +225,7 @@
     return f
 
   @classmethod
-  def SendRA(cls, netid, retranstimer=None):
+  def SendRA(cls, netid, retranstimer=None, reachabletime=0):
     validity = 300                 # seconds
     macaddr = cls.RouterMacAddress(netid)
     lladdr = cls._RouterAddress(netid, 6)
@@ -238,7 +242,8 @@
 
     ra = (scapy.Ether(src=macaddr, dst="33:33:00:00:00:01") /
           scapy.IPv6(src=lladdr, hlim=255) /
-          scapy.ICMPv6ND_RA(retranstimer=retranstimer,
+          scapy.ICMPv6ND_RA(reachabletime=reachabletime,
+                            retranstimer=retranstimer,
                             routerlifetime=routerlifetime) /
           scapy.ICMPv6NDOptSrcLLAddr(lladdr=macaddr) /
           scapy.ICMPv6NDOptPrefixInfo(prefix=cls.IPv6Prefix(netid),
diff --git a/tests/net_test/multinetwork_test.py b/tests/net_test/multinetwork_test.py
index 6ffb5d0..e9cd8f1 100755
--- a/tests/net_test/multinetwork_test.py
+++ b/tests/net_test/multinetwork_test.py
@@ -33,15 +33,11 @@
 PING_SEQ = 3
 PING_TOS = 0x83
 
+# For brevity.
+UDP_PAYLOAD = net_test.UDP_PAYLOAD
+
 IPV6_FLOWINFO = 11
 
-
-UDP_PAYLOAD = str(scapy.DNS(rd=1,
-                            id=random.randint(0, 65535),
-                            qd=scapy.DNSQR(qname="wWW.GoOGle.CoM",
-                                           qtype="AAAA")))
-
-
 IPV4_MARK_REFLECT_SYSCTL = "/proc/sys/net/ipv4/fwmark_reflect"
 IPV6_MARK_REFLECT_SYSCTL = "/proc/sys/net/ipv6/fwmark_reflect"
 SYNCOOKIES_SYSCTL = "/proc/sys/net/ipv4/tcp_syncookies"
diff --git a/tests/net_test/neighbour_test.py b/tests/net_test/neighbour_test.py
new file mode 100755
index 0000000..828a86b
--- /dev/null
+++ b/tests/net_test/neighbour_test.py
@@ -0,0 +1,216 @@
+#!/usr/bin/python
+#
+# Copyright 2015 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import errno
+import random
+from socket import *  # pylint: disable=wildcard-import
+import time
+import unittest
+
+from scapy import all as scapy
+
+import multinetwork_base
+import net_test
+
+
+RTMGRP_NEIGH = 4
+
+NUD_INCOMPLETE = 0x01
+NUD_REACHABLE = 0x02
+NUD_STALE = 0x04
+NUD_DELAY = 0x08
+NUD_PROBE = 0x10
+NUD_FAILED = 0x20
+NUD_PERMANENT = 0x80
+
+
+# TODO: Support IPv4.
+class NeighbourTest(multinetwork_base.MultiNetworkBaseTest):
+
+  # Set a 100-ms retrans timer so we can test for ND retransmits without
+  # waiting too long. Apparently this cannot go below 500ms.
+  RETRANS_TIME_MS = 500
+
+  # This can only be in seconds, so 1000 is the minimum.
+  DELAY_TIME_MS = 1000
+
+  # Unfortunately, this must be above the delay timer or the kernel ND code will
+  # not behave correctly (e.g., go straight from REACHABLE into DELAY). This is
+  # is fuzzed by the kernel from 0.5x to 1.5x of its value, so we need a value
+  # that's 2x the delay timer.
+  REACHABLE_TIME_MS = 2 * DELAY_TIME_MS
+
+  @classmethod
+  def setUpClass(cls):
+    super(NeighbourTest, cls).setUpClass()
+    for netid in cls.tuns:
+      iface = cls.GetInterfaceName(netid)
+      # This can't be set in an RA.
+      cls.SetSysctl(
+          "/proc/sys/net/ipv6/neigh/%s/delay_first_probe_time" % iface,
+          cls.DELAY_TIME_MS / 1000)
+
+  def setUp(self):
+    super(NeighbourTest, self).setUp()
+
+    self.sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)
+    self.sock.bind((0, RTMGRP_NEIGH))
+    net_test.SetNonBlocking(self.sock)
+
+    for netid in self.tuns:
+      self.SendRA(netid,
+                  retranstimer=self.RETRANS_TIME_MS,
+                  reachabletime=self.REACHABLE_TIME_MS)
+
+    self.netid = random.choice(self.tuns.keys())
+    self.ifindex = self.ifindices[self.netid]
+
+  def GetNeighbour(self, addr):
+    version = 6 if ":" in addr else 4
+    for msg, args in self.iproute.DumpNeighbours(version):
+      if args["NDA_DST"] == addr:
+        return msg, args
+
+  def GetNdEntry(self, addr):
+    return self.GetNeighbour(addr)
+
+  def CheckNoNdEvents(self):
+    self.assertRaisesErrno(errno.EAGAIN, self.sock.recvfrom, 4096, MSG_PEEK)
+
+  def assertNeighbourState(self, state, addr):
+    self.assertEquals(state, self.GetNdEntry(addr)[0].state)
+
+  def assertNeighbourAttr(self, addr, name, value):
+    self.assertEquals(value, self.GetNdEntry(addr)[1][name])
+
+  def ExpectNeighbourNotification(self, addr, state, attrs=None):
+    msg = self.sock.recv(4096)
+    msg, actual_attrs = self.iproute.ParseNeighbourMessage(msg)
+    self.assertEquals(addr, actual_attrs["NDA_DST"])
+    self.assertEquals(state, msg.state)
+    if attrs:
+      for name in attrs:
+        self.assertEquals(attrs[name], actual_attrs[name])
+
+  def ExpectUnicastProbe(self, addr):
+    version = 6 if ":" in addr else 4
+    if version == 6:
+      expected = (
+          scapy.IPv6(src=self.MyLinkLocalAddress(self.netid), dst=addr) /
+          scapy.ICMPv6ND_NS(tgt=addr) /
+          scapy.ICMPv6NDOptSrcLLAddr(lladdr=self.MyMacAddress(self.netid))
+      )
+      self.ExpectPacketOn(self.netid, "Unicast probe", expected)
+    else:
+      raise NotImplementedError
+
+  def ReceiveUnicastAdvertisement(self, addr, mac):
+    version = 6 if ":" in addr else 4
+    if version == 6:
+      packet = (
+          scapy.Ether(src=mac, dst=self.MyMacAddress(self.netid)) /
+          scapy.IPv6(src=addr, dst=self.MyLinkLocalAddress(self.netid)) /
+          scapy.ICMPv6ND_NA(tgt=addr, S=1, O=0) /
+          scapy.ICMPv6NDOptDstLLAddr(lladdr=mac)
+      )
+      self.ReceiveEtherPacketOn(self.netid, packet)
+    else:
+      raise NotImplementedError
+
+  def MonitorSleep(self, intervalseconds, addr):
+    slept = 0
+    while slept < intervalseconds:
+      time.sleep(0.1)
+      slept += 0.1
+      print self.GetNdEntry(addr)
+
+  def SleepMs(self, ms):
+    time.sleep(ms / 1000.0)
+
+  def testNotifications(self):
+    router4 = self._RouterAddress(self.netid, 4)
+    router6 = self._RouterAddress(self.netid, 6)
+    self.assertNeighbourState(NUD_PERMANENT, router4)
+    self.assertNeighbourState(NUD_STALE, router6)
+
+    # Send a packet and check that we go into DELAY.
+    routing_mode = random.choice(["mark", "oif", "uid"])
+    s = self.BuildSocket(6, net_test.UDPSocket, self.netid, routing_mode)
+    s.connect((net_test.IPV6_ADDR, 53))
+    s.send(net_test.UDP_PAYLOAD)
+    self.assertNeighbourState(NUD_DELAY, router6)
+
+    # Wait for the probe interval, then check that we're in PROBE, and that the
+    # kernel has notified us.
+    self.SleepMs(self.DELAY_TIME_MS)
+    self.ExpectNeighbourNotification(router6, NUD_PROBE)
+    self.assertNeighbourState(NUD_PROBE, router6)
+    self.ExpectUnicastProbe(router6)
+
+    # Respond to the NS and verify we're in REACHABLE again.
+    self.ReceiveUnicastAdvertisement(router6, self.RouterMacAddress(self.netid))
+    self.assertNeighbourState(NUD_REACHABLE, router6)
+
+    # Wait until the reachable time has passed, and verify we're in STALE.
+    self.SleepMs(self.REACHABLE_TIME_MS * 1.5)
+    self.assertNeighbourState(NUD_STALE, router6)
+    self.ExpectNeighbourNotification(router6, NUD_STALE)
+
+    # Send a packet, and verify we go into DELAY and then to PROBE.
+    s.send(net_test.UDP_PAYLOAD)
+    self.assertNeighbourState(NUD_DELAY, router6)
+    self.SleepMs(self.DELAY_TIME_MS)
+    self.assertNeighbourState(NUD_PROBE, router6)
+    self.ExpectNeighbourNotification(router6, NUD_PROBE)
+
+    # Wait for the probes to time out, and expect a FAILED notification.
+    self.assertNeighbourAttr(router6, "NDA_PROBES", 1)
+    self.ExpectUnicastProbe(router6)
+
+    self.SleepMs(self.RETRANS_TIME_MS)
+    self.ExpectUnicastProbe(router6)
+    self.assertNeighbourAttr(router6, "NDA_PROBES", 2)
+
+    self.SleepMs(self.RETRANS_TIME_MS)
+    self.ExpectUnicastProbe(router6)
+    self.assertNeighbourAttr(router6, "NDA_PROBES", 3)
+
+    self.SleepMs(self.RETRANS_TIME_MS)
+    self.assertNeighbourState(NUD_FAILED, router6)
+    self.ExpectNeighbourNotification(router6, NUD_FAILED, {"NDA_PROBES": 3})
+
+  def testRepeatedProbes(self):
+    router4 = self._RouterAddress(self.netid, 4)
+    router6 = self._RouterAddress(self.netid, 6)
+    routermac = self.RouterMacAddress(self.netid)
+    self.assertNeighbourState(NUD_PERMANENT, router4)
+    self.assertNeighbourState(NUD_STALE, router6)
+
+    def ForceProbe(addr, mac):
+      self.iproute.UpdateNeighbour(6, addr, None, self.ifindex, NUD_PROBE)
+      self.assertNeighbourState(NUD_PROBE, addr)
+      self.SleepMs(1)  # TODO: Why is this necessary?
+      self.assertNeighbourState(NUD_PROBE, addr)
+      self.ExpectUnicastProbe(addr)
+      self.ReceiveUnicastAdvertisement(addr, mac)
+      self.assertNeighbourState(NUD_REACHABLE, addr)
+
+    for i in xrange(5):
+      ForceProbe(router6, routermac)
+
+
+if __name__ == "__main__":
+  unittest.main()
diff --git a/tests/net_test/net_test.py b/tests/net_test/net_test.py
index a87b71b..be034d0 100755
--- a/tests/net_test/net_test.py
+++ b/tests/net_test/net_test.py
@@ -16,6 +16,7 @@
 
 import fcntl
 import os
+import random
 from socket import *  # pylint: disable=wildcard-import
 import struct
 import unittest
@@ -63,6 +64,12 @@
                          "st tx_queue rx_queue tr tm->when retrnsmt"
                          "   uid  timeout inode ref pointer drops\n")
 
+# Arbitrary packet payload.
+UDP_PAYLOAD = str(scapy.DNS(rd=1,
+                            id=random.randint(0, 65535),
+                            qd=scapy.DNSQR(qname="wWW.GoOGle.CoM",
+                                           qtype="AAAA")))
+
 # Unix group to use if we want to open sockets as non-root.
 AID_INET = 3003
 
diff --git a/tests/net_test/run_net_test.sh b/tests/net_test/run_net_test.sh
index a12f3cf..119171e 100755
--- a/tests/net_test/run_net_test.sh
+++ b/tests/net_test/run_net_test.sh
@@ -93,7 +93,7 @@
 fi
 
 # Compile the kernel.
-make -j12 linux ARCH=um SUBARCH=x86_64 CROSS_COMPILE=
+make -j32 linux ARCH=um SUBARCH=x86_64 CROSS_COMPILE=
 
 # Get the absolute path to the test file that's being run.
 dir=/host$(dirname $(readlink -f $0))