/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2014-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/portability/PThread.h>
#include <folly/portability/SysMman.h>
#include <folly/portability/Unistd.h>
+#include <folly/synchronization/CallOnce.h>
#include <limits.h>
#include <stdio.h>
}
try {
- mallctlCall("thread.tcache.flush");
+ // Not using mallctlCall as this will fail if tcache is disabled.
+ mallctl("thread.tcache.flush", nullptr, nullptr, nullptr, 0);
// By default jemalloc has 4 arenas per cpu, and then assigns each
// thread to one of those arenas. This means that in any service
mallctlRead("thread.arena", &arenaForCurrent);
if (narenas > 2 * CacheLocality::system().numCpus &&
mallctlnametomib("arena.0.purge", mib, &miblen) == 0) {
- mib[1] = size_t(arenaForCurrent);
+ mib[1] = static_cast<size_t>(arenaForCurrent);
mallctlbymib(mib, miblen, nullptr, nullptr, nullptr, 0);
}
} catch (const std::runtime_error& ex) {
}
static void fetchStackLimits() {
+ int err;
pthread_attr_t attr;
- pthread_getattr_np(pthread_self(), &attr);
+ if ((err = pthread_getattr_np(pthread_self(), &attr))) {
+ // some restricted environments can't access /proc
+ static folly::once_flag flag;
+ folly::call_once(flag, [err]() {
+ LOG(WARNING) << "pthread_getaddr_np failed errno=" << err;
+ });
+
+ tls_stackSize = 1;
+ return;
+ }
SCOPE_EXIT { pthread_attr_destroy(&attr); };
void* addr;
size_t rawSize;
- int err;
if ((err = pthread_attr_getstack(&attr, &addr, &rawSize))) {
// unexpected, but it is better to continue in prod than do nothing
FB_LOG_EVERY_MS(ERROR, 10000) << "pthread_attr_getstack error " << err;
assert(rawSize > guardSize);
// stack goes down, so guard page adds to the base addr
- tls_stackLimit = uintptr_t(addr) + guardSize;
+ tls_stackLimit = reinterpret_cast<uintptr_t>(addr) + guardSize;
tls_stackSize = rawSize - guardSize;
assert((tls_stackLimit & (pageSize() - 1)) == 0);
FOLLY_NOINLINE static uintptr_t getStackPtr() {
char marker;
- auto rv = uintptr_t(&marker);
+ auto rv = reinterpret_cast<uintptr_t>(&marker);
return rv;
}
if (tls_stackSize == 0) {
fetchStackLimits();
}
- if (tls_stackSize <= std::max(size_t(1), retain)) {
+ if (tls_stackSize <= std::max(static_cast<size_t>(1), retain)) {
// covers both missing stack info, and impossibly large retain
return;
}
#endif
-}}
+} // namespace detail
+} // namespace folly