xhprof安裝了graphviz還報錯failedto execute cmd "dot-Tpng"

使用XHProf我們肯定需要查看它強大的圖形統計結果分析圖,而xhprof是用dot進行繪圖的,在xhprof程序安裝包中xhprof_lib/utils/callgraph_utils.php文件中可看到方法function xhprof_generate_image_by_dot()中有接頭這個CMD命令。
Dot是什麼?dot是一個適合程序員使用的繪圖工具。讓你可以用幾行代碼就能繪製出一些流程圖出來。
dot本身是Graphviz工具包中的一個工具。Graphviz是大名鼎鼎的貝爾實驗室的幾位牛人開發的一個畫圖工具,它提供了“所想即所得”的理念,通過dot語言來編寫腳本並繪製圖形,簡單易懂。我們使用一個文本文件通過dot語法描述圖形關係,然後用dot生成最終的圖形。dot負責佈局我們所描述的圖形。就算圖形對象非常多,關係非常複雜,dot也能將其佈局的非常清楚。dot文件語法非常簡單,可以像程序一樣手動編寫,不過很多時候是通過其他程序生成。 如果在使用XHProf的時候我們沒有安裝dot(即安裝Graphviz),則可能會報錯如下:
Error: either we can not find profile data for run_id 58eb5359be406 or the threshold 0.01 is too small or you do not have dot image generation utility installed.
這時我們就需要安裝一下Graphviz工具包:

yum list 'graphviz*'         #可以看到有很多種語言的包
yum install -y graphviz      #安裝graphviz工具包
yum install 'graphviz-php*'  #PHP的包。

安裝好之後再進行xhprof的圖形分析界面,就不會出現這個報錯了,但有可能會報這個錯誤:
failed to execute cmd " dot -Tpng"
我開始以爲是沒有安裝好dot,或者dot沒有安裝完全。找了些答案。但都沒有幫我解決。最後我從xhprof的程序入手自己看,程序如下:

100 function xhprof_generate_image_by_dot($dot_script, $type) {
101   $descriptorspec = array(
102        // stdin is a pipe that the child will read from
103        0 => array("pipe", "r"),
104        // stdout is a pipe that the child will write to
105        1 => array("pipe", "w"),
106        // stderr is a pipe that the child will write to
107        2 => array("pipe", "w")
108        );
109 
110   $cmd = " dot -T".$type;
111 
112   $process = proc_open($cmd, $descriptorspec, $pipes, "/tmp", array());
113   if (is_resource($process)) {
114     fwrite($pipes[0], $dot_script);
115     fclose($pipes[0]);
116 
117     $output = stream_get_contents($pipes[1]);
118
119     $err = stream_get_contents($pipes[2]);
120     if (!empty($err)) {
121       print "failed to execute cmd: \"$cmd\". stderr: `$err'\n";
122       exit;
123     }
124 
125     fclose($pipes[2]);
126     fclose($pipes[1]);
127     proc_close($process);
128     return $output;
129   }
130   print "failed to execute cmd \"$cmd\"";
131   exit();
132 }

此報錯是在130行報錯,非121行報錯,121行報錯會報出標準錯誤。可見是這個proc_open方法執行不成功。於是我便想到php配置文件中的禁用函數:找到php的配置文件:把disable_functions中的proc_open方法去除,重啓php解決問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章