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

在macosx上开启jemalloc和malloc_hook #2032

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

myzhan
Copy link

@myzhan myzhan commented Feb 13, 2025

之前编译不过的原因,是 mac 系统上,没有 memalign 函数。如果 jemalloc 的 autoconf 检测不到 memalign 函数,则它不会暴露 je_memalign 符号。

这个 PR,尝试在 macos 系统上,使用 posix_memalign 来实现 skynet_memalign,使其能编译通过。

是我本地开发机是 mac,需要使用到 malloc_hook,期望能支持一下。

@cloudwu
Copy link
Owner

cloudwu commented Feb 13, 2025

建议自己维护一个分支,或修改一份 jemalloc 解决。

@cloudfreexiao
Copy link
Contributor

好东西

@myzhan
Copy link
Author

myzhan commented Feb 14, 2025

嗯,我先在自己的 skynet 分支上面测试。目前只是在 macos 上启用了 jemalloc,不确定有没有其他梗。

@myzhan
Copy link
Author

myzhan commented Feb 14, 2025

发现第一个问题,启动崩溃。原因是 snlua_init 里面调用 skynet_malloc 去申请的内存,实际调用的系统库的 malloc,但是在 dispatch_message 里面调用 skynet_free 去释放 msg->data 的时候,用的是 je_free。

image

image

分配和释放用的不同的分配器,所以崩溃。可参考:jemalloc/jemalloc#2362

解决方案如下,但不太适合,这样全局的 malloc 劫持就失效了。

#ifdef NOUSE_JEMALLOC
#define skynet_malloc malloc
#define skynet_calloc calloc
#define skynet_realloc realloc
#define skynet_free free
#define skynet_memalign memalign
#define skynet_aligned_alloc aligned_alloc
#define skynet_posix_memalign posix_memalign
#endif

@myzhan
Copy link
Author

myzhan commented Feb 18, 2025

问题一已经修复,在 macos 上使用 DYLD_INTERPOSE 来调整 linker 的加载优先级。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants