gpt4 book ai didi

darwin - 我应该如何调试 Mach 端口泄漏?

转载 作者:行者123 更新时间:2023-12-02 01:29:15 27 4
gpt4 key购买 nike

我有一个用 C++ 编写的服务器,它在 osx 上运行时会泄漏 Mach 端口。具体来说,运行时 top我注意到它有大约 50000(在 #PORTS 下)。奇怪的是,我让它运行了一夜,第二天机器基本上死机了(用了 15 分钟来响应 ctrl-c,不接受新的 ssh 连接)所以它不得不重新启动它。这样的泄漏会像这样导致系统瘫痪吗?它没有以 root 身份运行。

无论如何,有什么好的策略可以寻找这种泄漏的原因?有什么好的工具吗?

我发现一项测试在可靠运行时会泄漏 5 个端口,但仅此而已。

编辑:我发现我们的线程类会造成 mach 端口泄漏,但我不明白为什么。在构造函数中,我们有以下代码:

// Initialize the default attributes.
if (0 != pthread_attr_init(&m_threadAttributes))
{
throw "blah";
}

// Set the thread to be joinable.
if (0 != pthread_attr_setdetachstate(&m_threadAttributes, PTHREAD_CREATE_JOINABLE))
{
pthread_attr_destroy(&m_threadAttributes);
throw "blah";
}

if (0 != pthread_create(
&m_thread,
&m_threadAttributes,
&StartThreadFunction,
reinterpret_cast<void*>(this)))
{
throw "blah";
}

我注意到在调用 pthread_create 后进程的端口数增加了 1。 ,这是预期的。

然后,稍后我使用以下代码加入线程:
if (0 != pthread_join(m_thread, NULL))
{
throw "blah";
}

并且没有抛出异常,所以我只能假设 pthread_join返回 0 并因此成功,但顶部的端口数量没有下降。我还需要做些什么来清理线程吗?

最佳答案

您可以使用 Dtrace检测正在运行的系统上的 mach 端口使用情况。有许多与 mach_port 相关的探测器:
sudo dtrace -l | grep mach_port
您可以编写一个 Dtrace 脚本来跟踪每个端口创建或保留调用是否由相应的版本平衡。用于跟踪内存泄漏的 Dtrace 脚本将是一个有用的起点。

一旦您拥有适合您的目的的脚本 you can use Instruments to control the trace session and graph the results .

关于darwin - 我应该如何调试 Mach 端口泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35098346/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com