Remove Stream
authorTudor Bosman <tudorb@fb.com>
Tue, 13 Nov 2012 21:19:17 +0000 (13:19 -0800)
committerJordan DeLong <jdelong@fb.com>
Sun, 16 Dec 2012 22:44:56 +0000 (14:44 -0800)
Summary:
Switch existing code to gen::byLine.  Also fix namespaces in
folly/experimental/(symbolizer|exception_tracer).

Test Plan:
fbconfig -r folly && fbmake runtests_opt, ran non-test folly
binaries by hand, fbgs for other uses (there aren't any)

Reviewed By: tjackson@fb.com

FB internal diff: D629576

17 files changed:
folly/experimental/Gen-inl.h
folly/experimental/exception_tracer/ExceptionTracer.cpp
folly/experimental/exception_tracer/ExceptionTracer.h
folly/experimental/exception_tracer/ExceptionTracerLib.cpp
folly/experimental/exception_tracer/ExceptionTracerTest.cpp
folly/experimental/io/HugePages.cpp
folly/experimental/symbolizer/Dwarf.cpp
folly/experimental/symbolizer/Dwarf.h
folly/experimental/symbolizer/Elf-inl.h
folly/experimental/symbolizer/Elf.cpp
folly/experimental/symbolizer/Elf.h
folly/experimental/symbolizer/ElfUtil.cpp
folly/experimental/symbolizer/Symbolizer.cpp
folly/experimental/symbolizer/Symbolizer.h
folly/experimental/symbolizer/SymbolizerTest.cpp
folly/experimental/test/GenTest.cpp
folly/test/SubprocessTest.cpp

index 7a93d56b8ee73179312c0c0c48acf340fdaf4ca9..414ac66e6996f65d837eccdf5d74ec02eaf7f368 100644 (file)
@@ -247,9 +247,9 @@ template<class Value,
          class Gen,
          class Handler>
 typename std::enable_if<
-  IsCompatibleSignature<Handler, bool(Value)>::value>::type
+  IsCompatibleSignature<Handler, bool(Value)>::value, bool>::type
 operator|(const GenImpl<Value, Gen>& gen, Handler&& handler) {
-  gen.self().apply(std::forward<Handler>(handler));
+  return gen.self().apply(std::forward<Handler>(handler));
 }
 
 /**
index 82e18639a7e338efd04c46cb22decc3de4b6b144..5fac0dcc169b853ffa8676801e6905f1f1786e51 100644 (file)
@@ -36,9 +36,10 @@ GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
 
 }  // namespace
 
-using namespace ::facebook::symbolizer;
+using namespace ::folly::symbolizer;
 using namespace __cxxabiv1;
 
+namespace folly {
 namespace exception_tracer {
 
 std::ostream& operator<<(std::ostream& out, const ExceptionInfo& info) {
@@ -198,4 +199,5 @@ void installHandlers() {
 }
 
 }  // namespace exception_tracer
+}  // namespace folly
 
index b742f190c6c779880136b924929c2f0658b7055f..ded9a1917b6f442dde48a747e34a67a0cf132e41 100644 (file)
@@ -23,6 +23,7 @@
 #include <vector>
 #include <iostream>
 
+namespace folly {
 namespace exception_tracer {
 
 struct ExceptionInfo {
@@ -47,6 +48,7 @@ std::vector<ExceptionInfo> getCurrentExceptions();
 void installHandlers();
 
 }  // namespace exception_tracer
+}  // namespace folly
 
 #endif /* FOLLY_EXPERIMENTAL_EXCEPTION_TRACER_EXCEPTIONTRACER_H_ */
 
index 16fe73fcdf85b7e9cc154666e7e6f4e6e129934f..788898212a4ce8bf0096f46d37a3bcf842e9ca3d 100644 (file)
@@ -173,7 +173,7 @@ namespace {
 struct Initializer {
   Initializer() {
     try {
-      exception_tracer::installHandlers();
+      ::folly::exception_tracer::installHandlers();
     } catch (...) {
     }
   }
index 4427c87060917cf4a3388e5aac412b8922af5b5b..1d5d27ddf129993572d9b71cc3ac2a38d299c800 100644 (file)
@@ -25,7 +25,7 @@ void bar() {
 
 void dumpExceptions(const char* prefix) {
   std::cerr << "--- " << prefix << "\n";
-  auto exceptions = exception_tracer::getCurrentExceptions();
+  auto exceptions = ::folly::exception_tracer::getCurrentExceptions();
   for (auto& exc : exceptions) {
     std::cerr << exc << "\n";
   }
index e2deabf52770a042f5e2dc85f6e6dd53a976c4fe..e0e33bcc5821615d2f40bd5bc4f47d85c298751f 100644 (file)
 #include "folly/Range.h"
 #include "folly/ScopeGuard.h"
 #include "folly/String.h"
-#include "folly/experimental/io/Stream.h"
+
+#include "folly/experimental/Gen.h"
+#include "folly/experimental/FileGen.h"
+#include "folly/experimental/StringGen.h"
 
 namespace folly {
 
@@ -48,15 +51,23 @@ namespace {
 size_t getDefaultHugePageSize() {
   // We need to parse /proc/meminfo
   static const boost::regex regex(R"!(Hugepagesize:\s*(\d+)\s*kB)!");
+  size_t pageSize = 0;
   boost::cmatch match;
-  for (auto& byteLine : byLine("/proc/meminfo")) {
-    StringPiece line(byteLine);
-    if (boost::regex_match(line.begin(), line.end(), match, regex)) {
-      StringPiece numStr(line.begin() + match.position(1), match.length(1));
-      return to<size_t>(numStr) * 1024;  // in KiB
-    }
+
+  bool error = gen::byLine("/proc/meminfo") | gen::eachAs<StringPiece>() |
+    [&] (StringPiece line) -> bool {
+      if (boost::regex_match(line.begin(), line.end(), match, regex)) {
+        StringPiece numStr(line.begin() + match.position(1), match.length(1));
+        pageSize = to<size_t>(numStr) * 1024;  // in KiB
+        return false;  // stop
+      }
+      return true;
+    };
+
+  if (error) {
+    throw std::runtime_error("Can't find default huge page size");
   }
-  throw std::runtime_error("Can't find default huge page size");
+  return pageSize;
 }
 
 // Get raw huge page sizes (without mount points, they'll be filled later)
@@ -124,47 +135,48 @@ HugePageSizeVec getHugePageSizes() {
   // Read and parse /proc/mounts
   std::vector<StringPiece> parts;
   std::vector<StringPiece> options;
-  for (auto& byteLine : byLine("/proc/mounts")) {
-    StringPiece line(byteLine);
-    parts.clear();
-    split(" ", line, parts);
-    // device path fstype options uid gid
-    if (parts.size() != 6) {
-      throw std::runtime_error("Invalid /proc/mounts line");
-    }
-    if (parts[2] != "hugetlbfs") {
-      continue;  // we only care about hugetlbfs
-    }
 
-    options.clear();
-    split(",", parts[3], options);
-    size_t pageSize = defaultHugePageSize;
-    // Search for the "pagesize" option, which must have a value
-    for (auto& option : options) {
-      // key=value
-      const char* p = static_cast<const char*>(
-          memchr(option.data(), '=', option.size()));
-      if (!p) {
-        continue;
+  gen::byLine("/proc/mounts") | gen::eachAs<StringPiece>() |
+    [&](StringPiece line) {
+      parts.clear();
+      split(" ", line, parts);
+      // device path fstype options uid gid
+      if (parts.size() != 6) {
+        throw std::runtime_error("Invalid /proc/mounts line");
       }
-      if (StringPiece(option.data(), p) != "pagesize") {
-        continue;
+      if (parts[2] != "hugetlbfs") {
+        return;  // we only care about hugetlbfs
       }
-      pageSize = parsePageSizeValue(StringPiece(p + 1, option.end()));
-      break;
-    }
 
-    auto pos = std::lower_bound(sizeVec.begin(), sizeVec.end(), pageSize,
-                                PageSizeLess());
-    if (pos == sizeVec.end() || pos->size != pageSize) {
-      throw std::runtime_error("Mount page size not found");
-    }
-    if (pos->mountPoint.empty()) {
-      // Store mount point
-      pos->mountPoint = fs::canonical(fs::path(parts[1].begin(),
-                                               parts[1].end()));
-    }
-  }
+      options.clear();
+      split(",", parts[3], options);
+      size_t pageSize = defaultHugePageSize;
+      // Search for the "pagesize" option, which must have a value
+      for (auto& option : options) {
+        // key=value
+        const char* p = static_cast<const char*>(
+            memchr(option.data(), '=', option.size()));
+        if (!p) {
+          continue;
+        }
+        if (StringPiece(option.data(), p) != "pagesize") {
+          continue;
+        }
+        pageSize = parsePageSizeValue(StringPiece(p + 1, option.end()));
+        break;
+      }
+
+      auto pos = std::lower_bound(sizeVec.begin(), sizeVec.end(), pageSize,
+                                  PageSizeLess());
+      if (pos == sizeVec.end() || pos->size != pageSize) {
+        throw std::runtime_error("Mount page size not found");
+      }
+      if (pos->mountPoint.empty()) {
+        // Store mount point
+        pos->mountPoint = fs::canonical(fs::path(parts[1].begin(),
+                                                 parts[1].end()));
+      }
+    };
 
   return sizeVec;
 }
index 40e42d6cbd633226606bf6eb302c730e004ac66f..d3f9e05ef574fc2bfedfe0d1a197c724998ba1e5 100644 (file)
@@ -21,7 +21,7 @@
 
 #include <dwarf.h>
 
-namespace facebook {
+namespace folly {
 namespace symbolizer {
 
 Dwarf::Dwarf(const ElfFile* elf) : elf_(elf) {
@@ -815,5 +815,5 @@ bool Dwarf::LineNumberVM::findAddress(uintptr_t target, Path& file,
 }
 
 }  // namespace symbolizer
-}  // namespace facebook
+}  // namespace folly
 
index 32382c364e347ed5928649dbb5dc9df408821d3b..6074767b8193e8856b0fe1aa6542bbabd9ab6541 100644 (file)
@@ -24,7 +24,7 @@
 #include "folly/experimental/symbolizer/Elf.h"
 #include "folly/Range.h"
 
-namespace facebook {
+namespace folly {
 namespace symbolizer {
 
 /**
@@ -270,7 +270,7 @@ inline std::ostream& operator<<(std::ostream& out, const Dwarf::Path& path) {
 }
 
 }  // namespace symbolizer
-}  // namespace facebook
+}  // namespace folly
 
 #endif /* FOLLY_EXPERIMENTAL_SYMBOLIZER_DWARF_H_ */
 
index 033dc76f107fd20f0999c08c2105cbd461b16e4a..bc0984009e9e72f8c5cc92dc4126f040f1c77272 100644 (file)
@@ -19,7 +19,7 @@
 # error This file must be included from Elf.h
 #endif
 
-namespace facebook {
+namespace folly {
 namespace symbolizer {
 
 template <class Fn>
@@ -61,5 +61,5 @@ const char* ElfFile::iterateStrings(const ElfW(Shdr)& stringTable, Fn fn)
 
 
 }  // namespace symbolizer
-}  // namespace facebook
+}  // namespace folly
 
index b9eb372fbe6db4e162b4c3f6f1f225d286028e10..0c9177333ef964f13a9c64d9081b79e600903710 100644 (file)
@@ -29,7 +29,7 @@
 
 #include "folly/Conv.h"
 
-namespace facebook {
+namespace folly {
 namespace symbolizer {
 
 ElfFile::ElfFile()
@@ -287,5 +287,5 @@ const char* ElfFile::getSymbolName(Symbol symbol) const {
 }
 
 }  // namespace symbolizer
-}  // namespace facebook
+}  // namespace folly
 
index 4187aa695c19396b196c6128831b27db093222f3..a9d6c5519f1bc8783c49788cfac0179f66af554b 100644 (file)
@@ -30,7 +30,7 @@
 #include "folly/Range.h"
 #include "folly/Conv.h"
 
-namespace facebook {
+namespace folly {
 namespace symbolizer {
 
 /**
@@ -151,7 +151,7 @@ inline void enforce(bool v, Args... args) {
 }
 
 }  // namespace symbolizer
-}  // namespace facebook
+}  // namespace folly
 
 #include "folly/experimental/symbolizer/Elf-inl.h"
 
index 793ded6833e6930eef60b5577129a9b0022d180c..e6f32fb9f2b278daa48161c7240b48571aab1350 100644 (file)
@@ -22,8 +22,8 @@
 #include <gflags/gflags.h>
 #include <glog/logging.h>
 
-using namespace facebook;
-using namespace facebook::symbolizer;
+using namespace folly;
+using namespace folly::symbolizer;
 
 int main(int argc, char *argv[]) {
   google::ParseCommandLineFlags(&argc, &argv, true);
index 861b3321875b6094a6d6d48622693873f783a5e0..714cac7604931b0c62cffba06c449266f9f5e44d 100644 (file)
 #include "folly/experimental/symbolizer/Symbolizer.h"
 
 #include <boost/regex.hpp>
+#include <glog/logging.h>
 
 #include "folly/experimental/symbolizer/Elf.h"
 #include "folly/experimental/symbolizer/Dwarf.h"
-#include "glog/logging.h"
 #include "folly/Range.h"
 #include "folly/FBString.h"
 #include "folly/String.h"
-#include "folly/experimental/io/Stream.h"
+#include "folly/experimental/Gen.h"
+#include "folly/experimental/FileGen.h"
+#include "folly/experimental/StringGen.h"
 
-namespace facebook {
+namespace folly {
 namespace symbolizer {
 
 namespace {
-folly::StringPiece sp(const boost::csub_match& m) {
-  return folly::StringPiece(m.first, m.second);
+StringPiece sp(const boost::csub_match& m) {
+  return StringPiece(m.first, m.second);
 }
 
-uint64_t fromHex(folly::StringPiece s) {
+uint64_t fromHex(StringPiece s) {
   // Make a copy; we need a null-terminated string for strtoull
-  folly::fbstring str(s.data(), s.size());
+  fbstring str(s.data(), s.size());
   const char* p = str.c_str();
   char* end;
   uint64_t val = strtoull(p, &end, 16);
@@ -53,7 +55,7 @@ struct MappedFile {
 
 }  // namespace
 
-bool Symbolizer::symbolize(uintptr_t address, folly::StringPiece& symbolName,
+bool Symbolizer::symbolize(uintptr_t address, StringPiece& symbolName,
                            Dwarf::LocationInfo& location) {
   symbolName.clear();
   location = Dwarf::LocationInfo();
@@ -75,27 +77,27 @@ bool Symbolizer::symbolize(uintptr_t address, folly::StringPiece& symbolName,
   boost::cmatch match;
 
   MappedFile foundFile;
-  bool found = false;
-  for (auto& byteLine : folly::byLine("/proc/self/maps")) {
-    folly::StringPiece line(byteLine);
-    CHECK(boost::regex_match(line.begin(), line.end(), match, mapLineRegex));
-    uint64_t begin = fromHex(sp(match[1]));
-    uint64_t end = fromHex(sp(match[2]));
-    uint64_t fileOffset = fromHex(sp(match[3]));
-    if (fileOffset != 0) {
-      continue;  // main mapping starts at 0
-    }
+  bool error = gen::byLine("/proc/self/maps") | gen::eachAs<StringPiece>() |
+    [&] (StringPiece line) -> bool {
+      CHECK(boost::regex_match(line.begin(), line.end(), match, mapLineRegex));
+      uint64_t begin = fromHex(sp(match[1]));
+      uint64_t end = fromHex(sp(match[2]));
+      uint64_t fileOffset = fromHex(sp(match[3]));
+      if (fileOffset != 0) {
+        return true;  // main mapping starts at 0
+      }
 
-    if (begin <= address && address < end) {
-      found = true;
-      foundFile.begin = begin;
-      foundFile.end = end;
-      foundFile.name.assign(match[4].first, match[4].second);
-      break;
-    }
-  }
+      if (begin <= address && address < end) {
+        foundFile.begin = begin;
+        foundFile.end = end;
+        foundFile.name.assign(match[4].first, match[4].second);
+        return false;
+      }
+
+      return true;
+    };
 
-  if (!found) {
+  if (error) {
     return false;
   }
 
@@ -128,14 +130,14 @@ ElfFile& Symbolizer::getFile(const std::string& name) {
 }
 
 void Symbolizer::write(std::ostream& out, uintptr_t address,
-                       folly::StringPiece symbolName,
+                       StringPiece symbolName,
                        const Dwarf::LocationInfo& location) {
   char buf[20];
   sprintf(buf, "%#18jx", address);
   out << "    @ " << buf;
 
   if (!symbolName.empty()) {
-    out << " " << folly::demangle(symbolName.toString().c_str());
+    out << " " << demangle(symbolName.toString().c_str());
 
     std::string file;
     if (location.hasFileAndLine) {
@@ -158,4 +160,4 @@ void Symbolizer::write(std::ostream& out, uintptr_t address,
 }
 
 }  // namespace symbolizer
-}  // namespace facebook
+}  // namespace folly
index 0b38a5db406888a5859fb446db7256b6de2e9de5..3917b50210ea877b7064567f16d237fcc86eb24a 100644 (file)
@@ -26,7 +26,7 @@
 #include "folly/experimental/symbolizer/Elf.h"
 #include "folly/experimental/symbolizer/Dwarf.h"
 
-namespace facebook {
+namespace folly {
 namespace symbolizer {
 
 /**
@@ -55,7 +55,7 @@ class Symbolizer {
 };
 
 }  // namespace symbolizer
-}  // namespace facebook
+}  // namespace folly
 
 #endif /* FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_ */
 
index 8df7097636735f3bf1369c6704681377a15216cc..0b5850c5610d597c86a6820cfd7ec553d73218ef 100644 (file)
 
 #include "folly/experimental/symbolizer/Symbolizer.h"
 
-#include "common/init/Init.h"
-#include "glog/logging.h"
+#include <glog/logging.h>
 
-using namespace facebook;
-using namespace facebook::symbolizer;
+using namespace folly;
+using namespace folly::symbolizer;
 
 int main(int argc, char *argv[]) {
-  facebook::initFacebook(&argc, &argv);
+  google::InitGoogleLogging(argv[0]);
   Symbolizer s;
-  folly::StringPiece name;
+  StringPiece name;
   Dwarf::LocationInfo location;
   CHECK(s.symbolize(reinterpret_cast<uintptr_t>(main), name, location));
   LOG(INFO) << name << " " << location.file << " " << location.line << " ("
index 64a738432ff1d11ccef67e446b2d9a6378a52dc5..142678faee8e060c1d47044d833cb2fbb8a929ae 100644 (file)
@@ -619,7 +619,7 @@ TEST(StringGen, EmptySplit) {
   {
     auto pieces = split(",,", ',') | take(1) | collect;
     EXPECT_EQ(1, pieces.size());
-    EXPECT_EQ("", pieces[1]);
+    EXPECT_EQ("", pieces[0]);
   }
 }
 
index d0203cabb7ebc684f1415ea5c03d927abc70a121..bf8e07680286a8d9e9c27fb07e89b362f1bca8ca 100644 (file)
@@ -20,7 +20,9 @@
 #include <gtest/gtest.h>
 
 #include "folly/Format.h"
-#include "folly/experimental/io/Stream.h"
+#include "folly/experimental/Gen.h"
+#include "folly/experimental/FileGen.h"
+#include "folly/experimental/StringGen.h"
 
 using namespace folly;
 
@@ -57,12 +59,12 @@ TEST(SimpleSubprocessTest, ShellExitsWithError) {
 TEST(PopenSubprocessTest, PopenRead) {
   Subprocess proc("ls /", Subprocess::pipeStdout());
   int found = 0;
-  for (auto bline : byLine(proc.stdout())) {
-    StringPiece line(bline);
-    if (line == "etc" || line == "bin" || line == "usr") {
-      ++found;
-    }
-  }
+  gen::byLine(proc.stdout()) | gen::eachAs<StringPiece>() |
+    [&] (StringPiece line) {
+      if (line == "etc" || line == "bin" || line == "usr") {
+        ++found;
+      }
+    };
   EXPECT_EQ(3, found);
   proc.waitChecked();
 }