5

I want to profile my daemon program, that pauses the main thread:

sigset_t signal_mask;
sigemptyset(&signal_mask);
sigaddset(&signal_mask, SIGTERM);
sigaddset(&signal_mask, SIGINT);

int sig;
sigwait(&signal_mask, &sig);

All other threads simply block all signals. As far as I know the profiler uses SIGPROF signal for its operations. If I start profiling with such a code, the output .prof file is empty:

env CPUPROFILE=daemon.prof ./daemon

How should I properly handle signals in main thread and other threads to enable profiling? Or may be an issue is somewhere else?

4

2 回答 2

1

所有其他线程只是阻塞所有信号。

您只需要SIGPROF在所有线程(或您想要分析的线程)中解除阻塞。我们只是在多线程守护进程中解决完全相同的问题。

于 2016-07-21T07:34:31.427 回答
0

我需要查看更多您的代码,但是您关于“所有其他线程只是阻止所有信号”的声明引发了......信号。

您必须记住,大多数系统调用是在线程概念存在之前创建的。信号处理就是其中之一。因此,当您在任何线程上阻塞信号时,它可能会被所有线程阻塞。

事实上,查看 signal(2) 手册页:

The effects of signal() in a multithreaded process are unspecified.

是的,这很可悲,但这是使用低开销统计采样分析器必须付出的代价。解决它很容易:只需从信号掩码集中删除 SIGPROF(或 SIGALRM,如果您使用的是 REAL 模式),您应该没问题。

一般来说,除非你绝对必须这样做,否则你不应该在主线程以外的任何地方进行进程级信号屏蔽......其中“main”不一定意味着 MAIN() 正在运行的线程,但是相反,您认为是所有其他线程的“老板”,原因您已经说得太清楚了。:)

您也可以尝试使用pthread 库的 sigmask 包装器 pthread_sigmask,但我不清楚它在子线程从 sigmask 中删除条目(pthread 继承其父级的 pthread sigmask)等情况下的效果如何。

于 2014-04-10T19:28:57.167 回答