Strace診斷CPU跑高問題(java/php網站)

來源:互聯網
上載者:User

標籤:strace

       早些年,如果你知道有個 strace 命令,就很牛了,而現在大家基本都知道 strace 了,如果你遇到效能問題求助別人,十有八九會建議你用 strace 掛上去看看,不過當你掛上去了,看著滿屏翻滾的字元,卻十有八九看不出個所以然。本文通過一個簡單的案例,向你展示一下在用 strace 診斷問題時的一些套路。

如下真實案例,如有雷同,實屬必然!讓我們看一台高負載伺服器的 top 結果:

650) this.width=650;" class="size-full wp-image-475" src="http://huoding.com/wp-content/uploads/2015/10/top.jpg" alt="top" width="1200" height="1176" style="border:1px solid rgb(238,238,238);height:auto;width:auto;margin:-2px 0px 0px -2px;padding:6px;" />

top

技巧:運行 top 時,按「1」開啟 CPU 列表,按「shift+p」以 CPU 排序。

在本例中大家很容易發現 CPU 主要是被若干個 PHP 進程佔用了,同時 PHP 進程佔用的比較多的記憶體,不過系統記憶體尚有結餘,SWAP 也不嚴重,這並不是問題主因。

不過在 CPU 列表中能看到 CPU 主要消耗在核心態「sy」,而不是使用者態「us」,和我們的經驗不符。Linux 作業系統有很多用來跟蹤程式行為的工具,核心態的函數調用跟蹤用「strace」,使用者態的函數調用跟蹤用「ltrace」,所以這裡我們應該用「strace」:

shell> strace -p <PID>

不過如果直接用 strace 跟蹤某個進程的話,那麼等待你的往往是滿屏翻滾的字元,想從這裡看出問題的癥結並不是一件容易的事情,好在 strace  可以按操作匯總時間:

shell> strace -cp <PID>

通過「c」選項用來匯總各個操作的總耗時,運行後的結果大概如所示:

650) this.width=650;" class="size-full wp-image-476" src="http://huoding.com/wp-content/uploads/2015/10/strace1.jpg" alt="strace -cp <PID>" width="1080" height="600" style="border:1px solid rgb(238,238,238);height:auto;width:auto;margin:-2px 0px 0px -2px;padding:6px;" />

strace -cp

很明顯,我們能看到 CPU 主要被 clone 操作消耗了,還可以單獨跟蹤一下 clone:

shell> strace -T -e clone -p <PID>

通過「T」選項可以擷取操作實際消耗的時間,通過「e」選項可以跟蹤某個操作:

650) this.width=650;" class="size-full wp-image-477" src="http://huoding.com/wp-content/uploads/2015/10/strace2.jpg" alt="strace -T -e clone -p <PID>" width="1200" height="410" style="border:1px solid rgb(238,238,238);height:auto;width:auto;margin:-2px 0px 0px -2px;padding:6px;" />

strace -T -e clone -p

很明顯,一個 clone 操作需要幾百毫秒,至於 clone 的含義,參考 man 文檔:

clone() creates a new process, in a manner similar to fork(2). It is actually a library function layered on top of the underlying clone() system call, hereinafter referred to as sys_clone. A description of sys_clone is given towards the end of this page.

Unlike fork(2), these calls allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers. (Note that on this manual page, “calling process” normally corresponds to “parent process”. But see the description of CLONE_PARENT below.)

簡單來說,就是建立一個新進程。那麼在 PHP 裡什麼時候會出現此類系統調用呢?查詢業務代碼看到了 exec 函數,通過如下命令驗證它確實會導致 clone 系統調用:

shell> strace -eclone php -r ‘exec("ls");‘

最後再考大家一個題:如果我們用 strace 跟蹤一個進程,輸出結果很少,是不是說明進程很空閑?其實試試 ltrace,可能會發現別有洞天。記住有核心態和使用者態之分。


本文出自 “小菜鳥” 部落格,請務必保留此出處http://baishuchao.blog.51cto.com/12918589/1929783

Strace診斷CPU跑高問題(java/php網站)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.