Linux 線程佔用CPU過高定位分析

來源:互聯網
上載者:User

標籤:char   font   color   一個   []   代碼   sleep   test   線程id   

今天朋友問我一個Linux程式CPU佔用漲停了,該如何分析,

CPU佔用過高,類比CPU佔用過高的情況

先上一段代碼:

 1 #include <iostream> 2 #include <thread> 3 #include <vector> 4  5  6 int main(int argc, char **argv) { 7    8     std::vector<std::thread> test_threads; 9     for(int i = 0; i < 9; i++){10       test_threads.push_back(std::thread([]{11     while(1){12       std::this_thread::sleep_for(std::chrono::milliseconds(500));13     }14       }));15     }16     test_threads.push_back(std::thread([]{17       while(1){18       std::cout<<"cpu"<<std::endl;19       }20     }));21     22     for(auto &x : test_threads){23       x.join();24     }25     26     return 0;27 }

第10個線程中沒有進行睡眠,會獨佔進程的時間片,導致CPU利用率過高,

現在就要定位到第10個

第一步:top 查看程式進程id

 

第二步:top -H -p 96263 定位CPU佔用過高的線程id

 

第三步:使用pstack 96263或者strace -f -p 96263 定位線程堆棧

strace -f -p 96263

 

 

Linux 線程佔用CPU過高定位分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.