Commit d8c1a8c0 authored by Tudor Bosman's avatar Tudor Bosman Committed by Jordan DeLong

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
parent a0b4acce
......@@ -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));
}
/**
......
......@@ -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
......@@ -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_ */
......@@ -173,7 +173,7 @@ namespace {
struct Initializer {
Initializer() {
try {
exception_tracer::installHandlers();
::folly::exception_tracer::installHandlers();
} catch (...) {
}
}
......
......@@ -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";
}
......
......@@ -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<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;
}
......
......@@ -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
......@@ -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_ */
......@@ -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
......@@ -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
......@@ -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"
......
......@@ -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);
......
......@@ -18,26 +18,28 @@
#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
......@@ -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_ */
......@@ -18,16 +18,15 @@
#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 << " ("
......
......@@ -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]);
}
}
......
......@@ -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();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment