1
0
Fork 0

Avoid some exceptions in environment feature when reading files. (#8834)

The changes have been added to reduce the noise during startup.
This commit is contained in:
Jan Christoph Uhde 2019-04-26 10:21:34 +02:00 committed by Jan
parent 79461852b5
commit 89449df97d
3 changed files with 191 additions and 139 deletions

View File

@ -46,10 +46,13 @@ void EnvironmentFeature::prepare() {
#ifdef __linux__
try {
if (basics::FileUtils::exists("/proc/version")) {
std::string value =
basics::StringUtils::trim(basics::FileUtils::slurp("/proc/version"));
std::string content;
auto rv = basics::FileUtils::slurp("/proc/version", content);
if (rv.ok()) {
std::string value = basics::StringUtils::trim(content);
LOG_TOPIC("75ddc", INFO, Logger::FIXME) << "detected operating system: " << value;
}
}
} catch (...) {
// ignore any errors as the log output is just informational
}
@ -76,14 +79,16 @@ void EnvironmentFeature::prepare() {
// check overcommit_memory & overcommit_ratio
try {
std::string value =
basics::FileUtils::slurp("/proc/sys/vm/overcommit_memory");
uint64_t v = basics::StringUtils::uint64(value);
std::string content;
auto rv = basics::FileUtils::slurp("/proc/sys/vm/overcommit_memory", content);
if (rv.ok()) {
uint64_t v = basics::StringUtils::uint64(content);
if (v == 2) {
#ifdef ARANGODB_HAVE_JEMALLOC
LOG_TOPIC("fadc5", WARN, arangodb::Logger::MEMORY)
<< "/proc/sys/vm/overcommit_memory is set to a value of 2. this setting has been found to be problematic";
<< "/proc/sys/vm/overcommit_memory is set to a value of 2. this "
"setting has been found to be problematic";
LOG_TOPIC("d08d6", WARN, Logger::MEMORY)
<< "execute 'sudo bash -c \"echo 0 > "
<< "/proc/sys/vm/overcommit_memory\"'";
@ -97,9 +102,9 @@ void EnvironmentFeature::prepare() {
// memory until it actually runs out.
// When this flag is 2, the kernel uses a "never overcommit"
// policy that attempts to prevent any overcommit of memory.
std::string ratio =
basics::FileUtils::slurp("/proc/sys/vm/overcommit_ratio");
uint64_t r = basics::StringUtils::uint64(ratio);
rv = basics::FileUtils::slurp("/proc/sys/vm/overcommit_ratio", content);
if (rv.ok()) {
uint64_t r = basics::StringUtils::uint64(content);
// from https://www.kernel.org/doc/Documentation/sysctl/vm.txt:
//
// When overcommit_memory is set to 2, the committed address
@ -115,10 +120,12 @@ void EnvironmentFeature::prepare() {
if (static_cast<double>(r) < 0.99 * rr) {
LOG_TOPIC("b0a75", WARN, Logger::MEMORY)
<< "/proc/sys/vm/overcommit_ratio is set to '" << r
<< "'. It is recommended to set it to at least '" << std::llround(rr)
<< "' (100 * (max(0, (RAM - Swap Space)) / RAM)) to utilize all "
<< "available RAM. Setting it to this value will minimize swap "
<< "usage, but may result in more out-of-memory errors, while "
<< "'. It is recommended to set it to at least '"
<< std::llround(rr) << "' (100 * (max(0, (RAM - Swap Space)) / RAM)) to utilize all "
<< "available RAM. Setting it to this value will minimize "
"swap "
<< "usage, but may result in more out-of-memory errors, "
"while "
<< "setting it to 100 will allow the system to use both all "
<< "available RAM and swap space.";
LOG_TOPIC("1041e", WARN, Logger::MEMORY)
@ -127,6 +134,8 @@ void EnvironmentFeature::prepare() {
}
}
}
}
}
} catch (...) {
// file not found or value not convertible into integer
}
@ -143,10 +152,10 @@ void EnvironmentFeature::prepare() {
// test local ipv4 port range
try {
std::string value =
basics::FileUtils::slurp("/proc/sys/net/ipv4/ip_local_port_range");
std::vector<std::string> parts = basics::StringUtils::split(value, '\t');
std::string content;
auto rv = basics::FileUtils::slurp("/proc/sys/net/ipv4/ip_local_port_range", content);
if (rv.ok()) {
std::vector<std::string> parts = basics::StringUtils::split(content, '\t');
if (parts.size() == 2) {
uint64_t lower = basics::StringUtils::uint64(parts[0]);
uint64_t upper = basics::StringUtils::uint64(parts[1]);
@ -162,6 +171,7 @@ void EnvironmentFeature::prepare() {
"bigger port range";
}
}
}
} catch (...) {
// file not found or values not convertible into integers
}
@ -170,9 +180,10 @@ void EnvironmentFeature::prepare() {
// https://vincent.bernat.im/en/blog/2014-tcp-time-wait-state-linux
// https://stackoverflow.com/questions/8893888/dropping-of-connections-with-tcp-tw-recycle
try {
std::string value =
basics::FileUtils::slurp("/proc/sys/net/ipv4/tcp_tw_recycle");
uint64_t v = basics::StringUtils::uint64(value);
std::string content;
auto rv = basics::FileUtils::slurp("/proc/sys/net/ipv4/tcp_tw_recycle", content);
if (rv.ok()) {
uint64_t v = basics::StringUtils::uint64(content);
if (v != 0) {
LOG_TOPIC("c277c", WARN, Logger::COMMUNICATION)
<< "/proc/sys/net/ipv4/tcp_tw_recycle is enabled (" << v << ")"
@ -182,6 +193,7 @@ void EnvironmentFeature::prepare() {
<< "execute 'sudo bash -c \"echo 0 > "
"/proc/sys/net/ipv4/tcp_tw_recycle\"'";
}
}
} catch (...) {
// file not found or value not convertible into integer
}
@ -219,9 +231,10 @@ void EnvironmentFeature::prepare() {
// test zone_reclaim_mode
try {
std::string value =
basics::FileUtils::slurp("/proc/sys/vm/zone_reclaim_mode");
uint64_t v = basics::StringUtils::uint64(value);
std::string content;
auto rv = basics::FileUtils::slurp("/proc/sys/vm/zone_reclaim_mode", content);
if (rv.ok()) {
uint64_t v = basics::StringUtils::uint64(content);
if (v != 0) {
// from https://www.kernel.org/doc/Documentation/sysctl/vm.txt:
//
@ -238,6 +251,7 @@ void EnvironmentFeature::prepare() {
<< "execute 'sudo bash -c \"echo 0 > "
"/proc/sys/vm/zone_reclaim_mode\"'";
}
}
} catch (...) {
// file not found or value not convertible into integer
}
@ -249,7 +263,9 @@ void EnvironmentFeature::prepare() {
for (auto file : paths) {
try {
std::string value = basics::FileUtils::slurp(file);
std::string value;
auto rv = basics::FileUtils::slurp(file, value);
if (rv.ok()) {
size_t start = value.find('[');
size_t end = value.find(']');
@ -264,6 +280,7 @@ void EnvironmentFeature::prepare() {
showHuge = true;
}
}
}
} catch (...) {
// file not found
}
@ -280,8 +297,10 @@ void EnvironmentFeature::prepare() {
if (numa) {
try {
std::string value = basics::FileUtils::slurp("/proc/self/numa_maps");
auto values = basics::StringUtils::split(value, '\n', '\0');
std::string content;
auto rv = basics::FileUtils::slurp("/proc/self/numa_maps", content);
if (rv.ok()) {
auto values = basics::StringUtils::split(content, '\n', '\0');
if (!values.empty()) {
auto first = values[0];
@ -295,6 +314,7 @@ void EnvironmentFeature::prepare() {
<< "put 'numactl --interleave=all' in front of your command";
}
}
}
} catch (...) {
// file not found
}
@ -302,16 +322,17 @@ void EnvironmentFeature::prepare() {
// check kernel ASLR settings
try {
std::string value =
basics::FileUtils::slurp("/proc/sys/kernel/randomize_va_space");
uint64_t v = basics::StringUtils::uint64(value);
std::string content;
auto rv = basics::FileUtils::slurp("/proc/sys/kernel/randomize_va_space", content);
if (rv.ok()) {
uint64_t v = basics::StringUtils::uint64(content);
// from man proc:
//
// 0 No randomization. Everything is static.
// 1 Conservative randomization. Shared libraries, stack, mmap(), VDSO and
// heap are randomized. 2 Full randomization. In addition to elements
// listed in the previous point, memory managed through brk() is also
// randomized.
// 1 Conservative randomization. Shared libraries, stack, mmap(), VDSO
// and heap are randomized. 2 Full randomization. In addition to
// elements listed in the previous point, memory managed through brk() is
// also randomized.
char const* s = nullptr;
switch (v) {
case 0:
@ -328,6 +349,7 @@ void EnvironmentFeature::prepare() {
if (s != nullptr) {
LOG_TOPIC("63a7a", DEBUG, Logger::FIXME) << "host ASLR is in use for " << s;
}
}
} catch (...) {
// file not found or value not convertible into integer
}

View File

@ -204,6 +204,35 @@ void slurp(std::string const& filename, StringBuffer& result) {
fillStringBuffer(fd, filename, result, chunkSize);
}
Result slurpNoEx(std::string const& filename, StringBuffer& result) {
int fd = TRI_OPEN(filename.c_str(), O_RDONLY | TRI_O_CLOEXEC);
if (fd == -1) {
TRI_set_errno(TRI_ERROR_SYS_ERROR);
int res = TRI_errno();
std::string message("read failed for file '" + filename + "': " + strerror(res));
LOG_TOPIC("a1898", TRACE, arangodb::Logger::FIXME) << message;
return {TRI_ERROR_SYS_ERROR, message};
}
TRI_DEFER(TRI_CLOSE(fd));
result.reset();
constexpr size_t chunkSize = 8192;
fillStringBuffer(fd, filename, result, chunkSize);
return {};
}
Result slurp(std::string const& filename, std::string& result) {
constexpr size_t chunkSize = 8192;
StringBuffer buffer(chunkSize, false);
auto status = slurpNoEx(filename, buffer);
result = std::string(buffer.data(), buffer.length());
return status;
}
static void throwFileWriteError(std::string const& filename) {
TRI_set_errno(TRI_ERROR_SYS_ERROR);

View File

@ -60,6 +60,7 @@ inline std::string buildFilename(std::string const& path,
// reads file into string or buffer
std::string slurp(std::string const& filename);
void slurp(std::string const& filename, StringBuffer& result);
Result slurp(std::string const& filename, std::string& result);
// creates file and writes string to it
void spit(std::string const& filename, char const* ptr, size_t len, bool sync = false);