From: Tudor Bosman Date: Tue, 13 Nov 2012 21:19:17 +0000 (-0800) Subject: Remove Stream X-Git-Tag: v0.22.0~1141 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=d8c1a8c069f29498ea6db6094837042c2744913a Remove Stream 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 --- diff --git a/folly/experimental/Gen-inl.h b/folly/experimental/Gen-inl.h index 7a93d56b..414ac66e 100644 --- a/folly/experimental/Gen-inl.h +++ b/folly/experimental/Gen-inl.h @@ -247,9 +247,9 @@ template typename std::enable_if< - IsCompatibleSignature::value>::type + IsCompatibleSignature::value, bool>::type operator|(const GenImpl& gen, Handler&& handler) { - gen.self().apply(std::forward(handler)); + return gen.self().apply(std::forward(handler)); } /** diff --git a/folly/experimental/exception_tracer/ExceptionTracer.cpp b/folly/experimental/exception_tracer/ExceptionTracer.cpp index 82e18639..5fac0dcc 100644 --- a/folly/experimental/exception_tracer/ExceptionTracer.cpp +++ b/folly/experimental/exception_tracer/ExceptionTracer.cpp @@ -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 diff --git a/folly/experimental/exception_tracer/ExceptionTracer.h b/folly/experimental/exception_tracer/ExceptionTracer.h index b742f190..ded9a191 100644 --- a/folly/experimental/exception_tracer/ExceptionTracer.h +++ b/folly/experimental/exception_tracer/ExceptionTracer.h @@ -23,6 +23,7 @@ #include #include +namespace folly { namespace exception_tracer { struct ExceptionInfo { @@ -47,6 +48,7 @@ std::vector getCurrentExceptions(); void installHandlers(); } // namespace exception_tracer +} // namespace folly #endif /* FOLLY_EXPERIMENTAL_EXCEPTION_TRACER_EXCEPTIONTRACER_H_ */ diff --git a/folly/experimental/exception_tracer/ExceptionTracerLib.cpp b/folly/experimental/exception_tracer/ExceptionTracerLib.cpp index 16fe73fc..78889821 100644 --- a/folly/experimental/exception_tracer/ExceptionTracerLib.cpp +++ b/folly/experimental/exception_tracer/ExceptionTracerLib.cpp @@ -173,7 +173,7 @@ namespace { struct Initializer { Initializer() { try { - exception_tracer::installHandlers(); + ::folly::exception_tracer::installHandlers(); } catch (...) { } } diff --git a/folly/experimental/exception_tracer/ExceptionTracerTest.cpp b/folly/experimental/exception_tracer/ExceptionTracerTest.cpp index 4427c870..1d5d27dd 100644 --- a/folly/experimental/exception_tracer/ExceptionTracerTest.cpp +++ b/folly/experimental/exception_tracer/ExceptionTracerTest.cpp @@ -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"; } diff --git a/folly/experimental/io/HugePages.cpp b/folly/experimental/io/HugePages.cpp index e2deabf5..e0e33bcc 100644 --- a/folly/experimental/io/HugePages.cpp +++ b/folly/experimental/io/HugePages.cpp @@ -38,7 +38,10 @@ #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(numStr) * 1024; // in KiB - } + + bool error = gen::byLine("/proc/meminfo") | gen::eachAs() | + [&] (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(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 parts; std::vector 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( - memchr(option.data(), '=', option.size())); - if (!p) { - continue; + gen::byLine("/proc/mounts") | gen::eachAs() | + [&](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( + 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; } diff --git a/folly/experimental/symbolizer/Dwarf.cpp b/folly/experimental/symbolizer/Dwarf.cpp index 40e42d6c..d3f9e05e 100644 --- a/folly/experimental/symbolizer/Dwarf.cpp +++ b/folly/experimental/symbolizer/Dwarf.cpp @@ -21,7 +21,7 @@ #include -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 diff --git a/folly/experimental/symbolizer/Dwarf.h b/folly/experimental/symbolizer/Dwarf.h index 32382c36..6074767b 100644 --- a/folly/experimental/symbolizer/Dwarf.h +++ b/folly/experimental/symbolizer/Dwarf.h @@ -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_ */ diff --git a/folly/experimental/symbolizer/Elf-inl.h b/folly/experimental/symbolizer/Elf-inl.h index 033dc76f..bc098400 100644 --- a/folly/experimental/symbolizer/Elf-inl.h +++ b/folly/experimental/symbolizer/Elf-inl.h @@ -19,7 +19,7 @@ # error This file must be included from Elf.h #endif -namespace facebook { +namespace folly { namespace symbolizer { template @@ -61,5 +61,5 @@ const char* ElfFile::iterateStrings(const ElfW(Shdr)& stringTable, Fn fn) } // namespace symbolizer -} // namespace facebook +} // namespace folly diff --git a/folly/experimental/symbolizer/Elf.cpp b/folly/experimental/symbolizer/Elf.cpp index b9eb372f..0c917733 100644 --- a/folly/experimental/symbolizer/Elf.cpp +++ b/folly/experimental/symbolizer/Elf.cpp @@ -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 diff --git a/folly/experimental/symbolizer/Elf.h b/folly/experimental/symbolizer/Elf.h index 4187aa69..a9d6c551 100644 --- a/folly/experimental/symbolizer/Elf.h +++ b/folly/experimental/symbolizer/Elf.h @@ -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" diff --git a/folly/experimental/symbolizer/ElfUtil.cpp b/folly/experimental/symbolizer/ElfUtil.cpp index 793ded68..e6f32fb9 100644 --- a/folly/experimental/symbolizer/ElfUtil.cpp +++ b/folly/experimental/symbolizer/ElfUtil.cpp @@ -22,8 +22,8 @@ #include #include -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); diff --git a/folly/experimental/symbolizer/Symbolizer.cpp b/folly/experimental/symbolizer/Symbolizer.cpp index 861b3321..714cac76 100644 --- a/folly/experimental/symbolizer/Symbolizer.cpp +++ b/folly/experimental/symbolizer/Symbolizer.cpp @@ -18,26 +18,28 @@ #include "folly/experimental/symbolizer/Symbolizer.h" #include +#include #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 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 diff --git a/folly/experimental/symbolizer/Symbolizer.h b/folly/experimental/symbolizer/Symbolizer.h index 0b38a5db..3917b502 100644 --- a/folly/experimental/symbolizer/Symbolizer.h +++ b/folly/experimental/symbolizer/Symbolizer.h @@ -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_ */ diff --git a/folly/experimental/symbolizer/SymbolizerTest.cpp b/folly/experimental/symbolizer/SymbolizerTest.cpp index 8df70976..0b5850c5 100644 --- a/folly/experimental/symbolizer/SymbolizerTest.cpp +++ b/folly/experimental/symbolizer/SymbolizerTest.cpp @@ -18,16 +18,15 @@ #include "folly/experimental/symbolizer/Symbolizer.h" -#include "common/init/Init.h" -#include "glog/logging.h" +#include -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(main), name, location)); LOG(INFO) << name << " " << location.file << " " << location.line << " (" diff --git a/folly/experimental/test/GenTest.cpp b/folly/experimental/test/GenTest.cpp index 64a73843..142678fa 100644 --- a/folly/experimental/test/GenTest.cpp +++ b/folly/experimental/test/GenTest.cpp @@ -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]); } } diff --git a/folly/test/SubprocessTest.cpp b/folly/test/SubprocessTest.cpp index d0203cab..bf8e0768 100644 --- a/folly/test/SubprocessTest.cpp +++ b/folly/test/SubprocessTest.cpp @@ -20,7 +20,9 @@ #include #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 line) { + if (line == "etc" || line == "bin" || line == "usr") { + ++found; + } + }; EXPECT_EQ(3, found); proc.waitChecked(); }