Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DESKTOP_APP_REQUIRE_JEMALLOC switch #16717

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions Telegram/SourceFiles/platform/linux/specific_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ For license and copyright information please follow this link:
#include <sys/sendfile.h>
#endif // Q_OS_LINUX
#include <cstdlib>
#include <dlfcn.h>
#include <unistd.h>
#include <dirent.h>
#include <pwd.h>
Expand Down Expand Up @@ -684,6 +685,47 @@ void HaikuAutostart(bool start) {
}
#endif // __HAIKU__

decltype(mallctl)* mallctlLoad() {
#ifdef DESKTOP_APP_REQUIRE_JEMALLOC
return &mallctl;
#elif defined(__GLIBC__)
// Despite using glibc, one can choose jemalloc via LD_PRELOAD.
// Detect the case and configure the allocator.

Dl_info info;
int rc = dladdr(reinterpret_cast<void*>(&malloc), &info);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const?

if (!rc) {
LOG(("Can't retrieve malloc info: %1").arg(dlerror()));
return nullptr;
}
LOG(("We use allocator from %1").arg(info.dli_fname));
if (!QLatin1String(info.dli_fname)
.endsWith(QLatin1String("/libjemalloc.so.2"))) {
return nullptr;
}

void *handle = dlopen(info.dli_fname, RTLD_LAZY | RTLD_NOLOAD);
if (!handle) {
LOG(("Can't reopen jemalloc dynamic object: %1").arg(dlerror()));
return nullptr;
}

auto pointer = reinterpret_cast<decltype(mallctl)*>(
dlsym(handle, "mallctl"));
if (!pointer) {
LOG(("Can't find mallctl function: %1").arg(dlerror()));
}

rc = dlclose(handle);
if (rc) {
LOG(("Can't dlclose temporary handle: %1").arg(dlerror()));
}
return pointer;
#else // __GLIBC__
return nullptr;
#endif
}

} // namespace

QString psAppDataPath() {
Expand Down Expand Up @@ -727,8 +769,10 @@ int psFixPrevious() {
namespace Platform {

void start() {
auto backgroundThread = true;
mallctl("background_thread", nullptr, nullptr, &backgroundThread, sizeof(bool));
if (auto mallctlLoaded = mallctlLoad()) {
bool backgroundThread = true;
mallctlLoaded("background_thread", nullptr, nullptr, &backgroundThread, sizeof(bool));
}

LOG(("Launcher filename: %1").arg(QGuiApplication::desktopFileName()));

Expand Down