php xdebug:如何分析forked进程

Zso*_*gyi 12 php profiler fork xdebug

我正在运行一个PHP守护进程来进行分析.

启动的php进程加载所有必需的数据,分叉自己在所有核心上分配工作负载,等待分叉的子进程完成,并收集子进程生成的结果.

由于我与其他用户共享CLI环境,因此我需要通过将php.ini值注入shell调用来启动xdebug概要分析.

 $ php -d xdebug.profiler_enable=1 -d xdebug.profiler_output_dir="/home/xxx" daemon.php
Run Code Online (Sandbox Code Playgroud)

生成的cachegrind文件,如何配置父级,因此显示90%的睡眠.

有没有办法在不构建驱动程序的情况下对工作人员进行分析直接加载它们?

谢谢

Fra*_*ula 7

我遇到了同样的问题并在没有XDebug的情况下解决了.不幸的是我无法找到XDebug的方法.这就像某种程度上它并不关心任何分叉过程.

我解决了使用Xhprof配置文件和Xhgui来检查日志.Xhprof是Facebook内部开发的一个很棒的工具,然后在开源之后发布.很酷的是,您可以确定何时开始分析以及何时停止.这使您能够解决我们的问题.

首先让我们安装它!

sudo pecl install xhprof-beta
Run Code Online (Sandbox Code Playgroud)

如果您使用的是基于debian的发行版,请确保您还安装了graphviz.

sudo apt-get install graphviz
Run Code Online (Sandbox Code Playgroud)

现在让我们来看看代码.

$children = [];

do {
    if (count($children) < $maxConsumers) {
        $pid = pcntl_fork();
        $children[] = $pid;

        if ($pid == -1) {
            throw new \RuntimeException('Could not fork process');
        } else if ($pid === 0) {
            // we are the child

            if ($verbose) {
                $output->writeln(sprintf('Child here (PID = %s)', posix_getpid()));
            }

            if ($xhProf) {
                // here we enable xhprof thus the profiling starts
                xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
            }

            // DO YOUR MAGIC HERE!

            if ($xhProf) {
                // and here we call the xhgui header file that will
                // stop xhprof and write all the gathered data into
                // mongo or into a dedicated file
                require_once('/var/www/xhgui/external/header.php');
            }

            exit;
        } else {
            // we are the parent
            if ($verbose) {
                $output->writeln(sprintf('Parent here (PID = %s) spawned a new child (PID = %s)', posix_getpid(), $pid));
            }
        }
    } else {
        if ($verbose) {
            $output->writeln(sprintf("Parent - spawned enough children, let's wait them..."));
        }

        $deadPID = pcntl_wait($status);
        $children = array_diff($children, [$deadPID]);
} while (true);

// Waits for all remaining children
while (($pid = pcntl_waitpid(0, $status)) != -1) {
    if ($verbose) {
        $status = pcntl_wexitstatus($status);
        $output->writeln(sprintf("Child (PID %d) terminated. Status is %s.", $pid, $status));
    }
}
Run Code Online (Sandbox Code Playgroud)

为了检查日志,您还需要正确配置Xhgui.你可能也需要一个虚拟主机.

对于所有需要的配置和参考: