就让我
她不在这里,她无处追寻,可她在我心里 -- 挥之不去
failed to execute cmd: " dot -Tpng". stderr: `sh: dot: command not found '

xhprof为facebook的php性能分析工具使用实例如下:

http://localhost/xhprof/examples/sample.php
http://localhost/xhprof/xhprof_html/callgraph.php?run=56839487285fa&source=xhprof_foo

使用xhprof时,图片查看时[View Full Callgraph],出现以下错误

failed to execute cmd: " dot -Tpng". stderr: `sh: dot: command not found '

各种goole,stackoverflow,说需要安装graphviz,好了,本人mac使用brew安装

brew install graphviz

安装成功了,再试,依然提示以上错误。

而我直接在命令行输入 dot -V是正常的

bash-3.2# dot -V

dot - graphviz version 2.38.0 (20140413.2041)

那么就是php运行环境有问题了,然后就排查 xhprof的 callgraph.php,最后定位到

xhprof/xhprof_lib/utils/callgraph_utils.php  的 xhprof_generate_image_by_dot 函数

function xhprof_generate_image_by_dot($dot_script, $type) {
  $descriptorspec = array(
       // stdin is a pipe that the child will read from
       0 => array("pipe", "r"),
       // stdout is a pipe that the child will write to
       1 => array("pipe", "w"),
       // stderr is a pipe that the child will write to
       2 => array("pipe", "w")
       );

  $cmd = " dot -T".$type;
  $process = proc_open( $cmd, $descriptorspec, $pipes, sys_get_temp_dir(), array( 'PATH' => getenv( 'PATH' ) ) );
  if (is_resource($process)) {
    fwrite($pipes[0], $dot_script);
    fclose($pipes[0]);

    $output = stream_get_contents($pipes[1]);

    $err = stream_get_contents($pipes[2]);
    if (!empty($err)) {
      print "failed to execute cmd: \"$cmd\". stderr: `$err'\n";
      exit;
    }

    fclose($pipes[2]);
    fclose($pipes[1]);
    proc_close($process);
    return $output;
  }
  print "failed to execute cmd \"$cmd\"";
  exit();
}

问题出现在callgraph_utils.php 112行:

proc_open( $cmd, $descriptorspec, $pipes, sys_get_temp_dir(), array( 'PATH' => getenv( 'PATH' ) ) );

其中

getenv( 'PATH' ) 输出为:/usr/bin:/bin:/usr/sbin:/sbin

而使用brew 安装 的dot在

/usr/local/bin/dot

所以我追加了 在后面如下:/usr/local/bin

proc_open( $cmd, $descriptorspec, $pipes, sys_get_temp_dir(), array( 'PATH' => getenv( 'PATH' ).':/usr/local/bin' ) );

在访问,可以出正常分析图片了。

<< 上一篇 Mac PHP使用 pecl 安装扩展 移动端click事件延迟300ms 下一篇 >>
文章标签
随意 | Created At 2014 By William Clinton | 蜀ICP备14002619号-4 |